16 package org.openkilda.atdd.staging.steps;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
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;
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;
70 private List<IslInfoData> linksResponse;
77 @When(
"ISL between switches loses connectivity")
79 topologyUnderTest.getFlowIsls().forEach((flow, isls) -> {
85 aswitchService.removeFlows(Arrays.asList(aswFlowForward, aswFlowReverse));
86 changedIsls.add(islToRemove);
93 @When(
"Changed ISLs? restores? connectivity")
95 changedIsls.forEach(isl -> {
99 aswitchService.
addFlows(Arrays.asList(aswFlowForward, aswFlowReverse));
109 @Then(
"ISLs? status changes? to (\\w*)$")
114 @Then(
"ISLs? status is (\\w*)$")
117 changedIsls.forEach(isl -> {
119 PathNode src = link.getPath().get(0);
120 PathNode dst = link.getPath().get(1);
124 }).findFirst().get().getState();
125 assertEquals(expectedIslState, actualIslState);
129 @When(
"^request all available links from Northbound$")
134 @Then(
"^response has at least (\\d+) links?$")
136 assertTrue(linksResponse.size() >= linksAmount);
139 private RetryPolicy retryPolicy() {
140 return new RetryPolicy()
141 .withDelay(3, TimeUnit.SECONDS)
145 @Given(
"^select a random isl and alias it as '(.*)'$")
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);
154 private List<Isl> getUnaliasedIsls() {
155 List<Isl> aliasedIsls = topologyUnderTest.getAliasedObjects(Isl.class);
156 List<Isl> isls = (List<Isl>) CollectionUtils.subtract(
158 Assume.assumeTrue(
"No unaliased isls left, unable to proceed", !isls.isEmpty());
162 @Given(
"^select a random ISL with A-Switch and alias it as '(.*)'$")
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);
174 @And(
"^select a reverse path ISL for '(.*)' and alias it as '(.*)'$")
176 Isl theIsl = topologyUnderTest.getAliasedObject(islAlias);
177 Isl reversedIsl = islUtils.
reverseIsl(theIsl);
178 topologyUnderTest.
addAlias(newIslAlias, reversedIsl);
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);
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);
199 @Then(
"^ISL status changes to (.*) for ISLs: (.*)$")
201 List<Isl> isls = islAliases.stream()
202 .map(alias -> (Isl) topologyUnderTest.getAliasedObject(alias))
203 .collect(Collectors.toList());
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());
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);
221 }).findFirst().get().getState()).collect(Collectors.toList());
222 assertTrue(actualIslStates.stream().allMatch(state -> state.equals(expectedIslState)));
225 @And(
"^select a random not connected A-Switch link and alias it as '(.*)'$")
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);
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,
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(),
252 srcIsSource ? srcIsl.getAswitch().getInPort() : srcIsl.getAswitch().getOutPort(),
253 dstIsSource ? dstIsl.getAswitch().getInPort() : dstIsl.getAswitch().getOutPort()
255 topologyUnderTest.
addAlias(newAlias, newIsl);
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);
void requestAllAvailableLinksFromNorthbound()
void islsStatusChanges(String expectedStatus, List< String > islAliases)
void portsDown(List< Integer > ports)
void addFlows(List< ASwitchFlow > flows)
void portsDown(String isSourceStr, String islAlias)
void selectARandomIslWithASwitch(String islAlias)
void selectNotConnectedASwitchLink(String alias)
void islsStatusIs(String expectedStatus, List< String > islAliases)
List< Isl > getIslsForActiveSwitches()
void responseHasAtLeastLink(int linksAmount)
void portsUp(String isSourceStr, String islAlias)
void replug(String srcAlias, String srcIsSourceStr, String dstAlias, String dstIsSourceStr)
void addAlias(String alias, Object obj)
void potentialIsl(String srcAlias, String srcIsSourceStr, String dstAlias, String dstIsSourceStr, String newAlias)
void selectAReversePathIsl(String islAlias, String newIslAlias)
void selectARandomIslAndAliasItAsIsl(String islAlias)
void checkIslStatus(String islStatus)
void waitForIslStatus(String islStatus)
List< IslInfoData > getAllLinks()
void portsUp(List< Integer > ports)
List< Isl > getNotConnectedIsls()