1 package org.opendaylight.controller.northbound.integrationtest;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
6 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
7 import static org.ops4j.pax.exam.CoreOptions.options;
8 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
9 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
11 import java.io.BufferedReader;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.OutputStreamWriter;
15 import java.net.HttpURLConnection;
17 import java.nio.charset.Charset;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
24 import javax.inject.Inject;
26 import org.apache.commons.codec.binary.Base64;
27 import org.codehaus.jettison.json.JSONArray;
28 import org.codehaus.jettison.json.JSONException;
29 import org.codehaus.jettison.json.JSONObject;
30 import org.codehaus.jettison.json.JSONTokener;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.opendaylight.controller.hosttracker.IfIptoHost;
36 import org.opendaylight.controller.sal.core.Bandwidth;
37 import org.opendaylight.controller.sal.core.ConstructionException;
38 import org.opendaylight.controller.sal.core.Edge;
39 import org.opendaylight.controller.sal.core.Latency;
40 import org.opendaylight.controller.sal.core.Node;
41 import org.opendaylight.controller.sal.core.NodeConnector;
42 import org.opendaylight.controller.sal.core.Property;
43 import org.opendaylight.controller.sal.core.State;
44 import org.opendaylight.controller.sal.core.UpdateType;
45 import org.opendaylight.controller.sal.topology.IListenTopoUpdates;
46 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
47 import org.opendaylight.controller.switchmanager.IInventoryListener;
48 import org.opendaylight.controller.usermanager.IUserManager;
49 import org.ops4j.pax.exam.Option;
50 import org.ops4j.pax.exam.junit.Configuration;
51 import org.ops4j.pax.exam.junit.PaxExam;
52 import org.ops4j.pax.exam.util.PathUtils;
53 import org.osgi.framework.Bundle;
54 import org.osgi.framework.BundleContext;
55 import org.osgi.framework.ServiceReference;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 @RunWith(PaxExam.class)
60 public class NorthboundIT {
61 private final Logger log = LoggerFactory.getLogger(NorthboundIT.class);
62 // get the OSGI bundle context
64 private BundleContext bc;
65 private IUserManager userManager = null;
66 private IInventoryListener invtoryListener = null;
67 private IListenTopoUpdates topoUpdates = null;
69 private final Boolean debugMsg = false;
71 private String stateToString(int state) {
75 case Bundle.INSTALLED:
79 case Bundle.UNINSTALLED:
82 return "Not CONVERTED";
87 public void areWeReady() {
89 boolean debugit = false;
90 Bundle b[] = bc.getBundles();
91 for (int i = 0; i < b.length; i++) {
92 int state = b[i].getState();
93 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
94 log.debug("Bundle:" + b[i].getSymbolicName() + " state:" + stateToString(state));
99 log.debug("Do some debugging because some bundle is " + "unresolved");
101 // Assert if true, if false we are good to go!
102 assertFalse(debugit);
104 ServiceReference r = bc.getServiceReference(IUserManager.class.getName());
106 this.userManager = (IUserManager) bc.getService(r);
108 // If UserManager is null, cannot login to run tests.
109 assertNotNull(this.userManager);
111 r = bc.getServiceReference(IfIptoHost.class.getName());
113 this.invtoryListener = (IInventoryListener) bc.getService(r);
116 // If inventoryListener is null, cannot run hosttracker tests.
117 assertNotNull(this.invtoryListener);
119 r = bc.getServiceReference(IListenTopoUpdates.class.getName());
121 this.topoUpdates = (IListenTopoUpdates) bc.getService(r);
124 // If topologyManager is null, cannot run topology North tests.
125 assertNotNull(this.topoUpdates);
129 // static variable to pass response code from getJsonResult()
130 private static Integer httpResponseCode = null;
132 private String getJsonResult(String restUrl) {
133 return getJsonResult(restUrl, "GET", null);
136 private String getJsonResult(String restUrl, String method) {
137 return getJsonResult(restUrl, method, null);
140 private String getJsonResult(String restUrl, String method, String body) {
141 // initialize response code to indicate error
142 httpResponseCode = 400;
145 System.out.println("HTTP method: " + method + " url: " + restUrl.toString());
147 System.out.println("body: " + body);
151 URL url = new URL(restUrl);
152 this.userManager.getAuthorizationList();
153 this.userManager.authenticate("admin", "admin");
154 String authString = "admin:admin";
155 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
156 String authStringEnc = new String(authEncBytes);
158 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
159 connection.setRequestMethod(method);
160 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
161 connection.setRequestProperty("Content-Type", "application/json");
162 connection.setRequestProperty("Accept", "application/json");
165 connection.setDoOutput(true);
166 OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
170 connection.connect();
171 connection.getContentType();
173 // Response code for success should be 2xx
174 httpResponseCode = connection.getResponseCode();
175 if (httpResponseCode > 299)
176 return httpResponseCode.toString();
179 System.out.println("HTTP response code: " + connection.getResponseCode());
180 System.out.println("HTTP response message: " + connection.getResponseMessage());
183 InputStream is = connection.getInputStream();
184 BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
185 StringBuilder sb = new StringBuilder();
187 while ((cp = rd.read()) != -1) {
188 sb.append((char) cp);
191 connection.disconnect();
192 return sb.toString();
193 } catch (Exception e) {
198 private void testNodeProperties(JSONObject node, Integer nodeId, String nodeType, Integer timestamp,
199 String timestampName, Integer actionsValue, Integer capabilitiesValue, Integer tablesValue,
200 Integer buffersValue) throws JSONException {
202 JSONObject nodeInfo = node.getJSONObject("node");
203 Assert.assertEquals(nodeId, (Integer) nodeInfo.getInt("id"));
204 Assert.assertEquals(nodeType, nodeInfo.getString("type"));
206 JSONObject properties = node.getJSONObject("properties");
208 if (timestamp == null || timestampName == null) {
209 Assert.assertFalse(properties.has("timeStamp"));
211 Assert.assertEquals(timestamp, (Integer) properties.getJSONObject("timeStamp").getInt("value"));
212 Assert.assertEquals(timestampName, properties.getJSONObject("timeStamp").getString("name"));
214 if (actionsValue == null) {
215 Assert.assertFalse(properties.has("actions"));
217 Assert.assertEquals(actionsValue, (Integer) properties.getJSONObject("actions").getInt("value"));
219 if (capabilitiesValue == null) {
220 Assert.assertFalse(properties.has("capabilities"));
222 Assert.assertEquals(capabilitiesValue,
223 (Integer) properties.getJSONObject("capabilities").getInt("value"));
225 if (tablesValue == null) {
226 Assert.assertFalse(properties.has("tables"));
228 Assert.assertEquals(tablesValue, (Integer) properties.getJSONObject("tables").getInt("value"));
230 if (buffersValue == null) {
231 Assert.assertFalse(properties.has("buffers"));
233 Assert.assertEquals(buffersValue, (Integer) properties.getJSONObject("buffers").getInt("value"));
237 private void testNodeConnectorProperties(JSONObject nodeConnectorProperties, Integer ncId, String ncType,
238 Integer nodeId, String nodeType, Integer state, Integer capabilities, Integer bandwidth)
239 throws JSONException {
241 JSONObject nodeConnector = nodeConnectorProperties.getJSONObject("nodeconnector");
242 JSONObject node = nodeConnector.getJSONObject("node");
243 JSONObject properties = nodeConnectorProperties.getJSONObject("properties");
245 Assert.assertEquals(ncId, (Integer) nodeConnector.getInt("id"));
246 Assert.assertEquals(ncType, nodeConnector.getString("type"));
247 Assert.assertEquals(nodeId, (Integer) node.getInt("id"));
248 Assert.assertEquals(nodeType, node.getString("type"));
250 Assert.assertFalse(properties.has("state"));
252 Assert.assertEquals(state, (Integer) properties.getJSONObject("state").getInt("value"));
254 if (capabilities == null) {
255 Assert.assertFalse(properties.has("capabilities"));
257 Assert.assertEquals(capabilities,
258 (Integer) properties.getJSONObject("capabilities").getInt("value"));
260 if (bandwidth == null) {
261 Assert.assertFalse(properties.has("bandwidth"));
263 Assert.assertEquals(bandwidth, (Integer) properties.getJSONObject("bandwidth").getInt("value"));
269 public void testSubnetsNorthbound() throws JSONException {
270 System.out.println("Starting Subnets JAXB client.");
271 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/subnet/";
273 String name1 = "testSubnet1";
274 String subnet1 = "1.1.1.1/24";
276 String name2 = "testSubnet2";
277 String subnet2 = "2.2.2.2/24";
278 String[] nodePorts2 = {"2/1", "2/2", "2/3", "2/4"};
279 StringBuilder nodePortsJson2 = new StringBuilder();
280 nodePortsJson2.append(nodePorts2[0] + "," + nodePorts2[1] + "," + nodePorts2[2] + "," + nodePorts2[3]);
282 String name3 = "testSubnet3";
283 String subnet3 = "3.3.3.3/24";
284 String[] nodePorts3 = {"3/1", "3/2", "3/3"};
285 StringBuilder nodePortsJson3 = new StringBuilder();
286 nodePortsJson3.append(nodePorts3[0] + "," + nodePorts3[1] + "," + nodePorts3[2]);
287 StringBuilder nodePortsJson3_1 = new StringBuilder();
288 nodePortsJson3_1.append(nodePortsJson3).append(",").append(nodePortsJson2);
290 // Test GET subnets in default container
291 String result = getJsonResult(baseURL + "default/subnet/all");
292 JSONTokener jt = new JSONTokener(result);
293 JSONObject json = new JSONObject(jt);
294 Assert.assertEquals("{}", result);
296 // Test GET subnet1 expecting 404
297 result = getJsonResult(baseURL + "default/subnet/" + name1);
298 Assert.assertEquals(404, httpResponseCode.intValue());
301 JSONObject jo = new JSONObject().append("name", name1).append("subnet", subnet1);
302 // execute HTTP request and verify response code
303 result = getJsonResult(baseURL + "default/subnet/" + name1, "POST", jo.toString());
304 Assert.assertTrue(httpResponseCode == 201);
307 result = getJsonResult(baseURL + "default/subnet/" + name1);
308 jt = new JSONTokener(result);
309 json = new JSONObject(jt);
310 Assert.assertEquals(200, httpResponseCode.intValue());
311 Assert.assertEquals(name1, json.getString("name"));
312 Assert.assertEquals(subnet1, json.getString("subnet"));
315 JSONObject jo2 = new JSONObject().append("name", name2).append("subnet", subnet2);
316 // execute HTTP request and verify response code
317 result = getJsonResult(baseURL + "default/subnet/" + name2, "POST", jo2.toString());
318 Assert.assertEquals(201, httpResponseCode.intValue());
319 // Test POST nodePorts
320 jo2.append("nodePorts", nodePortsJson2);
321 // execute HTTP request and verify response code
322 result = getJsonResult(baseURL + "default/subnet/" + name2 + "/node-ports", "POST", jo2.toString());
323 Assert.assertEquals(200, httpResponseCode.intValue());
325 JSONObject jo3 = new JSONObject().append("name", name3).append("subnet", subnet3);
326 // execute HTTP request and verify response code
327 result = getJsonResult(baseURL + "default/subnet/" + name3, "POST", jo3.toString());
328 Assert.assertEquals(201, httpResponseCode.intValue());
329 // Test POST nodePorts
330 jo3.append("nodePorts", nodePortsJson3);
331 // execute HTTP request and verify response code
332 result = getJsonResult(baseURL + "default/subnet/" + name3 + "/node-ports", "POST", jo3.toString());
333 Assert.assertEquals(200, httpResponseCode.intValue());
334 // Test PUT nodePorts
335 jo3.remove("nodePorts");
336 jo3.append("nodePorts", nodePortsJson3_1);
337 result = getJsonResult(baseURL + "default/subnet/" + name3 + "/node-ports", "PUT", jo3.toString());
338 Assert.assertEquals(200, httpResponseCode.intValue());
340 // Test GET all subnets in default container
341 result = getJsonResult(baseURL + "default/subnet/all");
342 jt = new JSONTokener(result);
343 json = new JSONObject(jt);
344 JSONArray subnetConfigArray = json.getJSONArray("subnetConfig");
345 JSONObject subnetConfig;
346 Assert.assertEquals(3, subnetConfigArray.length());
347 for (int i = 0; i < subnetConfigArray.length(); i++) {
348 subnetConfig = subnetConfigArray.getJSONObject(i);
349 if (subnetConfig.getString("name").equals(name1)) {
350 Assert.assertEquals(subnet1, subnetConfig.getString("subnet"));
351 } else if (subnetConfig.getString("name").equals(name2)) {
352 Assert.assertEquals(subnet2, subnetConfig.getString("subnet"));
353 String[] nodePortsGet2 = subnetConfig.getString("nodePorts").split(",");
354 Assert.assertEquals(nodePorts2[0], nodePortsGet2[0]);
355 Assert.assertEquals(nodePorts2[1], nodePortsGet2[1]);
356 Assert.assertEquals(nodePorts2[2], nodePortsGet2[2]);
357 Assert.assertEquals(nodePorts2[3], nodePortsGet2[3]);
358 } else if (subnetConfig.getString("name").equals(name3)) {
359 Assert.assertEquals(subnet3, subnetConfig.getString("subnet"));
360 String[] nodePortsGet = subnetConfig.getString("nodePorts").split(",");
361 Assert.assertEquals(nodePorts3[0], nodePortsGet[0]);
362 Assert.assertEquals(nodePorts3[1], nodePortsGet[1]);
363 Assert.assertEquals(nodePorts3[2], nodePortsGet[2]);
364 Assert.assertEquals(nodePorts2[0], nodePortsGet[3]);
365 Assert.assertEquals(nodePorts2[1], nodePortsGet[4]);
366 Assert.assertEquals(nodePorts2[2], nodePortsGet[5]);
367 Assert.assertEquals(nodePorts2[3], nodePortsGet[6]);
369 // Unexpected config name
370 Assert.assertTrue(false);
374 // Test DELETE subnet1
375 result = getJsonResult(baseURL + "default/subnet/" + name1, "DELETE");
376 Assert.assertEquals(204, httpResponseCode.intValue());
378 // Test GET deleted subnet1
379 result = getJsonResult(baseURL + "default/subnet/" + name1);
380 Assert.assertEquals(404, httpResponseCode.intValue());
384 public void testStaticRoutingNorthbound() throws JSONException {
385 System.out.println("Starting StaticRouting JAXB client.");
386 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/staticroute/";
388 String name1 = "testRoute1";
389 String prefix1 = "192.168.1.1/24";
390 String nextHop1 = "0.0.0.0";
391 String name2 = "testRoute2";
392 String prefix2 = "192.168.1.1/16";
393 String nextHop2 = "1.1.1.1";
395 // Test GET static routes in default container, expecting no results
396 String result = getJsonResult(baseURL + "default");
397 JSONTokener jt = new JSONTokener(result);
398 JSONObject json = new JSONObject(jt);
399 Assert.assertEquals("{}", result);
401 // Test insert static route
402 String requestBody = "{\"name\":\"" + name1 + "\", \"prefix\":\"" + prefix1 + "\", \"nextHop\":\"" + nextHop1
404 result = getJsonResult(baseURL + "default/route/" + name1, "POST", requestBody);
405 Assert.assertEquals(201, httpResponseCode.intValue());
407 requestBody = "{\"name\":\"" + name2 + "\", \"prefix\":\"" + prefix2 + "\", \"nextHop\":\"" + nextHop2 + "\"}";
408 result = getJsonResult(baseURL + "default/route/" + name2, "POST", requestBody);
409 Assert.assertEquals(201, httpResponseCode.intValue());
411 // Test Get all static routes
412 result = getJsonResult(baseURL + "default");
413 jt = new JSONTokener(result);
414 json = new JSONObject(jt);
415 JSONArray staticRoutes = json.getJSONArray("staticRoute");
416 Assert.assertEquals(2, staticRoutes.length());
418 for (int i = 0; i < staticRoutes.length(); i++) {
419 route = staticRoutes.getJSONObject(i);
420 if (route.getString("name").equals(name1)) {
421 Assert.assertEquals(prefix1, route.getString("prefix"));
422 Assert.assertEquals(nextHop1, route.getString("nextHop"));
423 } else if (route.getString("name").equals(name2)) {
424 Assert.assertEquals(prefix2, route.getString("prefix"));
425 Assert.assertEquals(nextHop2, route.getString("nextHop"));
427 // static route has unknown name
428 Assert.assertTrue(false);
432 // Test get specific static route
433 result = getJsonResult(baseURL + "default/route/" + name1);
434 jt = new JSONTokener(result);
435 json = new JSONObject(jt);
437 Assert.assertEquals(name1, json.getString("name"));
438 Assert.assertEquals(prefix1, json.getString("prefix"));
439 Assert.assertEquals(nextHop1, json.getString("nextHop"));
441 result = getJsonResult(baseURL + "default/route/" + name2);
442 jt = new JSONTokener(result);
443 json = new JSONObject(jt);
445 Assert.assertEquals(name2, json.getString("name"));
446 Assert.assertEquals(prefix2, json.getString("prefix"));
447 Assert.assertEquals(nextHop2, json.getString("nextHop"));
449 // Test delete static route
450 result = getJsonResult(baseURL + "default/route/" + name1, "DELETE");
451 Assert.assertEquals(200, httpResponseCode.intValue());
453 result = getJsonResult(baseURL + "default");
454 jt = new JSONTokener(result);
455 json = new JSONObject(jt);
456 JSONObject singleStaticRoute = json.getJSONObject("staticRoute");
457 Assert.assertEquals(name2, singleStaticRoute.getString("name"));
462 public void testSwitchManager() throws JSONException {
463 System.out.println("Starting SwitchManager JAXB client.");
464 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/switch/default/";
466 // define Node/NodeConnector attributes for test
467 int nodeId_1 = 51966;
470 int nodeConnectorId_1 = 51966;
471 int nodeConnectorId_2 = 12;
472 int nodeConnectorId_3 = 34;
473 String nodeType = "STUB";
474 String ncType = "STUB";
475 int timestamp_1 = 100000;
476 String timestampName_1 = "connectedSince";
477 int actionsValue_1 = 2;
478 int capabilitiesValue_1 = 3;
479 int tablesValue_1 = 1;
480 int buffersValue_1 = 1;
482 int ncCapabilities = 1;
483 int ncBandwidth = 1000000000;
485 // Test GET all nodes
487 String result = getJsonResult(baseURL + "nodes");
488 JSONTokener jt = new JSONTokener(result);
489 JSONObject json = new JSONObject(jt);
491 // Test for first node
492 JSONObject node = getJsonInstance(json, "nodeProperties", nodeId_1);
493 Assert.assertNotNull(node);
494 testNodeProperties(node, nodeId_1, nodeType, timestamp_1, timestampName_1, actionsValue_1, capabilitiesValue_1,
495 tablesValue_1, buffersValue_1);
497 // Test 2nd node, properties of 2nd node same as first node
498 node = getJsonInstance(json, "nodeProperties", nodeId_2);
499 Assert.assertNotNull(node);
500 testNodeProperties(node, nodeId_2, nodeType, timestamp_1, timestampName_1, actionsValue_1, capabilitiesValue_1,
501 tablesValue_1, buffersValue_1);
503 // Test 3rd node, properties of 3rd node same as first node
504 node = getJsonInstance(json, "nodeProperties", nodeId_3);
505 Assert.assertNotNull(node);
506 testNodeProperties(node, nodeId_3, nodeType, timestamp_1, timestampName_1, actionsValue_1, capabilitiesValue_1,
507 tablesValue_1, buffersValue_1);
509 // Test GET nodeConnectors of a node
511 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1);
512 jt = new JSONTokener(result);
513 json = new JSONObject(jt);
514 JSONObject nodeConnectorProperties = json.getJSONObject("nodeConnectorProperties");
516 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_1, ncType, nodeId_1, nodeType, ncState,
517 ncCapabilities, ncBandwidth);
520 result = getJsonResult(baseURL + "node/STUB/" + nodeId_2);
521 jt = new JSONTokener(result);
522 json = new JSONObject(jt);
523 nodeConnectorProperties = json.getJSONObject("nodeConnectorProperties");
525 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_2, ncType, nodeId_2, nodeType, ncState,
526 ncCapabilities, ncBandwidth);
529 result = getJsonResult(baseURL + "node/STUB/" + nodeId_3);
530 jt = new JSONTokener(result);
531 json = new JSONObject(jt);
533 nodeConnectorProperties = json.getJSONObject("nodeConnectorProperties");
534 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_3, ncType, nodeId_3, nodeType, ncState,
535 ncCapabilities, ncBandwidth);
537 // Test add property to node
538 // Add Tier and Description property to node1
539 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/tier/1001", "PUT");
540 Assert.assertEquals(201, httpResponseCode.intValue());
541 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/description/node1", "PUT");
542 Assert.assertEquals(201, httpResponseCode.intValue());
544 // Test for first node
545 result = getJsonResult(baseURL + "nodes");
546 jt = new JSONTokener(result);
547 json = new JSONObject(jt);
548 node = getJsonInstance(json, "nodeProperties", nodeId_1);
549 Assert.assertNotNull(node);
550 Assert.assertEquals(1001, node.getJSONObject("properties").getJSONObject("tier").getInt("value"));
551 Assert.assertEquals("node1", node.getJSONObject("properties").getJSONObject("description").getString("value"));
553 // Test delete nodeConnector property
554 // Delete state property of nodeconnector1
555 result = getJsonResult(baseURL + "nodeconnector/STUB/" + nodeId_1 + "/STUB/" + nodeConnectorId_1
556 + "/property/state", "DELETE");
557 Assert.assertEquals(200, httpResponseCode.intValue());
559 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1);
560 jt = new JSONTokener(result);
561 json = new JSONObject(jt);
562 nodeConnectorProperties = json.getJSONObject("nodeConnectorProperties");
564 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_1, ncType, nodeId_1, nodeType, null,
565 ncCapabilities, ncBandwidth);
567 // Delete capabilities property of nodeconnector2
568 result = getJsonResult(baseURL + "nodeconnector/STUB/" + nodeId_2 + "/STUB/" + nodeConnectorId_2
569 + "/property/capabilities", "DELETE");
570 Assert.assertEquals(200, httpResponseCode.intValue());
572 result = getJsonResult(baseURL + "node/STUB/" + nodeId_2);
573 jt = new JSONTokener(result);
574 json = new JSONObject(jt);
575 nodeConnectorProperties = json.getJSONObject("nodeConnectorProperties");
577 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_2, ncType, nodeId_2, nodeType, ncState,
580 // Test PUT nodeConnector property
581 int newBandwidth = 1001;
583 // Add Name/Bandwidth property to nodeConnector1
584 result = getJsonResult(baseURL + "nodeconnector/STUB/" + nodeId_1 + "/STUB/" + nodeConnectorId_1
585 + "/property/bandwidth/" + newBandwidth, "PUT");
586 Assert.assertEquals(201, httpResponseCode.intValue());
588 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1);
589 jt = new JSONTokener(result);
590 json = new JSONObject(jt);
591 nodeConnectorProperties = json.getJSONObject("nodeConnectorProperties");
593 // Check for new bandwidth value, state value removed from previous
595 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_1, ncType, nodeId_1, nodeType, null,
596 ncCapabilities, newBandwidth);
601 public void testStatistics() throws JSONException {
602 final String actionTypes[] = { "drop", "loopback", "flood", "floodAll", "controller", "swPath", "hwPath", "output",
603 "setDlSrc", "setDlDst", "setDlType", "setVlanId", "setVlanPcp", "setVlanCfi", "popVlan", "pushVlan",
604 "setNwSrc", "setNwDst", "setNwTos", "setTpSrc", "setTpDst" };
605 System.out.println("Starting Statistics JAXB client.");
607 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/statistics/default/";
609 String result = getJsonResult(baseURL + "flow");
610 JSONTokener jt = new JSONTokener(result);
611 JSONObject json = new JSONObject(jt);
612 JSONObject flowStatistics = getJsonInstance(json, "flowStatistics", 0xCAFE);
613 JSONObject node = flowStatistics.getJSONObject("node");
614 // test that node was returned properly
615 Assert.assertTrue(node.getInt("id") == 0xCAFE);
616 Assert.assertEquals(node.getString("type"), "STUB");
618 // test that flow statistics results are correct
619 JSONArray flowStats = flowStatistics.getJSONArray("flowStatistic");
620 for (int i = 0; i < flowStats.length(); i++) {
622 JSONObject flowStat = flowStats.getJSONObject(i);
623 testFlowStat(flowStat, actionTypes[i], i);
627 // for /controller/nb/v2/statistics/default/port
628 result = getJsonResult(baseURL + "port");
629 jt = new JSONTokener(result);
630 json = new JSONObject(jt);
631 JSONObject portStatistics = getJsonInstance(json, "portStatistics", 0xCAFE);
632 JSONObject node2 = portStatistics.getJSONObject("node");
633 // test that node was returned properly
634 Assert.assertTrue(node2.getInt("id") == 0xCAFE);
635 Assert.assertEquals(node2.getString("type"), "STUB");
637 // test that port statistic results are correct
638 JSONObject portStat = portStatistics.getJSONObject("portStatistic");
639 Assert.assertTrue(portStat.getInt("receivePackets") == 250);
640 Assert.assertTrue(portStat.getInt("transmitPackets") == 500);
641 Assert.assertTrue(portStat.getInt("receiveBytes") == 1000);
642 Assert.assertTrue(portStat.getInt("transmitBytes") == 5000);
643 Assert.assertTrue(portStat.getInt("receiveDrops") == 2);
644 Assert.assertTrue(portStat.getInt("transmitDrops") == 50);
645 Assert.assertTrue(portStat.getInt("receiveErrors") == 3);
646 Assert.assertTrue(portStat.getInt("transmitErrors") == 10);
647 Assert.assertTrue(portStat.getInt("receiveFrameError") == 5);
648 Assert.assertTrue(portStat.getInt("receiveOverRunError") == 6);
649 Assert.assertTrue(portStat.getInt("receiveCrcError") == 1);
650 Assert.assertTrue(portStat.getInt("collisionCount") == 4);
652 // test for getting one specific node's stats
653 result = getJsonResult(baseURL + "flow/node/STUB/51966");
654 jt = new JSONTokener(result);
655 json = new JSONObject(jt);
656 node = json.getJSONObject("node");
657 // test that node was returned properly
658 Assert.assertTrue(node.getInt("id") == 0xCAFE);
659 Assert.assertEquals(node.getString("type"), "STUB");
661 // test that flow statistics results are correct
662 flowStats = json.getJSONArray("flowStatistic");
663 for (int i = 0; i < flowStats.length(); i++) {
664 JSONObject flowStat = flowStats.getJSONObject(i);
665 testFlowStat(flowStat, actionTypes[i], i);
668 result = getJsonResult(baseURL + "port/node/STUB/51966");
669 jt = new JSONTokener(result);
670 json = new JSONObject(jt);
671 node2 = json.getJSONObject("node");
672 // test that node was returned properly
673 Assert.assertTrue(node2.getInt("id") == 0xCAFE);
674 Assert.assertEquals(node2.getString("type"), "STUB");
676 // test that port statistic results are correct
677 portStat = json.getJSONObject("portStatistic");
678 Assert.assertTrue(portStat.getInt("receivePackets") == 250);
679 Assert.assertTrue(portStat.getInt("transmitPackets") == 500);
680 Assert.assertTrue(portStat.getInt("receiveBytes") == 1000);
681 Assert.assertTrue(portStat.getInt("transmitBytes") == 5000);
682 Assert.assertTrue(portStat.getInt("receiveDrops") == 2);
683 Assert.assertTrue(portStat.getInt("transmitDrops") == 50);
684 Assert.assertTrue(portStat.getInt("receiveErrors") == 3);
685 Assert.assertTrue(portStat.getInt("transmitErrors") == 10);
686 Assert.assertTrue(portStat.getInt("receiveFrameError") == 5);
687 Assert.assertTrue(portStat.getInt("receiveOverRunError") == 6);
688 Assert.assertTrue(portStat.getInt("receiveCrcError") == 1);
689 Assert.assertTrue(portStat.getInt("collisionCount") == 4);
692 private void testFlowStat(JSONObject flowStat, String actionType, int actIndex) throws JSONException {
693 Assert.assertTrue(flowStat.getInt("tableId") == 1);
694 Assert.assertTrue(flowStat.getInt("durationSeconds") == 40);
695 Assert.assertTrue(flowStat.getInt("durationNanoseconds") == 400);
696 Assert.assertTrue(flowStat.getInt("packetCount") == 200);
697 Assert.assertTrue(flowStat.getInt("byteCount") == 100);
699 // test that flow information is correct
700 JSONObject flow = flowStat.getJSONObject("flow");
701 Assert.assertTrue(flow.getInt("priority") == (3500 + actIndex));
702 Assert.assertTrue(flow.getInt("idleTimeout") == 1000);
703 Assert.assertTrue(flow.getInt("hardTimeout") == 2000);
704 Assert.assertTrue(flow.getInt("id") == 12345);
706 JSONObject match = (flow.getJSONObject("match").getJSONObject("matchField"));
707 Assert.assertTrue(match.getString("type").equals("NW_DST"));
708 Assert.assertTrue(match.getString("value").equals("1.1.1.1"));
710 JSONObject act = flow.getJSONObject("actions");
711 Assert.assertTrue(act.getString("@type").equals(actionType));
713 if (act.getString("@type").equals("output")) {
714 JSONObject port = act.getJSONObject("port");
715 JSONObject port_node = port.getJSONObject("node");
716 Assert.assertTrue(port.getInt("id") == 51966);
717 Assert.assertTrue(port.getString("type").equals("STUB"));
718 Assert.assertTrue(port_node.getInt("id") == 51966);
719 Assert.assertTrue(port_node.getString("type").equals("STUB"));
722 if (act.getString("@type").equals("setDlSrc")) {
723 byte srcMatch[] = { (byte) 5, (byte) 4, (byte) 3, (byte) 2, (byte) 1 };
724 String src = act.getString("address");
725 byte srcBytes[] = new byte[5];
726 srcBytes[0] = Byte.parseByte(src.substring(0, 2));
727 srcBytes[1] = Byte.parseByte(src.substring(2, 4));
728 srcBytes[2] = Byte.parseByte(src.substring(4, 6));
729 srcBytes[3] = Byte.parseByte(src.substring(6, 8));
730 srcBytes[4] = Byte.parseByte(src.substring(8, 10));
731 Assert.assertTrue(Arrays.equals(srcBytes, srcMatch));
734 if (act.getString("@type").equals("setDlDst")) {
735 byte dstMatch[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
736 String dst = act.getString("address");
737 byte dstBytes[] = new byte[5];
738 dstBytes[0] = Byte.parseByte(dst.substring(0, 2));
739 dstBytes[1] = Byte.parseByte(dst.substring(2, 4));
740 dstBytes[2] = Byte.parseByte(dst.substring(4, 6));
741 dstBytes[3] = Byte.parseByte(dst.substring(6, 8));
742 dstBytes[4] = Byte.parseByte(dst.substring(8, 10));
743 Assert.assertTrue(Arrays.equals(dstBytes, dstMatch));
745 if (act.getString("@type").equals("setDlType"))
746 Assert.assertTrue(act.getInt("dlType") == 10);
747 if (act.getString("@type").equals("setVlanId"))
748 Assert.assertTrue(act.getInt("vlanId") == 2);
749 if (act.getString("@type").equals("setVlanPcp"))
750 Assert.assertTrue(act.getInt("pcp") == 3);
751 if (act.getString("@type").equals("setVlanCfi"))
752 Assert.assertTrue(act.getInt("cfi") == 1);
754 if (act.getString("@type").equals("setNwSrc"))
755 Assert.assertTrue(act.getString("address").equals("2.2.2.2"));
756 if (act.getString("@type").equals("setNwDst"))
757 Assert.assertTrue(act.getString("address").equals("1.1.1.1"));
759 if (act.getString("@type").equals("pushVlan")) {
760 int head = act.getInt("VlanHeader");
761 // parsing vlan header
762 int id = head & 0xfff;
763 int cfi = (head >> 12) & 0x1;
764 int pcp = (head >> 13) & 0x7;
765 int tag = (head >> 16) & 0xffff;
766 Assert.assertTrue(id == 1234);
767 Assert.assertTrue(cfi == 1);
768 Assert.assertTrue(pcp == 1);
769 Assert.assertTrue(tag == 0x8100);
771 if (act.getString("@type").equals("setNwTos"))
772 Assert.assertTrue(act.getInt("tos") == 16);
773 if (act.getString("@type").equals("setTpSrc"))
774 Assert.assertTrue(act.getInt("port") == 4201);
775 if (act.getString("@type").equals("setTpDst"))
776 Assert.assertTrue(act.getInt("port") == 8080);
780 public void testFlowProgrammer() throws JSONException {
781 System.out.println("Starting FlowProgrammer JAXB client.");
782 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/flow/default/";
783 // Attempt to get a flow that doesn't exit. Should return 404
785 String result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test1", "GET");
786 Assert.assertTrue(result.equals("404"));
789 String fc = "{\"dynamic\":\"false\", \"name\":\"test1\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"DROP\"]}";
790 result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test1", "PUT", fc);
791 Assert.assertTrue(httpResponseCode == 201);
793 // test get returns flow that was added.
794 result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test1", "GET");
795 // check that result came out fine.
796 Assert.assertTrue(httpResponseCode == 200);
797 JSONTokener jt = new JSONTokener(result);
798 JSONObject json = new JSONObject(jt);
799 Assert.assertEquals(json.getString("name"), "test1");
800 Assert.assertEquals(json.getString("actions"), "DROP");
801 Assert.assertEquals(json.getString("installInHw"), "true");
802 JSONObject node = json.getJSONObject("node");
803 Assert.assertEquals(node.getString("type"), "STUB");
804 Assert.assertEquals(node.getString("id"), "51966");
805 // test adding same flow again fails due to repeat name..return 409
807 result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test1", "PUT", fc);
808 Assert.assertTrue(result.equals("409"));
810 fc = "{\"dynamic\":\"false\", \"name\":\"test2\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"DROP\"]}";
811 result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test2", "PUT", fc);
812 // test should return 409 for error due to same flow being added.
813 Assert.assertTrue(result.equals("409"));
815 // add second flow that's different
816 fc = "{\"dynamic\":\"false\", \"name\":\"test2\", \"nwSrc\":\"1.1.1.1\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"DROP\"]}";
817 result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test2", "PUT", fc);
818 Assert.assertTrue(httpResponseCode == 201);
820 // check that request returns both flows given node.
821 result = getJsonResult(baseURL + "node/STUB/51966/", "GET");
822 jt = new JSONTokener(result);
823 json = new JSONObject(jt);
824 Assert.assertTrue(json.get("flowConfig") instanceof JSONArray);
825 JSONArray ja = json.getJSONArray("flowConfig");
826 Integer count = ja.length();
827 Assert.assertTrue(count == 2);
829 // check that request returns both flows given just container.
830 result = getJsonResult(baseURL);
831 jt = new JSONTokener(result);
832 json = new JSONObject(jt);
833 Assert.assertTrue(json.get("flowConfig") instanceof JSONArray);
834 ja = json.getJSONArray("flowConfig");
836 Assert.assertTrue(count == 2);
838 // delete a flow, check that it's no longer in list.
839 result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test2", "DELETE");
840 Assert.assertTrue(httpResponseCode == 200);
842 result = getJsonResult(baseURL + "node/STUB/51966/static-flow/test2", "GET");
843 Assert.assertTrue(result.equals("404"));
846 // method to extract a JSONObject with specified node ID from a JSONObject
847 // that may contain an array of JSONObjects
848 // This is specifically written for statistics manager northbound REST
850 // array_name should be either "flowStatistics" or "portStatistics"
851 private JSONObject getJsonInstance(JSONObject json, String array_name, Integer nodeId) throws JSONException {
852 JSONObject result = null;
853 if (json.get(array_name) instanceof JSONArray) {
854 JSONArray json_array = json.getJSONArray(array_name);
855 for (int i = 0; i < json_array.length(); i++) {
856 result = json_array.getJSONObject(i);
857 Integer nid = result.getJSONObject("node").getInt("id");
858 if (nid.equals(nodeId))
862 result = json.getJSONObject(array_name);
863 Integer nid = result.getJSONObject("node").getInt("id");
864 if (!nid.equals(nodeId))
870 // a class to construct query parameter for HTTP request
871 private class QueryParameter {
872 StringBuilder queryString = null;
875 QueryParameter(String key, String value) {
876 queryString = new StringBuilder();
877 queryString.append("?").append(key).append("=").append(value);
880 // method to add more query parameter
881 QueryParameter add(String key, String value) {
882 this.queryString.append("&").append(key).append("=").append(value);
886 // method to get the query parameter string
888 return this.queryString.toString();
894 public void testHostTracker() throws JSONException {
896 System.out.println("Starting HostTracker JAXB client.");
898 // setup 2 host models for @POST method
900 String networkAddress_1 = "192.168.0.8";
901 String dataLayerAddress_1 = "11:22:33:44:55:66";
902 String nodeType_1 = "STUB";
903 Integer nodeId_1 = 3366;
904 String nodeConnectorType_1 = "STUB";
905 Integer nodeConnectorId_1 = 12;
909 String networkAddress_2 = "10.1.1.1";
910 String dataLayerAddress_2 = "1A:2B:3C:4D:5E:6F";
911 String nodeType_2 = "STUB";
912 Integer nodeId_2 = 4477;
913 String nodeConnectorType_2 = "STUB";
914 Integer nodeConnectorId_2 = 34;
917 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/host/default";
919 // test PUT method: addHost()
920 JSONObject fc_json = new JSONObject();
921 fc_json.put("dataLayerAddress", dataLayerAddress_1);
922 fc_json.put("nodeType", nodeType_1);
923 fc_json.put("nodeId", nodeId_1);
924 fc_json.put("nodeConnectorType", nodeType_1);
925 fc_json.put("nodeConnectorId", nodeConnectorId_1.toString());
926 fc_json.put("vlan", vlan_1);
927 fc_json.put("staticHost", "true");
928 fc_json.put("networkAddress", networkAddress_1);
930 String result = getJsonResult(baseURL + "/" + networkAddress_1, "PUT", fc_json.toString());
931 Assert.assertTrue(httpResponseCode == 201);
933 fc_json = new JSONObject();
934 fc_json.put("dataLayerAddress", dataLayerAddress_2);
935 fc_json.put("nodeType", nodeType_2);
936 fc_json.put("nodeId", nodeId_2);
937 fc_json.put("nodeConnectorType", nodeType_2);
938 fc_json.put("nodeConnectorId", nodeConnectorId_2.toString());
939 fc_json.put("vlan", vlan_2);
940 fc_json.put("staticHost", "true");
941 fc_json.put("networkAddress", networkAddress_2);
943 result = getJsonResult(baseURL + "/" + networkAddress_2 , "PUT", fc_json.toString());
944 Assert.assertTrue(httpResponseCode == 201);
946 // define variables for decoding returned strings
947 String networkAddress;
948 JSONObject host_jo, dl_jo, nc_jo, node_jo;
950 // the two hosts should be in inactive host DB
951 // test GET method: getInactiveHosts()
952 result = getJsonResult(baseURL + "/inactive", "GET");
953 Assert.assertTrue(httpResponseCode == 200);
955 JSONTokener jt = new JSONTokener(result);
956 JSONObject json = new JSONObject(jt);
957 // there should be at least two hosts in the DB
958 Assert.assertTrue(json.get("hostConfig") instanceof JSONArray);
959 JSONArray ja = json.getJSONArray("hostConfig");
960 Integer count = ja.length();
961 Assert.assertTrue(count == 2);
963 for (int i = 0; i < count; i++) {
964 host_jo = ja.getJSONObject(i);
965 networkAddress = host_jo.getString("networkAddress");
966 if (networkAddress.equalsIgnoreCase(networkAddress_1)) {
967 Assert.assertTrue(host_jo.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_1));
968 Assert.assertTrue(host_jo.getString("nodeConnectorType").equalsIgnoreCase(nodeConnectorType_1));
969 Assert.assertTrue(host_jo.getInt("nodeConnectorId") == nodeConnectorId_1);
970 Assert.assertTrue(host_jo.getString("nodeType").equalsIgnoreCase(nodeType_1));
971 Assert.assertTrue(host_jo.getInt("nodeId") == nodeId_1);
972 Assert.assertTrue(host_jo.getString("vlan").equalsIgnoreCase(vlan_1));
973 Assert.assertTrue(host_jo.getBoolean("staticHost"));
974 } else if (networkAddress.equalsIgnoreCase(networkAddress_2)) {
975 Assert.assertTrue(host_jo.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_2));
976 Assert.assertTrue(host_jo.getString("nodeConnectorType").equalsIgnoreCase(nodeConnectorType_2));
977 Assert.assertTrue(host_jo.getInt("nodeConnectorId") == nodeConnectorId_2);
978 Assert.assertTrue(host_jo.getString("nodeType").equalsIgnoreCase(nodeType_2));
979 Assert.assertTrue(host_jo.getInt("nodeId") == nodeId_2);
980 Assert.assertTrue(host_jo.getString("vlan").equalsIgnoreCase(vlan_2));
981 Assert.assertTrue(host_jo.getBoolean("staticHost"));
983 Assert.assertTrue(false);
987 // test GET method: getActiveHosts() - no host expected
988 result = getJsonResult(baseURL, "GET");
989 Assert.assertTrue(httpResponseCode == 200);
991 jt = new JSONTokener(result);
992 json = new JSONObject(jt);
993 Assert.assertFalse(hostInJson(json, networkAddress_1));
994 Assert.assertFalse(hostInJson(json, networkAddress_2));
996 // put the 1st host into active host DB
1000 nd = new Node(nodeType_1, nodeId_1);
1001 ndc = new NodeConnector(nodeConnectorType_1, nodeConnectorId_1, nd);
1002 this.invtoryListener.notifyNodeConnector(ndc, UpdateType.ADDED, null);
1003 } catch (ConstructionException e) {
1008 // verify the host shows up in active host DB
1010 result = getJsonResult(baseURL, "GET");
1011 Assert.assertTrue(httpResponseCode == 200);
1013 jt = new JSONTokener(result);
1014 json = new JSONObject(jt);
1016 Assert.assertTrue(hostInJson(json, networkAddress_1));
1018 // test GET method for getHostDetails()
1020 result = getJsonResult(baseURL + "/" + networkAddress_1, "GET");
1021 Assert.assertTrue(httpResponseCode == 200);
1023 jt = new JSONTokener(result);
1024 json = new JSONObject(jt);
1026 Assert.assertFalse(json.length() == 0);
1028 Assert.assertTrue(json.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_1));
1029 Assert.assertTrue(json.getString("nodeConnectorType").equalsIgnoreCase(nodeConnectorType_1));
1030 Assert.assertTrue(json.getInt("nodeConnectorId") == nodeConnectorId_1);
1031 Assert.assertTrue(json.getString("nodeType").equalsIgnoreCase(nodeType_1));
1032 Assert.assertTrue(json.getInt("nodeId") == nodeId_1);
1033 Assert.assertTrue(json.getString("vlan").equalsIgnoreCase(vlan_1));
1034 Assert.assertTrue(json.getBoolean("staticHost"));
1036 // test DELETE method for deleteFlow()
1038 result = getJsonResult(baseURL + "/" + networkAddress_1, "DELETE");
1039 Assert.assertTrue(httpResponseCode == 200);
1041 // verify host_1 removed from active host DB
1042 // test GET method: getActiveHosts() - no host expected
1044 result = getJsonResult(baseURL, "GET");
1045 Assert.assertTrue(httpResponseCode == 200);
1047 jt = new JSONTokener(result);
1048 json = new JSONObject(jt);
1050 Assert.assertFalse(hostInJson(json, networkAddress_1));
1054 private Boolean hostInJson(JSONObject json, String hostIp) throws JSONException {
1055 // input JSONObject may be empty
1056 if (json.length() == 0) {
1059 if (json.get("hostConfig") instanceof JSONArray) {
1060 JSONArray ja = json.getJSONArray("hostConfig");
1061 for (int i = 0; i < ja.length(); i++) {
1062 String na = ja.getJSONObject(i).getString("networkAddress");
1063 if (na.equalsIgnoreCase(hostIp))
1068 JSONObject ja = json.getJSONObject("hostConfig");
1069 String na = ja.getString("networkAddress");
1070 return (na.equalsIgnoreCase(hostIp)) ? true : false;
1075 public void testTopology() throws JSONException, ConstructionException {
1076 System.out.println("Starting Topology JAXB client.");
1077 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/topology/default";
1079 // === test GET method for getTopology()
1080 short state_1 = State.EDGE_UP, state_2 = State.EDGE_DOWN;
1081 long bw_1 = Bandwidth.BW10Gbps, bw_2 = Bandwidth.BW100Mbps;
1082 long lat_1 = Latency.LATENCY100ns, lat_2 = Latency.LATENCY1ms;
1083 String nodeType = "STUB";
1084 String nodeConnType = "STUB";
1085 int headNC1_nodeId = 1, headNC1_nodeConnId = 11;
1086 int tailNC1_nodeId = 2, tailNC1_nodeConnId = 22;
1087 int headNC2_nodeId = 2, headNC2_nodeConnId = 21;
1088 int tailNC2_nodeId = 1, tailNC2_nodeConnId = 12;
1090 List<TopoEdgeUpdate> topoedgeupdateList = new ArrayList<TopoEdgeUpdate>();
1091 NodeConnector headnc1 = new NodeConnector(nodeConnType, headNC1_nodeConnId, new Node(nodeType, headNC1_nodeId));
1092 NodeConnector tailnc1 = new NodeConnector(nodeConnType, tailNC1_nodeConnId, new Node(nodeType, tailNC1_nodeId));
1093 Edge e1 = new Edge(tailnc1, headnc1);
1094 Set<Property> props_1 = new HashSet<Property>();
1095 props_1.add(new State(state_1));
1096 props_1.add(new Bandwidth(bw_1));
1097 props_1.add(new Latency(lat_1));
1098 TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props_1, UpdateType.ADDED);
1099 topoedgeupdateList.add(teu1);
1101 NodeConnector headnc2 = new NodeConnector(nodeConnType, headNC2_nodeConnId, new Node(nodeType, headNC2_nodeId));
1102 NodeConnector tailnc2 = new NodeConnector(nodeConnType, tailNC2_nodeConnId, new Node(nodeType, tailNC2_nodeId));
1103 Edge e2 = new Edge(tailnc2, headnc2);
1104 Set<Property> props_2 = new HashSet<Property>();
1105 props_2.add(new State(state_2));
1106 props_2.add(new Bandwidth(bw_2));
1107 props_2.add(new Latency(lat_2));
1109 TopoEdgeUpdate teu2 = new TopoEdgeUpdate(e2, props_2, UpdateType.ADDED);
1110 topoedgeupdateList.add(teu2);
1112 topoUpdates.edgeUpdate(topoedgeupdateList);
1115 String result = getJsonResult(baseURL, "GET");
1116 Assert.assertTrue(httpResponseCode == 200);
1118 System.out.println("Get Topology: " + result);
1121 // returned data must be an array of edges
1122 JSONTokener jt = new JSONTokener(result);
1123 JSONObject json = new JSONObject(jt);
1124 Assert.assertTrue(json.get("edgeProperties") instanceof JSONArray);
1125 JSONArray ja = json.getJSONArray("edgeProperties");
1127 for (int i = 0; i < ja.length(); i++) {
1128 JSONObject edgeProp = ja.getJSONObject(i);
1129 JSONObject edge = edgeProp.getJSONObject("edge");
1130 JSONObject tailNC = edge.getJSONObject("tailNodeConnector");
1131 JSONObject tailNode = tailNC.getJSONObject("node");
1133 JSONObject headNC = edge.getJSONObject("headNodeConnector");
1134 JSONObject headNode = headNC.getJSONObject("node");
1135 JSONObject Props = edgeProp.getJSONObject("properties");
1136 JSONObject bandw = Props.getJSONObject("bandwidth");
1137 JSONObject stt = Props.getJSONObject("state");
1138 JSONObject ltc = Props.getJSONObject("latency");
1140 if (headNC.getInt("id") == headNC1_nodeConnId) {
1141 Assert.assertEquals(headNode.getString("type"), nodeType);
1142 Assert.assertEquals(headNode.getLong("id"), headNC1_nodeId);
1143 Assert.assertEquals(headNC.getString("type"), nodeConnType);
1144 Assert.assertEquals(tailNode.getString("type"),nodeType);
1145 Assert.assertEquals(tailNode.getString("type"), nodeConnType);
1146 Assert.assertEquals(tailNC.getLong("id"), tailNC1_nodeConnId);
1147 Assert.assertEquals(bandw.getLong("value"), bw_1);
1148 Assert.assertTrue((short) stt.getInt("value") == state_1);
1149 Assert.assertEquals(ltc.getLong("value"), lat_1);
1150 } else if (headNC.getInt("id") == headNC2_nodeConnId) {
1151 Assert.assertEquals(headNode.getString("type"),nodeType);
1152 Assert.assertEquals(headNode.getLong("id"), headNC2_nodeId);
1153 Assert.assertEquals(headNC.getString("type"), nodeConnType);
1154 Assert.assertEquals(tailNode.getString("type"), nodeType);
1155 Assert.assertTrue(tailNode.getInt("id") == tailNC2_nodeId);
1156 Assert.assertEquals(tailNC.getString("type"), nodeConnType);
1157 Assert.assertEquals(tailNC.getLong("id"), tailNC2_nodeConnId);
1158 Assert.assertEquals(bandw.getLong("value"), bw_2);
1159 Assert.assertTrue((short) stt.getInt("value") == state_2);
1160 Assert.assertEquals(ltc.getLong("value"), lat_2);
1164 // === test POST method for addUserLink()
1165 // define 2 sample nodeConnectors for user link configuration tests
1166 String nodeType_1 = "STUB";
1167 Integer nodeId_1 = 3366;
1168 String nodeConnectorType_1 = "STUB";
1169 Integer nodeConnectorId_1 = 12;
1171 String nodeType_2 = "STUB";
1172 Integer nodeId_2 = 4477;
1173 String nodeConnectorType_2 = "STUB";
1174 Integer nodeConnectorId_2 = 34;
1176 JSONObject jo = new JSONObject()
1177 .append("name", "userLink_1")
1178 .append("srcNodeConnector",
1179 nodeConnectorType_1 + "|" + nodeConnectorId_1 + "@" + nodeType_1 + "|" + nodeId_1)
1180 .append("dstNodeConnector",
1181 nodeConnectorType_2 + "|" + nodeConnectorId_2 + "@" + nodeType_2 + "|" + nodeId_2);
1182 // execute HTTP request and verify response code
1183 result = getJsonResult(baseURL + "/user-link", "PUT", jo.toString());
1184 Assert.assertTrue(httpResponseCode == 201);
1186 // === test GET method for getUserLinks()
1187 result = getJsonResult(baseURL + "/user-link", "GET");
1188 Assert.assertTrue(httpResponseCode == 200);
1190 System.out.println("result:" + result);
1193 jt = new JSONTokener(result);
1194 json = new JSONObject(jt);
1196 // should have at least one object returned
1197 Assert.assertFalse(json.length() == 0);
1198 JSONObject userlink = new JSONObject();
1200 if (json.get("userLinks") instanceof JSONArray) {
1201 ja = json.getJSONArray("userLinks");
1203 for (i = 0; i < ja.length(); i++) {
1204 userlink = ja.getJSONObject(i);
1205 if (userlink.getString("name").equalsIgnoreCase("userLink_1"))
1208 Assert.assertFalse(i == ja.length());
1210 userlink = json.getJSONObject("userLinks");
1211 Assert.assertTrue(userlink.getString("name").equalsIgnoreCase("userLink_1"));
1214 String[] level_1, level_2;
1215 level_1 = userlink.getString("srcNodeConnector").split("\\@");
1216 level_2 = level_1[0].split("\\|");
1217 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeConnectorType_1));
1218 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeConnectorId_1);
1219 level_2 = level_1[1].split("\\|");
1220 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeType_1));
1221 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeId_1);
1222 level_1 = userlink.getString("dstNodeConnector").split("\\@");
1223 level_2 = level_1[0].split("\\|");
1224 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeConnectorType_2));
1225 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeConnectorId_2);
1226 level_2 = level_1[1].split("\\|");
1227 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeType_2));
1228 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeId_2);
1230 // === test DELETE method for deleteUserLink()
1231 String userName = "userLink_1";
1232 result = getJsonResult(baseURL + "/user-link/" + userName, "DELETE");
1233 Assert.assertTrue(httpResponseCode == 200);
1235 // execute another getUserLinks() request to verify that userLink_1 is
1237 result = getJsonResult(baseURL + "/user-link", "GET");
1238 Assert.assertTrue(httpResponseCode == 200);
1240 System.out.println("result:" + result);
1242 jt = new JSONTokener(result);
1243 json = new JSONObject(jt);
1245 if (json.length() != 0) {
1246 if (json.get("userLinks") instanceof JSONArray) {
1247 ja = json.getJSONArray("userLinks");
1248 for (int i = 0; i < ja.length(); i++) {
1249 userlink = ja.getJSONObject(i);
1250 Assert.assertFalse(userlink.getString("name").equalsIgnoreCase("userLink_1"));
1253 userlink = json.getJSONObject("userLinks");
1254 Assert.assertFalse(userlink.getString("name").equalsIgnoreCase("userLink_1"));
1259 // Configure the OSGi container
1261 public Option[] config() {
1264 systemProperty("logback.configurationFile").value(
1265 "file:" + PathUtils.getBaseDir() + "/src/test/resources/logback.xml"),
1266 // To start OSGi console for inspection remotely
1267 systemProperty("osgi.console").value("2401"),
1268 systemProperty("org.eclipse.gemini.web.tomcat.config.path").value(
1269 PathUtils.getBaseDir() + "/src/test/resources/tomcat-server.xml"),
1271 // setting default level. Jersey bundles will need to be started
1273 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
1275 // Set the systemPackages (used by clustering)
1276 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
1277 mavenBundle("org.slf4j", "jcl-over-slf4j").versionAsInProject(),
1278 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
1279 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
1280 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
1281 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),
1282 mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
1283 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager").versionAsInProject(),
1285 // the plugin stub to get data for the tests
1286 mavenBundle("org.opendaylight.controller", "protocol_plugins.stub").versionAsInProject(),
1288 // List all the opendaylight modules
1289 mavenBundle("org.opendaylight.controller", "configuration").versionAsInProject(),
1290 mavenBundle("org.opendaylight.controller", "configuration.implementation").versionAsInProject(),
1291 mavenBundle("org.opendaylight.controller", "containermanager").versionAsInProject(),
1292 mavenBundle("org.opendaylight.controller", "containermanager.implementation").versionAsInProject(),
1293 mavenBundle("org.opendaylight.controller", "clustering.services").versionAsInProject(),
1294 mavenBundle("org.opendaylight.controller", "clustering.services-implementation").versionAsInProject(),
1295 mavenBundle("org.opendaylight.controller", "security").versionAsInProject().noStart(),
1296 mavenBundle("org.opendaylight.controller", "sal").versionAsInProject(),
1297 mavenBundle("org.opendaylight.controller", "sal.implementation").versionAsInProject(),
1298 mavenBundle("org.opendaylight.controller", "sal.connection").versionAsInProject(),
1299 mavenBundle("org.opendaylight.controller", "sal.connection.implementation").versionAsInProject(),
1300 mavenBundle("org.opendaylight.controller", "switchmanager").versionAsInProject(),
1301 mavenBundle("org.opendaylight.controller", "connectionmanager").versionAsInProject(),
1302 mavenBundle("org.opendaylight.controller", "connectionmanager.implementation").versionAsInProject(),
1303 mavenBundle("org.opendaylight.controller", "switchmanager.implementation").versionAsInProject(),
1304 mavenBundle("org.opendaylight.controller", "forwardingrulesmanager").versionAsInProject(),
1305 mavenBundle("org.opendaylight.controller",
1306 "forwardingrulesmanager.implementation").versionAsInProject(),
1307 mavenBundle("org.opendaylight.controller", "statisticsmanager").versionAsInProject(),
1308 mavenBundle("org.opendaylight.controller", "statisticsmanager.implementation").versionAsInProject(),
1309 mavenBundle("org.opendaylight.controller", "arphandler").versionAsInProject(),
1310 mavenBundle("org.opendaylight.controller", "hosttracker").versionAsInProject(),
1311 mavenBundle("org.opendaylight.controller", "hosttracker.implementation").versionAsInProject(),
1312 mavenBundle("org.opendaylight.controller", "arphandler").versionAsInProject(),
1313 mavenBundle("org.opendaylight.controller", "routing.dijkstra_implementation").versionAsInProject(),
1314 mavenBundle("org.opendaylight.controller", "topologymanager").versionAsInProject(),
1315 mavenBundle("org.opendaylight.controller", "usermanager").versionAsInProject(),
1316 mavenBundle("org.opendaylight.controller", "usermanager.implementation").versionAsInProject(),
1317 mavenBundle("org.opendaylight.controller", "logging.bridge").versionAsInProject(),
1318 // mavenBundle("org.opendaylight.controller", "clustering.test").versionAsInProject(),
1319 mavenBundle("org.opendaylight.controller", "forwarding.staticrouting").versionAsInProject(),
1321 // Northbound bundles
1322 mavenBundle("org.opendaylight.controller", "commons.northbound").versionAsInProject(),
1323 mavenBundle("org.opendaylight.controller", "forwarding.staticrouting.northbound").versionAsInProject(),
1324 mavenBundle("org.opendaylight.controller", "statistics.northbound").versionAsInProject(),
1325 mavenBundle("org.opendaylight.controller", "topology.northbound").versionAsInProject(),
1326 mavenBundle("org.opendaylight.controller", "hosttracker.northbound").versionAsInProject(),
1327 mavenBundle("org.opendaylight.controller", "switchmanager.northbound").versionAsInProject(),
1328 mavenBundle("org.opendaylight.controller", "flowprogrammer.northbound").versionAsInProject(),
1329 mavenBundle("org.opendaylight.controller", "subnets.northbound").versionAsInProject(),
1331 mavenBundle("org.codehaus.jackson", "jackson-mapper-asl").versionAsInProject(),
1332 mavenBundle("org.codehaus.jackson", "jackson-core-asl").versionAsInProject(),
1333 mavenBundle("org.codehaus.jackson", "jackson-jaxrs").versionAsInProject(),
1334 mavenBundle("org.codehaus.jettison", "jettison").versionAsInProject(),
1336 mavenBundle("commons-io", "commons-io").versionAsInProject(),
1338 mavenBundle("commons-fileupload", "commons-fileupload").versionAsInProject(),
1340 mavenBundle("equinoxSDK381", "javax.servlet").versionAsInProject(),
1341 mavenBundle("equinoxSDK381", "javax.servlet.jsp").versionAsInProject(),
1342 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
1343 mavenBundle("orbit", "javax.xml.rpc").versionAsInProject(),
1344 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
1345 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
1346 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
1347 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
1348 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
1349 mavenBundle("equinoxSDK381", "org.eclipse.equinox.cm").versionAsInProject(),
1350 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console").versionAsInProject(),
1351 mavenBundle("equinoxSDK381", "org.eclipse.equinox.launcher").versionAsInProject(),
1353 mavenBundle("geminiweb", "org.eclipse.gemini.web.core").versionAsInProject(),
1354 mavenBundle("geminiweb", "org.eclipse.gemini.web.extender").versionAsInProject(),
1355 mavenBundle("geminiweb", "org.eclipse.gemini.web.tomcat").versionAsInProject(),
1356 mavenBundle("geminiweb", "org.eclipse.virgo.kernel.equinox.extensions").versionAsInProject().noStart(),
1357 mavenBundle("geminiweb", "org.eclipse.virgo.util.common").versionAsInProject(),
1358 mavenBundle("geminiweb", "org.eclipse.virgo.util.io").versionAsInProject(),
1359 mavenBundle("geminiweb", "org.eclipse.virgo.util.math").versionAsInProject(),
1360 mavenBundle("geminiweb", "org.eclipse.virgo.util.osgi").versionAsInProject(),
1361 mavenBundle("geminiweb", "org.eclipse.virgo.util.osgi.manifest").versionAsInProject(),
1362 mavenBundle("geminiweb", "org.eclipse.virgo.util.parser.manifest").versionAsInProject(),
1364 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager").versionAsInProject(),
1365 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager.shell").versionAsInProject(),
1367 mavenBundle("com.google.code.gson", "gson").versionAsInProject(),
1368 mavenBundle("org.jboss.spec.javax.transaction", "jboss-transaction-api_1.1_spec").versionAsInProject(),
1369 mavenBundle("org.apache.felix", "org.apache.felix.fileinstall").versionAsInProject(),
1370 mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
1371 mavenBundle("commons-codec", "commons-codec").versionAsInProject(),
1372 mavenBundle("virgomirror", "org.eclipse.jdt.core.compiler.batch").versionAsInProject(),
1373 mavenBundle("eclipselink", "javax.persistence").versionAsInProject(),
1374 mavenBundle("eclipselink", "javax.resource").versionAsInProject(),
1376 mavenBundle("orbit", "javax.activation").versionAsInProject(),
1377 mavenBundle("orbit", "javax.annotation").versionAsInProject(),
1378 mavenBundle("orbit", "javax.ejb").versionAsInProject(),
1379 mavenBundle("orbit", "javax.el").versionAsInProject(),
1380 mavenBundle("orbit", "javax.mail.glassfish").versionAsInProject(),
1381 mavenBundle("orbit", "javax.xml.rpc").versionAsInProject(),
1382 mavenBundle("orbit", "org.apache.catalina").versionAsInProject(),
1383 // these are bundle fragments that can't be started on its own
1384 mavenBundle("orbit", "org.apache.catalina.ha").versionAsInProject().noStart(),
1385 mavenBundle("orbit", "org.apache.catalina.tribes").versionAsInProject().noStart(),
1386 mavenBundle("orbit", "org.apache.coyote").versionAsInProject().noStart(),
1387 mavenBundle("orbit", "org.apache.jasper").versionAsInProject().noStart(),
1389 mavenBundle("orbit", "org.apache.el").versionAsInProject(),
1390 mavenBundle("orbit", "org.apache.juli.extras").versionAsInProject(),
1391 mavenBundle("orbit", "org.apache.tomcat.api").versionAsInProject(),
1392 mavenBundle("orbit", "org.apache.tomcat.util").versionAsInProject().noStart(),
1393 mavenBundle("orbit", "javax.servlet.jsp.jstl").versionAsInProject(),
1394 mavenBundle("orbit", "javax.servlet.jsp.jstl.impl").versionAsInProject(),
1396 mavenBundle("org.ops4j.pax.exam", "pax-exam-container-native").versionAsInProject(),
1397 mavenBundle("org.ops4j.pax.exam", "pax-exam-junit4").versionAsInProject(),
1398 mavenBundle("org.ops4j.pax.exam", "pax-exam-link-mvn").versionAsInProject(),
1399 mavenBundle("org.ops4j.pax.url", "pax-url-aether").versionAsInProject(),
1401 mavenBundle("org.springframework", "org.springframework.asm").versionAsInProject(),
1402 mavenBundle("org.springframework", "org.springframework.aop").versionAsInProject(),
1403 mavenBundle("org.springframework", "org.springframework.context").versionAsInProject(),
1404 mavenBundle("org.springframework", "org.springframework.context.support").versionAsInProject(),
1405 mavenBundle("org.springframework", "org.springframework.core").versionAsInProject(),
1406 mavenBundle("org.springframework", "org.springframework.beans").versionAsInProject(),
1407 mavenBundle("org.springframework", "org.springframework.expression").versionAsInProject(),
1408 mavenBundle("org.springframework", "org.springframework.web").versionAsInProject(),
1410 mavenBundle("org.aopalliance", "com.springsource.org.aopalliance").versionAsInProject(),
1411 mavenBundle("org.springframework", "org.springframework.web.servlet").versionAsInProject(),
1412 mavenBundle("org.springframework.security", "spring-security-config").versionAsInProject(),
1413 mavenBundle("org.springframework.security", "spring-security-core").versionAsInProject(),
1414 mavenBundle("org.springframework.security", "spring-security-web").versionAsInProject(),
1415 mavenBundle("org.springframework.security", "spring-security-taglibs").versionAsInProject(),
1416 mavenBundle("org.springframework", "org.springframework.transaction").versionAsInProject(),
1418 mavenBundle("org.ow2.chameleon.management", "chameleon-mbeans").versionAsInProject(),
1419 mavenBundle("org.opendaylight.controller.thirdparty", "net.sf.jung2").versionAsInProject(),
1420 mavenBundle("org.opendaylight.controller.thirdparty", "com.sun.jersey.jersey-servlet")
1421 .versionAsInProject(),
1422 mavenBundle("org.opendaylight.controller.thirdparty", "org.apache.catalina.filters.CorsFilter")
1423 .versionAsInProject().noStart(),
1425 // Jersey needs to be started before the northbound application
1426 // bundles, using a lower start level
1427 mavenBundle("com.sun.jersey", "jersey-client").versionAsInProject(),
1428 mavenBundle("com.sun.jersey", "jersey-server").versionAsInProject().startLevel(2),
1429 mavenBundle("com.sun.jersey", "jersey-core").versionAsInProject().startLevel(2),
1430 mavenBundle("com.sun.jersey", "jersey-json").versionAsInProject().startLevel(2), junitBundles());