+ public void testSubnetsNorthbound() throws JSONException {
+ System.out.println("Starting Subnets JAXB client.");
+ String baseURL = "http://127.0.0.1:8080/controller/nb/v2/subnet/";
+
+ String name1 = "testSubnet1";
+ String subnet1 = "1.1.1.1/24";
+
+ String name2 = "testSubnet2";
+ String subnet2 = "2.2.2.2/24";
+ String[] nodePorts2 = {"2/1", "2/2", "2/3", "2/4"};
+ StringBuilder nodePortsJson2 = new StringBuilder();
+ nodePortsJson2.append(nodePorts2[0] + "," + nodePorts2[1] + "," + nodePorts2[2] + "," + nodePorts2[3]);
+
+ String name3 = "testSubnet3";
+ String subnet3 = "3.3.3.3/24";
+ String[] nodePorts3 = {"3/1", "3/2", "3/3"};
+ StringBuilder nodePortsJson3 = new StringBuilder();
+ nodePortsJson3.append(nodePorts3[0] + "," + nodePorts3[1] + "," + nodePorts3[2]);
+ StringBuilder nodePortsJson3_1 = new StringBuilder();
+ nodePortsJson3_1.append(nodePortsJson3).append(",").append(nodePortsJson2);
+
+ // Test GET subnets in default container
+ String result = getJsonResult(baseURL + "default/subnet/all");
+ JSONTokener jt = new JSONTokener(result);
+ JSONObject json = new JSONObject(jt);
+ Assert.assertEquals("{}", result);
+
+ // Test GET subnet1 expecting 404
+ result = getJsonResult(baseURL + "default/subnet/" + name1);
+ Assert.assertEquals(404, httpResponseCode.intValue());
+
+ // Test POST subnet1
+ JSONObject jo = new JSONObject().append("name", name1).append("subnet", subnet1);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name1, "POST", jo.toString());
+ Assert.assertTrue(httpResponseCode == 201);
+
+ // Test GET subnet1
+ result = getJsonResult(baseURL + "default/subnet/" + name1);
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+ Assert.assertEquals(200, httpResponseCode.intValue());
+ Assert.assertEquals(name1, json.getString("name"));
+ Assert.assertEquals(subnet1, json.getString("subnet"));
+
+ // Test POST subnet2
+ JSONObject jo2 = new JSONObject().append("name", name2).append("subnet", subnet2);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name2, "POST", jo2.toString());
+ Assert.assertEquals(201, httpResponseCode.intValue());
+ // Test POST nodePorts
+ jo2.append("nodePorts", nodePortsJson2);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name2 + "/node-ports", "POST", jo2.toString());
+ Assert.assertEquals(200, httpResponseCode.intValue());
+ // Test POST subnet3
+ JSONObject jo3 = new JSONObject().append("name", name3).append("subnet", subnet3);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name3, "POST", jo3.toString());
+ Assert.assertEquals(201, httpResponseCode.intValue());
+ // Test POST nodePorts
+ jo3.append("nodePorts", nodePortsJson3);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name3 + "/node-ports", "POST", jo3.toString());
+ Assert.assertEquals(200, httpResponseCode.intValue());
+ // Test PUT nodePorts
+ jo3.remove("nodePorts");
+ jo3.append("nodePorts", nodePortsJson3_1);
+ result = getJsonResult(baseURL + "default/subnet/" + name3 + "/node-ports", "PUT", jo3.toString());
+ Assert.assertEquals(200, httpResponseCode.intValue());
+
+ // Test GET all subnets in default container
+ result = getJsonResult(baseURL + "default/subnet/all");
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+ JSONArray subnetConfigArray = json.getJSONArray("subnetConfig");
+ JSONObject subnetConfig;
+ Assert.assertEquals(3, subnetConfigArray.length());
+ for (int i = 0; i < subnetConfigArray.length(); i++) {
+ subnetConfig = subnetConfigArray.getJSONObject(i);
+ if (subnetConfig.getString("name").equals(name1)) {
+ Assert.assertEquals(subnet1, subnetConfig.getString("subnet"));
+ } else if (subnetConfig.getString("name").equals(name2)) {
+ Assert.assertEquals(subnet2, subnetConfig.getString("subnet"));
+ String[] nodePortsGet2 = subnetConfig.getString("nodePorts").split(",");
+ Assert.assertEquals(nodePorts2[0], nodePortsGet2[0]);
+ Assert.assertEquals(nodePorts2[1], nodePortsGet2[1]);
+ Assert.assertEquals(nodePorts2[2], nodePortsGet2[2]);
+ Assert.assertEquals(nodePorts2[3], nodePortsGet2[3]);
+ } else if (subnetConfig.getString("name").equals(name3)) {
+ Assert.assertEquals(subnet3, subnetConfig.getString("subnet"));
+ String[] nodePortsGet = subnetConfig.getString("nodePorts").split(",");
+ Assert.assertEquals(nodePorts3[0], nodePortsGet[0]);
+ Assert.assertEquals(nodePorts3[1], nodePortsGet[1]);
+ Assert.assertEquals(nodePorts3[2], nodePortsGet[2]);
+ Assert.assertEquals(nodePorts2[0], nodePortsGet[3]);
+ Assert.assertEquals(nodePorts2[1], nodePortsGet[4]);
+ Assert.assertEquals(nodePorts2[2], nodePortsGet[5]);
+ Assert.assertEquals(nodePorts2[3], nodePortsGet[6]);
+ } else {
+ // Unexpected config name
+ Assert.assertTrue(false);
+ }
+ }
+
+ // Test DELETE subnet1
+ result = getJsonResult(baseURL + "default/subnet/" + name1, "DELETE");
+ Assert.assertEquals(204, httpResponseCode.intValue());
+
+ // Test GET deleted subnet1
+ result = getJsonResult(baseURL + "default/subnet/" + name1);
+ Assert.assertEquals(404, httpResponseCode.intValue());
+ }
+
+ @Test
+ public void testStaticRoutingNorthbound() throws JSONException {
+ System.out.println("Starting StaticRouting JAXB client.");
+ String baseURL = "http://127.0.0.1:8080/controller/nb/v2/staticroute/";
+
+ String name1 = "testRoute1";
+ String prefix1 = "192.168.1.1/24";
+ String nextHop1 = "0.0.0.0";
+ String name2 = "testRoute2";
+ String prefix2 = "192.168.1.1/16";
+ String nextHop2 = "1.1.1.1";
+
+ // Test GET static routes in default container, expecting no results
+ String result = getJsonResult(baseURL + "default");
+ JSONTokener jt = new JSONTokener(result);
+ JSONObject json = new JSONObject(jt);
+ Assert.assertEquals("{}", result);
+
+ // Test insert static route
+ String requestBody = "{\"name\":\"" + name1 + "\", \"prefix\":\"" + prefix1 + "\", \"nextHop\":\"" + nextHop1
+ + "\"}";
+ result = getJsonResult(baseURL + "default/route/" + name1, "POST", requestBody);
+ Assert.assertEquals(201, httpResponseCode.intValue());
+
+ requestBody = "{\"name\":\"" + name2 + "\", \"prefix\":\"" + prefix2 + "\", \"nextHop\":\"" + nextHop2 + "\"}";
+ result = getJsonResult(baseURL + "default/route/" + name2, "POST", requestBody);
+ Assert.assertEquals(201, httpResponseCode.intValue());
+
+ // Test Get all static routes
+ result = getJsonResult(baseURL + "default");
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+ JSONArray staticRoutes = json.getJSONArray("staticRoute");
+ Assert.assertEquals(2, staticRoutes.length());
+ JSONObject route;
+ for (int i = 0; i < staticRoutes.length(); i++) {
+ route = staticRoutes.getJSONObject(i);
+ if (route.getString("name").equals(name1)) {
+ Assert.assertEquals(prefix1, route.getString("prefix"));
+ Assert.assertEquals(nextHop1, route.getString("nextHop"));
+ } else if (route.getString("name").equals(name2)) {
+ Assert.assertEquals(prefix2, route.getString("prefix"));
+ Assert.assertEquals(nextHop2, route.getString("nextHop"));
+ } else {
+ // static route has unknown name
+ Assert.assertTrue(false);
+ }
+ }
+
+ // Test get specific static route
+ result = getJsonResult(baseURL + "default/route/" + name1);
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+
+ Assert.assertEquals(name1, json.getString("name"));
+ Assert.assertEquals(prefix1, json.getString("prefix"));
+ Assert.assertEquals(nextHop1, json.getString("nextHop"));
+
+ result = getJsonResult(baseURL + "default/route/" + name2);
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+
+ Assert.assertEquals(name2, json.getString("name"));
+ Assert.assertEquals(prefix2, json.getString("prefix"));
+ Assert.assertEquals(nextHop2, json.getString("nextHop"));
+
+ // Test delete static route
+ result = getJsonResult(baseURL + "default/route/" + name1, "DELETE");
+ Assert.assertEquals(200, httpResponseCode.intValue());
+
+ result = getJsonResult(baseURL + "default");
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+ JSONObject singleStaticRoute = json.getJSONObject("staticRoute");
+ Assert.assertEquals(name2, singleStaticRoute.getString("name"));
+
+ }
+
+ @Test
+ public void testSwitchManager() throws JSONException {
+ System.out.println("Starting SwitchManager JAXB client.");