16 package org.openkilda.atdd;
18 import static java.lang.String.format;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
41 import cucumber.api.java.en.Given;
42 import cucumber.api.java.en.Then;
43 import cucumber.api.java.en.When;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.util.Arrays;
49 import java.util.List;
50 import java.util.concurrent.TimeUnit;
54 private static final String fileName =
"topologies/multi-path-topology.json";
55 private static final List<ImmutablePair<String, String>> shortestPathLinks = Arrays.asList(
58 private static final List<ImmutablePair<String, String>> alternativePathLinks = Arrays.asList(
94 private String previousLastUpdated;
95 private String actualFlowName;
96 private long preStart;
99 @Given(
"^a multi-path topology$")
101 ClassLoader classLoader = getClass().getClassLoader();
102 File file =
new File(classLoader.getResource(fileName).getFile());
106 json =
new String(Files.readAllBytes(file.toPath()));
107 }
catch (IOException ex) {
111 preStart = System.currentTimeMillis();
113 start = System.currentTimeMillis();
116 @When(
"^all links have available bandwidth (\\d+)$")
120 long actualBandwidth = getBandwidth(expectedAvailableBandwidth,
121 new SwitchId(link.getPath().get(0).getSwitchId()),
122 String.valueOf(link.getPath().get(0).getPortNo()));
123 assertEquals(expectedAvailableBandwidth, actualBandwidth);
127 @Then(
"^shortest path links available bandwidth have available bandwidth (\\d+)$")
129 throws InterruptedException {
131 long actualBandwidth = getBandwidth(expectedAvailableBandwidth,
132 new SwitchId(expectedLink.getLeft()), expectedLink.getRight());
133 assertEquals(expectedAvailableBandwidth, actualBandwidth);
137 @Then(
"^alternative path links available bandwidth have available bandwidth (\\d+)$")
139 throws InterruptedException {
141 long actualBandwidth = getBandwidth(expectedAvailableBandwidth,
142 new SwitchId(expectedLink.getLeft()), expectedLink.getRight());
143 assertEquals(expectedAvailableBandwidth, actualBandwidth);
147 @Then(
"^flow (.*) with (.*) (\\d+) (\\d+) and (.*) (\\d+) (\\d+) and (\\d+) path correct$")
148 public
void flowPathCorrect(String flowId, String sourceSwitch,
int sourcePort,
int sourceVlan,
149 String destinationSwitch,
int destinationPort,
int destinationVlan,
long bandwidth)
153 new SwitchId(sourceSwitch), sourcePort, sourceVlan,
new SwitchId(destinationSwitch),
154 destinationPort, destinationVlan);
156 System.out.println(
path);
157 assertEquals(expectedShortestPath,
path);
160 private long getBandwidth(
long expectedBandwidth,
SwitchId srcSwitch, String srcPort)
throws InterruptedException {
161 long actualBandwidth = getLinkBandwidth(srcSwitch, srcPort);
162 if (actualBandwidth != expectedBandwidth) {
163 TimeUnit.SECONDS.sleep(2);
164 actualBandwidth = getLinkBandwidth(srcSwitch, srcPort);
166 return actualBandwidth;
169 @Given(
"^topology contains (\\d+) links$")
172 TimeUnit.SECONDS.sleep(4);
173 waitForVerifiedLinks(expectedLinks);
176 private void waitForVerifiedLinks(
int expectedLinks)
throws InterruptedException {
177 long actualLinks = 0;
179 for (
int i = 0;
i < 10;
i++) {
183 actualLinks = links.stream()
184 .filter(link -> link.getState() != FAILED)
185 .filter(link -> link.getPath().stream()
186 .noneMatch(pathNode -> pathNode.getSeqId() == 0
187 && pathNode.getSegLatency() == null))
190 if (actualLinks == expectedLinks) {
194 TimeUnit.SECONDS.sleep(3);
197 assertEquals(expectedLinks, actualLinks);
200 @When(
"^delete mininet topology$")
205 @When(
"^(\\d+) seconds passed$")
207 System.out.println(
format(
"\n==> Sleep for %d seconds", timeout));
208 System.out.println(
format(
"===> Sleep start at = %d", System.currentTimeMillis()));
209 TimeUnit.SECONDS.sleep(timeout);
210 System.out.println(
format(
"===> Sleep end at = %d", System.currentTimeMillis()));
213 @Then(
"^flow (.*) has updated timestamp$")
215 List<Flow> flows = dumpFlows();
217 if (flows == null || flows.isEmpty()) {
218 TimeUnit.SECONDS.sleep(2);
222 assertNotNull(flows);
223 assertEquals(2, flows.size());
225 Flow flow = flows.get(0);
226 String currentLastUpdated = flow.getLastUpdated();
227 System.out.println(
format(
"=====> Flow %s previous timestamp = %s", flowId, previousLastUpdated));
228 System.out.println(
format(
"=====> Flow %s current timestamp = %s", flowId, currentLastUpdated));
230 assertNotEquals(previousLastUpdated, currentLastUpdated);
231 previousLastUpdated = currentLastUpdated;
234 @When(
"^restore flows$")
void topologyContainsLinks(int expectedLinks)
void checkAvailableBandwidth(long expectedAvailableBandwidth)
void checkShortestPathAvailableBandwidthDecreased(long expectedAvailableBandwidth)
static List< LinkDto > dumpLinks()
static List< Flow > dumpFlows()
static FlowPathPayload getFlowPath(final String flowId)
static boolean CreateMininetTopology(String json)
void flowPathCorrect(String flowId, String sourceSwitch, int sourcePort, int sourceVlan, String destinationSwitch, int destinationPort, int destinationVlan, long bandwidth)
void secondsPassed(int timeout)
void checkAlternativePathAvailableBandwidthDecreased(long expectedAvailableBandwidth)
static boolean DeleteMininetTopology()
void flowRestoredHasValidLastUpdatedTimestamp(String flowId)
static void restoreFlows()
static String getFlowName(final String flowId)
void deleteMininetTopology()
static Integer getLinkBandwidth(final SwitchId srcSwitch, final String srcPort)