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