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();
193 System.out.println("Response : "+sb.toString());
195 return sb.toString();
196 } catch (Exception e) {
201 private void testNodeProperties(JSONObject node, Integer nodeId, String nodeType, Integer timestamp,
202 String timestampName, Integer actionsValue, Integer capabilitiesValue, Integer tablesValue,
203 Integer buffersValue) throws JSONException {
205 JSONObject nodeInfo = node.getJSONObject("node");
206 Assert.assertEquals(nodeId, (Integer) nodeInfo.getInt("id"));
207 Assert.assertEquals(nodeType, nodeInfo.getString("type"));
209 JSONArray propsArray = node.getJSONArray("properties");
211 for (int j = 0; j < propsArray.length(); j++) {
212 JSONObject properties = propsArray.getJSONObject(j);
213 String propName = properties.getString("name");
214 if (propName.equals("timeStamp")) {
215 if (timestamp == null || timestampName == null) {
216 Assert.assertFalse("Timestamp exist", true);
218 Assert.assertEquals(timestamp, (Integer) properties.getInt("value"));
219 Assert.assertEquals(timestampName, properties.getString("timestampName"));
222 if (propName.equals("actions")) {
223 if (actionsValue == null) {
224 Assert.assertFalse("Actions exist", true);
226 Assert.assertEquals(actionsValue, (Integer) properties.getInt("value"));
229 if (propName.equals("capabilities")) {
230 if (capabilitiesValue == null) {
231 Assert.assertFalse("Capabilities exist", true);
233 Assert.assertEquals(capabilitiesValue, (Integer) properties.getInt("value"));
236 if (propName.equals("tables")) {
237 if (tablesValue == null) {
238 Assert.assertFalse("Tables exist", true);
240 Assert.assertEquals(tablesValue, (Integer) properties.getInt("value"));
243 if (propName.equals("buffers")) {
244 if (buffersValue == null) {
245 Assert.assertFalse("Buffers exist", true);
247 Assert.assertEquals(buffersValue, (Integer) properties.getInt("value"));
253 private void testNodeConnectorProperties(JSONObject nodeConnectorProperties, Integer ncId, String ncType,
254 Integer nodeId, String nodeType, Integer state, Integer capabilities, Integer bandwidth)
255 throws JSONException {
257 JSONObject nodeConnector = nodeConnectorProperties.getJSONObject("nodeconnector");
258 JSONObject node = nodeConnector.getJSONObject("node");
260 Assert.assertEquals(ncId, (Integer) nodeConnector.getInt("id"));
261 Assert.assertEquals(ncType, nodeConnector.getString("type"));
262 Assert.assertEquals(nodeId, (Integer) node.getInt("id"));
263 Assert.assertEquals(nodeType, node.getString("type"));
265 JSONArray propsArray = nodeConnectorProperties.getJSONArray("properties");
266 for (int j = 0; j < propsArray.length(); j++) {
267 JSONObject properties = propsArray.getJSONObject(j);
268 String propName = properties.getString("name");
269 if (propName.equals("state")) {
271 Assert.assertFalse("State exist", true);
273 Assert.assertEquals(state, (Integer) properties.getInt("value"));
276 if (propName.equals("capabilities")) {
277 if (capabilities == null) {
278 Assert.assertFalse("Capabilities exist", true);
280 Assert.assertEquals(capabilities, (Integer) properties.getInt("value"));
283 if (propName.equals("bandwidth")) {
284 if (bandwidth == null) {
285 Assert.assertFalse("bandwidth exist", true);
287 Assert.assertEquals(bandwidth, (Integer) properties.getInt("value"));
294 public void testSubnetsNorthbound() throws JSONException {
295 System.out.println("Starting Subnets JAXB client.");
296 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/subnetservice/";
298 String name1 = "testSubnet1";
299 String subnet1 = "1.1.1.1/24";
301 String name2 = "testSubnet2";
302 String subnet2 = "2.2.2.2/24";
303 String[] nodePorts2 = {"2/1", "2/2", "2/3", "2/4"};
304 StringBuilder nodePortsJson2 = new StringBuilder();
305 nodePortsJson2.append(nodePorts2[0] + "," + nodePorts2[1] + "," + nodePorts2[2] + "," + nodePorts2[3]);
307 String name3 = "testSubnet3";
308 String subnet3 = "3.3.3.3/24";
309 String[] nodePorts3 = {"3/1", "3/2", "3/3"};
310 StringBuilder nodePortsJson3 = new StringBuilder();
311 nodePortsJson3.append(nodePorts3[0] + "," + nodePorts3[1] + "," + nodePorts3[2]);
312 StringBuilder nodePortsJson3_1 = new StringBuilder();
313 nodePortsJson3_1.append(nodePortsJson3).append(",").append(nodePortsJson2);
315 // Test GET subnets in default container
316 String result = getJsonResult(baseURL + "default/subnets");
317 JSONTokener jt = new JSONTokener(result);
318 JSONObject json = new JSONObject(jt);
319 JSONArray subnetConfigs = json.getJSONArray("subnetConfig");
320 Assert.assertEquals(subnetConfigs.length(), 0);
322 // Test GET subnet1 expecting 404
323 result = getJsonResult(baseURL + "default/subnet/" + name1);
324 Assert.assertEquals(404, httpResponseCode.intValue());
327 JSONObject jo = new JSONObject().put("name", name1).put("subnet", subnet1);
328 // execute HTTP request and verify response code
329 result = getJsonResult(baseURL + "default/subnet/" + name1, "PUT", jo.toString());
330 Assert.assertTrue(httpResponseCode == 201);
333 result = getJsonResult(baseURL + "default/subnet/" + name1);
334 jt = new JSONTokener(result);
335 json = new JSONObject(jt);
336 Assert.assertEquals(200, httpResponseCode.intValue());
337 Assert.assertEquals(name1, json.getString("name"));
338 Assert.assertEquals(subnet1, json.getString("subnet"));
341 JSONObject jo2 = new JSONObject().put("name", name2).put("subnet", subnet2);
342 // execute HTTP request and verify response code
343 result = getJsonResult(baseURL + "default/subnet/" + name2, "PUT", jo2.toString());
344 Assert.assertEquals(201, httpResponseCode.intValue());
345 // Test POST nodePorts
346 jo2.append("nodePorts", nodePortsJson2);
347 // execute HTTP request and verify response code
348 result = getJsonResult(baseURL + "default/subnet/" + name2 + "/nodePorts", "PUT", jo2.toString());
349 Assert.assertEquals(200, httpResponseCode.intValue());
351 JSONObject jo3 = new JSONObject().put("name", name3).put("subnet", subnet3);
352 // execute HTTP request and verify response code
353 result = getJsonResult(baseURL + "default/subnet/" + name3, "PUT", jo3.toString());
354 Assert.assertEquals(201, httpResponseCode.intValue());
355 // Test POST nodePorts
356 jo3.append("nodePorts", nodePortsJson3);
357 // execute HTTP request and verify response code
358 result = getJsonResult(baseURL + "default/subnet/" + name3 + "/nodePorts", "PUT", jo3.toString());
359 Assert.assertEquals(200, httpResponseCode.intValue());
360 // Test PUT nodePorts
361 jo3.remove("nodePorts");
362 jo3.append("nodePorts", nodePortsJson3_1);
363 result = getJsonResult(baseURL + "default/subnet/" + name3 + "/nodePorts", "POST", jo3.toString());
364 Assert.assertEquals(200, httpResponseCode.intValue());
366 // Test GET all subnets in default container
367 result = getJsonResult(baseURL + "default/subnets");
368 jt = new JSONTokener(result);
369 json = new JSONObject(jt);
370 JSONArray subnetConfigArray = json.getJSONArray("subnetConfig");
371 JSONObject subnetConfig;
372 Assert.assertEquals(3, subnetConfigArray.length());
373 for (int i = 0; i < subnetConfigArray.length(); i++) {
374 subnetConfig = subnetConfigArray.getJSONObject(i);
375 if (subnetConfig.getString("name").equals(name1)) {
376 Assert.assertEquals(subnet1, subnetConfig.getString("subnet"));
377 } else if (subnetConfig.getString("name").equals(name2)) {
378 Assert.assertEquals(subnet2, subnetConfig.getString("subnet"));
379 String[] nodePortsGet2 = subnetConfig.getJSONArray("nodePorts").getString(0).split(",");
380 Assert.assertEquals(nodePorts2[0], nodePortsGet2[0]);
381 Assert.assertEquals(nodePorts2[1], nodePortsGet2[1]);
382 Assert.assertEquals(nodePorts2[2], nodePortsGet2[2]);
383 Assert.assertEquals(nodePorts2[3], nodePortsGet2[3]);
384 } else if (subnetConfig.getString("name").equals(name3)) {
385 Assert.assertEquals(subnet3, subnetConfig.getString("subnet"));
386 String[] nodePortsGet = subnetConfig.getJSONArray("nodePorts").getString(0).split(",");
387 Assert.assertEquals(nodePorts3[0], nodePortsGet[0]);
388 Assert.assertEquals(nodePorts3[1], nodePortsGet[1]);
389 Assert.assertEquals(nodePorts3[2], nodePortsGet[2]);
390 Assert.assertEquals(nodePorts2[0], nodePortsGet[3]);
391 Assert.assertEquals(nodePorts2[1], nodePortsGet[4]);
392 Assert.assertEquals(nodePorts2[2], nodePortsGet[5]);
393 Assert.assertEquals(nodePorts2[3], nodePortsGet[6]);
395 // Unexpected config name
396 Assert.assertTrue(false);
400 // Test DELETE subnet1
401 result = getJsonResult(baseURL + "default/subnet/" + name1, "DELETE");
402 Assert.assertEquals(204, httpResponseCode.intValue());
404 // Test GET deleted subnet1
405 result = getJsonResult(baseURL + "default/subnet/" + name1);
406 Assert.assertEquals(404, httpResponseCode.intValue());
410 public void testStaticRoutingNorthbound() throws JSONException {
411 System.out.println("Starting StaticRouting JAXB client.");
412 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/staticroute/";
414 String name1 = "testRoute1";
415 String prefix1 = "192.168.1.1/24";
416 String nextHop1 = "0.0.0.0";
417 String name2 = "testRoute2";
418 String prefix2 = "192.168.1.1/16";
419 String nextHop2 = "1.1.1.1";
421 // Test GET static routes in default container, expecting no results
422 String result = getJsonResult(baseURL + "default/routes");
423 JSONTokener jt = new JSONTokener(result);
424 JSONObject json = new JSONObject(jt);
425 JSONArray staticRoutes = json.getJSONArray("staticRoute");
426 Assert.assertEquals(staticRoutes.length(), 0);
428 // Test insert static route
429 String requestBody = "{\"name\":\"" + name1 + "\", \"prefix\":\"" + prefix1 + "\", \"nextHop\":\"" + nextHop1
431 result = getJsonResult(baseURL + "default/route/" + name1, "PUT", requestBody);
432 Assert.assertEquals(201, httpResponseCode.intValue());
434 requestBody = "{\"name\":\"" + name2 + "\", \"prefix\":\"" + prefix2 + "\", \"nextHop\":\"" + nextHop2 + "\"}";
435 result = getJsonResult(baseURL + "default/route/" + name2, "PUT", requestBody);
436 Assert.assertEquals(201, httpResponseCode.intValue());
438 // Test Get all static routes
439 result = getJsonResult(baseURL + "default/routes");
440 jt = new JSONTokener(result);
441 json = new JSONObject(jt);
442 JSONArray staticRouteArray = json.getJSONArray("staticRoute");
443 Assert.assertEquals(2, staticRouteArray.length());
445 for (int i = 0; i < staticRoutes.length(); i++) {
446 route = staticRoutes.getJSONObject(i);
447 if (route.getString("name").equals(name1)) {
448 Assert.assertEquals(prefix1, route.getString("prefix"));
449 Assert.assertEquals(nextHop1, route.getString("nextHop"));
450 } else if (route.getString("name").equals(name2)) {
451 Assert.assertEquals(prefix2, route.getString("prefix"));
452 Assert.assertEquals(nextHop2, route.getString("nextHop"));
454 // static route has unknown name
455 Assert.assertTrue(false);
459 // Test get specific static route
460 result = getJsonResult(baseURL + "default/route/" + name1);
461 jt = new JSONTokener(result);
462 json = new JSONObject(jt);
464 Assert.assertEquals(name1, json.getString("name"));
465 Assert.assertEquals(prefix1, json.getString("prefix"));
466 Assert.assertEquals(nextHop1, json.getString("nextHop"));
468 result = getJsonResult(baseURL + "default/route/" + name2);
469 jt = new JSONTokener(result);
470 json = new JSONObject(jt);
472 Assert.assertEquals(name2, json.getString("name"));
473 Assert.assertEquals(prefix2, json.getString("prefix"));
474 Assert.assertEquals(nextHop2, json.getString("nextHop"));
476 // Test delete static route
477 result = getJsonResult(baseURL + "default/route/" + name1, "DELETE");
478 Assert.assertEquals(204, httpResponseCode.intValue());
480 result = getJsonResult(baseURL + "default/routes");
481 jt = new JSONTokener(result);
482 json = new JSONObject(jt);
484 staticRouteArray = json.getJSONArray("staticRoute");
485 JSONObject singleStaticRoute = staticRouteArray.getJSONObject(0);
486 Assert.assertEquals(name2, singleStaticRoute.getString("name"));
491 public void testSwitchManager() throws JSONException {
492 System.out.println("Starting SwitchManager JAXB client.");
493 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/switchmanager/default/";
495 // define Node/NodeConnector attributes for test
496 int nodeId_1 = 51966;
499 int nodeConnectorId_1 = 51966;
500 int nodeConnectorId_2 = 12;
501 int nodeConnectorId_3 = 34;
502 String nodeType = "STUB";
503 String ncType = "STUB";
504 int timestamp_1 = 100000;
505 String timestampName_1 = "connectedSince";
506 int actionsValue_1 = 2;
507 int capabilitiesValue_1 = 3;
508 int tablesValue_1 = 1;
509 int buffersValue_1 = 1;
511 int ncCapabilities = 1;
512 int ncBandwidth = 1000000000;
514 // Test GET all nodes
516 String result = getJsonResult(baseURL + "nodes");
517 JSONTokener jt = new JSONTokener(result);
518 JSONObject json = new JSONObject(jt);
520 // Test for first node
521 JSONObject node = getJsonInstance(json, "nodeProperties", nodeId_1);
522 Assert.assertNotNull(node);
523 testNodeProperties(node, nodeId_1, nodeType, timestamp_1, timestampName_1, actionsValue_1, capabilitiesValue_1,
524 tablesValue_1, buffersValue_1);
526 // Test 2nd node, properties of 2nd node same as first node
527 node = getJsonInstance(json, "nodeProperties", nodeId_2);
528 Assert.assertNotNull(node);
529 testNodeProperties(node, nodeId_2, nodeType, timestamp_1, timestampName_1, actionsValue_1, capabilitiesValue_1,
530 tablesValue_1, buffersValue_1);
532 // Test 3rd node, properties of 3rd node same as first node
533 node = getJsonInstance(json, "nodeProperties", nodeId_3);
534 Assert.assertNotNull(node);
535 testNodeProperties(node, nodeId_3, nodeType, timestamp_1, timestampName_1, actionsValue_1, capabilitiesValue_1,
536 tablesValue_1, buffersValue_1);
538 // Test GET nodeConnectors of a node
540 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1);
541 jt = new JSONTokener(result);
542 json = new JSONObject(jt);
543 JSONArray nodeConnectorPropertiesArray = json.getJSONArray("nodeConnectorProperties");
544 JSONObject nodeConnectorProperties = nodeConnectorPropertiesArray.getJSONObject(0);
546 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_1, ncType, nodeId_1, nodeType, ncState,
547 ncCapabilities, ncBandwidth);
550 result = getJsonResult(baseURL + "node/STUB/" + nodeId_2);
551 jt = new JSONTokener(result);
552 json = new JSONObject(jt);
554 nodeConnectorPropertiesArray = json.getJSONArray("nodeConnectorProperties");
555 nodeConnectorProperties = nodeConnectorPropertiesArray.getJSONObject(0);
558 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_2, ncType, nodeId_2, nodeType, ncState,
559 ncCapabilities, ncBandwidth);
562 result = getJsonResult(baseURL + "node/STUB/" + nodeId_3);
563 jt = new JSONTokener(result);
564 json = new JSONObject(jt);
566 nodeConnectorPropertiesArray = json.getJSONArray("nodeConnectorProperties");
567 nodeConnectorProperties = nodeConnectorPropertiesArray.getJSONObject(0);
568 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_3, ncType, nodeId_3, nodeType, ncState,
569 ncCapabilities, ncBandwidth);
571 // Test add property to node
572 // Add Tier and Description property to node1
573 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/tier/1001", "PUT");
574 Assert.assertEquals(201, httpResponseCode.intValue());
575 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/description/node1", "PUT");
576 Assert.assertEquals(201, httpResponseCode.intValue());
578 // Test for first node
579 result = getJsonResult(baseURL + "nodes");
580 jt = new JSONTokener(result);
581 json = new JSONObject(jt);
582 node = getJsonInstance(json, "nodeProperties", nodeId_1);
583 Assert.assertNotNull(node);
585 JSONArray propsArray = node.getJSONArray("properties");
587 for (int j = 0; j < propsArray.length(); j++) {
588 JSONObject properties = propsArray.getJSONObject(j);
589 String propName = properties.getString("name");
590 if (propName.equals("tier")) {
591 Assert.assertEquals(1001, properties.getInt("value"));
593 if (propName.equals("description")) {
594 Assert.assertEquals("node1", properties.getString("value"));
598 // Test delete nodeConnector property
599 // Delete state property of nodeconnector1
600 result = getJsonResult(baseURL + "nodeconnector/STUB/" + nodeId_1 + "/STUB/" + nodeConnectorId_1
601 + "/property/state", "DELETE");
602 Assert.assertEquals(204, httpResponseCode.intValue());
604 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1);
605 jt = new JSONTokener(result);
606 json = new JSONObject(jt);
607 nodeConnectorPropertiesArray = json.getJSONArray("nodeConnectorProperties");
608 nodeConnectorProperties = nodeConnectorPropertiesArray.getJSONObject(0);
610 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_1, ncType, nodeId_1, nodeType, null,
611 ncCapabilities, ncBandwidth);
613 // Delete capabilities property of nodeconnector2
614 result = getJsonResult(baseURL + "nodeconnector/STUB/" + nodeId_2 + "/STUB/" + nodeConnectorId_2
615 + "/property/capabilities", "DELETE");
616 Assert.assertEquals(204, httpResponseCode.intValue());
618 result = getJsonResult(baseURL + "node/STUB/" + nodeId_2);
619 jt = new JSONTokener(result);
620 json = new JSONObject(jt);
621 nodeConnectorPropertiesArray = json.getJSONArray("nodeConnectorProperties");
622 nodeConnectorProperties = nodeConnectorPropertiesArray.getJSONObject(0);
624 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_2, ncType, nodeId_2, nodeType, ncState,
627 // Test PUT nodeConnector property
628 int newBandwidth = 1001;
630 // Add Name/Bandwidth property to nodeConnector1
631 result = getJsonResult(baseURL + "nodeconnector/STUB/" + nodeId_1 + "/STUB/" + nodeConnectorId_1
632 + "/property/bandwidth/" + newBandwidth, "PUT");
633 Assert.assertEquals(201, httpResponseCode.intValue());
635 result = getJsonResult(baseURL + "node/STUB/" + nodeId_1);
636 jt = new JSONTokener(result);
637 json = new JSONObject(jt);
638 nodeConnectorPropertiesArray = json.getJSONArray("nodeConnectorProperties");
639 nodeConnectorProperties = nodeConnectorPropertiesArray.getJSONObject(0);
641 // Check for new bandwidth value, state value removed from previous
643 testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_1, ncType, nodeId_1, nodeType, null,
644 ncCapabilities, newBandwidth);
649 public void testStatistics() throws JSONException {
650 final String actionTypes[] = { "DROP", "LOOPBACK", "FLOOD", "FLOOD_ALL", "CONTROLLER", "SW_PATH", "HW_PATH", "OUTPUT",
651 "SET_DL_SRC", "SET_DL_DST", "SET_DL_TYPE", "SET_VLAN_ID", "SET_VLAN_PCP", "SET_VLAN_CFI", "POP_VLAN", "PUSH_VLAN",
652 "SET_NW_SRC", "SET_NW_DST", "SET_NW_TOS", "SET_TP_SRC", "SET_TP_DST" };
653 System.out.println("Starting Statistics JAXB client.");
655 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/statistics/default/";
657 String result = getJsonResult(baseURL + "flow");
658 JSONTokener jt = new JSONTokener(result);
659 JSONObject json = new JSONObject(jt);
660 JSONObject flowStatistics = getJsonInstance(json, "flowStatistics", 0xCAFE);
661 JSONObject node = flowStatistics.getJSONObject("node");
662 // test that node was returned properly
663 Assert.assertTrue(node.getInt("id") == 0xCAFE);
664 Assert.assertEquals(node.getString("type"), "STUB");
666 // test that flow statistics results are correct
667 JSONArray flowStats = flowStatistics.getJSONArray("flowStatistic");
668 for (int i = 0; i < flowStats.length(); i++) {
670 JSONObject flowStat = flowStats.getJSONObject(i);
671 testFlowStat(flowStat, actionTypes[i], i);
675 // for /controller/nb/v2/statistics/default/port
676 result = getJsonResult(baseURL + "port");
677 jt = new JSONTokener(result);
678 json = new JSONObject(jt);
679 JSONObject portStatistics = getJsonInstance(json, "portStatistics", 0xCAFE);
680 JSONObject node2 = portStatistics.getJSONObject("node");
681 // test that node was returned properly
682 Assert.assertTrue(node2.getInt("id") == 0xCAFE);
683 Assert.assertEquals(node2.getString("type"), "STUB");
685 // test that port statistic results are correct
686 JSONArray portStatArray = portStatistics.getJSONArray("portStatistic");
687 JSONObject portStat = portStatArray.getJSONObject(0);
688 Assert.assertTrue(portStat.getInt("receivePackets") == 250);
689 Assert.assertTrue(portStat.getInt("transmitPackets") == 500);
690 Assert.assertTrue(portStat.getInt("receiveBytes") == 1000);
691 Assert.assertTrue(portStat.getInt("transmitBytes") == 5000);
692 Assert.assertTrue(portStat.getInt("receiveDrops") == 2);
693 Assert.assertTrue(portStat.getInt("transmitDrops") == 50);
694 Assert.assertTrue(portStat.getInt("receiveErrors") == 3);
695 Assert.assertTrue(portStat.getInt("transmitErrors") == 10);
696 Assert.assertTrue(portStat.getInt("receiveFrameError") == 5);
697 Assert.assertTrue(portStat.getInt("receiveOverRunError") == 6);
698 Assert.assertTrue(portStat.getInt("receiveCrcError") == 1);
699 Assert.assertTrue(portStat.getInt("collisionCount") == 4);
701 // test for getting one specific node's stats
702 result = getJsonResult(baseURL + "flow/node/STUB/51966");
703 jt = new JSONTokener(result);
704 json = new JSONObject(jt);
705 node = json.getJSONObject("node");
706 // test that node was returned properly
707 Assert.assertTrue(node.getInt("id") == 0xCAFE);
708 Assert.assertEquals(node.getString("type"), "STUB");
710 // test that flow statistics results are correct
711 flowStats = json.getJSONArray("flowStatistic");
712 for (int i = 0; i < flowStats.length(); i++) {
713 JSONObject flowStat = flowStats.getJSONObject(i);
714 testFlowStat(flowStat, actionTypes[i], i);
717 result = getJsonResult(baseURL + "port/node/STUB/51966");
718 jt = new JSONTokener(result);
719 json = new JSONObject(jt);
720 node2 = json.getJSONObject("node");
721 // test that node was returned properly
722 Assert.assertTrue(node2.getInt("id") == 0xCAFE);
723 Assert.assertEquals(node2.getString("type"), "STUB");
725 // test that port statistic results are correct
726 portStatArray = json.getJSONArray("portStatistic");
727 portStat = portStatArray.getJSONObject(0);
729 Assert.assertTrue(portStat.getInt("receivePackets") == 250);
730 Assert.assertTrue(portStat.getInt("transmitPackets") == 500);
731 Assert.assertTrue(portStat.getInt("receiveBytes") == 1000);
732 Assert.assertTrue(portStat.getInt("transmitBytes") == 5000);
733 Assert.assertTrue(portStat.getInt("receiveDrops") == 2);
734 Assert.assertTrue(portStat.getInt("transmitDrops") == 50);
735 Assert.assertTrue(portStat.getInt("receiveErrors") == 3);
736 Assert.assertTrue(portStat.getInt("transmitErrors") == 10);
737 Assert.assertTrue(portStat.getInt("receiveFrameError") == 5);
738 Assert.assertTrue(portStat.getInt("receiveOverRunError") == 6);
739 Assert.assertTrue(portStat.getInt("receiveCrcError") == 1);
740 Assert.assertTrue(portStat.getInt("collisionCount") == 4);
743 private void testFlowStat(JSONObject flowStat, String actionType, int actIndex) throws JSONException {
744 Assert.assertTrue(flowStat.getInt("tableId") == 1);
745 Assert.assertTrue(flowStat.getInt("durationSeconds") == 40);
746 Assert.assertTrue(flowStat.getInt("durationNanoseconds") == 400);
747 Assert.assertTrue(flowStat.getInt("packetCount") == 200);
748 Assert.assertTrue(flowStat.getInt("byteCount") == 100);
750 // test that flow information is correct
751 JSONObject flow = flowStat.getJSONObject("flow");
752 Assert.assertTrue(flow.getInt("priority") == (3500 + actIndex));
753 Assert.assertTrue(flow.getInt("idleTimeout") == 1000);
754 Assert.assertTrue(flow.getInt("hardTimeout") == 2000);
755 Assert.assertTrue(flow.getInt("id") == 12345);
757 JSONArray matches = (flow.getJSONObject("match").getJSONArray("matchField"));
758 Assert.assertEquals(matches.length(), 1);
759 JSONObject match = matches.getJSONObject(0);
760 Assert.assertTrue(match.getString("type").equals("NW_DST"));
761 Assert.assertTrue(match.getString("value").equals("1.1.1.1"));
763 JSONArray actionsArray = flow.getJSONArray("actions");
764 Assert.assertEquals(actionsArray.length(), 1);
765 JSONObject act = actionsArray.getJSONObject(0);
766 Assert.assertTrue(act.getString("type").equals(actionType));
768 if (act.getString("type").equals("OUTPUT")) {
769 JSONObject port = act.getJSONObject("port");
770 JSONObject port_node = port.getJSONObject("node");
771 Assert.assertTrue(port.getInt("id") == 51966);
772 Assert.assertTrue(port.getString("type").equals("STUB"));
773 Assert.assertTrue(port_node.getInt("id") == 51966);
774 Assert.assertTrue(port_node.getString("type").equals("STUB"));
777 if (act.getString("type").equals("SET_DL_SRC")) {
778 byte srcMatch[] = { (byte) 5, (byte) 4, (byte) 3, (byte) 2, (byte) 1 };
779 String src = act.getString("address");
780 byte srcBytes[] = new byte[5];
781 srcBytes[0] = Byte.parseByte(src.substring(0, 2));
782 srcBytes[1] = Byte.parseByte(src.substring(2, 4));
783 srcBytes[2] = Byte.parseByte(src.substring(4, 6));
784 srcBytes[3] = Byte.parseByte(src.substring(6, 8));
785 srcBytes[4] = Byte.parseByte(src.substring(8, 10));
786 Assert.assertTrue(Arrays.equals(srcBytes, srcMatch));
789 if (act.getString("type").equals("SET_DL_DST")) {
790 byte dstMatch[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
791 String dst = act.getString("address");
792 byte dstBytes[] = new byte[5];
793 dstBytes[0] = Byte.parseByte(dst.substring(0, 2));
794 dstBytes[1] = Byte.parseByte(dst.substring(2, 4));
795 dstBytes[2] = Byte.parseByte(dst.substring(4, 6));
796 dstBytes[3] = Byte.parseByte(dst.substring(6, 8));
797 dstBytes[4] = Byte.parseByte(dst.substring(8, 10));
798 Assert.assertTrue(Arrays.equals(dstBytes, dstMatch));
800 if (act.getString("type").equals("SET_DL_TYPE"))
801 Assert.assertTrue(act.getInt("dlType") == 10);
802 if (act.getString("type").equals("SET_VLAN_ID"))
803 Assert.assertTrue(act.getInt("vlanId") == 2);
804 if (act.getString("type").equals("SET_VLAN_PCP"))
805 Assert.assertTrue(act.getInt("pcp") == 3);
806 if (act.getString("type").equals("SET_VLAN_CFI"))
807 Assert.assertTrue(act.getInt("cfi") == 1);
809 if (act.getString("type").equals("SET_NW_SRC"))
810 Assert.assertTrue(act.getString("address").equals("2.2.2.2"));
811 if (act.getString("type").equals("SET_NW_DST"))
812 Assert.assertTrue(act.getString("address").equals("1.1.1.1"));
814 if (act.getString("type").equals("PUSH_VLAN")) {
815 int head = act.getInt("VlanHeader");
816 // parsing vlan header
817 int id = head & 0xfff;
818 int cfi = (head >> 12) & 0x1;
819 int pcp = (head >> 13) & 0x7;
820 int tag = (head >> 16) & 0xffff;
821 Assert.assertTrue(id == 1234);
822 Assert.assertTrue(cfi == 1);
823 Assert.assertTrue(pcp == 1);
824 Assert.assertTrue(tag == 0x8100);
826 if (act.getString("type").equals("SET_NW_TOS"))
827 Assert.assertTrue(act.getInt("tos") == 16);
828 if (act.getString("type").equals("SET_TP_SRC"))
829 Assert.assertTrue(act.getInt("port") == 4201);
830 if (act.getString("type").equals("SET_TP_DST"))
831 Assert.assertTrue(act.getInt("port") == 8080);
835 public void testFlowProgrammer() throws JSONException {
836 System.out.println("Starting FlowProgrammer JAXB client.");
837 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/flowprogrammer/default/";
838 // Attempt to get a flow that doesn't exit. Should return 404
840 String result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test1", "GET");
841 Assert.assertTrue(result.equals("404"));
844 String fc = "{\"name\":\"test1\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"DROP\"]}";
845 result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test1", "PUT", fc);
846 Assert.assertTrue(httpResponseCode == 201);
848 // test get returns flow that was added.
849 result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test1", "GET");
850 // check that result came out fine.
851 Assert.assertTrue(httpResponseCode == 200);
852 JSONTokener jt = new JSONTokener(result);
853 JSONObject json = new JSONObject(jt);
854 Assert.assertEquals(json.getString("name"), "test1");
855 JSONArray actionsArray = json.getJSONArray("actions");
856 Assert.assertEquals(actionsArray.getString(0), "DROP");
857 Assert.assertEquals(json.getString("installInHw"), "true");
858 JSONObject node = json.getJSONObject("node");
859 Assert.assertEquals(node.getString("type"), "STUB");
860 Assert.assertEquals(node.getString("id"), "51966");
861 // test adding same flow again fails due to repeat name..return 409
863 result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test1", "PUT", fc);
864 Assert.assertTrue(result.equals("409"));
866 fc = "{\"name\":\"test2\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"DROP\"]}";
867 result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test2", "PUT", fc);
868 // test should return 409 for error due to same flow being added.
869 Assert.assertTrue(result.equals("409"));
871 // add second flow that's different
872 fc = "{\"name\":\"test2\", \"nwSrc\":\"1.1.1.1\", \"node\":{\"id\":\"51966\",\"type\":\"STUB\"}, \"actions\":[\"DROP\"]}";
873 result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test2", "PUT", fc);
874 Assert.assertTrue(httpResponseCode == 201);
876 // check that request returns both flows given node.
877 result = getJsonResult(baseURL + "node/STUB/51966/", "GET");
878 jt = new JSONTokener(result);
879 json = new JSONObject(jt);
880 Assert.assertTrue(json.get("flowConfig") instanceof JSONArray);
881 JSONArray ja = json.getJSONArray("flowConfig");
882 Integer count = ja.length();
883 Assert.assertTrue(count == 2);
885 // check that request returns both flows given just container.
886 result = getJsonResult(baseURL);
887 jt = new JSONTokener(result);
888 json = new JSONObject(jt);
889 Assert.assertTrue(json.get("flowConfig") instanceof JSONArray);
890 ja = json.getJSONArray("flowConfig");
892 Assert.assertTrue(count == 2);
894 // delete a flow, check that it's no longer in list.
895 result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test2", "DELETE");
896 Assert.assertTrue(httpResponseCode == 204);
898 result = getJsonResult(baseURL + "node/STUB/51966/staticFlow/test2", "GET");
899 Assert.assertTrue(result.equals("404"));
902 // method to extract a JSONObject with specified node ID from a JSONObject
903 // that may contain an array of JSONObjects
904 // This is specifically written for statistics manager northbound REST
906 // array_name should be either "flowStatistics" or "portStatistics"
907 private JSONObject getJsonInstance(JSONObject json, String array_name, Integer nodeId) throws JSONException {
908 JSONObject result = null;
909 if (json.get(array_name) instanceof JSONArray) {
910 JSONArray json_array = json.getJSONArray(array_name);
911 for (int i = 0; i < json_array.length(); i++) {
912 result = json_array.getJSONObject(i);
913 Integer nid = result.getJSONObject("node").getInt("id");
914 if (nid.equals(nodeId))
918 result = json.getJSONObject(array_name);
919 Integer nid = result.getJSONObject("node").getInt("id");
920 if (!nid.equals(nodeId))
926 // a class to construct query parameter for HTTP request
927 private class QueryParameter {
928 StringBuilder queryString = null;
931 QueryParameter(String key, String value) {
932 queryString = new StringBuilder();
933 queryString.append("?").append(key).append("=").append(value);
936 // method to add more query parameter
937 QueryParameter add(String key, String value) {
938 this.queryString.append("&").append(key).append("=").append(value);
942 // method to get the query parameter string
944 return this.queryString.toString();
950 public void testHostTracker() throws JSONException {
952 System.out.println("Starting HostTracker JAXB client.");
954 // setup 2 host models for @POST method
956 String networkAddress_1 = "192.168.0.8";
957 String dataLayerAddress_1 = "11:22:33:44:55:66";
958 String nodeType_1 = "STUB";
959 Integer nodeId_1 = 3366;
960 String nodeConnectorType_1 = "STUB";
961 Integer nodeConnectorId_1 = 12;
965 String networkAddress_2 = "10.1.1.1";
966 String dataLayerAddress_2 = "1A:2B:3C:4D:5E:6F";
967 String nodeType_2 = "STUB";
968 Integer nodeId_2 = 4477;
969 String nodeConnectorType_2 = "STUB";
970 Integer nodeConnectorId_2 = 34;
971 String vlan_2 = "123";
973 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/hosttracker/default";
975 // test PUT method: addHost()
976 JSONObject fc_json = new JSONObject();
977 fc_json.put("dataLayerAddress", dataLayerAddress_1);
978 fc_json.put("nodeType", nodeType_1);
979 fc_json.put("nodeId", nodeId_1);
980 fc_json.put("nodeConnectorType", nodeType_1);
981 fc_json.put("nodeConnectorId", nodeConnectorId_1.toString());
982 fc_json.put("vlan", vlan_1);
983 fc_json.put("staticHost", "true");
984 fc_json.put("networkAddress", networkAddress_1);
986 String result = getJsonResult(baseURL + "/address/" + networkAddress_1, "PUT", fc_json.toString());
987 Assert.assertTrue(httpResponseCode == 201);
989 fc_json = new JSONObject();
990 fc_json.put("dataLayerAddress", dataLayerAddress_2);
991 fc_json.put("nodeType", nodeType_2);
992 fc_json.put("nodeId", nodeId_2);
993 fc_json.put("nodeConnectorType", nodeType_2);
994 fc_json.put("nodeConnectorId", nodeConnectorId_2.toString());
995 fc_json.put("vlan", vlan_2);
996 fc_json.put("staticHost", "true");
997 fc_json.put("networkAddress", networkAddress_2);
999 result = getJsonResult(baseURL + "/address/" + networkAddress_2 , "PUT", fc_json.toString());
1000 Assert.assertTrue(httpResponseCode == 201);
1002 // define variables for decoding returned strings
1003 String networkAddress;
1004 JSONObject host_jo, dl_jo, nc_jo, node_jo;
1006 // the two hosts should be in inactive host DB
1007 // test GET method: getInactiveHosts()
1008 result = getJsonResult(baseURL + "/hosts/inactive", "GET");
1009 Assert.assertTrue(httpResponseCode == 200);
1011 JSONTokener jt = new JSONTokener(result);
1012 JSONObject json = new JSONObject(jt);
1013 // there should be at least two hosts in the DB
1014 Assert.assertTrue(json.get("hostConfig") instanceof JSONArray);
1015 JSONArray ja = json.getJSONArray("hostConfig");
1016 Integer count = ja.length();
1017 Assert.assertTrue(count == 2);
1019 for (int i = 0; i < count; i++) {
1020 host_jo = ja.getJSONObject(i);
1021 networkAddress = host_jo.getString("networkAddress");
1022 if (networkAddress.equalsIgnoreCase(networkAddress_1)) {
1023 Assert.assertTrue(host_jo.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_1));
1024 Assert.assertTrue(host_jo.getString("nodeConnectorType").equalsIgnoreCase(nodeConnectorType_1));
1025 Assert.assertTrue(host_jo.getInt("nodeConnectorId") == nodeConnectorId_1);
1026 Assert.assertTrue(host_jo.getString("nodeType").equalsIgnoreCase(nodeType_1));
1027 Assert.assertTrue(host_jo.getInt("nodeId") == nodeId_1);
1028 Assert.assertTrue(host_jo.getString("vlan").equals("0"));
1029 Assert.assertTrue(host_jo.getBoolean("staticHost"));
1030 } else if (networkAddress.equalsIgnoreCase(networkAddress_2)) {
1031 Assert.assertTrue(host_jo.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_2));
1032 Assert.assertTrue(host_jo.getString("nodeConnectorType").equalsIgnoreCase(nodeConnectorType_2));
1033 Assert.assertTrue(host_jo.getInt("nodeConnectorId") == nodeConnectorId_2);
1034 Assert.assertTrue(host_jo.getString("nodeType").equalsIgnoreCase(nodeType_2));
1035 Assert.assertTrue(host_jo.getInt("nodeId") == nodeId_2);
1036 Assert.assertTrue(host_jo.getString("vlan").equalsIgnoreCase(vlan_2));
1037 Assert.assertTrue(host_jo.getBoolean("staticHost"));
1039 Assert.assertTrue(false);
1043 // test GET method: getActiveHosts() - no host expected
1044 result = getJsonResult(baseURL + "/hosts/active", "GET");
1045 Assert.assertTrue(httpResponseCode == 200);
1047 jt = new JSONTokener(result);
1048 json = new JSONObject(jt);
1049 Assert.assertFalse(hostInJson(json, networkAddress_1));
1050 Assert.assertFalse(hostInJson(json, networkAddress_2));
1052 // put the 1st host into active host DB
1056 nd = new Node(nodeType_1, nodeId_1);
1057 ndc = new NodeConnector(nodeConnectorType_1, nodeConnectorId_1, nd);
1058 this.invtoryListener.notifyNodeConnector(ndc, UpdateType.ADDED, null);
1059 } catch (ConstructionException e) {
1064 // verify the host shows up in active host DB
1066 result = getJsonResult(baseURL + "/hosts/active", "GET");
1067 Assert.assertTrue(httpResponseCode == 200);
1069 jt = new JSONTokener(result);
1070 json = new JSONObject(jt);
1072 Assert.assertTrue(hostInJson(json, networkAddress_1));
1074 // test GET method for getHostDetails()
1076 result = getJsonResult(baseURL + "/address/" + networkAddress_1, "GET");
1077 Assert.assertTrue(httpResponseCode == 200);
1079 jt = new JSONTokener(result);
1080 json = new JSONObject(jt);
1082 Assert.assertFalse(json.length() == 0);
1084 Assert.assertTrue(json.getString("dataLayerAddress").equalsIgnoreCase(dataLayerAddress_1));
1085 Assert.assertTrue(json.getString("nodeConnectorType").equalsIgnoreCase(nodeConnectorType_1));
1086 Assert.assertTrue(json.getInt("nodeConnectorId") == nodeConnectorId_1);
1087 Assert.assertTrue(json.getString("nodeType").equalsIgnoreCase(nodeType_1));
1088 Assert.assertTrue(json.getInt("nodeId") == nodeId_1);
1089 Assert.assertTrue(json.getString("vlan").equals("0"));
1090 Assert.assertTrue(json.getBoolean("staticHost"));
1092 // test DELETE method for deleteFlow()
1094 result = getJsonResult(baseURL + "/address/" + networkAddress_1, "DELETE");
1095 Assert.assertTrue(httpResponseCode == 204);
1097 // verify host_1 removed from active host DB
1098 // test GET method: getActiveHosts() - no host expected
1100 result = getJsonResult(baseURL + "/hosts/active", "GET");
1101 Assert.assertTrue(httpResponseCode == 200);
1103 jt = new JSONTokener(result);
1104 json = new JSONObject(jt);
1106 Assert.assertFalse(hostInJson(json, networkAddress_1));
1110 private Boolean hostInJson(JSONObject json, String hostIp) throws JSONException {
1111 // input JSONObject may be empty
1112 if (json.length() == 0) {
1115 if (json.get("hostConfig") instanceof JSONArray) {
1116 JSONArray ja = json.getJSONArray("hostConfig");
1117 for (int i = 0; i < ja.length(); i++) {
1118 String na = ja.getJSONObject(i).getString("networkAddress");
1119 if (na.equalsIgnoreCase(hostIp))
1124 JSONObject ja = json.getJSONObject("hostConfig");
1125 String na = ja.getString("networkAddress");
1126 return (na.equalsIgnoreCase(hostIp)) ? true : false;
1131 public void testTopology() throws JSONException, ConstructionException {
1132 System.out.println("Starting Topology JAXB client.");
1133 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/topology/default";
1135 // === test GET method for getTopology()
1136 short state_1 = State.EDGE_UP, state_2 = State.EDGE_DOWN;
1137 long bw_1 = Bandwidth.BW10Gbps, bw_2 = Bandwidth.BW100Mbps;
1138 long lat_1 = Latency.LATENCY100ns, lat_2 = Latency.LATENCY1ms;
1139 String nodeType = "STUB";
1140 String nodeConnType = "STUB";
1141 int headNC1_nodeId = 1, headNC1_nodeConnId = 11;
1142 int tailNC1_nodeId = 2, tailNC1_nodeConnId = 22;
1143 int headNC2_nodeId = 2, headNC2_nodeConnId = 21;
1144 int tailNC2_nodeId = 1, tailNC2_nodeConnId = 12;
1146 List<TopoEdgeUpdate> topoedgeupdateList = new ArrayList<TopoEdgeUpdate>();
1147 NodeConnector headnc1 = new NodeConnector(nodeConnType, headNC1_nodeConnId, new Node(nodeType, headNC1_nodeId));
1148 NodeConnector tailnc1 = new NodeConnector(nodeConnType, tailNC1_nodeConnId, new Node(nodeType, tailNC1_nodeId));
1149 Edge e1 = new Edge(tailnc1, headnc1);
1150 Set<Property> props_1 = new HashSet<Property>();
1151 props_1.add(new State(state_1));
1152 props_1.add(new Bandwidth(bw_1));
1153 props_1.add(new Latency(lat_1));
1154 TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props_1, UpdateType.ADDED);
1155 topoedgeupdateList.add(teu1);
1157 NodeConnector headnc2 = new NodeConnector(nodeConnType, headNC2_nodeConnId, new Node(nodeType, headNC2_nodeId));
1158 NodeConnector tailnc2 = new NodeConnector(nodeConnType, tailNC2_nodeConnId, new Node(nodeType, tailNC2_nodeId));
1159 Edge e2 = new Edge(tailnc2, headnc2);
1160 Set<Property> props_2 = new HashSet<Property>();
1161 props_2.add(new State(state_2));
1162 props_2.add(new Bandwidth(bw_2));
1163 props_2.add(new Latency(lat_2));
1165 TopoEdgeUpdate teu2 = new TopoEdgeUpdate(e2, props_2, UpdateType.ADDED);
1166 topoedgeupdateList.add(teu2);
1168 topoUpdates.edgeUpdate(topoedgeupdateList);
1171 String result = getJsonResult(baseURL, "GET");
1172 Assert.assertTrue(httpResponseCode == 200);
1174 System.out.println("Get Topology: " + result);
1177 // returned data must be an array of edges
1178 JSONTokener jt = new JSONTokener(result);
1179 JSONObject json = new JSONObject(jt);
1180 Assert.assertTrue(json.get("edgeProperties") instanceof JSONArray);
1181 JSONArray ja = json.getJSONArray("edgeProperties");
1183 for (int i = 0; i < ja.length(); i++) {
1184 JSONObject edgeProp = ja.getJSONObject(i);
1185 JSONObject edge = edgeProp.getJSONObject("edge");
1186 JSONObject tailNC = edge.getJSONObject("tailNodeConnector");
1187 JSONObject tailNode = tailNC.getJSONObject("node");
1189 JSONObject headNC = edge.getJSONObject("headNodeConnector");
1190 JSONObject headNode = headNC.getJSONObject("node");
1192 JSONArray propsArray = edgeProp.getJSONArray("properties");
1194 JSONObject bandw = null;
1195 JSONObject stt = null;
1196 JSONObject ltc = null;
1198 for (int j = 0; j < propsArray.length(); j++) {
1199 JSONObject props = propsArray.getJSONObject(j);
1200 String propName = props.getString("name");
1201 if (propName.equals("bandwidth")) bandw = props;
1202 if (propName.equals("state")) stt = props;
1203 if (propName.equals("latency")) ltc = props;
1206 if (headNC.getInt("id") == headNC1_nodeConnId) {
1207 Assert.assertEquals(headNode.getString("type"), nodeType);
1208 Assert.assertEquals(headNode.getLong("id"), headNC1_nodeId);
1209 Assert.assertEquals(headNC.getString("type"), nodeConnType);
1210 Assert.assertEquals(tailNode.getString("type"),nodeType);
1211 Assert.assertEquals(tailNode.getString("type"), nodeConnType);
1212 Assert.assertEquals(tailNC.getLong("id"), tailNC1_nodeConnId);
1213 Assert.assertEquals(bandw.getLong("value"), bw_1);
1214 Assert.assertTrue((short) stt.getInt("value") == state_1);
1215 Assert.assertEquals(ltc.getLong("value"), lat_1);
1216 } else if (headNC.getInt("id") == headNC2_nodeConnId) {
1217 Assert.assertEquals(headNode.getString("type"),nodeType);
1218 Assert.assertEquals(headNode.getLong("id"), headNC2_nodeId);
1219 Assert.assertEquals(headNC.getString("type"), nodeConnType);
1220 Assert.assertEquals(tailNode.getString("type"), nodeType);
1221 Assert.assertTrue(tailNode.getInt("id") == tailNC2_nodeId);
1222 Assert.assertEquals(tailNC.getString("type"), nodeConnType);
1223 Assert.assertEquals(tailNC.getLong("id"), tailNC2_nodeConnId);
1224 Assert.assertEquals(bandw.getLong("value"), bw_2);
1225 Assert.assertTrue((short) stt.getInt("value") == state_2);
1226 Assert.assertEquals(ltc.getLong("value"), lat_2);
1230 // === test POST method for addUserLink()
1231 // define 2 sample nodeConnectors for user link configuration tests
1232 String nodeType_1 = "STUB";
1233 Integer nodeId_1 = 3366;
1234 String nodeConnectorType_1 = "STUB";
1235 Integer nodeConnectorId_1 = 12;
1237 String nodeType_2 = "STUB";
1238 Integer nodeId_2 = 4477;
1239 String nodeConnectorType_2 = "STUB";
1240 Integer nodeConnectorId_2 = 34;
1242 JSONObject jo = new JSONObject()
1243 .put("name", "userLink_1")
1244 .put("srcNodeConnector",
1245 nodeConnectorType_1 + "|" + nodeConnectorId_1 + "@" + nodeType_1 + "|" + nodeId_1)
1246 .put("dstNodeConnector",
1247 nodeConnectorType_2 + "|" + nodeConnectorId_2 + "@" + nodeType_2 + "|" + nodeId_2);
1248 // execute HTTP request and verify response code
1249 result = getJsonResult(baseURL + "/userLink/userLink_1", "PUT", jo.toString());
1250 Assert.assertTrue(httpResponseCode == 201);
1252 // === test GET method for getUserLinks()
1253 result = getJsonResult(baseURL + "/userLinks", "GET");
1254 Assert.assertTrue(httpResponseCode == 200);
1256 System.out.println("result:" + result);
1259 jt = new JSONTokener(result);
1260 json = new JSONObject(jt);
1262 // should have at least one object returned
1263 Assert.assertFalse(json.length() == 0);
1264 JSONObject userlink = new JSONObject();
1266 if (json.get("userLinks") instanceof JSONArray) {
1267 ja = json.getJSONArray("userLinks");
1269 for (i = 0; i < ja.length(); i++) {
1270 userlink = ja.getJSONObject(i);
1271 if (userlink.getString("name").equalsIgnoreCase("userLink_1"))
1274 Assert.assertFalse(i == ja.length());
1276 userlink = json.getJSONObject("userLinks");
1277 Assert.assertTrue(userlink.getString("name").equalsIgnoreCase("userLink_1"));
1280 String[] level_1, level_2;
1281 level_1 = userlink.getString("srcNodeConnector").split("\\@");
1282 level_2 = level_1[0].split("\\|");
1283 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeConnectorType_1));
1284 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeConnectorId_1);
1285 level_2 = level_1[1].split("\\|");
1286 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeType_1));
1287 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeId_1);
1288 level_1 = userlink.getString("dstNodeConnector").split("\\@");
1289 level_2 = level_1[0].split("\\|");
1290 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeConnectorType_2));
1291 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeConnectorId_2);
1292 level_2 = level_1[1].split("\\|");
1293 Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeType_2));
1294 Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeId_2);
1296 // === test DELETE method for deleteUserLink()
1297 String userName = "userLink_1";
1298 result = getJsonResult(baseURL + "/userLink/" + userName, "DELETE");
1299 Assert.assertTrue(httpResponseCode == 204);
1301 // execute another getUserLinks() request to verify that userLink_1 is
1303 result = getJsonResult(baseURL + "/userLinks", "GET");
1304 Assert.assertTrue(httpResponseCode == 200);
1306 System.out.println("result:" + result);
1308 jt = new JSONTokener(result);
1309 json = new JSONObject(jt);
1311 if (json.length() != 0) {
1312 if (json.get("userLinks") instanceof JSONArray) {
1313 ja = json.getJSONArray("userLinks");
1314 for (int i = 0; i < ja.length(); i++) {
1315 userlink = ja.getJSONObject(i);
1316 Assert.assertFalse(userlink.getString("name").equalsIgnoreCase("userLink_1"));
1319 userlink = json.getJSONObject("userLinks");
1320 Assert.assertFalse(userlink.getString("name").equalsIgnoreCase("userLink_1"));
1325 // Configure the OSGi container
1327 public Option[] config() {
1330 systemProperty("logback.configurationFile").value(
1331 "file:" + PathUtils.getBaseDir() + "/src/test/resources/logback.xml"),
1332 // To start OSGi console for inspection remotely
1333 systemProperty("osgi.console").value("2401"),
1334 systemProperty("org.eclipse.gemini.web.tomcat.config.path").value(
1335 PathUtils.getBaseDir() + "/src/test/resources/tomcat-server.xml"),
1337 // setting default level. Jersey bundles will need to be started
1339 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
1341 // Set the systemPackages (used by clustering)
1342 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
1343 mavenBundle("org.slf4j", "jcl-over-slf4j").versionAsInProject(),
1344 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
1345 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
1346 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
1347 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),
1348 mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
1349 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager").versionAsInProject(),
1351 // the plugin stub to get data for the tests
1352 mavenBundle("org.opendaylight.controller", "protocol_plugins.stub").versionAsInProject(),
1354 // List all the opendaylight modules
1355 mavenBundle("org.opendaylight.controller", "configuration").versionAsInProject(),
1356 mavenBundle("org.opendaylight.controller", "configuration.implementation").versionAsInProject(),
1357 mavenBundle("org.opendaylight.controller", "containermanager").versionAsInProject(),
1358 mavenBundle("org.opendaylight.controller", "containermanager.implementation").versionAsInProject(),
1359 mavenBundle("org.opendaylight.controller", "clustering.services").versionAsInProject(),
1360 mavenBundle("org.opendaylight.controller", "clustering.services-implementation").versionAsInProject(),
1361 mavenBundle("org.opendaylight.controller", "security").versionAsInProject().noStart(),
1362 mavenBundle("org.opendaylight.controller", "sal").versionAsInProject(),
1363 mavenBundle("org.opendaylight.controller", "sal.implementation").versionAsInProject(),
1364 mavenBundle("org.opendaylight.controller", "sal.connection").versionAsInProject(),
1365 mavenBundle("org.opendaylight.controller", "sal.connection.implementation").versionAsInProject(),
1366 mavenBundle("org.opendaylight.controller", "switchmanager").versionAsInProject(),
1367 mavenBundle("org.opendaylight.controller", "connectionmanager").versionAsInProject(),
1368 mavenBundle("org.opendaylight.controller", "connectionmanager.implementation").versionAsInProject(),
1369 mavenBundle("org.opendaylight.controller", "switchmanager.implementation").versionAsInProject(),
1370 mavenBundle("org.opendaylight.controller", "forwardingrulesmanager").versionAsInProject(),
1371 mavenBundle("org.opendaylight.controller",
1372 "forwardingrulesmanager.implementation").versionAsInProject(),
1373 mavenBundle("org.opendaylight.controller", "statisticsmanager").versionAsInProject(),
1374 mavenBundle("org.opendaylight.controller", "statisticsmanager.implementation").versionAsInProject(),
1375 mavenBundle("org.opendaylight.controller", "arphandler").versionAsInProject(),
1376 mavenBundle("org.opendaylight.controller", "hosttracker").versionAsInProject(),
1377 mavenBundle("org.opendaylight.controller", "hosttracker.implementation").versionAsInProject(),
1378 mavenBundle("org.opendaylight.controller", "arphandler").versionAsInProject(),
1379 mavenBundle("org.opendaylight.controller", "routing.dijkstra_implementation").versionAsInProject(),
1380 mavenBundle("org.opendaylight.controller", "topologymanager").versionAsInProject(),
1381 mavenBundle("org.opendaylight.controller", "usermanager").versionAsInProject(),
1382 mavenBundle("org.opendaylight.controller", "usermanager.implementation").versionAsInProject(),
1383 mavenBundle("org.opendaylight.controller", "logging.bridge").versionAsInProject(),
1384 // mavenBundle("org.opendaylight.controller", "clustering.test").versionAsInProject(),
1385 mavenBundle("org.opendaylight.controller", "forwarding.staticrouting").versionAsInProject(),
1387 // Northbound bundles
1388 mavenBundle("org.opendaylight.controller", "commons.northbound").versionAsInProject(),
1389 mavenBundle("org.opendaylight.controller", "forwarding.staticrouting.northbound").versionAsInProject(),
1390 mavenBundle("org.opendaylight.controller", "statistics.northbound").versionAsInProject(),
1391 mavenBundle("org.opendaylight.controller", "topology.northbound").versionAsInProject(),
1392 mavenBundle("org.opendaylight.controller", "hosttracker.northbound").versionAsInProject(),
1393 mavenBundle("org.opendaylight.controller", "switchmanager.northbound").versionAsInProject(),
1394 mavenBundle("org.opendaylight.controller", "flowprogrammer.northbound").versionAsInProject(),
1395 mavenBundle("org.opendaylight.controller", "subnets.northbound").versionAsInProject(),
1397 mavenBundle("org.codehaus.jackson", "jackson-mapper-asl").versionAsInProject(),
1398 mavenBundle("org.codehaus.jackson", "jackson-core-asl").versionAsInProject(),
1399 mavenBundle("org.codehaus.jackson", "jackson-jaxrs").versionAsInProject(),
1400 mavenBundle("org.codehaus.jackson", "jackson-xc").versionAsInProject(),
1401 mavenBundle("org.codehaus.jettison", "jettison").versionAsInProject(),
1403 mavenBundle("commons-io", "commons-io").versionAsInProject(),
1405 mavenBundle("commons-fileupload", "commons-fileupload").versionAsInProject(),
1407 mavenBundle("equinoxSDK381", "javax.servlet").versionAsInProject(),
1408 mavenBundle("equinoxSDK381", "javax.servlet.jsp").versionAsInProject(),
1409 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
1410 mavenBundle("orbit", "javax.xml.rpc").versionAsInProject(),
1411 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
1412 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
1413 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
1414 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
1415 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
1416 mavenBundle("equinoxSDK381", "org.eclipse.equinox.cm").versionAsInProject(),
1417 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console").versionAsInProject(),
1418 mavenBundle("equinoxSDK381", "org.eclipse.equinox.launcher").versionAsInProject(),
1420 mavenBundle("geminiweb", "org.eclipse.gemini.web.core").versionAsInProject(),
1421 mavenBundle("geminiweb", "org.eclipse.gemini.web.extender").versionAsInProject(),
1422 mavenBundle("geminiweb", "org.eclipse.gemini.web.tomcat").versionAsInProject(),
1423 mavenBundle("geminiweb", "org.eclipse.virgo.kernel.equinox.extensions").versionAsInProject().noStart(),
1424 mavenBundle("geminiweb", "org.eclipse.virgo.util.common").versionAsInProject(),
1425 mavenBundle("geminiweb", "org.eclipse.virgo.util.io").versionAsInProject(),
1426 mavenBundle("geminiweb", "org.eclipse.virgo.util.math").versionAsInProject(),
1427 mavenBundle("geminiweb", "org.eclipse.virgo.util.osgi").versionAsInProject(),
1428 mavenBundle("geminiweb", "org.eclipse.virgo.util.osgi.manifest").versionAsInProject(),
1429 mavenBundle("geminiweb", "org.eclipse.virgo.util.parser.manifest").versionAsInProject(),
1431 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager").versionAsInProject(),
1432 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager.shell").versionAsInProject(),
1434 mavenBundle("com.google.code.gson", "gson").versionAsInProject(),
1435 mavenBundle("org.jboss.spec.javax.transaction", "jboss-transaction-api_1.1_spec").versionAsInProject(),
1436 mavenBundle("org.apache.felix", "org.apache.felix.fileinstall").versionAsInProject(),
1437 mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
1438 mavenBundle("commons-codec", "commons-codec").versionAsInProject(),
1439 mavenBundle("virgomirror", "org.eclipse.jdt.core.compiler.batch").versionAsInProject(),
1440 mavenBundle("eclipselink", "javax.persistence").versionAsInProject(),
1441 mavenBundle("eclipselink", "javax.resource").versionAsInProject(),
1443 mavenBundle("orbit", "javax.activation").versionAsInProject(),
1444 mavenBundle("orbit", "javax.annotation").versionAsInProject(),
1445 mavenBundle("orbit", "javax.ejb").versionAsInProject(),
1446 mavenBundle("orbit", "javax.el").versionAsInProject(),
1447 mavenBundle("orbit", "javax.mail.glassfish").versionAsInProject(),
1448 mavenBundle("orbit", "javax.xml.rpc").versionAsInProject(),
1449 mavenBundle("orbit", "org.apache.catalina").versionAsInProject(),
1450 // these are bundle fragments that can't be started on its own
1451 mavenBundle("orbit", "org.apache.catalina.ha").versionAsInProject().noStart(),
1452 mavenBundle("orbit", "org.apache.catalina.tribes").versionAsInProject().noStart(),
1453 mavenBundle("orbit", "org.apache.coyote").versionAsInProject().noStart(),
1454 mavenBundle("orbit", "org.apache.jasper").versionAsInProject().noStart(),
1456 mavenBundle("orbit", "org.apache.el").versionAsInProject(),
1457 mavenBundle("orbit", "org.apache.juli.extras").versionAsInProject(),
1458 mavenBundle("orbit", "org.apache.tomcat.api").versionAsInProject(),
1459 mavenBundle("orbit", "org.apache.tomcat.util").versionAsInProject().noStart(),
1460 mavenBundle("orbit", "javax.servlet.jsp.jstl").versionAsInProject(),
1461 mavenBundle("orbit", "javax.servlet.jsp.jstl.impl").versionAsInProject(),
1463 mavenBundle("org.ops4j.pax.exam", "pax-exam-container-native").versionAsInProject(),
1464 mavenBundle("org.ops4j.pax.exam", "pax-exam-junit4").versionAsInProject(),
1465 mavenBundle("org.ops4j.pax.exam", "pax-exam-link-mvn").versionAsInProject(),
1466 mavenBundle("org.ops4j.pax.url", "pax-url-aether").versionAsInProject(),
1468 mavenBundle("org.springframework", "org.springframework.asm").versionAsInProject(),
1469 mavenBundle("org.springframework", "org.springframework.aop").versionAsInProject(),
1470 mavenBundle("org.springframework", "org.springframework.context").versionAsInProject(),
1471 mavenBundle("org.springframework", "org.springframework.context.support").versionAsInProject(),
1472 mavenBundle("org.springframework", "org.springframework.core").versionAsInProject(),
1473 mavenBundle("org.springframework", "org.springframework.beans").versionAsInProject(),
1474 mavenBundle("org.springframework", "org.springframework.expression").versionAsInProject(),
1475 mavenBundle("org.springframework", "org.springframework.web").versionAsInProject(),
1477 mavenBundle("org.aopalliance", "com.springsource.org.aopalliance").versionAsInProject(),
1478 mavenBundle("org.springframework", "org.springframework.web.servlet").versionAsInProject(),
1479 mavenBundle("org.springframework.security", "spring-security-config").versionAsInProject(),
1480 mavenBundle("org.springframework.security", "spring-security-core").versionAsInProject(),
1481 mavenBundle("org.springframework.security", "spring-security-web").versionAsInProject(),
1482 mavenBundle("org.springframework.security", "spring-security-taglibs").versionAsInProject(),
1483 mavenBundle("org.springframework", "org.springframework.transaction").versionAsInProject(),
1485 mavenBundle("org.ow2.chameleon.management", "chameleon-mbeans").versionAsInProject(),
1486 mavenBundle("org.opendaylight.controller.thirdparty", "net.sf.jung2").versionAsInProject(),
1487 mavenBundle("org.opendaylight.controller.thirdparty", "com.sun.jersey.jersey-servlet")
1488 .versionAsInProject(),
1489 mavenBundle("org.opendaylight.controller.thirdparty", "org.apache.catalina.filters.CorsFilter")
1490 .versionAsInProject().noStart(),
1492 // Jersey needs to be started before the northbound application
1493 // bundles, using a lower start level
1494 mavenBundle("com.sun.jersey", "jersey-client").versionAsInProject(),
1495 mavenBundle("com.sun.jersey", "jersey-server").versionAsInProject().startLevel(2),
1496 mavenBundle("com.sun.jersey", "jersey-core").versionAsInProject().startLevel(2),
1497 mavenBundle("com.sun.jersey", "jersey-json").versionAsInProject().startLevel(2), junitBundles());