Merge "Fix exception in HostTracker when non-OF node is removed."
[controller.git] / opendaylight / switchmanager / integrationtest / src / test / java / org / opendaylight / controller / switchmanager / internal / SwitchmanagerIntegrationTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.switchmanager.internal;
11
12
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.Map.Entry;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.osgi.framework.ServiceReference;
27 import org.osgi.framework.Bundle;
28 import javax.inject.Inject;
29
30 import org.eclipse.osgi.framework.console.CommandProvider;
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.junit.Before;
34 import org.junit.After;
35 import org.junit.runner.RunWith;
36 import org.opendaylight.controller.sal.core.Actions;
37 import org.opendaylight.controller.sal.core.Bandwidth;
38 import org.opendaylight.controller.sal.core.Buffers;
39 import org.opendaylight.controller.sal.core.Capabilities;
40 import org.opendaylight.controller.sal.core.ConstructionException;
41 import org.opendaylight.controller.sal.core.Node;
42 import org.opendaylight.controller.sal.core.NodeConnector;
43 import org.opendaylight.controller.sal.core.State;
44 import org.opendaylight.controller.sal.core.TimeStamp;
45 import org.opendaylight.controller.sal.core.UpdateType;
46 import org.opendaylight.controller.sal.core.Property;
47 import org.opendaylight.controller.sal.core.Capabilities.CapabilitiesType;
48 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
49 import org.opendaylight.controller.sal.utils.NodeCreator;
50 import org.opendaylight.controller.sal.utils.Status;
51 import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates;
52 import org.opendaylight.controller.switchmanager.*;
53 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
54 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
55 import org.opendaylight.controller.switchmanager.ISwitchManager;
56
57 import org.ops4j.pax.exam.junit.PaxExam;
58 import org.ops4j.pax.exam.util.Filter;
59 import org.osgi.framework.BundleContext;
60 import static org.junit.Assert.*;
61 import org.ops4j.pax.exam.junit.Configuration;
62 import static org.ops4j.pax.exam.CoreOptions.*;
63
64 import org.ops4j.pax.exam.Option;
65 import org.ops4j.pax.exam.util.PathUtils;
66 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
67 import org.ops4j.pax.exam.spi.reactors.PerClass;
68
69 @RunWith(PaxExam.class)
70 public class SwitchmanagerIntegrationTest {
71     private Logger log = LoggerFactory
72             .getLogger(SwitchmanagerIntegrationTest.class);
73     // get the OSGI bundle context
74     @Inject
75     private BundleContext bc;
76
77     private ISwitchManager switchManager = null;
78
79     // Configure the OSGi container
80     @Configuration
81     public Option[] config() {
82         return options(
83                 systemProperty("logback.configurationFile").value(
84                         "file:" + PathUtils.getBaseDir()
85                                 + "/src/test/resources/logback.xml"),
86                 // To start OSGi console for inspection remotely
87                 systemProperty("osgi.console").value("2401"),
88                 // Set the systemPackages (used by clustering)
89                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
90                 // List framework bundles
91                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
92                         "1.0.0.v20120522-1841"),
93                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
94                         "1.0.400.v20120522-2049"),
95                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
96                         "3.3.100.v20120522-1822"),
97                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
98                         "1.4.0.v20120522-1841"),
99                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
100                         "0.8.0.v201108120515"),
101                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
102                         "0.8.0.v201108120515"),
103                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
104                         "0.8.0.v201110170705"),
105                 // List logger bundles
106                 mavenBundle("org.slf4j", "slf4j-api", "1.7.2"),
107                 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
108                 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
109                 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
110
111                 mavenBundle("org.opendaylight.controller", "switchmanager",
112                         "0.4.0-SNAPSHOT"),
113                 mavenBundle("org.opendaylight.controller", "switchmanager.implementation",
114                         "0.4.0-SNAPSHOT"),
115                 mavenBundle("org.opendaylight.controller", "sal",
116                         "0.5.0-SNAPSHOT"),
117                 mavenBundle("org.opendaylight.controller", "sal.implementation",
118                         "0.4.0-SNAPSHOT"),
119                mavenBundle("org.opendaylight.controller", "containermanager",
120                         "0.4.0-SNAPSHOT"),
121                mavenBundle("org.opendaylight.controller", "containermanager.implementation",
122                        "0.4.0-SNAPSHOT"),
123                mavenBundle("org.opendaylight.controller", "clustering.services",
124                        "0.4.0-SNAPSHOT"),
125                mavenBundle("org.opendaylight.controller", "clustering.stub",
126                        "0.4.0-SNAPSHOT"),
127                 mavenBundle("org.opendaylight.controller", "configuration",
128                         "0.4.0-SNAPSHOT"),
129                 mavenBundle("org.opendaylight.controller", "configuration.implementation", 
130                         "0.4.0-SNAPSHOT"),
131                 mavenBundle("org.opendaylight.controller",
132                         "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
133                 mavenBundle("org.jboss.spec.javax.transaction",
134                         "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
135                 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
136                 mavenBundle("org.apache.felix",
137                         "org.apache.felix.dependencymanager", "3.1.0"),
138                 junitBundles());
139     }
140
141     private String stateToString(int state) {
142         switch (state) {
143         case Bundle.ACTIVE:
144             return "ACTIVE";
145         case Bundle.INSTALLED:
146             return "INSTALLED";
147         case Bundle.RESOLVED:
148             return "RESOLVED";
149         case Bundle.UNINSTALLED:
150             return "UNINSTALLED";
151         default:
152             return "Not CONVERTED";
153         }
154     }
155
156     @Before
157     public void areWeReady() {
158         assertNotNull(bc);
159         boolean debugit = false;
160         Bundle b[] = bc.getBundles();
161         for (int i = 0; i < b.length; i++) {
162             int state = b[i].getState();
163             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
164                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
165                         + stateToString(state));
166                 debugit = true;
167             }
168         }
169         if (debugit) {
170             log.debug("Do some debugging because some bundle is "
171                     + "unresolved");
172         }
173
174         // Assert if true, if false we are good to go!
175         assertFalse(debugit);
176
177          // Now lets create a hosttracker for testing purpose
178          ServiceReference s = bc
179              .getServiceReference(ISwitchManager.class.getName());
180          if (s != null) {
181                  this.switchManager = (ISwitchManager)bc.getService(s);
182          }
183
184          // If StatisticsManager is null, cannot run tests.
185          assertNotNull(this.switchManager);
186     }
187
188
189     @Test
190     public void testNodeProp() throws UnknownHostException {
191         assertNotNull(this.switchManager);
192
193         Node node;
194         try{
195             node = new Node("STUB", new Integer(0xCAFE));
196         }catch(ConstructionException e){
197             //test failed if node cannot be created.
198             node = null;
199             Assert.assertTrue(false);
200         }
201         Map<String, Property> propMap = this.switchManager.getNodeProps(node);
202         Assert.assertFalse(propMap.isEmpty());
203
204         Assert.assertTrue(this.switchManager.getNodeProp
205                 (node, Capabilities.CapabilitiesPropName)
206                 .equals(new Capabilities((int)3)));
207         Assert.assertTrue(this.switchManager.getNodeProp
208                 (node, Actions.ActionsPropName)
209                 .equals(new Actions((int)2)));
210         Assert.assertTrue(this.switchManager.getNodeProp
211                 (node, Buffers.BuffersPropName)
212                 .equals(new Buffers((int)1)));
213         Assert.assertTrue(this.switchManager.getNodeProp
214                 (node, TimeStamp.TimeStampPropName)
215                 .equals(new TimeStamp(100000L, "connectedSince")));
216     }
217
218     @Test
219     public void testNodeConnectorProp() throws UnknownHostException {
220         assertNotNull(this.switchManager);
221         Node node;
222         NodeConnector nc;
223         try {
224             node = new Node("STUB", 0xCAFE);
225             nc = new NodeConnector("STUB", 0xCAFE, node);
226         }
227         catch(ConstructionException e){
228             node = null;
229             nc = null;
230             Assert.assertTrue(false);
231         }
232         Map<String, Property> propMap = this.switchManager.getNodeConnectorProps(nc);
233         Assert.assertFalse(propMap.isEmpty());
234
235         Assert.assertTrue(this.switchManager.getNodeConnectorProp
236                 (nc, Capabilities.CapabilitiesPropName)
237                 .equals(new Capabilities
238                         (CapabilitiesType.FLOW_STATS_CAPABILITY.getValue())));
239         Assert.assertTrue(this.switchManager.getNodeConnectorProp
240                 (nc, Bandwidth.BandwidthPropName)
241                 .equals(new Bandwidth (Bandwidth.BW1Gbps)));
242         Assert.assertTrue(this.switchManager.getNodeConnectorProp
243                 (nc, State.StatePropName)
244                 .equals(new State (State.EDGE_UP)));
245     }
246 }