import java.net.UnknownHostException;\r
import java.util.ArrayList;\r
import java.util.Arrays;\r
-import java.util.HashMap;\r
import java.util.HashSet;\r
import java.util.List;\r
import java.util.Map;\r
import org.opendaylight.controller.sal.match.MatchField;\r
import org.opendaylight.controller.sal.match.MatchType;\r
import org.opendaylight.controller.sal.reader.FlowOnNode;\r
+import org.opendaylight.controller.sal.utils.EtherTypes;\r
+import org.opendaylight.controller.sal.utils.IPProtocols;\r
\r
public class AnalyticsManagerTest extends TestCase {\r
\r
}\r
\r
@Test\r
- public void testGetByteCountBetweenHosts() {\r
- // TODO: This test should exist, but it involves a lot of\r
- // integration with the statisticsManager, switchManager, and\r
- // hostTracker, and I'm not entirely sure how to go about that.\r
- Assert.assertTrue(true);\r
+ public void testSubnetMatching() {\r
+\r
+ AnalyticsManager am = new AnalyticsManager();\r
+ am.init();\r
+\r
+ try {\r
+ // Set up nodes\r
+ Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(100L));\r
+ Node n2 = new Node(Node.NodeIDType.OPENFLOW, new Long(101L));\r
+ Node n3 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));\r
+ Node n4 = new Node(Node.NodeIDType.OPENFLOW, new Long(111L));\r
+\r
+ // Set up node connectors\r
+ NodeConnector nc1 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFC), n1);\r
+ NodeConnector nc2 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFD), n2);\r
+ NodeConnector nc3 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFE), n3);\r
+ NodeConnector nc4 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFF), n4);\r
+\r
+ // Set up host node connectors\r
+ HostNodeConnector hnc1 = new HostNodeConnector(InetAddress.getByName("128.0.0.1"), nc1);\r
+ HostNodeConnector hnc2 = new HostNodeConnector(InetAddress.getByName("128.0.0.2"), nc2);\r
+ HostNodeConnector hnc3 = new HostNodeConnector(InetAddress.getByName("129.0.0.1"), nc3);\r
+ HostNodeConnector hnc4 = new HostNodeConnector(InetAddress.getByName("129.0.0.2"), nc4);\r
+ Set<HostNodeConnector> allHosts = new HashSet<HostNodeConnector>(Arrays.asList(hnc1, hnc2, hnc3, hnc4));\r
+\r
+ String subnet1 = "128.0.0.0/8"; // matches 128.*\r
+ Set<Host> matchedHosts = am.getHostsInSubnet(subnet1, allHosts);\r
+ Set<Host> unmatchedHosts = am.getHostsNotInSubnet(subnet1, allHosts);\r
+ Assert.assertTrue(matchedHosts.size() == 2);\r
+ Assert.assertTrue(matchedHosts.contains(hnc1));\r
+ Assert.assertTrue(matchedHosts.contains(hnc2));\r
+ Assert.assertTrue(unmatchedHosts.size() == 2);\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc3));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc4));\r
+\r
+ String subnet2 = "128.0.0.0/7"; // matches 128.* and 129.*\r
+ matchedHosts = am.getHostsInSubnet(subnet2, allHosts);\r
+ unmatchedHosts = am.getHostsNotInSubnet(subnet2, allHosts);\r
+ Assert.assertTrue(matchedHosts.size() == 4);\r
+ Assert.assertTrue(matchedHosts.contains(hnc1));\r
+ Assert.assertTrue(matchedHosts.contains(hnc2));\r
+ Assert.assertTrue(matchedHosts.contains(hnc3));\r
+ Assert.assertTrue(matchedHosts.contains(hnc4));\r
+ Assert.assertTrue(unmatchedHosts.size() == 0);\r
+ \r
+ String subnet3 = "128.0.0.2/32"; // matches 128.0.0.2\r
+ matchedHosts = am.getHostsInSubnet(subnet3, allHosts);\r
+ unmatchedHosts = am.getHostsNotInSubnet(subnet3, allHosts);\r
+ Assert.assertTrue(matchedHosts.size() == 1);\r
+ Assert.assertTrue(matchedHosts.contains(hnc2));\r
+ Assert.assertTrue(unmatchedHosts.size() == 3);\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc1));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc3));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc4)); \r
+ \r
+ String subnet4 = "128.0.0.1/31"; // matches 128.0.0.1\r
+ matchedHosts = am.getHostsInSubnet(subnet4, allHosts);\r
+ unmatchedHosts = am.getHostsNotInSubnet(subnet4, allHosts);\r
+ Assert.assertTrue(matchedHosts.size() == 1);\r
+ Assert.assertTrue(matchedHosts.contains(hnc1));\r
+ Assert.assertTrue(unmatchedHosts.size() == 3);\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc2));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc3));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc4));\r
+\r
+ String subnet5 = "10.0.0.0/8"; // matches none\r
+ matchedHosts = am.getHostsInSubnet(subnet5, allHosts);\r
+ unmatchedHosts = am.getHostsNotInSubnet(subnet5, allHosts);\r
+ Assert.assertTrue(matchedHosts.size() == 0);\r
+ Assert.assertTrue(unmatchedHosts.size() == 4);\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc1));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc2));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc3));\r
+ Assert.assertTrue(unmatchedHosts.contains(hnc4));\r
+ } catch (ConstructionException e) {\r
+ Assert.assertTrue(false);\r
+ } catch (UnknownHostException e ) {\r
+ Assert.assertTrue(false);\r
+ } finally {\r
+ am.destroy();\r
+ }\r
+ }\r
+\r
+ @Test\r
+ public void testGetStatsBetweenHosts() {\r
+ AnalyticsManager am = new AnalyticsManager();\r
+ am.init();\r
+ try {\r
+ // Set up the network\r
+ Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(100L));\r
+ Node n2 = new Node(Node.NodeIDType.OPENFLOW, new Long(101L));\r
+ NodeConnector nc1 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFC), n1);\r
+ NodeConnector nc2 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFD), n2);\r
+ HostNodeConnector hnc1 = new HostNodeConnector(new byte[]{0,0,0,0,0,1}, InetAddress.getByName("10.0.0.1"), nc1, (short) 1);\r
+ HostNodeConnector hnc2 = new HostNodeConnector(new byte[]{0,0,0,0,0,2}, InetAddress.getByName("10.0.0.2"), nc2, (short) 1);\r
+ Set<HostNodeConnector> allHosts = new HashSet<HostNodeConnector>(Arrays.asList(hnc1, hnc2));\r
+\r
+ // Two flows between the hosts; different protocols\r
+ Match match1 = new Match();\r
+ match1.setField(new MatchField(MatchType.IN_PORT, nc1));\r
+ match1.setField(new MatchField(MatchType.DL_DST, new byte[]{0,0,0,0,0,2}));\r
+ match1.setField(new MatchField(MatchType.DL_TYPE, EtherTypes.IPv4.shortValue()));\r
+ match1.setField(new MatchField(MatchType.NW_PROTO, IPProtocols.ICMP.byteValue()));\r
+ Flow f1 = new Flow();\r
+ f1.setMatch(match1);\r
+ FlowOnNode fon1 = new FlowOnNode(f1);\r
+ fon1.setByteCount(200);\r
+ fon1.setDurationSeconds(1);\r
+ fon1.setDurationNanoseconds(100000000); // 1.1s\r
+ List<FlowOnNode> flowStatsList = new ArrayList<FlowOnNode>();\r
+ flowStatsList.add(fon1);\r
+\r
+ Match match2 = new Match();\r
+ match2.setField(new MatchField(MatchType.IN_PORT, nc1));\r
+ match2.setField(new MatchField(MatchType.DL_DST, new byte[]{0,0,0,0,0,2}));\r
+ match2.setField(new MatchField(MatchType.DL_TYPE, EtherTypes.IPv4.shortValue()));\r
+ match2.setField(new MatchField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue()));\r
+ Flow f2 = new Flow();\r
+ f2.setMatch(match2);\r
+ FlowOnNode fon2 = new FlowOnNode(f2);\r
+ fon2.setByteCount(76);\r
+ fon2.setDurationSeconds(2);\r
+ fon2.setDurationNanoseconds(0);\r
+ flowStatsList.add(fon2);\r
+\r
+ // Basic stats\r
+ am.nodeFlowStatisticsUpdated(n1, flowStatsList, allHosts);\r
+ Assert.assertTrue(am.getByteCount(hnc1, hnc2) == 276);\r
+ Assert.assertTrue(am.getBitRate(hnc1, hnc2) == (276 * 8.0) / 2.0);\r
+\r
+ // Per-protocol stats\r
+ Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.ICMP.byteValue()) == 200);\r
+ Assert.assertTrue(am.getBitRate(hnc1, hnc2, IPProtocols.ICMP.byteValue()) == (200 * 8.0) / 1.1);\r
+ Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.UDP.byteValue()) == 76);\r
+ Assert.assertTrue(am.getBitRate(hnc1, hnc2, IPProtocols.UDP.byteValue()) == (76 * 8.0) / 2.0);\r
+ Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.TCP.byteValue()) == 0);\r
+ Assert.assertTrue(am.getBitRate(hnc1, hnc2, IPProtocols.TCP.byteValue()) == 0.0);\r
+\r
+ // All stats\r
+ Map<Byte, Long> byteCounts = am.getAllByteCounts(hnc1, hnc2);\r
+ Map<Byte, Double> bitRates = am.getAllBitRates(hnc1, hnc2);\r
+ Assert.assertTrue(byteCounts.get(IPProtocols.ICMP.byteValue()) == am.getByteCount(hnc1, hnc2, IPProtocols.ICMP.byteValue()));\r
+ Assert.assertTrue(bitRates.get(IPProtocols.ICMP.byteValue()) == am.getBitRate(hnc1, hnc2, IPProtocols.ICMP.byteValue()));\r
+ Assert.assertTrue(byteCounts.get(IPProtocols.UDP.byteValue()) == am.getByteCount(hnc1, hnc2, IPProtocols.UDP.byteValue()));\r
+ Assert.assertTrue(bitRates.get(IPProtocols.UDP.byteValue()) == am.getBitRate(hnc1, hnc2, IPProtocols.UDP.byteValue()));\r
+ Assert.assertTrue(byteCounts.get(IPProtocols.TCP.byteValue()) == null);\r
+ Assert.assertTrue(bitRates.get(IPProtocols.TCP.byteValue()) == null);\r
+\r
+ // Correct flow over-writing\r
+ FlowOnNode fon3 = new FlowOnNode(f1);\r
+ fon3.setByteCount(300);\r
+ fon3.setDurationSeconds(3);\r
+ fon3.setDurationNanoseconds(100000000); // 3.1s\r
+ flowStatsList.add(fon3);\r
+ am.nodeFlowStatisticsUpdated(n2, flowStatsList, allHosts);\r
+ Assert.assertTrue(am.getByteCount(hnc1, hnc2) == 376);\r
+ Assert.assertTrue(am.getBitRate(hnc1, hnc2) == (376 * 8.0) / 3.1);\r
+ Assert.assertTrue(am.getByteCount(hnc1, hnc2, IPProtocols.ICMP.byteValue()) == 300);\r
+ } catch (ConstructionException e) {\r
+ Assert.assertTrue(false);\r
+ } catch (UnknownHostException e) {\r
+ Assert.assertTrue(false);\r
+ } finally {\r
+ am.destroy();\r
+ }\r
}\r
}\r