Open Kilda Java Documentation
SwitchSteps.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.steps;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20 
28 
29 import cucumber.api.java.en.And;
30 import cucumber.api.java.en.Given;
31 import cucumber.api.java.en.Then;
32 import cucumber.api.java.en.When;
33 import lombok.extern.slf4j.Slf4j;
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.junit.Assume;
36 import org.springframework.beans.factory.annotation.Autowired;
37 
38 import java.util.List;
39 
40 @Slf4j
41 public class SwitchSteps {
42 
43  @Autowired
44  private NorthboundService northboundService;
45 
46  @Autowired
47  private TopologyDefinition topologyDefinition;
48 
49  @Autowired
50  private TopologyUnderTest topologyUnderTest;
51 
52  private List<SwitchInfoData> allSwitchesResponse;
53  private SwitchFlowEntries switchRulesResponse;
54 
55  @When("^request all available switches from Northbound$")
56  public void requestSwitches() {
57  allSwitchesResponse = northboundService.getAllSwitches();
58  }
59 
60  @Then("response has at least (\\d+) switch(?:es)?")
61  public void verifySwitchesAmount(int expectedSwitchesAmount) {
62  assertTrue(allSwitchesResponse.size() >= expectedSwitchesAmount);
63  }
64 
65  @Given("^select a switch and alias it as '(.*)'$")
66  public void selectARandomSwitch(String switchAlias) {
67  List<Switch> switches = getUnaliasedSwitches();
68  Assume.assumeFalse("All switches are already aliased", CollectionUtils.isEmpty(switches));
69  Switch theSwitch = switches.get(0);
70  log.info("Selected switch with id: {}", theSwitch.getDpId());
71  topologyUnderTest.addAlias(switchAlias, theSwitch);
72  }
73 
74  @Given("^select a switch with Openflow version '(.*)' and alias it as '(.*)'$")
75  public void selectARandomSwitchWithSpecificOfVersion(String ofVersion, String switchAlias) {
76  List<Switch> switches = getUnaliasedSwitches();
77 
78  for (Switch s: switches) {
79  if (ofVersion.equalsIgnoreCase(s.getOfVersion())) {
80  log.info("Selected switch with id: {}", s.getDpId());
81  topologyUnderTest.addAlias(switchAlias, s);
82  return;
83  }
84  }
85  Assume.assumeTrue("No switches found with OpenFlow version " + ofVersion, false);
86  }
87 
88  @When("^request all switch rules for switch '(.*)'$")
89  public void requestAllSwitchRulesForSwitch(String switchAlias) {
90  switchRulesResponse = northboundService.getSwitchRules(
91  ((Switch) topologyUnderTest.getAliasedObject(switchAlias)).getDpId());
92  }
93 
94  @Then("^response switch_id matches id of '(.*)'$")
95  public void responseSwitch_idMatchesIdOfSwitch(String switchAlias) {
96  assertEquals(((Switch) topologyUnderTest.getAliasedObject(switchAlias)).getDpId(),
97  switchRulesResponse.getSwitchId());
98  }
99 
100  @Then("^response has at least (\\d+) rules? installed$")
101  public void responseHasAtLeastRulesInstalled(int rulesAmount) {
102  assertTrue(switchRulesResponse.getFlowEntries().size() >= rulesAmount);
103  }
104 
105  @And("^select a switch with direct link to '(.*)' and alias it as '(.*)'$")
106  public void selectSwitchWithDirectLink(String nearSwitchAlias, String newSwitchAlias) {
107  Switch nearSwitch = topologyUnderTest.getAliasedObject(nearSwitchAlias);
108  Isl link = topologyDefinition.getIslsForActiveSwitches().stream().filter(isl ->
109  isl.getSrcSwitch().getDpId().equals(nearSwitch.getDpId())
110  || isl.getDstSwitch().getDpId().equals(nearSwitch.getDpId())).findAny().get();
111  Switch newSwitch = link.getSrcSwitch().getDpId().equals(nearSwitch.getDpId())
112  ? link.getDstSwitch() : link.getDstSwitch();
113  topologyUnderTest.addAlias(newSwitchAlias, newSwitch);
114  }
115 
116  private List<Switch> getUnaliasedSwitches() {
117  List<Switch> aliasedSwitches = topologyUnderTest.getAliasedObjects(Switch.class);
118  List<Switch> switches = (List<Switch>) CollectionUtils.subtract(
119  topologyDefinition.getActiveSwitches(), aliasedSwitches);
120  Assume.assumeTrue("No unaliased switches left, unable to proceed", !switches.isEmpty());
121  return switches;
122  }
123 }
void responseSwitch_idMatchesIdOfSwitch(String switchAlias)
void selectARandomSwitch(String switchAlias)
void responseHasAtLeastRulesInstalled(int rulesAmount)
void requestAllSwitchRulesForSwitch(String switchAlias)
void selectARandomSwitchWithSpecificOfVersion(String ofVersion, String switchAlias)
void selectSwitchWithDirectLink(String nearSwitchAlias, String newSwitchAlias)
void verifySwitchesAmount(int expectedSwitchesAmount)
SwitchFlowEntries getSwitchRules(SwitchId switchId)