+ }
+
+ @Test
+ public void testSubnetsNorthbound() throws JSONException, ConstructionException {
+ System.out.println("Starting Subnets JAXB client.");
+ String baseURL = "http://127.0.0.1:8080/controller/nb/v2/subnetservice/";
+
+ String name1 = "testSubnet1";
+ String subnet1 = "1.1.1.1/24";
+
+ String name2 = "testSubnet2";
+ String subnet2 = "2.2.2.2/24";
+
+ String name3 = "testSubnet3";
+ String subnet3 = "3.3.3.3/24";
+
+ /*
+ * Create the node connector string list for the two subnets as:
+ * portList2 = {"OF|1@OF|00:00:00:00:00:00:00:02", "OF|2@OF|00:00:00:00:00:00:00:02", "OF|3@OF|00:00:00:00:00:00:00:02", "OF|4@OF|00:00:00:00:00:00:00:02"};
+ * portList3 = {"OF|1@OF|00:00:00:00:00:00:00:03", "OF|2@OF|00:00:00:00:00:00:00:03", "OF|3@OF|00:00:00:00:00:00:00:03"};
+ */
+ Node node2 = new Node(Node.NodeIDType.OPENFLOW, 2L);
+ List<String> portList2 = new ArrayList<String>();
+ NodeConnector nc21 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)1, node2);
+ NodeConnector nc22 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)2, node2);
+ NodeConnector nc23 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)3, node2);
+ NodeConnector nc24 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)3, node2);
+ portList2.add(nc21.toString());
+ portList2.add(nc22.toString());
+ portList2.add(nc23.toString());
+ portList2.add(nc24.toString());
+
+ List<String> portList3 = new ArrayList<String>();
+ Node node3 = new Node(Node.NodeIDType.OPENFLOW, 3L);
+ NodeConnector nc31 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)1, node3);
+ NodeConnector nc32 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)2, node3);
+ NodeConnector nc33 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)3, node3);
+ portList3.add(nc31.toString());
+ portList3.add(nc32.toString());
+ portList3.add(nc33.toString());
+
+ // Test GET subnets in default container
+ String result = getJsonResult(baseURL + "default/subnets");
+ JSONTokener jt = new JSONTokener(result);
+ JSONObject json = new JSONObject(jt);
+ JSONArray subnetConfigs = json.getJSONArray("subnetConfig");
+ Assert.assertEquals(subnetConfigs.length(), 0);
+
+ // Test GET subnet1 expecting 404
+ result = getJsonResult(baseURL + "default/subnet/" + name1);
+ Assert.assertEquals(404, httpResponseCode.intValue());
+
+ // Test POST subnet1
+ JSONObject jo = new JSONObject().put("name", name1).put("subnet", subnet1);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name1, "PUT", 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 PUT subnet2
+ JSONObject jo2 = new JSONObject().put("name", name2).put("subnet", subnet2).put("nodeConnectors", portList2);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name2, "PUT", jo2.toString());
+ Assert.assertEquals(201, httpResponseCode.intValue());
+ // Test PUT subnet3
+ JSONObject jo3 = new JSONObject().put("name", name3).put("subnet", subnet3);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name3, "PUT", jo3.toString());
+ Assert.assertEquals(201, httpResponseCode.intValue());
+ // Test POST subnet3 (modify port list: add)
+ JSONObject jo3New = new JSONObject().put("name", name3).put("subnet", subnet3).put("nodeConnectors", portList3);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name3, "POST", jo3New.toString());
+ Assert.assertEquals(200, httpResponseCode.intValue());
+
+ // Test GET all subnets in default container
+ result = getJsonResult(baseURL + "default/subnets");
+ 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"));
+ JSONArray portListGet = subnetConfig.getJSONArray("nodeConnectors");
+ Assert.assertEquals(portList2.get(0), portListGet.get(0));
+ Assert.assertEquals(portList2.get(1), portListGet.get(1));
+ Assert.assertEquals(portList2.get(2), portListGet.get(2));
+ Assert.assertEquals(portList2.get(3), portListGet.get(3));
+ } else if (subnetConfig.getString("name").equals(name3)) {
+ Assert.assertEquals(subnet3, subnetConfig.getString("subnet"));
+ JSONArray portListGet = subnetConfig.getJSONArray("nodeConnectors");
+ Assert.assertEquals(portList3.get(0), portListGet.get(0));
+ Assert.assertEquals(portList3.get(1), portListGet.get(1));
+ Assert.assertEquals(portList3.get(2), portListGet.get(2));
+ } else {
+ // Unexpected config name
+ Assert.assertTrue(false);
+ }
+ }
+
+ // Test POST subnet2 (modify port list: remove one port only)
+ List<String> newPortList2 = new ArrayList<String>(portList2);
+ newPortList2.remove(3);
+ JSONObject jo2New = new JSONObject().put("name", name2).put("subnet", subnet2).put("nodeConnectors", newPortList2);
+ // execute HTTP request and verify response code
+ result = getJsonResult(baseURL + "default/subnet/" + name2, "POST", jo2New.toString());
+ Assert.assertEquals(200, httpResponseCode.intValue());
+
+ // Test GET subnet2: verify contains only the first three ports
+ result = getJsonResult(baseURL + "default/subnet/" + name2);
+ jt = new JSONTokener(result);
+ subnetConfig = new JSONObject(jt);
+ Assert.assertEquals(200, httpResponseCode.intValue());
+ JSONArray portListGet2 = subnetConfig.getJSONArray("nodeConnectors");
+ Assert.assertEquals(portList2.get(0), portListGet2.get(0));
+ Assert.assertEquals(portList2.get(1), portListGet2.get(1));
+ Assert.assertEquals(portList2.get(2), portListGet2.get(2));
+ Assert.assertTrue(portListGet2.length() == 3);
+
+ // 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 PUT bad subnet, expect 400, validate JSON exception mapper
+ JSONObject joBad = new JSONObject().put("foo", "bar");
+ result = getJsonResult(baseURL + "default/subnet/foo", "PUT", joBad.toString());
+ Assert.assertEquals(400, 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/routes");
+ JSONTokener jt = new JSONTokener(result);
+ JSONObject json = new JSONObject(jt);
+ JSONArray staticRoutes = json.getJSONArray("staticRoute");
+ Assert.assertEquals(staticRoutes.length(), 0);
+
+ // Test insert static route
+ String requestBody = "{\"name\":\"" + name1 + "\", \"prefix\":\"" + prefix1 + "\", \"nextHop\":\"" + nextHop1
+ + "\"}";
+ result = getJsonResult(baseURL + "default/route/" + name1, "PUT", requestBody);
+ Assert.assertEquals(201, httpResponseCode.intValue());
+
+ requestBody = "{\"name\":\"" + name2 + "\", \"prefix\":\"" + prefix2 + "\", \"nextHop\":\"" + nextHop2 + "\"}";
+ result = getJsonResult(baseURL + "default/route/" + name2, "PUT", requestBody);
+ Assert.assertEquals(201, httpResponseCode.intValue());
+
+ // Test Get all static routes
+ result = getJsonResult(baseURL + "default/routes");
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+ JSONArray staticRouteArray = json.getJSONArray("staticRoute");
+ Assert.assertEquals(2, staticRouteArray.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(204, httpResponseCode.intValue());
+
+ result = getJsonResult(baseURL + "default/routes");
+ jt = new JSONTokener(result);
+ json = new JSONObject(jt);
+
+ staticRouteArray = json.getJSONArray("staticRoute");
+ JSONObject singleStaticRoute = staticRouteArray.getJSONObject(0);
+ Assert.assertEquals(name2, singleStaticRoute.getString("name"));