Open Kilda Java Documentation
FlowPathTest.java
Go to the documentation of this file.
1 /* Copyright 2017 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;
17 
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;
23 import static org.openkilda.flow.FlowUtils.dumpFlows;
27 
28 import org.openkilda.LinksUtils;
40 
41 import cucumber.api.java.en.Given;
42 import cucumber.api.java.en.Then;
43 import cucumber.api.java.en.When;
44 
45 import java.io.File;
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;
51 
52 
53 public class FlowPathTest {
54  private static final String fileName = "topologies/multi-path-topology.json";
55  private static final List<ImmutablePair<String, String>> shortestPathLinks = Arrays.asList(
56  new ImmutablePair<>("00:00:00:00:00:00:00:07", "1"), new ImmutablePair<>("00:00:00:00:00:00:00:03", "2"),
57  new ImmutablePair<>("00:00:00:00:00:00:00:02", "2"), new ImmutablePair<>("00:00:00:00:00:00:00:03", "1"));
58  private static final List<ImmutablePair<String, String>> alternativePathLinks = Arrays.asList(
59  new ImmutablePair<>("00:00:00:00:00:00:00:02", "3"), new ImmutablePair<>("00:00:00:00:00:00:00:04", "1"),
60  new ImmutablePair<>("00:00:00:00:00:00:00:05", "1"), new ImmutablePair<>("00:00:00:00:00:00:00:04", "2"),
61  new ImmutablePair<>("00:00:00:00:00:00:00:05", "2"), new ImmutablePair<>("00:00:00:00:00:00:00:06", "1"),
62  new ImmutablePair<>("00:00:00:00:00:00:00:06", "2"), new ImmutablePair<>("00:00:00:00:00:00:00:07", "3"));
63  static final ImmutablePair<PathInfoData, PathInfoData> expectedShortestPath = new ImmutablePair<>(
64  new PathInfoData(0L, Arrays.asList(
65  new PathNode(new SwitchId(2L), 2, 0, 0L),
66  new PathNode(new SwitchId(3L), 1, 1, 0L),
67  new PathNode(new SwitchId(3L), 2, 2, 0L),
68  new PathNode(new SwitchId(7L), 1, 3, 0L))),
69  new PathInfoData(0L, Arrays.asList(
70  new PathNode(new SwitchId(7L), 1, 0, 0L),
71  new PathNode(new SwitchId(3L), 2, 1, 0L),
72  new PathNode(new SwitchId(3L), 1, 2, 0L),
73  new PathNode(new SwitchId(2L), 2, 3, 0L))));
74  static final ImmutablePair<PathInfoData, PathInfoData> expectedAlternatePath = new ImmutablePair<>(
75  new PathInfoData(0L, Arrays.asList(
76  new PathNode(new SwitchId(2L), 3, 0, 0L),
77  new PathNode(new SwitchId(4L), 1, 1, 0L),
78  new PathNode(new SwitchId(4L), 2, 2, 0L),
79  new PathNode(new SwitchId(5L), 1, 3, 0L),
80  new PathNode(new SwitchId(5L), 2, 0, 0L),
81  new PathNode(new SwitchId(6L), 1, 1, 0L),
82  new PathNode(new SwitchId(6L), 2, 2, 0L),
83  new PathNode(new SwitchId(7L), 3, 3, 0L))),
84  new PathInfoData(0L, Arrays.asList(
85  new PathNode(new SwitchId(7L), 3, 3, 0L),
86  new PathNode(new SwitchId(6L), 2, 2, 0L),
87  new PathNode(new SwitchId(6L), 1, 1, 0L),
88  new PathNode(new SwitchId(5L), 2, 0, 0L),
89  new PathNode(new SwitchId(5L), 1, 3, 0L),
90  new PathNode(new SwitchId(4L), 2, 2, 0L),
91  new PathNode(new SwitchId(4L), 1, 1, 0L),
92  new PathNode(new SwitchId(2L), 3, 0, 0L))));
93 
94  private String previousLastUpdated;
95  private String actualFlowName;
96  private long preStart;
97  private long start;
98 
99  @Given("^a multi-path topology$")
100  public void multiPathTopology() {
101  ClassLoader classLoader = getClass().getClassLoader();
102  File file = new File(classLoader.getResource(fileName).getFile());
103  String json;
104 
105  try {
106  json = new String(Files.readAllBytes(file.toPath()));
107  } catch (IOException ex) {
108  throw new TopologyProcessingException(format("Unable to read the topology file '%s'.", fileName), ex);
109  }
110 
111  preStart = System.currentTimeMillis();
112  assertTrue(TopologyHelp.CreateMininetTopology(json));
113  start = System.currentTimeMillis();
114  }
115 
116  @When("^all links have available bandwidth (\\d+)$")
117  public void checkAvailableBandwidth(long expectedAvailableBandwidth) throws InterruptedException {
118  List<LinkDto> links = LinksUtils.dumpLinks();
119  for (LinkDto link : links) {
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);
124  }
125  }
126 
127  @Then("^shortest path links available bandwidth have available bandwidth (\\d+)$")
128  public void checkShortestPathAvailableBandwidthDecreased(long expectedAvailableBandwidth)
129  throws InterruptedException {
130  for (ImmutablePair<String, String> expectedLink : shortestPathLinks) {
131  long actualBandwidth = getBandwidth(expectedAvailableBandwidth,
132  new SwitchId(expectedLink.getLeft()), expectedLink.getRight());
133  assertEquals(expectedAvailableBandwidth, actualBandwidth);
134  }
135  }
136 
137  @Then("^alternative path links available bandwidth have available bandwidth (\\d+)$")
138  public void checkAlternativePathAvailableBandwidthDecreased(long expectedAvailableBandwidth)
139  throws InterruptedException {
140  for (ImmutablePair<String, String> expectedLink : alternativePathLinks) {
141  long actualBandwidth = getBandwidth(expectedAvailableBandwidth,
142  new SwitchId(expectedLink.getLeft()), expectedLink.getRight());
143  assertEquals(expectedAvailableBandwidth, actualBandwidth);
144  }
145  }
146 
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)
150  throws UnroutablePathException, InterruptedException, RecoverableException {
151  Flow flow =
152  new Flow(FlowUtils.getFlowName(flowId), bandwidth, false, flowId,
153  new SwitchId(sourceSwitch), sourcePort, sourceVlan, new SwitchId(destinationSwitch),
154  destinationPort, destinationVlan);
156  System.out.println(path);
157  assertEquals(expectedShortestPath, path);
158  }
159 
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);
165  }
166  return actualBandwidth;
167  }
168 
169  @Given("^topology contains (\\d+) links$")
170  public void topologyContainsLinks(int expectedLinks) throws InterruptedException {
171  // give WFM time to send discovery requests and notify TE.
172  TimeUnit.SECONDS.sleep(4);
173  waitForVerifiedLinks(expectedLinks);
174  }
175 
176  private void waitForVerifiedLinks(int expectedLinks) throws InterruptedException {
177  long actualLinks = 0;
178 
179  for (int i = 0; i < 10; i++) {
180  List<LinkDto> links = LinksUtils.dumpLinks();
181 
182  // Count verified and healthy links
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))
188  .count();
189 
190  if (actualLinks == expectedLinks) {
191  return;
192  }
193 
194  TimeUnit.SECONDS.sleep(3);
195  }
196 
197  assertEquals(expectedLinks, actualLinks);
198  }
199 
200  @When("^delete mininet topology$")
201  public void deleteMininetTopology() {
203  }
204 
205  @When("^(\\d+) seconds passed$")
206  public void secondsPassed(int timeout) throws InterruptedException {
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()));
211  }
212 
213  @Then("^flow (.*) has updated timestamp$")
214  public void flowRestoredHasValidLastUpdatedTimestamp(String flowId) throws Throwable {
215  List<Flow> flows = dumpFlows();
216 
217  if (flows == null || flows.isEmpty()) {
218  TimeUnit.SECONDS.sleep(2);
219  flows = dumpFlows();
220  }
221 
222  assertNotNull(flows);
223  assertEquals(2, flows.size());
224 
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));
229 
230  assertNotEquals(previousLastUpdated, currentLastUpdated);
231  previousLastUpdated = currentLastUpdated;
232  }
233 
234  @When("^restore flows$")
235  public void flowRestore() {
236  restoreFlows();
237  }
238 }
void topologyContainsLinks(int expectedLinks)
void checkAvailableBandwidth(long expectedAvailableBandwidth)
void checkShortestPathAvailableBandwidthDecreased(long expectedAvailableBandwidth)
static List< Flow > dumpFlows()
Definition: FlowUtils.java:444
static FlowPathPayload getFlowPath(final String flowId)
Definition: FlowUtils.java:275
static boolean CreateMininetTopology(String json)
void flowPathCorrect(String flowId, String sourceSwitch, int sourcePort, int sourceVlan, String destinationSwitch, int destinationPort, int destinationVlan, long bandwidth)
void checkAlternativePathAvailableBandwidthDecreased(long expectedAvailableBandwidth)
static boolean DeleteMininetTopology()
void flowRestoredHasValidLastUpdatedTimestamp(String flowId)
static String getFlowName(final String flowId)
Definition: FlowUtils.java:595
static Integer getLinkBandwidth(final SwitchId srcSwitch, final String srcPort)
Definition: FlowUtils.java:478