182bdf47963993c285978643d6c7b36b33c400bd
[affinity.git] / analytics / implementation / src / test / java / org / opendaylight / affinity / analytics / internal / AnalyticsManagerTest.java
1 /*\r
2  * Copyright (c) 2013 Plexxi, Inc.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.affinity.analytics.internal;\r
10 \r
11 import java.net.InetAddress;\r
12 import java.net.UnknownHostException;\r
13 import java.util.ArrayList;\r
14 import java.util.Arrays;\r
15 import java.util.HashMap;\r
16 import java.util.HashSet;\r
17 import java.util.List;\r
18 import java.util.Map;\r
19 import java.util.Set;\r
20 \r
21 import junit.framework.TestCase;\r
22 import org.junit.Assert;\r
23 import org.junit.Test;\r
24 \r
25 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;\r
26 import org.opendaylight.controller.sal.core.ConstructionException;\r
27 import org.opendaylight.controller.sal.core.Host;\r
28 import org.opendaylight.controller.sal.core.Node;\r
29 import org.opendaylight.controller.sal.core.NodeConnector;\r
30 import org.opendaylight.controller.sal.flowprogrammer.Flow;\r
31 import org.opendaylight.controller.sal.match.Match;\r
32 import org.opendaylight.controller.sal.match.MatchField;\r
33 import org.opendaylight.controller.sal.match.MatchType;\r
34 import org.opendaylight.controller.sal.reader.FlowOnNode;\r
35 \r
36 public class AnalyticsManagerTest extends TestCase {\r
37 \r
38     @Test\r
39     public void testAnalyticsManagerCreation() {\r
40         AnalyticsManager am = new AnalyticsManager();\r
41         Assert.assertTrue(am != null);\r
42     }\r
43 \r
44     @Test\r
45     public void testGetHostsFromFlows() {\r
46 \r
47         AnalyticsManager am = new AnalyticsManager();\r
48         am.init();\r
49 \r
50         try {\r
51             // Set up nodes\r
52             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(100L));\r
53             Node n2 = new Node(Node.NodeIDType.OPENFLOW, new Long(101L));\r
54             Node n3 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));\r
55             Node n4 = new Node(Node.NodeIDType.OPENFLOW, new Long(111L));\r
56 \r
57             // Set up node connectors\r
58             NodeConnector nc1 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFC), n1);\r
59             NodeConnector nc2 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFD), n2);\r
60             NodeConnector nc3 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFE), n3);\r
61             NodeConnector nc4 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, new Short((short) 0xCAFF), n4);\r
62 \r
63             // Set up host node connectors\r
64             HostNodeConnector hnc1 = new HostNodeConnector(new byte[]{0,0,0,0,0,1}, InetAddress.getByName("10.0.0.1"), nc1, (short) 1);\r
65             HostNodeConnector hnc2 = new HostNodeConnector(new byte[]{0,0,0,0,0,2}, InetAddress.getByName("10.0.0.2"), nc2, (short) 1);\r
66             HostNodeConnector hnc3 = new HostNodeConnector(new byte[]{0,0,0,0,0,3}, InetAddress.getByName("10.0.0.3"), nc3, (short) 1);\r
67             Set<HostNodeConnector> hosts = new HashSet<HostNodeConnector>(Arrays.asList(hnc1, hnc2, hnc3));\r
68 \r
69             // Set up a flow from nc1 to nc2\r
70             Match match = new Match();\r
71             match.setField(new MatchField(MatchType.IN_PORT, nc1));\r
72             match.setField(new MatchField(MatchType.DL_DST, new byte[]{0,0,0,0,0,2}));\r
73             Flow f = new Flow();\r
74             f.setMatch(match);\r
75 \r
76             Host dstHost = am.getDestinationHostFromFlow(f, hosts);\r
77             Host srcHost = am.getSourceHostFromFlow(f, hosts);\r
78 \r
79             Assert.assertTrue(dstHost.equals(hnc2));\r
80             Assert.assertTrue(srcHost.equals(hnc1));\r
81 \r
82             // Set up a flow from nc3 to nc1\r
83             match = new Match();\r
84             match.setField(new MatchField(MatchType.IN_PORT, nc3));\r
85             match.setField(new MatchField(MatchType.DL_DST, new byte[]{0,0,0,0,0,1}));\r
86             f = new Flow();\r
87             f.setMatch(match);\r
88 \r
89             dstHost = am.getDestinationHostFromFlow(f, hosts);\r
90             srcHost = am.getSourceHostFromFlow(f, hosts);\r
91 \r
92             Assert.assertTrue(dstHost.equals(hnc1));\r
93             Assert.assertTrue(srcHost.equals(hnc3));\r
94 \r
95             // Set up a flow from a switch to a non-host..\r
96             match = new Match();\r
97             match.setField(new MatchField(MatchType.IN_PORT, nc4));\r
98             match.setField(new MatchField(MatchType.DL_DST, new byte[]{0,0,0,0,0,2}));\r
99             f = new Flow();\r
100             f.setMatch(match);\r
101 \r
102             dstHost = am.getDestinationHostFromFlow(f, hosts);\r
103             srcHost = am.getSourceHostFromFlow(f, hosts);\r
104 \r
105             Assert.assertTrue(dstHost.equals(hnc2));\r
106             Assert.assertTrue(srcHost == null);\r
107         } catch (ConstructionException e) {\r
108             Assert.assertTrue(false);\r
109         } catch (UnknownHostException e ) {\r
110             Assert.assertTrue(false);\r
111         } finally {\r
112             am.destroy();\r
113         }\r
114     }\r
115 \r
116     @Test\r
117     public void testGetByteCountBetweenHosts() {\r
118         // TODO: This test should exist, but it involves a lot of\r
119         // integration with the statisticsManager, switchManager, and\r
120         // hostTracker, and I'm not entirely sure how to go about that.\r
121         Assert.assertTrue(true);\r
122     }\r
123 }\r