Open Kilda Java Documentation
DefaultFlowsChecker.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.atdd.staging.helpers;
17 
25 
26 import cucumber.api.Scenario;
27 import org.apache.commons.lang3.StringUtils;
28 
29 import java.util.Objects;
30 
31 public final class DefaultFlowsChecker {
32  private static final String VERSION_12 = "OF_12";
33  private static final String BROADCAST_FLOW = "flow-0x8000000000000002";
34  private static final String DROP_FLOW = "flow-0x8000000000000001";
35  private static final String NON_BROADCAST_FLOW = "flow-0x8000000000000003";
36  private static final String VERIFICATION_DST = "08:ed:02:ef:ff:ff";
37 
38  private static final String CONTROLLER_OUTPUT = "controller";
39  private static final int DPID_LENGTH = 23;
40 
44  public static boolean validateDefaultRules(SwitchEntry sw, FlowEntriesMap map, Scenario scenario) {
45  FlowEntry flow = map.get(BROADCAST_FLOW);
46  boolean result = isValidDefaultFlow(BROADCAST_FLOW, flow, sw.getSwitchId(), scenario);
47 
48  if (!VERSION_12.equals(sw.getOfVersion())) {
49  result = result & isValidDefaultFlow(NON_BROADCAST_FLOW,
50  map.get(NON_BROADCAST_FLOW), sw.getSwitchId(), scenario);
51  result = result & isValidDropRule(map.get(DROP_FLOW), sw.getSwitchId(), scenario);
52  }
53 
54  return result;
55  }
56 
57  private static boolean isValidDropRule(FlowEntry flow, SwitchId switchId, Scenario scenario) {
58  boolean valid = true;
59  if (Objects.isNull(flow)) {
60  scenario.write(String.format("Switch %s doesn't contain %s flow", switchId, DROP_FLOW));
61  return false;
62  }
63 
64  if (flow.getPriority() != 1) {
65  scenario.write(String.format("Switch %s has incorrect priority for flow %s", switchId, flow.getCookie()));
66  valid = false;
67  }
68 
69  FlowInstructions instructions = flow.getInstructions();
70  if (Objects.nonNull(instructions.getApplyActions())) {
71  scenario.write(String.format("Switch %s has incorrect instructions for flow %s",
72  switchId, flow.getCookie()));
73  valid = false;
74  }
75 
76  return valid;
77  }
78 
79  private static boolean isValidDefaultFlow(String flowId, FlowEntry flow, SwitchId switchId, Scenario scenario) {
80  boolean valid = true;
81  if (Objects.isNull(flow)) {
82  scenario.write(String.format("Switch %s doesn't contain %s flow", switchId, flowId));
83  return false;
84  }
85 
86  FlowInstructions instructions = flow.getInstructions();
87  FlowApplyActions flowActions = instructions.getApplyActions();
88  if (!isValidSetFieldProperty(flowActions.getField(), switchId)) {
89  scenario.write(String.format("Switch %s has incorrect set field action for flow %s",
90  switchId, flow.getCookie()));
91  valid = false;
92  }
93 
94  String flowOutput = flowActions.getFlowOutput();
95  if (!CONTROLLER_OUTPUT.equals(flowOutput)) {
96  scenario.write(String.format("Switch %s has incorrect output action name for flow %s", switchId,
97  flow.getCookie()));
98  valid = false;
99  }
100 
101  FlowMatchField flowMatch = flow.getMatch();
102  if (BROADCAST_FLOW.equals(flow.getCookie())) {
103  if (!VERIFICATION_DST.equals(flowMatch.getEthDst())) {
104  scenario.write(String.format("Switch %s has incorrect verification broadcast packets destination",
105  switchId));
106  valid = false;
107  }
108  } else if (NON_BROADCAST_FLOW.equals(flow.getCookie())) {
109  if (!switchId.toMacAddress().equals(flowMatch.getEthDst())) {
110  scenario.write(String.format("Switch %s contains incorrect eth_dst: %s",
111  switchId, flowMatch.getEthDst()));
112  valid = false;
113  }
114  }
115  return valid;
116  }
117 
118  private static boolean isValidSetFieldProperty(String value, SwitchId switchId) {
119  return StringUtils.startsWith(value, switchId.toMacAddress());
120  }
121 
122  private DefaultFlowsChecker() {
123  }
124 
125 }
value
Definition: nodes.py:62
Definition: FlowEntry.java:30
list result
Definition: plan-d.py:72
static boolean validateDefaultRules(SwitchEntry sw, FlowEntriesMap map, Scenario scenario)