16 package org.openkilda.atdd;
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;
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;
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;
53 private static final int DEFAULT_DISCOVERY_INTERVAL = 10;
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;
62 @Given(
"^basic multi-path topology$")
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));
71 File file =
new File(resource.getFile());
72 String json =
new String(Files.readAllBytes(file.toPath()));
76 @When(
"^a flow (.*) is successfully created$")
81 bandwidth,
false, flowId, null,
FlowState.
UP.getState());
84 assertNotNull(response);
85 response.setLastUpdated(null);
87 assertEquals(flowPayload, response);
88 System.out.println(response.toString());
89 TimeUnit.SECONDS.sleep(5);
92 @When(
"^traffic flows through (.+) flow$")
94 assertTrue(trafficIsOk(
true));
97 @When(
"^traffic does not flow through (.+) flow$")
99 assertFalse(trafficIsOk(
false));
102 @When(
"^flow (.+) path is shortest$")
107 assertNotNull(payload);
109 assertEquals(flowName, payload.getId());
110 assertEquals(
FlowPathTest.expectedShortestPath.getLeft(), payload.getForwardPath());
113 @When(
"^flow (.+) path is alternate$")
118 assertNotNull(payload);
120 assertEquals(flowName, payload.getId());
121 assertEquals(
FlowPathTest.expectedAlternatePath.getLeft(), payload.getForwardPath());
124 @When(
"^a switch (.*) port (\\d+) is disabled")
126 String switchName = getSwitchName(switchId);
127 assertTrue(portDown(switchName, String.valueOf(portNo)));
128 TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
131 @When(
"^a switch (.*) port (\\d+) is enabled")
133 String switchName = getSwitchName(switchId);
134 assertTrue(portUp(switchName, String.valueOf(portNo)));
135 TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
138 @When(
"^a switch (.+) is disconnected$")
140 String switchName = getSwitchName(switchId);
141 assertTrue(disconnectSwitch(switchName));
142 TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
145 @When(
"^a switch (.+) is connected$")
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);
153 @When(
"^an isl switch (.*) port (\\d+) is failed$")
155 String switchName = getSwitchName(switchId);
157 TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
160 @When(
"^an isl switch (.*) port (\\d+) is discovered")
162 String switchName = getSwitchName(switchId);
163 assertTrue(islDiscovered(switchName, String.valueOf(portNo)));
164 TimeUnit.SECONDS.sleep(DEFAULT_DISCOVERY_INTERVAL);
169 for (
int i = 0;
i < 10;
i++) {
171 if (payload != null && expectedPath.equals(payload.getForwardPath())) {
174 TimeUnit.SECONDS.sleep(2);
179 private String getSwitchName(String switchId) {
180 String dpid = switchId.replaceAll(
":",
"");
181 Integer number = Integer.parseInt(dpid);
182 return String.format(
"s%d", number);
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));
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");
198 JSONObject controller = (JSONObject) controllers.get(0);
199 return (String) controller.get(
"host");
202 private boolean trafficIsOk(
boolean expectedResult)
throws Throwable {
203 if (isTrafficTestsEnabled()) {
204 System.out.println(
"=====> Send traffic");
206 long current = System.currentTimeMillis();
207 Client client = ClientBuilder.newClient(
new ClientConfig());
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")
220 System.out.println(String.format(
"======> Response = %s",
result.toString()));
221 System.out.println(String.format(
"======> Send traffic Time: %,.3f", getTimeDuration(current)));
223 return result.getStatus() == 200;
225 return expectedResult;
229 private boolean disconnectSwitch(String switchName)
throws Exception {
230 System.out.println(
"\n==> Disconnect Switch");
232 long current = System.currentTimeMillis();
233 Client client = ClientBuilder.newClient(
new ClientConfig());
235 .target(trafficEndpoint)
236 .path(
"/knockoutswitch")
237 .queryParam(
"switch", switchName)
239 .post(Entity.json(
""));
241 System.out.println(String.format(
"===> Response = %s",
result.toString()));
242 System.out.println(String.format(
"===> Disconnect Switch Time: %,.3f", getTimeDuration(current)));
244 return result.getStatus() == 200;
247 private boolean connectSwitch(String switchName, String controller)
throws Exception {
248 System.out.println(
"\n==> Connect Switch");
250 long current = System.currentTimeMillis();
251 Client client = ClientBuilder.newClient(
new ClientConfig());
253 .target(trafficEndpoint)
254 .path(
"/reviveswitch")
255 .queryParam(
"switch", switchName)
256 .queryParam(
"controller",
"tcp:" + controller +
":6653")
258 .post(Entity.json(
""));
260 System.out.println(String.format(
"===> Response = %s",
result.toString()));
261 System.out.println(String.format(
"===> Connect Switch Time: %,.3f", getTimeDuration(current)));
263 return result.getStatus() == 200;
266 private boolean islDiscovered(String switchName, String portNo)
throws Throwable {
267 System.out.println(
"\n==> Set ISL Discovered");
269 long current = System.currentTimeMillis();
270 Client client = ClientBuilder.newClient(
new ClientConfig());
272 .target(trafficEndpoint)
273 .path(
"/restorelink")
274 .queryParam(
"switch", switchName)
275 .queryParam(
"port", portNo)
277 .post(Entity.json(
""));
279 System.out.println(String.format(
"===> Response = %s",
result.toString()));
280 System.out.println(String.format(
"===> Set ISL Discovered Time: %,.3f", getTimeDuration(current)));
282 return result.getStatus() == 200;
285 private boolean portDown(String switchName, String portNo)
throws Throwable {
286 System.out.println(
"\n==> Set Port Down");
288 long current = System.currentTimeMillis();
289 Client client = ClientBuilder.newClient(
new ClientConfig());
291 .target(trafficEndpoint)
293 .queryParam(
"switch", switchName)
294 .queryParam(
"port", portNo)
296 .post(Entity.json(
""));
298 System.out.println(String.format(
"===> Response = %s",
result.toString()));
299 System.out.println(String.format(
"===> Set Port Down Time: %,.3f", getTimeDuration(current)));
301 return result.getStatus() == 200;
304 private boolean portUp(String switchName, String portNo)
throws Throwable {
305 System.out.println(
"\n==> Set Port Up");
307 long current = System.currentTimeMillis();
308 Client client = ClientBuilder.newClient(
new ClientConfig());
310 .target(trafficEndpoint)
312 .queryParam(
"switch", switchName)
313 .queryParam(
"port", portNo)
315 .post(Entity.json(
""));
317 System.out.println(String.format(
"===> Response = %s",
result.toString()));
318 System.out.println(String.format(
"===> Set Port Up Time: %,.3f", getTimeDuration(current)));
320 return result.getStatus() == 200;
void switchConnected(String switchId)
static boolean islFail(String switchName, String portNo)
void trafficFlows(String flowId)
static boolean isTrafficTestsEnabled()
void flowPathIsShortest(String flowId)
void flowPathIsAlternate(String flowId)
static FlowPathPayload getFlowPath(final String flowId)
static final String trafficEndpoint
void anIslSwitchPortFails(String switchId, int portNo)
static boolean CreateMininetTopology(String json)
void switchDisconnected(String switchId)
void switchPortDisable(String switchId, int portNo)
static FlowPayload putFlow(final FlowPayload payload)
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)
static double getTimeDuration(final long current)