From: Katrina LaCurts Date: Mon, 25 Nov 2013 19:32:31 +0000 (-0500) Subject: Unit tests for host-pair statistics. X-Git-Tag: jenkins-affinity-bulk-release-prepare-only-1~19^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=a99dc1a3017da4f33ce966ff69c4ded2498f6133;p=affinity.git Unit tests for host-pair statistics. Signed-off-by: Katrina LaCurts --- diff --git a/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java b/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java index ac85dbe..0032e88 100644 --- a/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java +++ b/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java @@ -475,8 +475,12 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager } private Set getHostsNotInSubnet(String subnet) { - Set hostsInSubnet = getHostsInSubnet(subnet); - Set otherHosts = this.hostTracker.getAllHosts(); + return getHostsNotInSubnet(subnet, this.hostTracker.getAllHosts()); + } + + protected Set getHostsNotInSubnet(String subnet, Set allHosts) { + Set hostsInSubnet = getHostsInSubnet(subnet, allHosts); + Set otherHosts = new HashSet(allHosts); // copy constructor otherHosts.removeAll(hostsInSubnet); Set hostsNotInSubnet = new HashSet(); for (Host h : otherHosts) @@ -485,6 +489,10 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager } private Set getHostsInSubnet(String subnet) { + return getHostsInSubnet(subnet, this.hostTracker.getAllHosts()); + } + + protected Set getHostsInSubnet(String subnet, Set allHosts) { InetAddress ip; Short mask; Set hosts = new HashSet(); @@ -501,7 +509,6 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager // Match on subnetes InetAddress targetSubnet = getSubnet(ip, mask); - Set allHosts = this.hostTracker.getAllHosts(); for (HostNodeConnector host : allHosts) { InetAddress hostSubnet = getSubnet(host.getNetworkAddress(), mask); if (hostSubnet.equals(targetSubnet)) @@ -532,8 +539,10 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager @Override public void nodeFlowStatisticsUpdated(Node node, List flowStatsList) { - Set allHosts = this.hostTracker.getAllHosts(); + nodeFlowStatisticsUpdated(node, flowStatsList, this.hostTracker.getAllHosts()); + } + protected void nodeFlowStatisticsUpdated(Node node, List flowStatsList, Set allHosts) { for (FlowOnNode f : flowStatsList) { Host srcHost = getSourceHostFromFlow(f.getFlow(), allHosts); Host dstHost = getDestinationHostFromFlow(f.getFlow(), allHosts); diff --git a/analytics/implementation/src/test/java/org/opendaylight/affinity/analytics/internal/AnalyticsManagerTest.java b/analytics/implementation/src/test/java/org/opendaylight/affinity/analytics/internal/AnalyticsManagerTest.java index 182bdf4..b0e6ec6 100644 --- a/analytics/implementation/src/test/java/org/opendaylight/affinity/analytics/internal/AnalyticsManagerTest.java +++ b/analytics/implementation/src/test/java/org/opendaylight/affinity/analytics/internal/AnalyticsManagerTest.java @@ -12,7 +12,6 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -32,6 +31,8 @@ import org.opendaylight.controller.sal.match.Match; import org.opendaylight.controller.sal.match.MatchField; import org.opendaylight.controller.sal.match.MatchType; import org.opendaylight.controller.sal.reader.FlowOnNode; +import org.opendaylight.controller.sal.utils.EtherTypes; +import org.opendaylight.controller.sal.utils.IPProtocols; public class AnalyticsManagerTest extends TestCase { @@ -114,10 +115,170 @@ public class AnalyticsManagerTest extends TestCase { } @Test - public void testGetByteCountBetweenHosts() { - // TODO: This test should exist, but it involves a lot of - // integration with the statisticsManager, switchManager, and - // hostTracker, and I'm not entirely sure how to go about that. - Assert.assertTrue(true); + public void testSubnetMatching() { + + AnalyticsManager am = new AnalyticsManager(); + am.init(); + + try { + // Set up nodes + Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(100L)); + Node n2 = new Node(Node.NodeIDType.OPENFLOW, new Long(101L)); + Node n3 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L)); + Node n4 = new Node(Node.NodeIDType.OPENFLOW, new Long(111L)); + + // Set up node connectors + NodeConnector nc1 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFC), n1); + NodeConnector nc2 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFD), n2); + NodeConnector nc3 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFE), n3); + NodeConnector nc4 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFF), n4); + + // Set up host node connectors + HostNodeConnector hnc1 = new HostNodeConnector(InetAddress.getByName("128.0.0.1"), nc1); + HostNodeConnector hnc2 = new HostNodeConnector(InetAddress.getByName("128.0.0.2"), nc2); + HostNodeConnector hnc3 = new HostNodeConnector(InetAddress.getByName("129.0.0.1"), nc3); + HostNodeConnector hnc4 = new HostNodeConnector(InetAddress.getByName("129.0.0.2"), nc4); + Set allHosts = new HashSet(Arrays.asList(hnc1, hnc2, hnc3, hnc4)); + + String subnet1 = "128.0.0.0/8"; // matches 128.* + Set matchedHosts = am.getHostsInSubnet(subnet1, allHosts); + Set unmatchedHosts = am.getHostsNotInSubnet(subnet1, allHosts); + Assert.assertTrue(matchedHosts.size() == 2); + Assert.assertTrue(matchedHosts.contains(hnc1)); + Assert.assertTrue(matchedHosts.contains(hnc2)); + Assert.assertTrue(unmatchedHosts.size() == 2); + Assert.assertTrue(unmatchedHosts.contains(hnc3)); + Assert.assertTrue(unmatchedHosts.contains(hnc4)); + + String subnet2 = "128.0.0.0/7"; // matches 128.* and 129.* + matchedHosts = am.getHostsInSubnet(subnet2, allHosts); + unmatchedHosts = am.getHostsNotInSubnet(subnet2, allHosts); + Assert.assertTrue(matchedHosts.size() == 4); + Assert.assertTrue(matchedHosts.contains(hnc1)); + Assert.assertTrue(matchedHosts.contains(hnc2)); + Assert.assertTrue(matchedHosts.contains(hnc3)); + Assert.assertTrue(matchedHosts.contains(hnc4)); + Assert.assertTrue(unmatchedHosts.size() == 0); + + String subnet3 = "128.0.0.2/32"; // matches 128.0.0.2 + matchedHosts = am.getHostsInSubnet(subnet3, allHosts); + unmatchedHosts = am.getHostsNotInSubnet(subnet3, allHosts); + Assert.assertTrue(matchedHosts.size() == 1); + Assert.assertTrue(matchedHosts.contains(hnc2)); + Assert.assertTrue(unmatchedHosts.size() == 3); + Assert.assertTrue(unmatchedHosts.contains(hnc1)); + Assert.assertTrue(unmatchedHosts.contains(hnc3)); + Assert.assertTrue(unmatchedHosts.contains(hnc4)); + + String subnet4 = "128.0.0.1/31"; // matches 128.0.0.1 + matchedHosts = am.getHostsInSubnet(subnet4, allHosts); + unmatchedHosts = am.getHostsNotInSubnet(subnet4, allHosts); + Assert.assertTrue(matchedHosts.size() == 1); + Assert.assertTrue(matchedHosts.contains(hnc1)); + Assert.assertTrue(unmatchedHosts.size() == 3); + Assert.assertTrue(unmatchedHosts.contains(hnc2)); + Assert.assertTrue(unmatchedHosts.contains(hnc3)); + Assert.assertTrue(unmatchedHosts.contains(hnc4)); + + String subnet5 = "10.0.0.0/8"; // matches none + matchedHosts = am.getHostsInSubnet(subnet5, allHosts); + unmatchedHosts = am.getHostsNotInSubnet(subnet5, allHosts); + Assert.assertTrue(matchedHosts.size() == 0); + Assert.assertTrue(unmatchedHosts.size() == 4); + Assert.assertTrue(unmatchedHosts.contains(hnc1)); + Assert.assertTrue(unmatchedHosts.contains(hnc2)); + Assert.assertTrue(unmatchedHosts.contains(hnc3)); + Assert.assertTrue(unmatchedHosts.contains(hnc4)); + } catch (ConstructionException e) { + Assert.assertTrue(false); + } catch (UnknownHostException e ) { + Assert.assertTrue(false); + } finally { + am.destroy(); + } + } + + @Test + public void testGetStatsBetweenHosts() { + AnalyticsManager am = new AnalyticsManager(); + am.init(); + try { + // Set up the network + Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(100L)); + Node n2 = new Node(Node.NodeIDType.OPENFLOW, new Long(101L)); + NodeConnector nc1 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFC), n1); + NodeConnector nc2 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFD), n2); + HostNodeConnector hnc1 = new HostNodeConnector(new byte[]{0,0,0,0,0,1}, InetAddress.getByName("10.0.0.1"), nc1, (short) 1); + HostNodeConnector hnc2 = new HostNodeConnector(new byte[]{0,0,0,0,0,2}, InetAddress.getByName("10.0.0.2"), nc2, (short) 1); + Set allHosts = new HashSet(Arrays.asList(hnc1, hnc2)); + + // Two flows between the hosts; different protocols + Match match1 = new Match(); + match1.setField(new MatchField(MatchType.IN_PORT, nc1)); + match1.setField(new MatchField(MatchType.DL_DST, new byte[]{0,0,0,0,0,2})); + match1.setField(new MatchField(MatchType.DL_TYPE, EtherTypes.IPv4.shortValue())); + match1.setField(new MatchField(MatchType.NW_PROTO, IPProtocols.ICMP.byteValue())); + Flow f1 = new Flow(); + f1.setMatch(match1); + FlowOnNode fon1 = new FlowOnNode(f1); + fon1.setByteCount(200); + fon1.setDurationSeconds(1); + fon1.setDurationNanoseconds(100000000); // 1.1s + List flowStatsList = new ArrayList(); + flowStatsList.add(fon1); + + Match match2 = new Match(); + match2.setField(new MatchField(MatchType.IN_PORT, nc1)); + match2.setField(new MatchField(MatchType.DL_DST, new byte[]{0,0,0,0,0,2})); + match2.setField(new MatchField(MatchType.DL_TYPE, EtherTypes.IPv4.shortValue())); + match2.setField(new MatchField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue())); + Flow f2 = new Flow(); + f2.setMatch(match2); + FlowOnNode fon2 = new FlowOnNode(f2); + fon2.setByteCount(76); + fon2.setDurationSeconds(2); + fon2.setDurationNanoseconds(0); + flowStatsList.add(fon2); + + // Basic stats + am.nodeFlowStatisticsUpdated(n1, flowStatsList, allHosts); + Assert.assertTrue(am.getByteCount(hnc1, hnc2) == 276); + Assert.assertTrue(am.getBitRate(hnc1, hnc2) == (276 * 8.0) / 2.0); + + // Per-protocol stats + Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.ICMP.byteValue()) == 200); + Assert.assertTrue(am.getBitRate(hnc1, hnc2, IPProtocols.ICMP.byteValue()) == (200 * 8.0) / 1.1); + Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.UDP.byteValue()) == 76); + Assert.assertTrue(am.getBitRate(hnc1, hnc2, IPProtocols.UDP.byteValue()) == (76 * 8.0) / 2.0); + Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.TCP.byteValue()) == 0); + Assert.assertTrue(am.getBitRate(hnc1, hnc2, IPProtocols.TCP.byteValue()) == 0.0); + + // All stats + Map byteCounts = am.getAllByteCounts(hnc1, hnc2); + Map bitRates = am.getAllBitRates(hnc1, hnc2); + Assert.assertTrue(byteCounts.get(IPProtocols.ICMP.byteValue()) == am.getByteCount(hnc1, hnc2, IPProtocols.ICMP.byteValue())); + Assert.assertTrue(bitRates.get(IPProtocols.ICMP.byteValue()) == am.getBitRate(hnc1, hnc2, IPProtocols.ICMP.byteValue())); + Assert.assertTrue(byteCounts.get(IPProtocols.UDP.byteValue()) == am.getByteCount(hnc1, hnc2, IPProtocols.UDP.byteValue())); + Assert.assertTrue(bitRates.get(IPProtocols.UDP.byteValue()) == am.getBitRate(hnc1, hnc2, IPProtocols.UDP.byteValue())); + Assert.assertTrue(byteCounts.get(IPProtocols.TCP.byteValue()) == null); + Assert.assertTrue(bitRates.get(IPProtocols.TCP.byteValue()) == null); + + // Correct flow over-writing + FlowOnNode fon3 = new FlowOnNode(f1); + fon3.setByteCount(300); + fon3.setDurationSeconds(3); + fon3.setDurationNanoseconds(100000000); // 3.1s + flowStatsList.add(fon3); + am.nodeFlowStatisticsUpdated(n2, flowStatsList, allHosts); + Assert.assertTrue(am.getByteCount(hnc1, hnc2) == 376); + Assert.assertTrue(am.getBitRate(hnc1, hnc2) == (376 * 8.0) / 3.1); + Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.ICMP.byteValue()) == 300); + } catch (ConstructionException e) { + Assert.assertTrue(false); + } catch (UnknownHostException e) { + Assert.assertTrue(false); + } finally { + am.destroy(); + } } }