Open Kilda Java Documentation
NorthboundRunTest.java
Go to the documentation of this file.
1 /* Copyright 2017 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;
17 
18 import static org.hamcrest.Matchers.empty;
19 import static org.hamcrest.Matchers.hasItems;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertThat;
23 import static org.junit.Assert.assertTrue;
25 
39 
40 import cucumber.api.java.en.Given;
41 import cucumber.api.java.en.Then;
42 import cucumber.api.java.en.When;
43 
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.stream.Collectors;
47 
48 public class NorthboundRunTest {
49  private static final FlowState expectedFlowStatus = FlowState.UP;
50  private static final List<PathNodePayload> expectedForwardFlowPath = Arrays.asList(
51  new PathNodePayload(new SwitchId("de:ad:be:ef:00:00:00:03"), 11, 2),
52  new PathNodePayload(new SwitchId("de:ad:be:ef:00:00:00:04"), 1, 2),
53  new PathNodePayload(new SwitchId("de:ad:be:ef:00:00:00:05"), 1, 12));
54  private static final List<PathNodePayload> expectedReverseFlowPath = Arrays.asList(
55  new PathNodePayload(new SwitchId("de:ad:be:ef:00:00:00:05"), 12, 1),
56  new PathNodePayload(new SwitchId("de:ad:be:ef:00:00:00:04"), 2, 1),
57  new PathNodePayload(new SwitchId("de:ad:be:ef:00:00:00:03"), 2, 11));
58 
59  @Then("^path of flow (\\w+) could be read$")
60  public void checkFlowPath(final String flowId) {
61  String flowName = FlowUtils.getFlowName(flowId);
62 
63  FlowPathPayload payload = FlowUtils.getFlowPath(flowName);
64  assertNotNull(payload);
65 
66  assertEquals(flowName, payload.getId());
67  assertEquals(expectedForwardFlowPath, payload.getForwardPath());
68  assertEquals(expectedReverseFlowPath, payload.getReversePath());
69  }
70 
71  @Then("^status of flow (\\w+) could be read$")
72  public void checkFlowStatus(final String flowId) throws Exception {
73  String flowName = FlowUtils.getFlowName(flowId);
74  FlowIdStatusPayload payload = FlowUtils.waitFlowStatus(flowName, expectedFlowStatus);
75 
76  assertNotNull(payload);
77 
78  assertEquals(flowName, payload.getId());
79  assertEquals(expectedFlowStatus, payload.getStatus());
80  }
81 
82  @Then("^flows dump contains (\\d+) flows$")
83  public void checkDumpFlows(final int flowCount) {
84  List<FlowPayload> flows = FlowUtils.getFlowDump();
85  assertNotNull(flows);
86  flows.forEach(flow -> System.out.println(flow.getId()));
87  assertEquals(flowCount, flows.size());
88  }
89 
90  @Given("^health check$")
91  public void healthCheck() throws Throwable {
92  assertEquals(200, getHealthCheck());
93  }
94 
95  @Then("^flow (\\w+) in (\\w+) state$")
96  public void flowState(String flowId, String state) throws Throwable {
97  String flowName = FlowUtils.getFlowName(flowId);
98  FlowState flowState = FlowState.valueOf(state);
100  assertNotNull(payload);
101  assertEquals(flowName, payload.getId());
102  assertEquals(flowState, payload.getStatus());
103  }
104 
105  @Then("^delete all non-default rules request on ([\\w:]+) switch is successful with (\\d+) rules deleted$")
106  public void deleteAllNonDefaultRules(String switchId, int deletedFlowsCount) {
107  List<Long> cookies = SwitchesUtils.deleteSwitchRules(switchId, DeleteRulesAction.IGNORE_DEFAULTS);
108  assertNotNull(cookies);
109  assertEquals(deletedFlowsCount, cookies.size());
110  cookies.forEach(cookie -> System.out.println(cookie));
111  }
112 
113  @Then("^delete all rules request on (.*) switch is successful with (\\d+) rules deleted$")
114  public void deleteAllDefaultRules(String switchId, int deletedFlowsCount) {
115  List<Long> cookies = SwitchesUtils.deleteSwitchRules(switchId, DeleteRulesAction.DROP_ALL);
116  assertNotNull(cookies);
117  assertEquals(deletedFlowsCount, cookies.size());
118  cookies.forEach(cookie -> System.out.println(cookie));
119  }
120 
121  @Then("^delete rules request by (\\d+) in-port on (.*) switch is successful with (\\d+) rules deleted$")
122  public void deleteRulesByInPort(int inPort, String switchId, int deletedFlowsCount) {
123  List<Long> cookies = SwitchesUtils.deleteSwitchRules(switchId, inPort, null);
124  assertNotNull(cookies);
125  assertEquals(deletedFlowsCount, cookies.size());
126  cookies.forEach(cookie -> System.out.println(cookie));
127  }
128 
129  @Then("^delete rules request by (\\d+) in-port and (\\d+) "
130  + "in-vlan on (.*) switch is successful with (\\d+) rules deleted$")
131  public void deleteRulesByInPortVlan(int inPort, int inVlan, String switchId, int deletedFlowsCount) {
132  List<Long> cookies = SwitchesUtils.deleteSwitchRules(switchId, inPort, inVlan);
133  assertNotNull(cookies);
134  assertEquals(deletedFlowsCount, cookies.size());
135  cookies.forEach(cookie -> System.out.println(cookie));
136  }
137 
138  @Then("^delete rules request by (\\d+) in-vlan on (.*) switch is successful with (\\d+) rules deleted$")
139  public void deleteRulesByInVlan(int inVlan, String switchId, int deletedFlowsCount) {
140  List<Long> cookies = SwitchesUtils.deleteSwitchRules(switchId, null, inVlan);
141  assertNotNull(cookies);
142  assertEquals(deletedFlowsCount, cookies.size());
143  cookies.forEach(cookie -> System.out.println(cookie));
144  }
145 
146  @Then("^delete rules request by (\\d+) out-port on (.*) switch is successful with (\\d+) rules deleted$")
147  public void deleteRulesByOutPort(int outPort, String switchId, int deletedFlowsCount) {
148  List<Long> cookies = SwitchesUtils.deleteSwitchRules(switchId, outPort);
149  assertNotNull(cookies);
150  assertEquals(deletedFlowsCount, cookies.size());
151  cookies.forEach(cookie -> System.out.println(cookie));
152  }
153 
154  @Then("^synchronize flow cache is successful with (\\d+) dropped flows$")
155  public void synchronizeFlowCache(final int droppedFlowsCount) {
157  assertNotNull(results);
158  assertEquals(droppedFlowsCount, results.getDroppedFlows().size());
159  }
160 
161  @Then("^invalidate flow cache is successful with (\\d+) dropped flows$")
162  public void invalidateFlowCache(final int droppedFlowsCount) {
164  assertNotNull(results);
165  assertEquals(droppedFlowsCount, results.getDroppedFlows().size());
166  }
167 
168  @When("^flow (\\w+) could be deleted from DB$")
169  public void deleteFlowFromDbViaTe(final String flowName) {
170  String flowId = FlowUtils.getFlowName(flowName);
171  boolean deleted = FlowUtils.deleteFlowViaTe(flowId);
172 
173  assertTrue(deleted);
174  }
175 
176  @Then("^validation of rules on ([\\w:]+) switch is successful with no discrepancies$")
177  public void validateSwitchRules(String switchId) {
178  RulesValidationResult validateResult = SwitchesUtils.validateSwitchRules(switchId);
179 
180  assertNotNull(validateResult);
181  assertThat("The switch has excessive rules.", validateResult.getExcessRules(), empty());
182  assertThat("The switch has missing rules.", validateResult.getMissingRules(), empty());
183  }
184 
185  @Then("^validation of rules on ([\\w:]+) switch has passed and (\\d+) rules are missing$")
186  public void validateSwitchWithMissingRules(String switchId, int missingRulesCount) {
187  RulesValidationResult validateResult = SwitchesUtils.validateSwitchRules(switchId);
188 
189  assertNotNull(validateResult);
190  assertEquals("Switch doesn't have expected missing rules.", missingRulesCount,
191  validateResult.getMissingRules().size());
192  assertThat("The switch has excessive rules.", validateResult.getExcessRules(), empty());
193  }
194 
195  @Then("^validation of rules on ([\\w:]+) switch has passed and (\\d+) excessive rules are present$")
196  public void validateSwitchWithExcessiveRules(String switchId, int excessiveRulesCount) {
197  RulesValidationResult validateResult = SwitchesUtils.validateSwitchRules(switchId);
198 
199  assertNotNull(validateResult);
200  assertEquals("Switch doesn't have expected excessive rules.", excessiveRulesCount,
201  validateResult.getExcessRules().size());
202  assertThat("The switch has missing rules.", validateResult.getMissingRules(), empty());
203  }
204 
205  @Then("^synchronization of rules on ([\\w:]+) switch is successful with (\\d+) rules installed$")
206  public void synchronizeSwitchWithInstalledRules(String switchId, int installedRulesCount) {
207  RulesSyncResult validateResult = SwitchesUtils.synchronizeSwitchRules(switchId);
208 
209  assertNotNull(validateResult);
210  assertEquals("Switch doesn't have expected installed rules.", installedRulesCount,
211  validateResult.getInstalledRules().size());
212  }
213 
214  @Then("^([\\w,]+) rules are installed on ([\\w:]+) switch$")
215  public void checkThatRulesAreInstalled(String ruleCookies, String switchId) {
216  List<FlowEntry> actualRules = SwitchesUtils.dumpSwitchRules(switchId);
217  assertNotNull(actualRules);
218  List<String> actualRuleCookies = actualRules.stream()
219  .map(entry -> Long.toHexString(entry.getCookie()))
220  .collect(Collectors.toList());
221 
222  assertThat("Switch doesn't contain expected rules.", actualRuleCookies, hasItems(ruleCookies.split(",")));
223  }
224 
225  @Then("No rules installed on ([\\w:]+) switch$")
226  public void checkThatNoRulesAreInstalled(String switchId) {
227  List<FlowEntry> actualRules = SwitchesUtils.dumpSwitchRules(switchId);
228 
229  assertNotNull(actualRules);
230  assertTrue("Switch contains rules, but expect to have none.", actualRules.isEmpty());
231  }
232 }
void flowState(String flowId, String state)
void checkThatRulesAreInstalled(String ruleCookies, String switchId)
static boolean deleteFlowViaTe(final String flowId)
Definition: FlowUtils.java:720
void deleteFlowFromDbViaTe(final String flowName)
static List< FlowPayload > getFlowDump()
Definition: FlowUtils.java:409
void checkThatNoRulesAreInstalled(String switchId)
void deleteRulesByInPortVlan(int inPort, int inVlan, String switchId, int deletedFlowsCount)
void synchronizeSwitchWithInstalledRules(String switchId, int installedRulesCount)
void validateSwitchWithExcessiveRules(String switchId, int excessiveRulesCount)
void invalidateFlowCache(final int droppedFlowsCount)
Definition: FlowEntry.java:29
void synchronizeFlowCache(final int droppedFlowsCount)
void checkFlowStatus(final String flowId)
void deleteRulesByInPort(int inPort, String switchId, int deletedFlowsCount)
void checkFlowPath(final String flowId)
static List< FlowEntry > dumpSwitchRules(String switchId)
static FlowPathPayload getFlowPath(final String flowId)
Definition: FlowUtils.java:275
void validateSwitchWithMissingRules(String switchId, int missingRulesCount)
static List< Long > deleteSwitchRules(String switchId, DeleteRulesAction deleteAction)
static FlowCacheSyncResults invalidateFlowCache()
Definition: FlowUtils.java:691
void deleteAllNonDefaultRules(String switchId, int deletedFlowsCount)
void deleteRulesByInVlan(int inVlan, String switchId, int deletedFlowsCount)
static FlowCacheSyncResults syncFlowCache()
Definition: FlowUtils.java:660
static RulesSyncResult synchronizeSwitchRules(String switchId)
void checkDumpFlows(final int flowCount)
static FlowIdStatusPayload waitFlowStatus(String flowName, FlowState expected)
Definition: FlowUtils.java:325
static String getFlowName(final String flowId)
Definition: FlowUtils.java:595
void deleteAllDefaultRules(String switchId, int deletedFlowsCount)
void deleteRulesByOutPort(int outPort, String switchId, int deletedFlowsCount)
static RulesValidationResult validateSwitchRules(String switchId)