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.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.*;
50 @RunWith(PaxExam.class)
51 public class HostTrackerIT {
52 private Logger log = LoggerFactory.getLogger(HostTrackerIT.class);
53 // get the OSGI bundle context
55 private BundleContext bc;
57 private IfIptoHost hosttracker = null;
58 private IInventoryListener invtoryListener = null;
59 // Configure the OSGi container
61 public Option[] config() {
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(),
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(),
93 // needed by statisticsmanager
94 mavenBundle("org.opendaylight.controller", "containermanager").versionAsInProject(),
95 mavenBundle("org.opendaylight.controller", "containermanager.implementation").versionAsInProject(),
97 mavenBundle("org.opendaylight.controller", "clustering.services").versionAsInProject(),
98 mavenBundle("org.opendaylight.controller", "clustering.stub").versionAsInProject(),
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(),
110 // needed by hosttracker
111 mavenBundle("org.opendaylight.controller", "topologymanager").versionAsInProject(),
112 mavenBundle("org.opendaylight.controller", "arphandler").versionAsInProject(),
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(),
120 private String stateToString(int state) {
124 case Bundle.INSTALLED:
126 case Bundle.RESOLVED:
128 case Bundle.UNINSTALLED:
129 return "UNINSTALLED";
131 return "Not CONVERTED";
136 public void areWeReady() {
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));
148 log.debug("Do some debugging because some bundle is " + "unresolved");
151 // Assert if true, if false we are good to go!
152 assertFalse(debugit);
154 // Now lets create a hosttracker for testing purpose
155 ServiceReference s = bc.getServiceReference(IfIptoHost.class.getName());
157 this.hosttracker = (IfIptoHost) bc.getService(s);
158 this.invtoryListener = (IInventoryListener) this.hosttracker;
161 // If StatisticsManager is null, cannot run tests.
162 assertNotNull(this.hosttracker);
166 public void testStaticHost() throws UnknownHostException {
169 assertNotNull(this.hosttracker);
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);
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());
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"));
189 // check active host DB
190 hnci = this.hosttracker.getActiveStaticHosts().iterator();
191 Assert.assertFalse(hnci.hasNext());
193 // test removeStaticHost()
194 st = this.hosttracker.removeStaticHost("192.168.0.8");
195 Assert.assertTrue(st.isSuccess());
197 hnci = this.hosttracker.getInactiveStaticHosts().iterator();
198 while (hnci.hasNext()) {
199 ip = hnci.next().getNetworkAddressAsString();
200 Assert.assertTrue(ip.equals("192.168.0.13"));
205 public void testNotifyNodeConnector() throws UnknownHostException {
208 assertNotNull(this.invtoryListener);
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);
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());
222 this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
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"));
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"));
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"));
247 public void testHostFind() throws UnknownHostException {
249 assertNotNull(this.invtoryListener);
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);
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, "");
260 HostNodeConnector hnc_1 = this.hosttracker.hostFind(InetAddress.getByName("192.168.0.8"));
263 this.invtoryListener.notifyNodeConnector(nc1_1, UpdateType.ADDED, null);
265 hnc_1 = this.hosttracker.hostFind(InetAddress.getByName("192.168.0.8"));
266 assertNotNull(hnc_1);