57e2714ff1c23bf3aded4c40d4b98ae96ed0135c
[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").versionAsInProject(),
73                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
74                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
75                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
76                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
77                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
78                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
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", "forwardingrulesmanager").versionAsInProject(),
103                 mavenBundle("org.opendaylight.controller", "statisticsmanager").versionAsInProject(),
104                 mavenBundle("org.opendaylight.controller", "switchmanager.implementation").versionAsInProject(),
105                 mavenBundle("org.opendaylight.controller", "configuration").versionAsInProject(),
106                 mavenBundle("org.opendaylight.controller", "configuration.implementation").versionAsInProject(),
107                 mavenBundle("org.opendaylight.controller", "hosttracker").versionAsInProject(),
108                 mavenBundle("org.opendaylight.controller", "hosttracker.implementation").versionAsInProject(),
109
110                 // needed by hosttracker
111                 mavenBundle("org.opendaylight.controller", "topologymanager").versionAsInProject(),
112                 mavenBundle("org.opendaylight.controller", "arphandler").versionAsInProject(),
113
114                 mavenBundle("org.jboss.spec.javax.transaction", "jboss-transaction-api_1.1_spec").versionAsInProject(),
115                 mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),
116                 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager").versionAsInProject(),
117                 junitBundles());
118     }
119
120     private String stateToString(int state) {
121         switch (state) {
122         case Bundle.ACTIVE:
123             return "ACTIVE";
124         case Bundle.INSTALLED:
125             return "INSTALLED";
126         case Bundle.RESOLVED:
127             return "RESOLVED";
128         case Bundle.UNINSTALLED:
129             return "UNINSTALLED";
130         default:
131             return "Not CONVERTED";
132         }
133     }
134
135     @Before
136     public void areWeReady() {
137         assertNotNull(bc);
138         boolean debugit = false;
139         Bundle b[] = bc.getBundles();
140         for (int i = 0; i < b.length; i++) {
141             int state = b[i].getState();
142             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
143                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:" + stateToString(state));
144                 debugit = true;
145             }
146         }
147         if (debugit) {
148             log.debug("Do some debugging because some bundle is " + "unresolved");
149         }
150
151         // Assert if true, if false we are good to go!
152         assertFalse(debugit);
153
154         // Now lets create a hosttracker for testing purpose
155         ServiceReference s = bc.getServiceReference(IfIptoHost.class.getName());
156         if (s != null) {
157             this.hosttracker = (IfIptoHost) bc.getService(s);
158             this.invtoryListener = (IInventoryListener) this.hosttracker;
159         }
160
161         // If StatisticsManager is null, cannot run tests.
162         assertNotNull(this.hosttracker);
163     }
164
165     @Test
166     public void testStaticHost() throws UnknownHostException {
167         String ip;
168
169         assertNotNull(this.hosttracker);
170
171         // create one node and two node connectors
172         Node node1 = NodeCreator.createOFNode(1L);
173         NodeConnector nc1_1 = NodeConnectorCreator.createOFNodeConnector((short) 1, node1);
174         NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
175
176         // test addStaticHost(), store into inactive host DB
177         Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, null);
178         Assert.assertTrue(st.isSuccess());
179         st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "");
180         Assert.assertTrue(st.isSuccess());
181
182         // check inactive DB
183         Iterator<HostNodeConnector> hnci = this.hosttracker.getInactiveStaticHosts().iterator();
184         while (hnci.hasNext()) {
185             ip = hnci.next().getNetworkAddressAsString();
186             Assert.assertTrue(ip.equals("192.168.0.8") || ip.equals("192.168.0.13"));
187         }
188
189         // check active host DB
190         hnci = this.hosttracker.getActiveStaticHosts().iterator();
191         Assert.assertFalse(hnci.hasNext());
192
193         // test removeStaticHost()
194         st = this.hosttracker.removeStaticHost("192.168.0.8");
195         Assert.assertTrue(st.isSuccess());
196
197         hnci = this.hosttracker.getInactiveStaticHosts().iterator();
198         while (hnci.hasNext()) {
199             ip = hnci.next().getNetworkAddressAsString();
200             Assert.assertTrue(ip.equals("192.168.0.13"));
201         }
202     }
203
204     @Test
205     public void testNotifyNodeConnector() throws UnknownHostException {
206         String ip;
207
208         assertNotNull(this.invtoryListener);
209
210         // create one node and two node connectors
211         Node node1 = NodeCreator.createOFNode(1L);
212         NodeConnector nc1_1 = NodeConnectorCreator.createOFNodeConnector((short) 1, node1);
213         NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
214
215         // test addStaticHost(), put into inactive host DB if not verifiable
216         Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, null);
217         Assert.assertTrue(st.isSuccess());
218         st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "0");
219         Assert.assertFalse(st.isSuccess());
220
221
222         this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
223
224         // check all host list
225         Iterator<HostNodeConnector> hnci = this.hosttracker.getAllHosts().iterator();
226         while (hnci.hasNext()) {
227             ip = hnci.next().getNetworkAddressAsString();
228             Assert.assertTrue(ip.equals("192.168.0.8"));
229         }
230
231         // check active host DB
232         hnci = this.hosttracker.getActiveStaticHosts().iterator();
233         while (hnci.hasNext()) {
234             ip = hnci.next().getNetworkAddressAsString();
235             Assert.assertTrue(ip.equals("192.168.0.8"));
236         }
237
238         // check inactive host DB
239         hnci = this.hosttracker.getInactiveStaticHosts().iterator();
240         while (hnci.hasNext()) {
241             ip = hnci.next().getNetworkAddressAsString();
242             Assert.assertTrue(ip.equals("192.168.0.13"));
243         }
244     }
245
246     @Test
247     public void testHostFind() throws UnknownHostException {
248
249         assertNotNull(this.invtoryListener);
250
251         // create one node and two node connectors
252         Node node1 = NodeCreator.createOFNode(1L);
253         NodeConnector nc1_1 = NodeConnectorCreator.createOFNodeConnector((short) 1, node1);
254         NodeConnector nc1_2 = NodeConnectorCreator.createOFNodeConnector((short) 2, node1);
255
256         // test addStaticHost(), put into inactive host DB if not verifiable
257         Status st = this.hosttracker.addStaticHost("192.168.0.8", "11:22:33:44:55:66", nc1_1, null);
258         st = this.hosttracker.addStaticHost("192.168.0.13", "11:22:33:44:55:77", nc1_2, "");
259
260         HostNodeConnector hnc_1 = this.hosttracker.hostFind(InetAddress.getByName("192.168.0.8"));
261         assertNull(hnc_1);
262
263         this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
264
265         hnc_1 = this.hosttracker.hostFind(InetAddress.getByName("192.168.0.8"));
266         assertNotNull(hnc_1);
267
268     }
269
270 }