2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.hosttracker.internal;
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;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.util.Iterator;
24 import javax.inject.Inject;
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;
51 //import org.opendaylight.controller.hosttracker.*;
53 @RunWith(PaxExam.class)
54 public class HostTrackerIT {
55 private Logger log = LoggerFactory.getLogger(HostTrackerIT.class);
56 // get the OSGI bundle context
58 private BundleContext bc;
60 private IfIptoHost hosttracker = null;
61 private IInventoryListener invtoryListener = null;
63 // Configure the OSGi container
65 public Option[] config() {
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(),
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(),
97 // needed by statisticsmanager
98 mavenBundle("org.opendaylight.controller", "containermanager").versionAsInProject(),
99 mavenBundle("org.opendaylight.controller", "containermanager.it.implementation").versionAsInProject(),
101 mavenBundle("org.opendaylight.controller", "clustering.services").versionAsInProject(),
102 mavenBundle("org.opendaylight.controller", "clustering.stub").versionAsInProject(),
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(),
114 // needed by hosttracker
115 mavenBundle("org.opendaylight.controller", "topologymanager").versionAsInProject(),
116 mavenBundle("org.opendaylight.controller", "arphandler").versionAsInProject(),
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(),
124 private String stateToString(int state) {
128 case Bundle.INSTALLED:
130 case Bundle.RESOLVED:
132 case Bundle.UNINSTALLED:
133 return "UNINSTALLED";
135 return "Not CONVERTED";
140 public void areWeReady() {
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));
152 log.debug("Do some debugging because some bundle is " + "unresolved");
155 // Assert if true, if false we are good to go!
156 assertFalse(debugit);
158 // Now lets create a hosttracker for testing purpose
159 ServiceReference s = bc.getServiceReference(IfIptoHost.class.getName());
161 this.hosttracker = (IfIptoHost) bc.getService(s);
162 this.invtoryListener = (IInventoryListener) this.hosttracker;
165 // If StatisticsManager is null, cannot run tests.
166 assertNotNull(this.hosttracker);
170 public void testStaticHost() throws UnknownHostException {
173 assertNotNull(this.hosttracker);
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);
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());
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"));
193 // check active host DB
194 hnci = this.hosttracker.getActiveStaticHosts().iterator();
195 Assert.assertFalse(hnci.hasNext());
197 // test removeStaticHost()
198 st = this.hosttracker.removeStaticHost("192.168.0.8");
199 Assert.assertTrue(st.isSuccess());
201 hnci = this.hosttracker.getInactiveStaticHosts().iterator();
202 while (hnci.hasNext()) {
203 ip = hnci.next().getNetworkAddressAsString();
204 Assert.assertTrue(ip.equals("192.168.0.13"));
209 public void testNotifyNodeConnector() throws UnknownHostException {
212 assertNotNull(this.invtoryListener);
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);
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());
225 this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
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"));
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"));
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"));
250 public void testHostFind() throws UnknownHostException {
252 assertNotNull(this.invtoryListener);
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);
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, "");
263 IHostId id1 = IPHostId.fromIP(InetAddress.getByName("192.168.0.8"));
264 HostNodeConnector hnc_1 = this.hosttracker.hostFind(id1);
267 this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
269 IHostId id2 = IPHostId.fromIP(InetAddress.getByName("192.168.0.8"));
270 hnc_1 = this.hosttracker.hostFind(id2);
272 assertNotNull(hnc_1);