+ @Test
+ public void testTopology() throws JSONException, ConstructionException {
+ System.out.println("Starting Topology JAXB client.");
+ String baseURL = "http://127.0.0.1:8080/controller/nb/v2/topology/default";
+
+ // === test GET method for getTopology()
+ short state_1 = State.EDGE_UP, state_2 = State.EDGE_DOWN;
+ long bw_1 = Bandwidth.BW10Gbps, bw_2 = Bandwidth.BW100Mbps;
+ long lat_1 = Latency.LATENCY100ns, lat_2 = Latency.LATENCY1ms;
+ String nodeType = "STUB";
+ String nodeConnType = "STUB";
+ int headNC1_nodeId = 1, headNC1_nodeConnId = 11;
+ int tailNC1_nodeId = 2, tailNC1_nodeConnId = 22;
+ int headNC2_nodeId = 2, headNC2_nodeConnId = 21;
+ int tailNC2_nodeId = 1, tailNC2_nodeConnId = 12;
+
+ List<TopoEdgeUpdate> topoedgeupdateList = new ArrayList<TopoEdgeUpdate>();
+ NodeConnector headnc1 = new NodeConnector(nodeConnType, headNC1_nodeConnId, new Node(nodeType, headNC1_nodeId));
+ NodeConnector tailnc1 = new NodeConnector(nodeConnType, tailNC1_nodeConnId, new Node(nodeType, tailNC1_nodeId));
+ Edge e1 = new Edge(tailnc1, headnc1);
+ Set<Property> props_1 = new HashSet<Property>();
+ props_1.add(new State(state_1));
+ props_1.add(new Bandwidth(bw_1));
+ props_1.add(new Latency(lat_1));
+ TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props_1, UpdateType.ADDED);
+ topoedgeupdateList.add(teu1);
+
+ NodeConnector headnc2 = new NodeConnector(nodeConnType, headNC2_nodeConnId, new Node(nodeType, headNC2_nodeId));
+ NodeConnector tailnc2 = new NodeConnector(nodeConnType, tailNC2_nodeConnId, new Node(nodeType, tailNC2_nodeId));
+ Edge e2 = new Edge(tailnc2, headnc2);
+ Set<Property> props_2 = new HashSet<Property>();
+ props_2.add(new State(state_2));
+ props_2.add(new Bandwidth(bw_2));
+ props_2.add(new Latency(lat_2));
+
+ TopoEdgeUpdate teu2 = new TopoEdgeUpdate(e2, props_2, UpdateType.ADDED);
+ topoedgeupdateList.add(teu2);
+
+ topoUpdates.edgeUpdate(topoedgeupdateList);
+
+ // HTTP request
+ String result = getJsonResult(baseURL, "GET");
+ Assert.assertTrue(httpResponseCode == 200);
+ if (debugMsg) {
+ System.out.println("Get Topology: " + result);
+ }
+
+ // returned data must be an array of edges
+ JSONTokener jt = new JSONTokener(result);
+ JSONObject json = new JSONObject(jt);
+ Assert.assertTrue(json.get("edgeProperties") instanceof JSONArray);
+ JSONArray ja = json.getJSONArray("edgeProperties");
+
+ for (int i = 0; i < ja.length(); i++) {
+ JSONObject edgeProp = ja.getJSONObject(i);
+ JSONObject edge = edgeProp.getJSONObject("edge");
+ JSONObject tailNC = edge.getJSONObject("tailNodeConnector");
+ JSONObject tailNode = tailNC.getJSONObject("node");
+
+ JSONObject headNC = edge.getJSONObject("headNodeConnector");
+ JSONObject headNode = headNC.getJSONObject("node");
+ JSONObject Props = edgeProp.getJSONObject("properties");
+ JSONObject bandw = Props.getJSONObject("bandwidth");
+ JSONObject stt = Props.getJSONObject("state");
+ JSONObject ltc = Props.getJSONObject("latency");
+
+ if (headNC.getInt("id") == headNC1_nodeConnId) {
+ Assert.assertEquals(headNode.getString("type"), nodeType);
+ Assert.assertEquals(headNode.getLong("id"), headNC1_nodeId);
+ Assert.assertEquals(headNC.getString("type"), nodeConnType);
+ Assert.assertEquals(tailNode.getString("type"),nodeType);
+ Assert.assertEquals(tailNode.getString("type"), nodeConnType);
+ Assert.assertEquals(tailNC.getLong("id"), tailNC1_nodeConnId);
+ Assert.assertEquals(bandw.getLong("value"), bw_1);
+ Assert.assertTrue((short) stt.getInt("value") == state_1);
+ Assert.assertEquals(ltc.getLong("value"), lat_1);
+ } else if (headNC.getInt("id") == headNC2_nodeConnId) {
+ Assert.assertEquals(headNode.getString("type"),nodeType);
+ Assert.assertEquals(headNode.getLong("id"), headNC2_nodeId);
+ Assert.assertEquals(headNC.getString("type"), nodeConnType);
+ Assert.assertEquals(tailNode.getString("type"), nodeType);
+ Assert.assertTrue(tailNode.getInt("id") == tailNC2_nodeId);
+ Assert.assertEquals(tailNC.getString("type"), nodeConnType);
+ Assert.assertEquals(tailNC.getLong("id"), tailNC2_nodeConnId);
+ Assert.assertEquals(bandw.getLong("value"), bw_2);
+ Assert.assertTrue((short) stt.getInt("value") == state_2);
+ Assert.assertEquals(ltc.getLong("value"), lat_2);
+ }
+ }
+
+ // === test POST method for addUserLink()
+ // define 2 sample nodeConnectors for user link configuration tests
+ String nodeType_1 = "STUB";
+ Integer nodeId_1 = 3366;
+ String nodeConnectorType_1 = "STUB";
+ Integer nodeConnectorId_1 = 12;
+ String nodeType_2 = "STUB";
+ Integer nodeId_2 = 4477;
+ String nodeConnectorType_2 = "STUB";
+ Integer nodeConnectorId_2 = 34;
+
+ JSONObject jo = new JSONObject()
+ .put("name", "userLink_1")
+ .put("srcNodeConnector",
+ nodeConnectorType_1 + "|" + nodeConnectorId_1 + "@" + nodeType_1 + "|" + nodeId_1)
+ .put("dstNodeConnector",
+ nodeConnectorType_2 + "|" + nodeConnectorId_2 + "@" + nodeType_2 + "|" + nodeId_2);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "/userLink/userLink_1", "PUT", jo.toString());
+ Assert.assertTrue(httpResponseCode == 201);
+
+ // === test GET method for getUserLinks()
+ result = getJsonResult(baseURL + "/userLinks", "GET");
+ Assert.assertTrue(httpResponseCode == 200);
+ if (debugMsg) {
+ System.out.println("result:" + result);
+ }
+
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+
+ // should have at least one object returned
+ Assert.assertFalse(json.length() == 0);
+ JSONObject userlink = new JSONObject();
+
+ if (json.get("userLinks") instanceof JSONArray) {
+ ja = json.getJSONArray("userLinks");
+ int i;
+ for (i = 0; i < ja.length(); i++) {
+ userlink = ja.getJSONObject(i);
+ if (userlink.getString("name").equalsIgnoreCase("userLink_1")) {
+ break;
+ }
+ }
+ Assert.assertFalse(i == ja.length());
+ } else {
+ userlink = json.getJSONObject("userLinks");
+ Assert.assertTrue(userlink.getString("name").equalsIgnoreCase("userLink_1"));
+ }
+
+ String[] level_1, level_2;
+ level_1 = userlink.getString("srcNodeConnector").split("\\@");
+ level_2 = level_1[0].split("\\|");
+ Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeConnectorType_1));
+ Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeConnectorId_1);
+ level_2 = level_1[1].split("\\|");
+ Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeType_1));
+ Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeId_1);
+ level_1 = userlink.getString("dstNodeConnector").split("\\@");
+ level_2 = level_1[0].split("\\|");
+ Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeConnectorType_2));
+ Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeConnectorId_2);
+ level_2 = level_1[1].split("\\|");
+ Assert.assertTrue(level_2[0].equalsIgnoreCase(nodeType_2));
+ Assert.assertTrue(Integer.parseInt(level_2[1]) == nodeId_2);
+
+ // === test DELETE method for deleteUserLink()
+ String userName = "userLink_1";
+ result = getJsonResult(baseURL + "/userLink/" + userName, "DELETE");
+ Assert.assertTrue(httpResponseCode == 204);
+
+ // execute another getUserLinks() request to verify that userLink_1 is
+ // removed
+ result = getJsonResult(baseURL + "/userLinks", "GET");
+ Assert.assertTrue(httpResponseCode == 200);
+ if (debugMsg) {
+ System.out.println("result:" + result);
+ }
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+
+ if (json.length() != 0) {
+ if (json.get("userLinks") instanceof JSONArray) {
+ ja = json.getJSONArray("userLinks");
+ for (int i = 0; i < ja.length(); i++) {
+ userlink = ja.getJSONObject(i);
+ Assert.assertFalse(userlink.getString("name").equalsIgnoreCase("userLink_1"));
+ }
+ } else {
+ userlink = json.getJSONObject("userLinks");
+ Assert.assertFalse(userlink.getString("name").equalsIgnoreCase("userLink_1"));
+ }
+ }
+ }