Open Kilda Java Documentation
FlowFfrTest.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 org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
25 
26 import org.openkilda.LinksUtils;
35 
36 import cucumber.api.java.en.Given;
37 import cucumber.api.java.en.When;
38 import org.glassfish.jersey.client.ClientConfig;
39 import org.json.simple.JSONArray;
40 import org.json.simple.JSONObject;
41 import org.json.simple.parser.JSONParser;
42 
43 import java.io.File;
44 import java.net.URL;
45 import java.nio.file.Files;
46 import java.util.concurrent.TimeUnit;
47 import javax.ws.rs.client.Client;
48 import javax.ws.rs.client.ClientBuilder;
49 import javax.ws.rs.client.Entity;
50 import javax.ws.rs.core.Response;
51 
52 public class FlowFfrTest {
53  private static final int DEFAULT_DISCOVERY_INTERVAL = 10;
54  private static final SwitchId sourceSwitch = new SwitchId(2L);
55  private static final SwitchId destinationSwitch = new SwitchId(7L);
56  private static final Integer sourcePort = 1;
57  private static final Integer destinationPort = 2;
58  private static final Integer sourceVlan = 1000;
59  private static final Integer destinationVlan = 1000;
60  private static final int bandwidth = 1000;
61 
62  @Given("^basic multi-path topology$")
63  public void multiPathTopology() throws Throwable {
64  String fileName = "topologies/barebones-topology.json";
65  ClassLoader classLoader = getClass().getClassLoader();
66  URL resource = classLoader.getResource(fileName);
67  if (resource == null) {
68  throw new IllegalArgumentException(String.format("No such topology json file: %s", fileName));
69  }
70 
71  File file = new File(resource.getFile());
72  String json = new String(Files.readAllBytes(file.toPath()));
73  assertTrue(TopologyHelp.CreateMininetTopology(json));
74  }
75 
76  @When("^a flow (.*) is successfully created$")
77  public void successfulFlowCreation(String flowId) throws Throwable {
78  FlowPayload flowPayload = new FlowPayload(FlowUtils.getFlowName(flowId),
79  new FlowEndpointPayload(sourceSwitch, sourcePort, sourceVlan),
80  new FlowEndpointPayload(destinationSwitch, destinationPort, destinationVlan),
81  bandwidth, false, flowId, null, FlowState.UP.getState());
82 
83  FlowPayload response = FlowUtils.putFlow(flowPayload);
84  assertNotNull(response);
85  response.setLastUpdated(null);
86 
87  assertEquals(flowPayload, response);
88  System.out.println(response.toString());
89  TimeUnit.SECONDS.sleep(5);
90  }
91 
92  @When("^traffic flows through (.+) flow$")
93  public void trafficFlows(String flowId) throws Throwable {
94  assertTrue(trafficIsOk(true));
95  }
96 
97  @When("^traffic does not flow through (.+) flow$")
98  public void trafficNotFlows(String flowId) throws Throwable {
99  assertFalse(trafficIsOk(false));
100  }
101 
102  @When("^flow (.+) path is shortest$")
103  public void flowPathIsShortest(String flowId) throws Throwable {
104  String flowName = FlowUtils.getFlowName(flowId);
105 
106  FlowPathPayload payload = getFlowPath(flowName, FlowPathTest.expectedShortestPath.getLeft());
107  assertNotNull(payload);
108 
109  assertEquals(flowName, payload.getId());
110  assertEquals(FlowPathTest.expectedShortestPath.getLeft(), payload.getForwardPath());
111  }
112 
113  @When("^flow (.+) path is alternate$")
114  public void flowPathIsAlternate(String flowId) throws Throwable {
115  String flowName = FlowUtils.getFlowName(flowId);
116 
117  FlowPathPayload payload = getFlowPath(flowName, FlowPathTest.expectedAlternatePath.getLeft());
118  assertNotNull(payload);
119 
120  assertEquals(flowName, payload.getId());
121  assertEquals(FlowPathTest.expectedAlternatePath.getLeft(), payload.getForwardPath());
122  }
123 
124  @When("^a switch (.*) port (\\d+) is disabled")
125  public void switchPortDisable(String switchId, int portNo) throws Throwable {
126  String switchName = getSwitchName(switchId);
127  assertTrue(portDown(switchName, String.valueOf(portNo)));
128  TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
129  }
130 
131  @When("^a switch (.*) port (\\d+) is enabled")
132  public void switchPortEnable(String switchId, int portNo) throws Throwable {
133  String switchName = getSwitchName(switchId);
134  assertTrue(portUp(switchName, String.valueOf(portNo)));
135  TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
136  }
137 
138  @When("^a switch (.+) is disconnected$")
139  public void switchDisconnected(String switchId) throws Exception {
140  String switchName = getSwitchName(switchId);
141  assertTrue(disconnectSwitch(switchName));
142  TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
143  }
144 
145  @When("^a switch (.+) is connected$")
146  public void switchConnected(String switchId) throws Exception {
147  String controller = getController("topologies/multi-path-topology.json");
148  String switchName = getSwitchName(switchId);
149  assertTrue(connectSwitch(switchName, controller));
150  TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
151  }
152 
153  @When("^an isl switch (.*) port (\\d+) is failed$")
154  public void anIslSwitchPortFails(String switchId, int portNo) throws Throwable {
155  String switchName = getSwitchName(switchId);
156  assertTrue(LinksUtils.islFail(switchName, String.valueOf(portNo)));
157  TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
158  }
159 
160  @When("^an isl switch (.*) port (\\d+) is discovered")
161  public void anIslSwitchPortDiscovered(String switchId, int portNo) throws Throwable {
162  String switchName = getSwitchName(switchId);
163  assertTrue(islDiscovered(switchName, String.valueOf(portNo)));
164  TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
165  }
166 
167  private FlowPathPayload getFlowPath(String flowName, PathInfoData expectedPath) throws Exception {
168  FlowPathPayload payload = FlowUtils.getFlowPath(flowName);
169  for (int i = 0; i < 10; i++) {
170  payload = FlowUtils.getFlowPath(flowName);
171  if (payload != null && expectedPath.equals(payload.getForwardPath())) {
172  break;
173  }
174  TimeUnit.SECONDS.sleep(2);
175  }
176  return payload;
177  }
178 
179  private String getSwitchName(String switchId) {
180  String dpid = switchId.replaceAll(":", "");
181  Integer number = Integer.parseInt(dpid);
182  return String.format("s%d", number);
183  }
184 
185  private String getController(String topologyFile) throws Exception {
186  ClassLoader classLoader = getClass().getClassLoader();
187  URL resource = classLoader.getResource(topologyFile);
188  if (resource == null) {
189  throw new IllegalArgumentException(String.format("No such topology json file: %s", topologyFile));
190  }
191 
192  File file = new File(resource.getFile());
193  String json = new String(Files.readAllBytes(file.toPath()));
194  JSONParser parser = new JSONParser();
195  JSONObject jsonObject = (JSONObject) parser.parse(json);
196  JSONArray controllers = (JSONArray) jsonObject.get("controllers");
197 
198  JSONObject controller = (JSONObject) controllers.get(0);
199  return (String) controller.get("host");
200  }
201 
202  private boolean trafficIsOk(boolean expectedResult) throws Throwable {
203  if (isTrafficTestsEnabled()) {
204  System.out.println("=====> Send traffic");
205 
206  long current = System.currentTimeMillis();
207  Client client = ClientBuilder.newClient(new ClientConfig());
208  Response result = client
209  .target(trafficEndpoint)
210  .path("/checkflowtraffic")
211  .queryParam("srcswitch", "s1")
212  .queryParam("dstswitch", "s8")
213  .queryParam("srcport", "1")
214  .queryParam("dstport", "1")
215  .queryParam("srcvlan", "1000")
216  .queryParam("dstvlan", "1000")
217  .request()
218  .get();
219 
220  System.out.println(String.format("======> Response = %s", result.toString()));
221  System.out.println(String.format("======> Send traffic Time: %,.3f", getTimeDuration(current)));
222 
223  return result.getStatus() == 200;
224  } else {
225  return expectedResult;
226  }
227  }
228 
229  private boolean disconnectSwitch(String switchName) throws Exception {
230  System.out.println("\n==> Disconnect Switch");
231 
232  long current = System.currentTimeMillis();
233  Client client = ClientBuilder.newClient(new ClientConfig());
234  Response result = client
235  .target(trafficEndpoint)
236  .path("/knockoutswitch")
237  .queryParam("switch", switchName)
238  .request()
239  .post(Entity.json(""));
240 
241  System.out.println(String.format("===> Response = %s", result.toString()));
242  System.out.println(String.format("===> Disconnect Switch Time: %,.3f", getTimeDuration(current)));
243 
244  return result.getStatus() == 200;
245  }
246 
247  private boolean connectSwitch(String switchName, String controller) throws Exception {
248  System.out.println("\n==> Connect Switch");
249 
250  long current = System.currentTimeMillis();
251  Client client = ClientBuilder.newClient(new ClientConfig());
252  Response result = client
253  .target(trafficEndpoint)
254  .path("/reviveswitch")
255  .queryParam("switch", switchName)
256  .queryParam("controller", "tcp:" + controller + ":6653")
257  .request()
258  .post(Entity.json(""));
259 
260  System.out.println(String.format("===> Response = %s", result.toString()));
261  System.out.println(String.format("===> Connect Switch Time: %,.3f", getTimeDuration(current)));
262 
263  return result.getStatus() == 200;
264  }
265 
266  private boolean islDiscovered(String switchName, String portNo) throws Throwable {
267  System.out.println("\n==> Set ISL Discovered");
268 
269  long current = System.currentTimeMillis();
270  Client client = ClientBuilder.newClient(new ClientConfig());
271  Response result = client
272  .target(trafficEndpoint)
273  .path("/restorelink")
274  .queryParam("switch", switchName)
275  .queryParam("port", portNo)
276  .request()
277  .post(Entity.json(""));
278 
279  System.out.println(String.format("===> Response = %s", result.toString()));
280  System.out.println(String.format("===> Set ISL Discovered Time: %,.3f", getTimeDuration(current)));
281 
282  return result.getStatus() == 200;
283  }
284 
285  private boolean portDown(String switchName, String portNo) throws Throwable {
286  System.out.println("\n==> Set Port Down");
287 
288  long current = System.currentTimeMillis();
289  Client client = ClientBuilder.newClient(new ClientConfig());
290  Response result = client
291  .target(trafficEndpoint)
292  .path("/port/down")
293  .queryParam("switch", switchName)
294  .queryParam("port", portNo)
295  .request()
296  .post(Entity.json(""));
297 
298  System.out.println(String.format("===> Response = %s", result.toString()));
299  System.out.println(String.format("===> Set Port Down Time: %,.3f", getTimeDuration(current)));
300 
301  return result.getStatus() == 200;
302  }
303 
304  private boolean portUp(String switchName, String portNo) throws Throwable {
305  System.out.println("\n==> Set Port Up");
306 
307  long current = System.currentTimeMillis();
308  Client client = ClientBuilder.newClient(new ClientConfig());
309  Response result = client
310  .target(trafficEndpoint)
311  .path("/port/up")
312  .queryParam("switch", switchName)
313  .queryParam("port", portNo)
314  .request()
315  .post(Entity.json(""));
316 
317  System.out.println(String.format("===> Response = %s", result.toString()));
318  System.out.println(String.format("===> Set Port Up Time: %,.3f", getTimeDuration(current)));
319 
320  return result.getStatus() == 200;
321  }
322 }
void switchConnected(String switchId)
void trafficFlows(String flowId)
static boolean isTrafficTestsEnabled()
Definition: FlowUtils.java:612
void flowPathIsShortest(String flowId)
void flowPathIsAlternate(String flowId)
static FlowPathPayload getFlowPath(final String flowId)
Definition: FlowUtils.java:275
void anIslSwitchPortFails(String switchId, int portNo)
static boolean CreateMininetTopology(String json)
void switchDisconnected(String switchId)
list result
Definition: plan-d.py:72
void switchPortDisable(String switchId, int portNo)
static FlowPayload putFlow(final FlowPayload payload)
Definition: FlowUtils.java:163
void successfulFlowCreation(String flowId)
void switchPortEnable(String switchId, int portNo)
void anIslSwitchPortDiscovered(String switchId, int portNo)
void trafficNotFlows(String flowId)
static String getFlowName(final String flowId)
Definition: FlowUtils.java:595
static double getTimeDuration(final long current)
Definition: FlowUtils.java:605