From 604c7eb86b08f9e187df6702833f562dce4612ec Mon Sep 17 00:00:00 2001 From: Kalvin Hom Date: Tue, 4 Jun 2013 18:15:47 -0700 Subject: [PATCH] FlowProgrammer.Northbound test Added northbound integration tests for adding, getting, and deleting flows. Change-Id: I2ed169db37ff065d650b84e29f8801744c061c26 Signed-off-by: Kalvin Hom --- .../NorthboundIntegrationTest.java | 323 +++++++++++------- 1 file changed, 208 insertions(+), 115 deletions(-) diff --git a/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIntegrationTest.java b/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIntegrationTest.java index 32a9c5a6fc..53eb40c4a5 100644 --- a/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIntegrationTest.java +++ b/opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIntegrationTest.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import org.osgi.framework.ServiceReference; import org.osgi.framework.Bundle; import javax.inject.Inject; + import org.junit.Assert; import org.junit.Test; import org.junit.Before; @@ -19,10 +20,9 @@ import org.ops4j.pax.exam.util.PathUtils; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; import java.nio.charset.Charset; import java.util.Arrays; @@ -41,6 +41,7 @@ import org.opendaylight.controller.sal.core.UpdateType; import org.opendaylight.controller.switchmanager.IInventoryListener; import org.opendaylight.controller.usermanager.IUserManager; + @RunWith(PaxExam.class) public class NorthboundIntegrationTest { private Logger log = LoggerFactory @@ -96,28 +97,31 @@ public class NorthboundIntegrationTest { r = bc.getServiceReference(IfIptoHost.class.getName()); if (r != null) { - this.invtoryListener = (IInventoryListener)bc.getService(r); + this.invtoryListener = (IInventoryListener) bc.getService(r); } // If inventoryListener is null, cannot run hosttracker tests. assertNotNull(this.invtoryListener); - + } // static variable to pass response code from getJsonResult() private static Integer httpResponseCode = null; private String getJsonResult(String restUrl) { - return getJsonResult(restUrl, "GET"); + return getJsonResult(restUrl, "GET", null); } private String getJsonResult(String restUrl, String method) { + return getJsonResult(restUrl, method, null); + } + + private String getJsonResult(String restUrl, String method, String body) { // initialize response code to indicate error httpResponseCode = 400; try { URL url = new URL(restUrl); - this.users.getAuthorizationList(); this.users.authenticate("admin", "admin"); String authString = "admin:admin"; @@ -132,11 +136,20 @@ public class NorthboundIntegrationTest { connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); + if (body != null) { + connection.setDoOutput(true); + OutputStreamWriter wr = new OutputStreamWriter( + connection.getOutputStream()); + wr.write(body); + wr.flush(); + } connection.connect(); connection.getContentType(); // Response code for success should be 2xx httpResponseCode = connection.getResponseCode(); + if (httpResponseCode > 299) + return httpResponseCode.toString(); InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, @@ -152,7 +165,6 @@ public class NorthboundIntegrationTest { } catch (Exception e) { return null; } - } @Test @@ -169,8 +181,8 @@ public class NorthboundIntegrationTest { String result = getJsonResult(baseURL + "flowstats"); JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); - JSONObject flowStatistics = - getJsonInstance (json, "flowStatistics", 0xCAFE); + JSONObject flowStatistics = getJsonInstance(json, "flowStatistics", + 0xCAFE); JSONObject node = flowStatistics.getJSONObject("node"); // test that node was returned properly Assert.assertTrue(node.getInt("@id") == 0xCAFE); @@ -189,8 +201,8 @@ public class NorthboundIntegrationTest { result = getJsonResult(baseURL + "portstats"); jt = new JSONTokener(result); json = new JSONObject(jt); - JSONObject portStatistics = - getJsonInstance (json, "portStatistics", 0xCAFE); + JSONObject portStatistics = getJsonInstance(json, "portStatistics", + 0xCAFE); JSONObject node2 = portStatistics.getJSONObject("node"); // test that node was returned properly Assert.assertTrue(node2.getInt("@id") == 0xCAFE); @@ -351,54 +363,126 @@ public class NorthboundIntegrationTest { } } + @Test + public void testFlowProgrammer() { + try { + String baseURL = "http://127.0.0.1:8080/controller/nb/v2/flow/default/"; + // Attempt to get a flow that doesn't exit. Should return 404 + // status. + String result = getJsonResult(baseURL + "STUB/51966/test1", "GET"); + Assert.assertTrue(result.equals("404")); + + // test add flow1 + String fc = "{\"dynamic\":\"false\", \"name\":\"test1\", \"node\":{\"@id\":\"51966\",\"@type\":\"STUB\"}, \"actions\":[\"DROP\"]}"; + result = getJsonResult(baseURL + "STUB/51966/test1", "POST", fc); + Assert.assertTrue(httpResponseCode == 201); + + // test get returns flow that was added. + result = getJsonResult(baseURL + "STUB/51966/test1", "GET"); + // check that result came out fine. + Assert.assertTrue(httpResponseCode == 200); + JSONTokener jt = new JSONTokener(result); + JSONObject json = new JSONObject(jt); + Assert.assertTrue(json.getString("name").equals("test1")); + Assert.assertTrue(json.getString("actions").equals("DROP")); + Assert.assertTrue(json.getString("installInHw").equals("true")); + JSONObject node = json.getJSONObject("node"); + Assert.assertTrue(node.getString("@type").equals("STUB")); + Assert.assertTrue(node.getString("@id").equals("51966")); + // test adding same flow again fails due to repeat name..return 409 + // code + result = getJsonResult(baseURL + "STUB/51966/test1", "POST", fc); + Assert.assertTrue(result.equals("409")); + + fc = "{\"dynamic\":\"false\", \"name\":\"test2\", \"node\":{\"@id\":\"51966\",\"@type\":\"STUB\"}, \"actions\":[\"DROP\"]}"; + result = getJsonResult(baseURL + "STUB/51966/test2", "POST", fc); + // test should return 500 for error due to same flow being added. + Assert.assertTrue(result.equals("500")); + + // add second flow that's different + fc = "{\"dynamic\":\"false\", \"name\":\"test2\", \"nwSrc\":\"1.1.1.1\", \"node\":{\"@id\":\"51966\",\"@type\":\"STUB\"}, \"actions\":[\"DROP\"]}"; + result = getJsonResult(baseURL + "STUB/51966/test2", "POST", fc); + Assert.assertTrue(httpResponseCode == 201); + + // check that request returns both flows given node. + result = getJsonResult(baseURL + "STUB/51966/", "GET"); + jt = new JSONTokener(result); + json = new JSONObject(jt); + Assert.assertTrue(json.get("flowConfig") instanceof JSONArray); + JSONArray ja = json.getJSONArray("flowConfig"); + Integer count = ja.length(); + Assert.assertTrue(count == 2); + + // check that request returns both flows given just container. + result = getJsonResult(baseURL); + jt = new JSONTokener(result); + json = new JSONObject(jt); + Assert.assertTrue(json.get("flowConfig") instanceof JSONArray); + ja = json.getJSONArray("flowConfig"); + count = ja.length(); + Assert.assertTrue(count == 2); + + // delete a flow, check that it's no longer in list. + result = getJsonResult(baseURL + "STUB/51966/test2", "DELETE"); + Assert.assertTrue(httpResponseCode == 200); + + result = getJsonResult(baseURL + "STUB/51966/test2", "GET"); + Assert.assertTrue(result.equals("404")); + + } catch (Exception e) { + Assert.assertTrue(false); + } + + } + // method to extract a JSONObject with specified node ID from a JSONObject - // that may contain an array of JSONObjects - // This is specifically written for statistics manager northbound REST interface + // that may contain an array of JSONObjects + // This is specifically written for statistics manager northbound REST + // interface // array_name should be either "flowStatistics" or "portStatistics" - private JSONObject getJsonInstance (JSONObject json, String array_name, Integer nodeId) throws JSONException - { + private JSONObject getJsonInstance(JSONObject json, String array_name, + Integer nodeId) throws JSONException { JSONObject result = null; - if (json.get(array_name) instanceof JSONArray){ + if (json.get(array_name) instanceof JSONArray) { JSONArray json_array = json.getJSONArray(array_name); for (int i = 0; i < json_array.length(); i++) { result = json_array.getJSONObject(i); Integer nid = result.getJSONObject("node").getInt("@id"); - if ( nid.equals(nodeId) ) break; + if (nid.equals(nodeId)) + break; } - } - else { + } else { result = json.getJSONObject(array_name); Integer nid = result.getJSONObject("node").getInt("@id"); - if ( ! nid.equals(nodeId) ) result = null; + if (!nid.equals(nodeId)) + result = null; } return result; } - // a class to construct query parameter for HTTP request private class QueryParameter { StringBuilder queryString = null; - + // constructor - QueryParameter (String key, String value) { + QueryParameter(String key, String value) { queryString = new StringBuilder(); queryString.append("?").append(key).append("=").append(value); } - + // method to add more query parameter - QueryParameter add (String key, String value){ + QueryParameter add(String key, String value) { this.queryString.append("&").append(key).append("=").append(value); return this; } - + // method to get the query parameter string - String getString (){ + String getString() { return this.queryString.toString(); } } - - + @Test public void testHostTracker() { @@ -413,8 +497,7 @@ public class NorthboundIntegrationTest { String nodeConnectorType_1 = "STUB"; Integer nodeConnectorId_1 = 12; String vlan_1 = "4"; - - + // 2nd host String networkAddress_2 = "10.1.1.1"; String dataLayerAddress_2 = "1A:2B:3C:4D:5E:6F"; @@ -423,81 +506,87 @@ public class NorthboundIntegrationTest { String nodeConnectorType_2 = "STUB"; Integer nodeConnectorId_2 = 34; String vlan_2 = "0"; - + String baseURL = "http://127.0.0.1:8080/controller/nb/v2/host/default"; // test POST method: addHost() try { - String queryParameter = - new QueryParameter("dataLayerAddress", dataLayerAddress_1) - .add("nodeType", nodeType_1) + String queryParameter = new QueryParameter("dataLayerAddress", + dataLayerAddress_1).add("nodeType", nodeType_1) .add("nodeId", nodeId_1.toString()) .add("nodeConnectorType", nodeConnectorType_1) .add("nodeConnectorId", nodeConnectorId_1.toString()) - .add("vlan", vlan_1) - .getString(); + .add("vlan", vlan_1).getString(); - String result = getJsonResult(baseURL +"/" + networkAddress_1 + queryParameter, "POST"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 201); - - // vlan is not passed through query parameter but should be defaulted to "0" - queryParameter = - new QueryParameter("dataLayerAddress", dataLayerAddress_2) - .add("nodeType", nodeType_2) + String result = getJsonResult(baseURL + "/" + networkAddress_1 + + queryParameter, "POST"); + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 201); + + // vlan is not passed through query parameter but should be + // defaulted to "0" + queryParameter = new QueryParameter("dataLayerAddress", + dataLayerAddress_2).add("nodeType", nodeType_2) .add("nodeId", nodeId_2.toString()) .add("nodeConnectorType", nodeConnectorType_2) .add("nodeConnectorId", nodeConnectorId_2.toString()) .getString(); - result = getJsonResult(baseURL +"/" + networkAddress_2 + queryParameter, "POST"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 201); + result = getJsonResult(baseURL + "/" + networkAddress_2 + + queryParameter, "POST"); + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 201); } catch (Exception e) { // Got an unexpected exception Assert.assertTrue(false); } - + // define variables for decoding returned strings String networkAddress; JSONObject host_jo, dl_jo, nc_jo, node_jo; - + // the two hosts should be in inactive host DB // test GET method: getInactiveHosts() try { - String result = getJsonResult(baseURL +"/inactive", "GET"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 200); - + String result = getJsonResult(baseURL + "/inactive", "GET"); + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 200); + JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); // there should be at least two hosts in the DB - Assert.assertTrue (json.get("host") instanceof JSONArray); + Assert.assertTrue(json.get("host") instanceof JSONArray); JSONArray ja = json.getJSONArray("host"); Integer count = ja.length(); - Assert.assertTrue (count == 2); - - for ( int i = 0; i < count; i++) { + Assert.assertTrue(count == 2); + + for (int i = 0; i < count; i++) { host_jo = ja.getJSONObject(i); dl_jo = host_jo.getJSONObject("dataLayerAddress"); nc_jo = host_jo.getJSONObject("nodeConnector"); node_jo = nc_jo.getJSONObject("node"); - + networkAddress = host_jo.getString("networkAddress"); if (networkAddress.equalsIgnoreCase(networkAddress_1)) { - Assert.assertTrue (dl_jo.getString("macAddress").equalsIgnoreCase(dataLayerAddress_1)); - Assert.assertTrue (nc_jo.getString("@type").equalsIgnoreCase(nodeConnectorType_1)); - Assert.assertTrue (Integer.parseInt(nc_jo.getString("@id")) == nodeConnectorId_1); - Assert.assertTrue (node_jo.getString("@type").equalsIgnoreCase(nodeType_1)); - Assert.assertTrue (Integer.parseInt(node_jo.getString("@id")) == nodeId_1); - Assert.assertTrue (host_jo.getString("vlan").equalsIgnoreCase(vlan_1)); - } - else if (networkAddress.equalsIgnoreCase(networkAddress_2)) { - Assert.assertTrue (dl_jo.getString("macAddress").equalsIgnoreCase(dataLayerAddress_2)); - Assert.assertTrue (nc_jo.getString("@type").equalsIgnoreCase(nodeConnectorType_2)); - Assert.assertTrue (Integer.parseInt(nc_jo.getString("@id")) == nodeConnectorId_2); - Assert.assertTrue (node_jo.getString("@type").equalsIgnoreCase(nodeType_2)); - Assert.assertTrue (Integer.parseInt(node_jo.getString("@id")) == nodeId_2); - Assert.assertTrue (host_jo.getString("vlan").equalsIgnoreCase(vlan_2)); - } - else { + Assert.assertTrue(dl_jo.getString("macAddress") + .equalsIgnoreCase(dataLayerAddress_1)); + Assert.assertTrue(nc_jo.getString("@type") + .equalsIgnoreCase(nodeConnectorType_1)); + Assert.assertTrue(Integer.parseInt(nc_jo.getString("@id")) == nodeConnectorId_1); + Assert.assertTrue(node_jo.getString("@type") + .equalsIgnoreCase(nodeType_1)); + Assert.assertTrue(Integer.parseInt(node_jo.getString("@id")) == nodeId_1); + Assert.assertTrue(host_jo.getString("vlan") + .equalsIgnoreCase(vlan_1)); + } else if (networkAddress.equalsIgnoreCase(networkAddress_2)) { + Assert.assertTrue(dl_jo.getString("macAddress") + .equalsIgnoreCase(dataLayerAddress_2)); + Assert.assertTrue(nc_jo.getString("@type") + .equalsIgnoreCase(nodeConnectorType_2)); + Assert.assertTrue(Integer.parseInt(nc_jo.getString("@id")) == nodeConnectorId_2); + Assert.assertTrue(node_jo.getString("@type") + .equalsIgnoreCase(nodeType_2)); + Assert.assertTrue(Integer.parseInt(node_jo.getString("@id")) == nodeId_2); + Assert.assertTrue(host_jo.getString("vlan") + .equalsIgnoreCase(vlan_2)); + } else { Assert.assertTrue(false); } } @@ -505,12 +594,12 @@ public class NorthboundIntegrationTest { // Got an unexpected exception Assert.assertTrue(false); } - + // test GET method: getActiveHosts() - no host expected try { String result = getJsonResult(baseURL, "GET"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 200); - + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 200); + JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); Assert.assertFalse(hostInJson(json, networkAddress_1)); @@ -519,108 +608,112 @@ public class NorthboundIntegrationTest { // Got an unexpected exception Assert.assertTrue(false); } - + // put the 1st host into active host DB Node nd; NodeConnector ndc; try { nd = new Node(nodeType_1, nodeId_1); ndc = new NodeConnector(nodeConnectorType_1, nodeConnectorId_1, nd); - this.invtoryListener.notifyNodeConnector(ndc, - UpdateType.ADDED, null); - }catch(ConstructionException e){ + this.invtoryListener.notifyNodeConnector(ndc, UpdateType.ADDED, + null); + } catch (ConstructionException e) { ndc = null; nd = null; } - + // verify the host shows up in active host DB try { String result = getJsonResult(baseURL, "GET"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 200); - + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 200); + JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); - + Assert.assertTrue(hostInJson(json, networkAddress_1)); } catch (Exception e) { // Got an unexpected exception Assert.assertTrue(false); } - - + // test GET method for getHostDetails() try { - String result = getJsonResult(baseURL+"/"+networkAddress_1, "GET"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 200); - + String result = getJsonResult(baseURL + "/" + networkAddress_1, + "GET"); + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 200); + JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); Assert.assertFalse(json.length() == 0); - + dl_jo = json.getJSONObject("dataLayerAddress"); nc_jo = json.getJSONObject("nodeConnector"); node_jo = nc_jo.getJSONObject("node"); - - Assert.assertTrue (json.getString("networkAddress").equalsIgnoreCase(networkAddress_1)); - Assert.assertTrue (dl_jo.getString("macAddress").equalsIgnoreCase(dataLayerAddress_1)); - Assert.assertTrue (nc_jo.getString("@type").equalsIgnoreCase(nodeConnectorType_1)); - Assert.assertTrue (Integer.parseInt(nc_jo.getString("@id")) == nodeConnectorId_1); - Assert.assertTrue (node_jo.getString("@type").equalsIgnoreCase(nodeType_1)); - Assert.assertTrue (Integer.parseInt(node_jo.getString("@id")) == nodeId_1); - Assert.assertTrue (json.getString("vlan").equalsIgnoreCase(vlan_1)); + + Assert.assertTrue(json.getString("networkAddress") + .equalsIgnoreCase(networkAddress_1)); + Assert.assertTrue(dl_jo.getString("macAddress").equalsIgnoreCase( + dataLayerAddress_1)); + Assert.assertTrue(nc_jo.getString("@type").equalsIgnoreCase( + nodeConnectorType_1)); + Assert.assertTrue(Integer.parseInt(nc_jo.getString("@id")) == nodeConnectorId_1); + Assert.assertTrue(node_jo.getString("@type").equalsIgnoreCase( + nodeType_1)); + Assert.assertTrue(Integer.parseInt(node_jo.getString("@id")) == nodeId_1); + Assert.assertTrue(json.getString("vlan").equalsIgnoreCase(vlan_1)); } catch (Exception e) { // Got an unexpected exception Assert.assertTrue(false); } - + // test DELETE method for deleteFlow() try { - String result = getJsonResult(baseURL+"/"+networkAddress_1, "DELETE"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 200); - + String result = getJsonResult(baseURL + "/" + networkAddress_1, + "DELETE"); + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 200); + } catch (Exception e) { // Got an unexpected exception Assert.assertTrue(false); } - + // verify host_1 removed from active host DB // test GET method: getActiveHosts() - no host expected try { String result = getJsonResult(baseURL, "GET"); - Assert.assertTrue (httpResponseCode.intValue() == (Integer) 200); - + Assert.assertTrue(httpResponseCode.intValue() == (Integer) 200); + JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); - + Assert.assertFalse(hostInJson(json, networkAddress_1)); } catch (Exception e) { // Got an unexpected exception Assert.assertTrue(false); } - } - - private Boolean hostInJson (JSONObject json, String hostIp) throws JSONException { - // input JSONObject may be empty - if ( json.length() == 0 ) { + } + + private Boolean hostInJson(JSONObject json, String hostIp) + throws JSONException { + // input JSONObject may be empty + if (json.length() == 0) { return false; } - if (json.get("host") instanceof JSONArray){ + if (json.get("host") instanceof JSONArray) { JSONArray ja = json.getJSONArray("host"); for (int i = 0; i < ja.length(); i++) { String na = ja.getJSONObject(i).getString("networkAddress"); if (na.equalsIgnoreCase(hostIp)) return true; - } + } return false; - } - else { + } else { String na = json.getJSONObject("host").getString("networkAddress"); return (na.equalsIgnoreCase(hostIp)) ? true : false; } } - // Configure the OSGi container @Configuration public Option[] config() { -- 2.36.6