From 61c918367bb084e048b03e12c60282cb689cddea Mon Sep 17 00:00:00 2001 From: Kalvin Hom Date: Thu, 23 May 2013 17:35:34 -0700 Subject: [PATCH] Statistics Northbound Test Added to stub plugin's ReadService to return a flow for each action type Adding additional tests for checking that actions are being passed up properly to the northbound Change-Id: I01548fad2efadada3d7991616787baf6f8e9b0b8 Signed-off-by: Kalvin Hom --- .../NorthboundIntegrationTest.java | 178 +++++++++++++++--- .../stub/internal/ReadService.java | 109 ++++++++--- .../StatisticsManagerIntegrationTest.java | 2 +- 3 files changed, 233 insertions(+), 56 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 47dfa76b28..f8af5c49ec 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 @@ -19,11 +19,15 @@ import org.ops4j.pax.exam.util.PathUtils; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; +import java.util.Arrays; + import org.apache.commons.codec.binary.Base64; +import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONTokener; @@ -83,14 +87,9 @@ public class NorthboundIntegrationTest { } - @Test - public void testStatistics() { - - System.out.println("Starting Statistics JAXB client."); - - String baseURL = "http://127.0.0.1:8080/controller/nb/v2/statistics/default/flowstats"; + private String getJsonResult(String restUrl) { try { - URL url = new URL(baseURL); + URL url = new URL(restUrl); this.users.getAuthorizationList(); this.users.authenticate("admin", "admin"); @@ -116,7 +115,26 @@ public class NorthboundIntegrationTest { sb.append((char) cp); } is.close(); - JSONTokener jt = new JSONTokener(sb.toString()); + return sb.toString(); + } catch (Exception e) { + return null; + } + } + + @Test + public void testStatistics() { + + System.out.println("Starting Statistics JAXB client."); + + String baseURL = "http://127.0.0.1:8080/controller/nb/v2/statistics/default/"; + try { + String actionTypes[] = { "drop", "loopback", "flood", "floodAll", + "controller", "swPath", "hwPath", "output", "setDlSrc", + "setDlDst", "setDlType", "setVlanId", "setVlanPcp", + "setVlanCfi", "popVlan", "pushVlan", "setNwSrc", + "setNwDst", "setNwTos", "setTpSrc", "setTpDst" }; + String result = getJsonResult(baseURL + "flowstats"); + JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); JSONObject flowStatistics = json.getJSONObject("flowStatistics"); JSONObject node = flowStatistics.getJSONObject("node"); @@ -125,27 +143,129 @@ public class NorthboundIntegrationTest { Assert.assertTrue(node.getString("@type").equals("STUB")); // test that flow statistics results are correct - JSONObject flowStat = flowStatistics.getJSONObject("flowStat"); - Assert.assertTrue(flowStat.getInt("tableId") == 1); - Assert.assertTrue(flowStat.getInt("durationSeconds") == 40); - Assert.assertTrue(flowStat.getInt("durationNanoseconds") == 400); - Assert.assertTrue(flowStat.getInt("packetCount") == 200); - Assert.assertTrue(flowStat.getInt("byteCount") == 100); - - // test that flow information is correct - JSONObject flow = flowStat.getJSONObject("flow"); - Assert.assertTrue(flow.getInt("priority") == 3500); - Assert.assertTrue(flow.getInt("idleTimeout") == 1000); - Assert.assertTrue(flow.getInt("hardTimeout") == 2000); - Assert.assertTrue(flow.getInt("id") == 12345); - - JSONObject match = (flow.getJSONObject("match") - .getJSONObject("matchField")); - Assert.assertTrue(match.getString("type").equals("NW_DST")); - Assert.assertTrue(match.getString("value").equals("1.1.1.1")); - - Assert.assertTrue(flow.getJSONObject("actions").getString("@type") - .equals("drop")); + JSONArray flowStats = flowStatistics.getJSONArray("flowStat"); + for (int i = 0; i < flowStats.length(); i++) { + + JSONObject flowStat = flowStats.getJSONObject(i); + Assert.assertTrue(flowStat.getInt("tableId") == 1); + Assert.assertTrue(flowStat.getInt("durationSeconds") == 40); + Assert.assertTrue(flowStat.getInt("durationNanoseconds") == 400); + Assert.assertTrue(flowStat.getInt("packetCount") == 200); + Assert.assertTrue(flowStat.getInt("byteCount") == 100); + + // test that flow information is correct + JSONObject flow = flowStat.getJSONObject("flow"); + Assert.assertTrue(flow.getInt("priority") == 3500); + Assert.assertTrue(flow.getInt("idleTimeout") == 1000); + Assert.assertTrue(flow.getInt("hardTimeout") == 2000); + Assert.assertTrue(flow.getInt("id") == 12345); + + JSONObject match = (flow.getJSONObject("match") + .getJSONObject("matchField")); + Assert.assertTrue(match.getString("type").equals("NW_DST")); + Assert.assertTrue(match.getString("value").equals("1.1.1.1")); + + JSONObject act = flow.getJSONObject("actions"); + Assert.assertTrue(act.getString("@type").equals(actionTypes[i])); + + if (act.getString("@type").equals("output")) { + JSONObject port = act.getJSONObject("port"); + JSONObject port_node = port.getJSONObject("node"); + Assert.assertTrue(port.getInt("@id") == 51966); + Assert.assertTrue(port.getString("@type").equals("STUB")); + Assert.assertTrue(port_node.getInt("@id") == 51966); + Assert.assertTrue(port_node.getString("@type").equals( + "STUB")); + } + + if (act.getString("@type").equals("setDlSrc")) { + byte srcMatch[] = { (byte) 5, (byte) 4, (byte) 3, (byte) 2, + (byte) 1 }; + String src = act.getString("address"); + byte srcBytes[] = new byte[5]; + srcBytes[0] = Byte.parseByte(src.substring(0, 2)); + srcBytes[1] = Byte.parseByte(src.substring(2, 4)); + srcBytes[2] = Byte.parseByte(src.substring(4, 6)); + srcBytes[3] = Byte.parseByte(src.substring(6, 8)); + srcBytes[4] = Byte.parseByte(src.substring(8, 10)); + Assert.assertTrue(Arrays.equals(srcBytes, srcMatch)); + } + + if (act.getString("@type").equals("setDlDst")) { + byte dstMatch[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, + (byte) 5 }; + String dst = act.getString("address"); + byte dstBytes[] = new byte[5]; + dstBytes[0] = Byte.parseByte(dst.substring(0, 2)); + dstBytes[1] = Byte.parseByte(dst.substring(2, 4)); + dstBytes[2] = Byte.parseByte(dst.substring(4, 6)); + dstBytes[3] = Byte.parseByte(dst.substring(6, 8)); + dstBytes[4] = Byte.parseByte(dst.substring(8, 10)); + Assert.assertTrue(Arrays.equals(dstBytes, dstMatch)); + } + if (act.getString("@type").equals("setDlType")) + Assert.assertTrue(act.getInt("dlType") == 10); + if (act.getString("@type").equals("setVlanId")) + Assert.assertTrue(act.getInt("vlanId") == 2); + if (act.getString("@type").equals("setVlanPcp")) + Assert.assertTrue(act.getInt("pcp") == 3); + if (act.getString("@type").equals("setVlanCfi")) + Assert.assertTrue(act.getInt("cfi") == 1); + + if (act.getString("@type").equals("setNwSrc")) + Assert.assertTrue(act.getString("address") + .equals("2.2.2.2")); + if (act.getString("@type").equals("setNwDst")) + Assert.assertTrue(act.getString("address") + .equals("1.1.1.1")); + + if (act.getString("@type").equals("pushVlan")) { + int head = act.getInt("VlanHeader"); + // parsing vlan header + int id = head & 0xfff; + int cfi = (head >> 12) & 0x1; + int pcp = (head >> 13) & 0x7; + int tag = (head >> 16) & 0xffff; + Assert.assertTrue(id == 1234); + Assert.assertTrue(cfi == 1); + Assert.assertTrue(pcp == 1); + Assert.assertTrue(tag == 0x8100); + } + if (act.getString("@type").equals("setNwTos")) + Assert.assertTrue(act.getInt("tos") == 16); + if (act.getString("@type").equals("setTpSrc")) + Assert.assertTrue(act.getInt("port") == 4201); + if (act.getString("@type").equals("setTpDst")) + Assert.assertTrue(act.getInt("port") == 8080); + } + + // for /controller/nb/v2/statistics/default/portstats + result = getJsonResult(baseURL + "portstats"); + jt = new JSONTokener(result); + json = new JSONObject(jt); + JSONObject portStatistics = json.getJSONObject("portStatistics"); + JSONObject node2 = portStatistics.getJSONObject("node"); + // test that node was returned properly + Assert.assertTrue(node2.getInt("@id") == 0xCAFE); + Assert.assertTrue(node2.getString("@type").equals("STUB")); + + // test that port statistic results are correct + JSONObject portStat = portStatistics.getJSONObject("portStat"); + Assert.assertTrue(portStat.getInt("receivePackets") == 250); + Assert.assertTrue(portStat.getInt("transmitPackets") == 500); + Assert.assertTrue(portStat.getInt("receiveBytes") == 1000); + Assert.assertTrue(portStat.getInt("transmitBytes") == 5000); + Assert.assertTrue(portStat.getInt("receiveDrops") == 2); + Assert.assertTrue(portStat.getInt("transmitDrops") == 50); + Assert.assertTrue(portStat.getInt("receiveErrors") == 3); + Assert.assertTrue(portStat.getInt("transmitErrors") == 10); + Assert.assertTrue(portStat.getInt("receiveFrameError") == 5); + Assert.assertTrue(portStat.getInt("receiveOverRunError") == 6); + Assert.assertTrue(portStat.getInt("receiveCrcError") == 1); + Assert.assertTrue(portStat.getInt("collisionCount") == 4); + + // String result = getJsonResult(baseURL+"flowstats/STUB/51966"); + // System.out.println(result); } catch (Exception e) { // Got an unexpected exception diff --git a/opendaylight/protocol_plugins/stub/src/main/java/org/opendaylight/controller/protocol_plugins/stub/internal/ReadService.java b/opendaylight/protocol_plugins/stub/src/main/java/org/opendaylight/controller/protocol_plugins/stub/internal/ReadService.java index d331ff0602..68bd3af751 100644 --- a/opendaylight/protocol_plugins/stub/src/main/java/org/opendaylight/controller/protocol_plugins/stub/internal/ReadService.java +++ b/opendaylight/protocol_plugins/stub/src/main/java/org/opendaylight/controller/protocol_plugins/stub/internal/ReadService.java @@ -12,7 +12,27 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.opendaylight.controller.sal.action.Action; +import org.opendaylight.controller.sal.action.Controller; import org.opendaylight.controller.sal.action.Drop; +import org.opendaylight.controller.sal.action.Flood; +import org.opendaylight.controller.sal.action.FloodAll; +import org.opendaylight.controller.sal.action.HwPath; +import org.opendaylight.controller.sal.action.Loopback; +import org.opendaylight.controller.sal.action.Output; +import org.opendaylight.controller.sal.action.PopVlan; +import org.opendaylight.controller.sal.action.PushVlan; +import org.opendaylight.controller.sal.action.SetDlDst; +import org.opendaylight.controller.sal.action.SetDlSrc; +import org.opendaylight.controller.sal.action.SetDlType; +import org.opendaylight.controller.sal.action.SetNwDst; +import org.opendaylight.controller.sal.action.SetNwSrc; +import org.opendaylight.controller.sal.action.SetNwTos; +import org.opendaylight.controller.sal.action.SetTpDst; +import org.opendaylight.controller.sal.action.SetTpSrc; +import org.opendaylight.controller.sal.action.SetVlanCfi; +import org.opendaylight.controller.sal.action.SetVlanId; +import org.opendaylight.controller.sal.action.SetVlanPcp; +import org.opendaylight.controller.sal.action.SwPath; import org.opendaylight.controller.sal.core.ConstructionException; import org.opendaylight.controller.sal.core.Node; import org.opendaylight.controller.sal.core.NodeConnector; @@ -80,34 +100,71 @@ public class ReadService implements IPluginInReadService { @Override public List readAllFlow(Node node, boolean cached) { - Flow flow = new Flow(); - Match match = new Match(); + ArrayList list = new ArrayList(); + ArrayList actionList = new ArrayList(); + actionList.add(new Drop()); + actionList.add(new Loopback()); + actionList.add(new Flood()); + actionList.add(new FloodAll()); + actionList.add(new Controller()); + actionList.add(new SwPath()); + actionList.add(new HwPath()); try { - match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1")); - } catch (UnknownHostException e) { + actionList.add(new Output(new NodeConnector("STUB", 0xCAFE, node))); + } catch (ConstructionException e) { } - flow.setMatch(match); - Action action = new Drop(); - - List actions = new ArrayList(); - actions.add(action); - flow.setActions(actions); - flow.setPriority((short)3500); - flow.setIdleTimeout((short)1000); - flow.setHardTimeout((short)2000); - flow.setId(12345); - - FlowOnNode fn1 = new FlowOnNode(flow); - fn1.setByteCount(100); - fn1.setDurationNanoseconds(400); - fn1.setDurationSeconds(40); - fn1.setTableId((byte) 0x1); - fn1.setPacketCount(200); + byte dst[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 }; + byte src[] = { (byte) 5, (byte) 4, (byte) 3, (byte) 2, (byte) 1 }; + actionList.add(new SetDlSrc(src)); + actionList.add(new SetDlDst(dst)); + actionList.add(new SetDlType(10)); - ArrayList list = new ArrayList(); - list.add(fn1); + actionList.add(new SetVlanId(2)); + actionList.add(new SetVlanPcp(3)); + actionList.add(new SetVlanCfi(1)); + + actionList.add(new PopVlan()); + actionList.add(new PushVlan(0x8100, 1, 1, 1234)); + + try { + actionList.add(new SetNwSrc(InetAddress.getByName("2.2.2.2"))); + actionList.add(new SetNwDst(InetAddress.getByName("1.1.1.1"))); + } catch (UnknownHostException e) { + + } + actionList.add(new SetNwTos(0x10)); + actionList.add(new SetTpSrc(4201)); + actionList.add(new SetTpDst(8080)); + + for (Action a : actionList) { + Flow flow = new Flow(); + Match match = new Match(); + try { + match.setField(MatchType.NW_DST, + InetAddress.getByName("1.1.1.1")); + } catch (UnknownHostException e) { + + } + flow.setMatch(match); + List actions = new ArrayList(); + actions.add(a); + flow.setActions(actions); + flow.setPriority((short) 3500); + flow.setIdleTimeout((short) 1000); + flow.setHardTimeout((short) 2000); + flow.setId(12345); + + FlowOnNode fn1 = new FlowOnNode(flow); + fn1.setByteCount(100); + fn1.setDurationNanoseconds(400); + fn1.setDurationSeconds(40); + fn1.setTableId((byte) 0x1); + fn1.setPacketCount(200); + + list.add(fn1); + } return list; } @@ -147,11 +204,11 @@ public class ReadService implements IPluginInReadService { public List readAllNodeConnector(Node node, boolean cached) { NodeConnectorStatistics stats = new NodeConnectorStatistics(); - try{ + try { NodeConnector nc = new NodeConnector("STUB", 0xCAFE, node); stats.setNodeConnector(nc); - }catch(ConstructionException e){ - //couldn't create nodeconnector. + } catch (ConstructionException e) { + // couldn't create nodeconnector. } stats.setCollisionCount(4); stats.setReceiveByteCount(1000); diff --git a/opendaylight/statisticsmanager/integrationtest/src/test/java/org/opendaylight/controller/statisticsmanager/internal/StatisticsManagerIntegrationTest.java b/opendaylight/statisticsmanager/integrationtest/src/test/java/org/opendaylight/controller/statisticsmanager/internal/StatisticsManagerIntegrationTest.java index 0fb63a369f..4dafbf87dc 100644 --- a/opendaylight/statisticsmanager/integrationtest/src/test/java/org/opendaylight/controller/statisticsmanager/internal/StatisticsManagerIntegrationTest.java +++ b/opendaylight/statisticsmanager/integrationtest/src/test/java/org/opendaylight/controller/statisticsmanager/internal/StatisticsManagerIntegrationTest.java @@ -247,7 +247,7 @@ public class StatisticsManagerIntegrationTest { public void testGetFlowsNumber() { try{ Node node = new Node("STUB", 0xCAFE); - Assert.assertTrue(this.manager.getFlowsNumber(node) == 1); + Assert.assertTrue(this.manager.getFlowsNumber(node) == 21); }catch(ConstructionException e){ Assert.assertTrue(false); } -- 2.36.6