HostTracker Bundle Separation
[controller.git] / opendaylight / hosttracker / integrationtest / src / test / java / org / opendaylight / controller / hosttracker / internal / HostTrackerIT.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  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.controller.hosttracker.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.HashSet;\r
15 import java.util.Iterator;\r
16 import java.util.List;\r
17 import java.util.Map;\r
18 import java.util.Set;\r
19 import java.util.Map.Entry;\r
20 \r
21 import org.slf4j.Logger;\r
22 import org.slf4j.LoggerFactory;\r
23 import org.osgi.framework.ServiceReference;\r
24 import org.osgi.framework.Bundle;\r
25 import javax.inject.Inject;\r
26 \r
27 import org.eclipse.osgi.framework.console.CommandProvider;\r
28 import org.junit.Assert;\r
29 import org.junit.Test;\r
30 import org.junit.Before;\r
31 import org.junit.After;\r
32 import org.junit.runner.RunWith;\r
33 import org.opendaylight.controller.sal.core.Node;\r
34 import org.opendaylight.controller.sal.core.NodeConnector;\r
35 import org.opendaylight.controller.sal.core.UpdateType;\r
36 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;\r
37 import org.opendaylight.controller.sal.utils.NodeCreator;\r
38 import org.opendaylight.controller.sal.utils.Status;\r
39 //import org.opendaylight.controller.hosttracker.*;\r
40 import org.opendaylight.controller.hosttracker.IfIptoHost;\r
41 import org.opendaylight.controller.hosttracker.IfHostListener;\r
42 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;\r
43 import org.opendaylight.controller.switchmanager.IInventoryListener;\r
44 import org.opendaylight.controller.switchmanager.ISwitchManager;\r
45 import org.opendaylight.controller.switchmanager.ISwitchManagerAware;\r
46 import org.opendaylight.controller.topologymanager.ITopologyManagerAware;\r
47 \r
48 import org.ops4j.pax.exam.junit.PaxExam;\r
49 import org.ops4j.pax.exam.util.Filter;\r
50 import org.osgi.framework.BundleContext;\r
51 import static org.junit.Assert.*;\r
52 import org.ops4j.pax.exam.junit.Configuration;\r
53 import static org.ops4j.pax.exam.CoreOptions.*;\r
54 \r
55 import org.ops4j.pax.exam.Option;\r
56 import org.ops4j.pax.exam.util.PathUtils;\r
57 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;\r
58 import org.ops4j.pax.exam.spi.reactors.PerClass;\r
59 \r
60 @RunWith(PaxExam.class)\r
61 public class HostTrackerIT {\r
62     private Logger log = LoggerFactory.getLogger(HostTrackerIT.class);\r
63     // get the OSGI bundle context\r
64     @Inject\r
65     private BundleContext bc;\r
66 \r
67     private IfIptoHost hosttracker = null;\r
68     private ISwitchManagerAware switchManagerAware = null;\r
69     private IInventoryListener invtoryListener = null;\r
70     private IfHostListener hostListener = null;\r
71     private ITopologyManagerAware topologyManagerAware = null;\r
72 \r
73     // Configure the OSGi container\r
74     @Configuration\r
75     public Option[] config() {\r
76         return options(\r
77                 //\r
78                 systemProperty("logback.configurationFile").value(\r
79                         "file:" + PathUtils.getBaseDir()\r
80                                 + "/src/test/resources/logback.xml"),\r
81                 // To start OSGi console for inspection remotely\r
82                 systemProperty("osgi.console").value("2401"),\r
83                 // Set the systemPackages (used by clustering)\r
84                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),\r
85                 // List framework bundles\r
86                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",\r
87                         "1.0.0.v20120522-1841"),\r
88                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",\r
89                         "1.0.400.v20120522-2049"),\r
90                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",\r
91                         "3.3.100.v20120522-1822"),\r
92                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",\r
93                         "1.4.0.v20120522-1841"),\r
94                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",\r
95                         "0.8.0.v201108120515"),\r
96                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",\r
97                         "0.8.0.v201108120515"),\r
98                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",\r
99                         "0.8.0.v201110170705"),\r
100                 // List logger bundles\r
101                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),\r
102                 mavenBundle("org.slf4j", "log4j-over-slf4j")\r
103                         .versionAsInProject(),\r
104                 mavenBundle("ch.qos.logback", "logback-core")\r
105                         .versionAsInProject(),\r
106                 mavenBundle("ch.qos.logback", "logback-classic")\r
107                         .versionAsInProject(),\r
108 \r
109                 // List all the bundles on which the test case depends\r
110                 mavenBundle("org.opendaylight.controller", "sal")\r
111                         .versionAsInProject(),\r
112                 mavenBundle("org.opendaylight.controller", "sal.implementation")\r
113                         .versionAsInProject(),\r
114 \r
115                 // needed by statisticsmanager\r
116                 mavenBundle("org.opendaylight.controller", "containermanager")\r
117                         .versionAsInProject(),\r
118                 mavenBundle("org.opendaylight.controller",\r
119                         "containermanager.implementation").versionAsInProject(),\r
120 \r
121                 mavenBundle("org.opendaylight.controller",\r
122                         "clustering.services").versionAsInProject(),\r
123                 mavenBundle("org.opendaylight.controller", "clustering.stub")\r
124                         .versionAsInProject(),\r
125 \r
126                 // needed by forwardingrulesmanager\r
127                 mavenBundle("org.opendaylight.controller", "switchmanager")\r
128                         .versionAsInProject(),\r
129                 mavenBundle("org.opendaylight.controller",\r
130                         "switchmanager.implementation").versionAsInProject(),\r
131                 mavenBundle("org.opendaylight.controller", "configuration")\r
132                         .versionAsInProject(),\r
133                 mavenBundle("org.opendaylight.controller",\r
134                         "configuration.implementation").versionAsInProject(),\r
135                 mavenBundle("org.opendaylight.controller", "hosttracker")\r
136                         .versionAsInProject(),\r
137                 mavenBundle("org.opendaylight.controller",\r
138                         "hosttracker.implementation").versionAsInProject(),\r
139 \r
140                 // needed by hosttracker\r
141                 mavenBundle("org.opendaylight.controller", "topologymanager")\r
142                         .versionAsInProject(),\r
143                 mavenBundle("org.opendaylight.controller", "arphandler")\r
144                         .versionAsInProject(),\r
145 \r
146                 mavenBundle("org.jboss.spec.javax.transaction",\r
147                         "jboss-transaction-api_1.1_spec").versionAsInProject(),\r
148                 mavenBundle("org.apache.commons", "commons-lang3")\r
149                         .versionAsInProject(),\r
150                 mavenBundle("org.apache.felix",\r
151                         "org.apache.felix.dependencymanager")\r
152                         .versionAsInProject(), junitBundles());\r
153     }\r
154 \r
155     private String stateToString(int state) {\r
156         switch (state) {\r
157         case Bundle.ACTIVE:\r
158             return "ACTIVE";\r
159         case Bundle.INSTALLED:\r
160             return "INSTALLED";\r
161         case Bundle.RESOLVED:\r
162             return "RESOLVED";\r
163         case Bundle.UNINSTALLED:\r
164             return "UNINSTALLED";\r
165         default:\r
166             return "Not CONVERTED";\r
167         }\r
168     }\r
169 \r
170     @Before\r
171     public void areWeReady() {\r
172         assertNotNull(bc);\r
173         boolean debugit = false;\r
174         Bundle b[] = bc.getBundles();\r
175         for (int i = 0; i < b.length; i++) {\r
176             int state = b[i].getState();\r
177             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {\r
178                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"\r
179                         + stateToString(state));\r
180                 debugit = true;\r
181             }\r
182         }\r
183         if (debugit) {\r
184             log.debug("Do some debugging because some bundle is "\r
185                     + "unresolved");\r
186         }\r
187 \r
188         // Assert if true, if false we are good to go!\r
189         assertFalse(debugit);\r
190 \r
191         // Now lets create a hosttracker for testing purpose\r
192         ServiceReference s = bc.getServiceReference(IfIptoHost.class.getName());\r
193         if (s != null) {\r
194             this.hosttracker = (IfIptoHost) bc.getService(s);\r
195             this.switchManagerAware = (ISwitchManagerAware) this.hosttracker;\r
196             this.invtoryListener = (IInventoryListener) this.hosttracker;\r
197             this.hostListener = (IfHostListener) this.hosttracker;\r
198             this.topologyManagerAware = (ITopologyManagerAware) this.hosttracker;\r
199         }\r
200 \r
201         // If StatisticsManager is null, cannot run tests.\r
202         assertNotNull(this.hosttracker);\r
203     }\r
204 \r
205     @Test\r
206     public void testStaticHost() throws UnknownHostException {\r
207         String ip;\r
208 \r
209         assertNotNull(this.hosttracker);\r
210 \r
211         // create one node and two node connectors\r
212         Node node1 = NodeCreator.createOFNode(1L);\r
213         NodeConnector nc1_1 = NodeConnectorCreator.createOFNodeConnector(\r
214                 (short) 1, node1);\r
215         NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector(\r
216                 (short) 2, node1);\r
217 \r
218         // test addStaticHost(), store into inactive host DB\r
219         Status st = this.hosttracker.addStaticHost("192.168.0.8",\r
220                 "11:22:33:44:55:66", nc1_1, "0");\r
221         Assert.assertTrue(st.isSuccess());\r
222         st = this.hosttracker.addStaticHost("192.168.0.13",\r
223                 "11:22:33:44:55:77", nc1_2, "0");\r
224         Assert.assertTrue(st.isSuccess());\r
225 \r
226         // check inactive DB\r
227         Iterator<HostNodeConnector> hnci = this.hosttracker\r
228                 .getInactiveStaticHosts().iterator();\r
229         while (hnci.hasNext()) {\r
230             ip = hnci.next().getNetworkAddressAsString();\r
231             Assert.assertTrue(ip.equals("192.168.0.8")\r
232                     || ip.equals("192.168.0.13"));\r
233         }\r
234 \r
235         // check active host DB\r
236         hnci = this.hosttracker.getActiveStaticHosts().iterator();\r
237         Assert.assertFalse(hnci.hasNext());\r
238 \r
239         // test removeStaticHost()\r
240         st = this.hosttracker.removeStaticHost("192.168.0.8");\r
241         Assert.assertTrue(st.isSuccess());\r
242 \r
243         hnci = this.hosttracker.getInactiveStaticHosts().iterator();\r
244         while (hnci.hasNext()) {\r
245             ip = hnci.next().getNetworkAddressAsString();\r
246             Assert.assertTrue(ip.equals("192.168.0.13"));\r
247         }\r
248     }\r
249 \r
250     @Test\r
251     public void testNotifyNodeConnector() throws UnknownHostException {\r
252         String ip;\r
253 \r
254         assertNotNull(this.invtoryListener);\r
255 \r
256         // create one node and two node connectors\r
257         Node node1 = NodeCreator.createOFNode(1L);\r
258         NodeConnector nc1_1 = NodeConnectorCreator.createOFNodeConnector(\r
259                 (short) 1, node1);\r
260         NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector(\r
261                 (short) 2, node1);\r
262 \r
263         // test addStaticHost(), put into inactive host DB if not verifiable\r
264         Status st = this.hosttracker.addStaticHost("192.168.0.8",\r
265                 "11:22:33:44:55:66", nc1_1, "0");\r
266         st = this.hosttracker.addStaticHost("192.168.0.13",\r
267                 "11:22:33:44:55:77", nc1_2, "0");\r
268 \r
269         this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);\r
270 \r
271         // check all host list\r
272         Iterator<HostNodeConnector> hnci = this.hosttracker.getAllHosts()\r
273                 .iterator();\r
274         while (hnci.hasNext()) {\r
275             ip = hnci.next().getNetworkAddressAsString();\r
276             Assert.assertTrue(ip.equals("192.168.0.8"));\r
277         }\r
278 \r
279         // check active host DB\r
280         hnci = this.hosttracker.getActiveStaticHosts().iterator();\r
281         while (hnci.hasNext()) {\r
282             ip = hnci.next().getNetworkAddressAsString();\r
283             Assert.assertTrue(ip.equals("192.168.0.8"));\r
284         }\r
285 \r
286         // check inactive host DB\r
287         hnci = this.hosttracker.getInactiveStaticHosts().iterator();\r
288         while (hnci.hasNext()) {\r
289             ip = hnci.next().getNetworkAddressAsString();\r
290             Assert.assertTrue(ip.equals("192.168.0.13"));\r
291         }\r
292     }\r
293 \r
294     @Test\r
295     public void testHostFind() throws UnknownHostException {\r
296 \r
297         assertNotNull(this.invtoryListener);\r
298 \r
299         // create one node and two node connectors\r
300         Node node1 = NodeCreator.createOFNode(1L);\r
301         NodeConnector nc1_1 = NodeConnectorCreator.createOFNodeConnector(\r
302                 (short) 1, node1);\r
303         NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector(\r
304                 (short) 2, node1);\r
305 \r
306         // test addStaticHost(), put into inactive host DB if not verifiable\r
307         Status st = this.hosttracker.addStaticHost("192.168.0.8",\r
308                 "11:22:33:44:55:66", nc1_1, "0");\r
309         st = this.hosttracker.addStaticHost("192.168.0.13",\r
310                 "11:22:33:44:55:77", nc1_2, "0");\r
311 \r
312         HostNodeConnector hnc_1 = this.hosttracker.hostFind(InetAddress\r
313                 .getByName("192.168.0.8"));\r
314         assertNull(hnc_1);\r
315 \r
316         this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);\r
317 \r
318         hnc_1 = this.hosttracker.hostFind(InetAddress.getByName("192.168.0.8"));\r
319         assertNotNull(hnc_1);\r
320 \r
321     }\r
322 \r
323 }\r