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