Open Kilda Java Documentation
IslSteps.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 
32 
33 import cucumber.api.java.en.And;
34 import cucumber.api.java.en.Given;
35 import cucumber.api.java.en.Then;
36 import cucumber.api.java.en.When;
37 import lombok.extern.slf4j.Slf4j;
38 import net.jodah.failsafe.RetryPolicy;
39 import org.apache.commons.collections4.CollectionUtils;
40 import org.junit.Assume;
41 import org.springframework.beans.factory.annotation.Autowired;
42 
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.Random;
48 import java.util.concurrent.TimeUnit;
49 import java.util.stream.Collectors;
50 
51 @Slf4j
52 public class IslSteps {
53 
54  @Autowired
55  private NorthboundService northboundService;
56 
57  @Autowired
58  private ASwitchService aswitchService;
59 
60  @Autowired
61  private TopologyUnderTest topologyUnderTest;
62 
63  @Autowired
64  private TopologyDefinition topologyDefinition;
65 
66  @Autowired
67  private IslUtils islUtils;
68 
69  private List<TopologyDefinition.Isl> changedIsls = new ArrayList<>();
70  private List<IslInfoData> linksResponse;
71 
77  @When("ISL between switches loses connectivity")
78  public void transitIslDown() {
79  topologyUnderTest.getFlowIsls().forEach((flow, isls) -> {
80  TopologyDefinition.Isl islToRemove = isls.stream().filter(isl -> isl.getAswitch() != null)
81  .findFirst().get();
82  TopologyDefinition.ASwitch aswitch = islToRemove.getAswitch();
83  ASwitchFlow aswFlowForward = new ASwitchFlow(aswitch.getInPort(), aswitch.getOutPort());
84  ASwitchFlow aswFlowReverse = new ASwitchFlow(aswitch.getOutPort(), aswitch.getInPort());
85  aswitchService.removeFlows(Arrays.asList(aswFlowForward, aswFlowReverse));
86  changedIsls.add(islToRemove);
87  });
88  }
89 
93  @When("Changed ISLs? restores? connectivity")
94  public void transitIslUp() {
95  changedIsls.forEach(isl -> {
96  TopologyDefinition.ASwitch aswitch = isl.getAswitch();
97  ASwitchFlow aswFlowForward = new ASwitchFlow(aswitch.getInPort(), aswitch.getOutPort());
98  ASwitchFlow aswFlowReverse = new ASwitchFlow(aswitch.getOutPort(), aswitch.getInPort());
99  aswitchService.addFlows(Arrays.asList(aswFlowForward, aswFlowReverse));
100  });
101  }
102 
109  @Then("ISLs? status changes? to (\\w*)$")
110  public void waitForIslStatus(String islStatus) {
111  islUtils.waitForIslStatus(changedIsls, islStatus);
112  }
113 
114  @Then("ISLs? status is (\\w*)$")
115  public void checkIslStatus(String islStatus) {
116  IslChangeType expectedIslState = IslChangeType.valueOf(islStatus);
117  changedIsls.forEach(isl -> {
118  IslChangeType actualIslState = northboundService.getAllLinks().stream().filter(link -> {
119  PathNode src = link.getPath().get(0);
120  PathNode dst = link.getPath().get(1);
121  return src.getPortNo() == isl.getSrcPort() && dst.getPortNo() == isl.getDstPort()
122  && src.getSwitchId().equals(isl.getSrcSwitch().getDpId())
123  && dst.getSwitchId().equals(isl.getDstSwitch().getDpId());
124  }).findFirst().get().getState();
125  assertEquals(expectedIslState, actualIslState);
126  });
127  }
128 
129  @When("^request all available links from Northbound$")
131  linksResponse = northboundService.getAllLinks();
132  }
133 
134  @Then("^response has at least (\\d+) links?$")
135  public void responseHasAtLeastLink(int linksAmount) {
136  assertTrue(linksResponse.size() >= linksAmount);
137  }
138 
139  private RetryPolicy retryPolicy() {
140  return new RetryPolicy()
141  .withDelay(3, TimeUnit.SECONDS)
142  .withMaxRetries(15);
143  }
144 
145  @Given("^select a random isl and alias it as '(.*)'$")
146  public void selectARandomIslAndAliasItAsIsl(String islAlias) {
147  List<Isl> isls = getUnaliasedIsls();
148  Random r = new Random();
149  Isl theIsl = isls.get(r.nextInt(isls.size()));
150  log.info("Selected random isl: {}", theIsl.toString());
151  topologyUnderTest.addAlias(islAlias, theIsl);
152  }
153 
154  private List<Isl> getUnaliasedIsls() {
155  List<Isl> aliasedIsls = topologyUnderTest.getAliasedObjects(Isl.class);
156  List<Isl> isls = (List<Isl>) CollectionUtils.subtract(
157  topologyDefinition.getIslsForActiveSwitches(), aliasedIsls);
158  Assume.assumeTrue("No unaliased isls left, unable to proceed", !isls.isEmpty());
159  return isls;
160  }
161 
162  @Given("^select a random ISL with A-Switch and alias it as '(.*)'$")
163  public void selectARandomIslWithASwitch(String islAlias) {
164  List<Isl> isls = getUnaliasedIsls().stream()
165  .filter(isl -> isl.getAswitch() != null && isl.getAswitch().getInPort() != null
166  && isl.getAswitch().getOutPort() != null)
167  .collect(Collectors.toList());
168  Random r = new Random();
169  Isl theIsl = isls.get(r.nextInt(isls.size()));
170  log.info("Selected random isl with A-switch: {}", theIsl.toString());
171  topologyUnderTest.addAlias(islAlias, theIsl);
172  }
173 
174  @And("^select a reverse path ISL for '(.*)' and alias it as '(.*)'$")
175  public void selectAReversePathIsl(String islAlias, String newIslAlias) {
176  Isl theIsl = topologyUnderTest.getAliasedObject(islAlias);
177  Isl reversedIsl = islUtils.reverseIsl(theIsl);
178  topologyUnderTest.addAlias(newIslAlias, reversedIsl);
179  }
180 
181  @When("^(source|destination) port for ISL '(.*)' goes down$")
182  public void portsDown(String isSourceStr, String islAlias) {
183  boolean isSourcePort = "source".equals(isSourceStr);
184  ASwitch aswitch = ((Isl) topologyUnderTest.getAliasedObject(islAlias)).getAswitch();
185  List<Integer> portsToBringDown = Collections.singletonList(
186  isSourcePort ? aswitch.getInPort() : aswitch.getOutPort());
187  aswitchService.portsDown(portsToBringDown);
188  }
189 
190  @When("^(source|destination) port for ISL '(.*)' goes up")
191  public void portsUp(String isSourceStr, String islAlias) {
192  boolean isSourcePort = "source".equals(isSourceStr);
193  ASwitch aswitch = ((Isl) topologyUnderTest.getAliasedObject(islAlias)).getAswitch();
194  List<Integer> portsToBringUp = Collections.singletonList(
195  isSourcePort ? aswitch.getInPort() : aswitch.getOutPort());
196  aswitchService.portsUp(portsToBringUp);
197  }
198 
199  @Then("^ISL status changes to (.*) for ISLs: (.*)$")
200  public void islsStatusChanges(String expectedStatus, List<String> islAliases) {
201  List<Isl> isls = islAliases.stream()
202  .map(alias -> (Isl) topologyUnderTest.getAliasedObject(alias))
203  .collect(Collectors.toList());
204  islUtils.waitForIslStatus(isls, expectedStatus);
205  }
206 
207  @Then("^ISL status is (.*) for ISLs: (.*)$")
208  public void islsStatusIs(String expectedStatus, List<String> islAliases) {
209  List<Isl> isls = islAliases.stream()
210  .map(alias -> (Isl) topologyUnderTest.getAliasedObject(alias))
211  .collect(Collectors.toList());
212  IslChangeType expectedIslState = IslChangeType.valueOf(expectedStatus);
213 
214  List<IslInfoData> allLinks = northboundService.getAllLinks();
215  List<IslChangeType> actualIslStates = isls.stream().map(isl -> allLinks.stream().filter(link -> {
216  PathNode src = link.getPath().get(0);
217  PathNode dst = link.getPath().get(1);
218  return src.getPortNo() == isl.getSrcPort() && dst.getPortNo() == isl.getDstPort()
219  && src.getSwitchId().equals(isl.getSrcSwitch().getDpId())
220  && dst.getSwitchId().equals(isl.getDstSwitch().getDpId());
221  }).findFirst().get().getState()).collect(Collectors.toList());
222  assertTrue(actualIslStates.stream().allMatch(state -> state.equals(expectedIslState)));
223  }
224 
225  @And("^select a random not connected A-Switch link and alias it as '(.*)'$")
226  public void selectNotConnectedASwitchLink(String alias) {
227  List<Isl> links = topologyDefinition.getNotConnectedIsls().stream()
228  .filter(isl -> isl.getSrcSwitch().isActive()
229  && isl.getAswitch() != null
230  && isl.getAswitch().getOutPort() == null)
231  .collect(Collectors.toList());
232  Random r = new Random();
233  Isl theLink = links.get(r.nextInt(links.size()));
234  log.info("Selecting link {}", theLink.toString());
235  topologyUnderTest.addAlias(alias, theLink);
236  }
237 
238  @And("^a potential ISL from '(.*)' (source|destination) to '(.*)' (source|destination) aliased as '(.*)'$")
239  public void potentialIsl(String srcAlias, String srcIsSourceStr, String dstAlias, String dstIsSourceStr,
240  String newAlias) {
241  final boolean srcIsSource = "source".equals(srcIsSourceStr);
242  final boolean dstIsSource = "source".equals(dstIsSourceStr);
243  Isl srcIsl = topologyUnderTest.getAliasedObject(srcAlias);
244  Isl dstIsl = topologyUnderTest.getAliasedObject(dstAlias);
245  Isl newIsl = Isl.factory(
246  srcIsSource ? srcIsl.getSrcSwitch() : srcIsl.getDstSwitch(),
247  srcIsSource ? srcIsl.getSrcPort() : srcIsl.getDstPort(),
248  dstIsSource ? dstIsl.getSrcSwitch() : dstIsl.getDstSwitch(),
249  dstIsSource ? dstIsl.getSrcPort() : dstIsl.getDstPort(),
250  0,
251  new ASwitch(
252  srcIsSource ? srcIsl.getAswitch().getInPort() : srcIsl.getAswitch().getOutPort(),
253  dstIsSource ? dstIsl.getAswitch().getInPort() : dstIsl.getAswitch().getOutPort()
254  ));
255  topologyUnderTest.addAlias(newAlias, newIsl);
256  }
257 
258  @When("^replug '(.*)' (source|destination) to '(.*)' (source|destination)$")
259  public void replug(String srcAlias, String srcIsSourceStr, String dstAlias, String dstIsSourceStr) {
260  final boolean dstIsSource = "source".equals(dstIsSourceStr);
261  final boolean srcIsSource = "source".equals(srcIsSourceStr);
262  Isl srcIsl = topologyUnderTest.getAliasedObject(srcAlias);
263  Isl dstIsl = topologyUnderTest.getAliasedObject(dstAlias);
264  islUtils.replug(srcIsl, srcIsSource, dstIsl, dstIsSource);
265  }
266 }
void islsStatusChanges(String expectedStatus, List< String > islAliases)
Definition: IslSteps.java:200
void portsDown(String isSourceStr, String islAlias)
Definition: IslSteps.java:182
TopologyDefinition.Isl replug(TopologyDefinition.Isl srcIsl, boolean replugSource, TopologyDefinition.Isl dstIsl, boolean plugIntoSource)
Definition: IslUtils.java:130
void selectARandomIslWithASwitch(String islAlias)
Definition: IslSteps.java:163
void selectNotConnectedASwitchLink(String alias)
Definition: IslSteps.java:226
void islsStatusIs(String expectedStatus, List< String > islAliases)
Definition: IslSteps.java:208
void responseHasAtLeastLink(int linksAmount)
Definition: IslSteps.java:135
void portsUp(String isSourceStr, String islAlias)
Definition: IslSteps.java:191
void replug(String srcAlias, String srcIsSourceStr, String dstAlias, String dstIsSourceStr)
Definition: IslSteps.java:259
void potentialIsl(String srcAlias, String srcIsSourceStr, String dstAlias, String dstIsSourceStr, String newAlias)
Definition: IslSteps.java:239
void selectAReversePathIsl(String islAlias, String newIslAlias)
Definition: IslSteps.java:175
void selectARandomIslAndAliasItAsIsl(String islAlias)
Definition: IslSteps.java:146
void waitForIslStatus(List< Isl > isls, String expectedStatus, RetryPolicy retryPolicy)
Definition: IslUtils.java:60
void checkIslStatus(String islStatus)
Definition: IslSteps.java:115
void waitForIslStatus(String islStatus)
Definition: IslSteps.java:110
net
Definition: plan-b.py:46