16 package org.openkilda.atdd;
18 import static org.hamcrest.Matchers.equalTo;
19 import static org.hamcrest.Matchers.hasItems;
20 import static org.hamcrest.Matchers.hasProperty;
21 import static org.hamcrest.Matchers.is;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertThat;
24 import static org.junit.Assert.assertTrue;
35 import cucumber.api.PendingException;
36 import cucumber.api.java.en.Then;
37 import cucumber.api.java.en.When;
38 import net.jodah.failsafe.Failsafe;
39 import net.jodah.failsafe.RetryPolicy;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.commons.lang.math.NumberUtils;
42 import org.junit.Before;
44 import java.util.Comparator;
45 import java.util.List;
46 import java.util.concurrent.TimeUnit;
47 import java.util.stream.Collectors;
48 import java.util.stream.IntStream;
56 private LinkDto manipulatedLink;
60 manipulatedLink = null;
63 @When(
"^multiple links exist between all switches$")
65 List<String> switchIds = IntStream.range(1, 6)
67 .collect(Collectors.toList());
68 assertTrue(
"Switches should have multiple links",
69 getSwitchesWithoutMultipleLinks(switchIds).isEmpty());
72 private List<String> getSwitchesWithoutMultipleLinks(List<String> switches)
throws Exception {
75 return switches.stream()
76 .filter(sw -> isSwitchHasLessThanTwoLinks(sw, links))
77 .collect(Collectors.toList());
80 @When(
"^a link is dropped in the middle$")
83 manipulatedLink = getMiddleLink(links);
89 @Then(
"^the link will have no health checks$")
92 List<LinkDto> cutLinks = links.stream()
94 .collect(Collectors.toList());
96 assertFalse(
"Link should be cut", cutLinks.isEmpty());
97 assertThat(
"Only one link should be cut", cutLinks.size(), is(1));
100 @Then(
"^the link disappears from the topology engine in (\\d+) seconds\\.$")
102 List<LinkDto> cutLinks = Failsafe.with(
new RetryPolicy()
103 .withDelay(2, TimeUnit.SECONDS)
104 .withMaxDuration(timeout, TimeUnit.SECONDS)
105 .retryIf(links -> links instanceof List && ((List) links).isEmpty()))
108 .collect(Collectors.toList())
111 assertFalse(
"Link should be cut", cutLinks.isEmpty());
112 assertThat(
"Only one link should be cut", cutLinks,
113 hasItems(hasProperty(
"path", equalTo(manipulatedLink.getPath()))));
116 @When(
"^a link is added in the middle$")
119 LinkDto middleLink = getMiddleLink(links);
121 String srcSwitch = getSwitchName(middleLink.getPath().get(0).getSwitchId());
122 String dstSwitch = getSwitchName(middleLink.getPath().get(1).getSwitchId());
124 TimeUnit.SECONDS.sleep(2);
127 @Then(
"^the link will have health checks$")
130 throw new PendingException();
133 @Then(
"^the link appears in the topology engine\\.$")
136 assertThat(
"Amount of links should be 18 (initial 16 and 2 newly created)", links.size(), is(18));
139 @When(
"^a switch is dropped in the middle$")
142 SwitchDto middleSwitch = getMiddleSwitch(switches);
143 assertTrue(
"Should successfully knockout switch",
146 TimeUnit.SECONDS.sleep(1);
148 SwitchDto deactivatedSwitch = updatedSwitches.stream()
149 .filter(sw -> sw.getSwitchId().equals(middleSwitch.getSwitchId()))
150 .findFirst().orElseThrow(() ->
new IllegalStateException(
"Switch should exist"));
154 @Then(
"^all links through the dropped switch will have no health checks$")
157 throw new PendingException();
160 @Then(
"^the links disappear from the topology engine\\.$")
164 TimeUnit.SECONDS.sleep(15);
168 List<LinkDto> switchLinks = links.stream()
169 .filter(isl -> isLinkBelongToSwitch(middleSwitch.getSwitchId(), isl))
171 .collect(Collectors.toList());
172 assertTrue(
"Switch shouldn't have any active links", switchLinks.isEmpty());
175 @Then(
"^the switch disappears from the topology engine\\.$")
178 SwitchDto middleSwitch = getMiddleSwitch(switches);
184 @When(
"^a switch is added at the edge$")
186 assertTrue(
"Should add switch to mininet topology",
188 TimeUnit.SECONDS.sleep(1);
191 @When(
"^links are added between the new switch and its neighbor$")
196 SwitchDto switchWithoutLinks = switches.stream()
197 .filter(sw -> links.stream()
198 .anyMatch(isl -> isLinkBelongToSwitch(sw.getSwitchId(), isl)))
200 .orElseThrow(() ->
new IllegalStateException(
"At least one switch should exist"));
202 SwitchDto latestConnectedSwitch = switches.stream()
203 .sorted(Comparator.comparing(SwitchDto::getSwitchId).reversed())
207 getSwitchName(latestConnectedSwitch.getSwitchId())));
209 getSwitchName(latestConnectedSwitch.getSwitchId())));
210 TimeUnit.SECONDS.sleep(1);
213 @Then(
"^all links through the added switch will have health checks$")
216 throw new PendingException();
219 @Then(
"^now amount of switches is (\\d+)\\.$")
222 List<SwitchDto> activeSwitches = switchList.stream()
224 .collect(Collectors.toList());
226 assertThat(
"Switch should disappear from neo4j", activeSwitches.size(), is(switches));
229 private boolean isSwitchHasLessThanTwoLinks(String switchId, List<LinkDto> links) {
230 int inputsAmount = 0;
231 int outputsAmount = 0;
234 if (switchId.equalsIgnoreCase(
node.getSwitchId())) {
235 if (
node.getSeqId() == 0) {
237 }
else if (
node.getSeqId() == 1) {
245 return inputsAmount <= NumberUtils.INTEGER_ONE && outputsAmount <= NumberUtils.INTEGER_ONE;
248 private LinkDto getMiddleLink(List<LinkDto> links) {
249 return links.stream()
250 .sorted(Comparator.comparing((isl) -> isl.getPath().get(0).getSwitchId()))
251 .collect(Collectors.toList())
252 .
get((links.size() / 2) + 1);
255 private SwitchDto getMiddleSwitch(List<SwitchDto> switches) {
256 return switches.stream()
257 .sorted(Comparator.comparing(SwitchDto::getSwitchId))
258 .collect(Collectors.toList())
259 .
get(switches.size() / 2);
262 private String getSwitchName(String switchId) {
263 return switchId.replaceAll(
"(\\A.*?:.*?:.*?:.*?:)|[:]", StringUtils.EMPTY);
266 private boolean isLinkBelongToSwitch(String switchId, LinkDto isl) {
267 return switchId.equalsIgnoreCase(isl.getPath().get(0).getSwitchId())
268 || switchId.equalsIgnoreCase(isl.getPath().get(1).getSwitchId());
static List< SwitchDto > dumpSwitches()
static boolean islFail(String switchName, String portNo)
void the_link_will_have_no_health_checks()
static List< LinkDto > dumpLinks()
void allLinksThroughTheDroppedSwitchWillHaveNoHealthChecks()
void theLinkAppearsInTheTopologyEngine()
void the_link_will_have_health_checks()
void theSwitchDisappearsFromTheTopologyEngine()
void allLinksThroughTheAddedSwitchWillHaveHealthChecks()
void linkIsAddedInTheMiddle()
void theSwitchAppearsInTheTopologyEngine(int switches)
static boolean addSwitch(String switchName, String dpid)
void switchIsDroppedInTheMiddle()
void linkIsDroppedInTheMiddle()
void switchIsAddedAtTheEdge()
void theLinkDisappearsFromTheTopologyEngine(int timeout)
void theLinksDisappearFromTheTopologyEngine()
void multipleLinksExistBetweenAllSwitches()
static boolean knockoutSwitch(String switchName)
static boolean addLink(String srcSwitchName, String destSwitchName)
static String intToSwitchId(int i)
void linksAreAddedBetweenTheNewSwitchAndItsNeighbor()