450a7174e062c25e0627c8ddf6f6a0f14c64e33c
[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.Node;
41 import org.opendaylight.controller.sal.core.NodeConnector;
42 import org.opendaylight.controller.sal.core.State;
43 import org.opendaylight.controller.sal.core.TimeStamp;
44 import org.opendaylight.controller.sal.core.UpdateType;
45 import org.opendaylight.controller.sal.core.Property;
46 import org.opendaylight.controller.sal.core.Capabilities.CapabilitiesType;
47 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
48 import org.opendaylight.controller.sal.utils.NodeCreator;
49 import org.opendaylight.controller.sal.utils.Status;
50 import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates;
51 import org.opendaylight.controller.switchmanager.*;
52 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
53 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
54 import org.opendaylight.controller.switchmanager.ISwitchManager;
55
56 import org.ops4j.pax.exam.junit.PaxExam;
57 import org.ops4j.pax.exam.util.Filter;
58 import org.osgi.framework.BundleContext;
59 import static org.junit.Assert.*;
60 import org.ops4j.pax.exam.junit.Configuration;
61 import static org.ops4j.pax.exam.CoreOptions.*;
62
63 import org.ops4j.pax.exam.Option;
64 import org.ops4j.pax.exam.util.PathUtils;
65 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
66 import org.ops4j.pax.exam.spi.reactors.PerClass;
67
68 @RunWith(PaxExam.class)
69 public class SwitchmanagerIntegrationTest {
70     private Logger log = LoggerFactory
71             .getLogger(SwitchmanagerIntegrationTest.class);
72     // get the OSGI bundle context
73     @Inject
74     private BundleContext bc;
75
76     private ISwitchManager switchManager = null;
77
78     // Configure the OSGi container
79     @Configuration
80     public Option[] config() {
81         return options(
82                 systemProperty("logback.configurationFile").value(
83                         "file:" + PathUtils.getBaseDir()
84                                 + "/src/test/resources/logback.xml"),
85                 // To start OSGi console for inspection remotely
86                 systemProperty("osgi.console").value("2401"),
87                 // Set the systemPackages (used by clustering)
88                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
89                 // List framework bundles
90                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
91                         "1.0.0.v20120522-1841"),
92                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
93                         "1.0.400.v20120522-2049"),
94                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
95                         "3.3.100.v20120522-1822"),
96                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
97                         "1.4.0.v20120522-1841"),
98                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
99                         "0.8.0.v201108120515"),
100                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
101                         "0.8.0.v201108120515"),
102                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
103                         "0.8.0.v201110170705"),
104                 // List logger bundles
105                 mavenBundle("org.slf4j", "slf4j-api", "1.7.2"),
106                 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
107                 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
108                 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
109
110                 mavenBundle("org.opendaylight.controller", "switchmanager",
111                         "0.4.0-SNAPSHOT"),
112                 mavenBundle("org.opendaylight.controller", "switchmanager.implementation",
113                         "0.4.0-SNAPSHOT"),
114                 mavenBundle("org.opendaylight.controller", "sal",
115                         "0.5.0-SNAPSHOT"),
116                 mavenBundle("org.opendaylight.controller", "sal.implementation",
117                         "0.4.0-SNAPSHOT"),
118                mavenBundle("org.opendaylight.controller", "containermanager",
119                         "0.4.0-SNAPSHOT"),
120                mavenBundle("org.opendaylight.controller", "containermanager.implementation",
121                        "0.4.0-SNAPSHOT"),
122                mavenBundle("org.opendaylight.controller", "clustering.services",
123                        "0.4.0-SNAPSHOT"),
124                mavenBundle("org.opendaylight.controller", "clustering.stub",
125                        "0.4.0-SNAPSHOT"),
126                 mavenBundle("org.opendaylight.controller", "configuration",
127                         "0.4.0-SNAPSHOT"),
128                 mavenBundle("org.opendaylight.controller", "configuration.implementation", 
129                         "0.4.0-SNAPSHOT"),
130                 mavenBundle("org.opendaylight.controller",
131                         "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
132                 mavenBundle("org.jboss.spec.javax.transaction",
133                         "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
134                 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
135                 mavenBundle("org.apache.felix",
136                         "org.apache.felix.dependencymanager", "3.1.0"),
137                 junitBundles());
138     }
139
140     private String stateToString(int state) {
141         switch (state) {
142         case Bundle.ACTIVE:
143             return "ACTIVE";
144         case Bundle.INSTALLED:
145             return "INSTALLED";
146         case Bundle.RESOLVED:
147             return "RESOLVED";
148         case Bundle.UNINSTALLED:
149             return "UNINSTALLED";
150         default:
151             return "Not CONVERTED";
152         }
153     }
154
155     @Before
156     public void areWeReady() {
157         assertNotNull(bc);
158         boolean debugit = false;
159         Bundle b[] = bc.getBundles();
160         for (int i = 0; i < b.length; i++) {
161             int state = b[i].getState();
162             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
163                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
164                         + stateToString(state));
165                 debugit = true;
166             }
167         }
168         if (debugit) {
169             log.debug("Do some debugging because some bundle is "
170                     + "unresolved");
171         }
172
173         // Assert if true, if false we are good to go!
174         assertFalse(debugit);
175
176          // Now lets create a hosttracker for testing purpose
177          ServiceReference s = bc
178              .getServiceReference(ISwitchManager.class.getName());
179          if (s != null) {
180                  this.switchManager = (ISwitchManager)bc.getService(s);
181          }
182
183          // If StatisticsManager is null, cannot run tests.
184          assertNotNull(this.switchManager);
185     }
186
187
188     @Test
189     public void testNodeProp() throws UnknownHostException {
190         assertNotNull(this.switchManager);
191
192         Node node = NodeCreator.createOFNode((long)2);
193         Map<String, Property> propMap = this.switchManager.getNodeProps(node);
194         Assert.assertFalse(propMap.isEmpty());
195
196         Assert.assertTrue(this.switchManager.getNodeProp
197                 (node, Capabilities.CapabilitiesPropName)
198                 .equals(new Capabilities((int)3)));
199         Assert.assertTrue(this.switchManager.getNodeProp
200                 (node, Actions.ActionsPropName)
201                 .equals(new Actions((int)2)));
202         Assert.assertTrue(this.switchManager.getNodeProp
203                 (node, Buffers.BuffersPropName)
204                 .equals(new Buffers((int)1)));
205         Assert.assertTrue(this.switchManager.getNodeProp
206                 (node, TimeStamp.TimeStampPropName)
207                 .equals(new TimeStamp(100000L, "connectedSince")));
208     }
209
210     @Test
211     public void testNodeConnectorProp() throws UnknownHostException {
212         assertNotNull(this.switchManager);
213
214         NodeConnector nc = NodeConnectorCreator.createOFNodeConnector
215                 ((short)2, NodeCreator.createOFNode((long)3));
216         Map<String, Property> propMap = this.switchManager.getNodeConnectorProps(nc);
217         Assert.assertFalse(propMap.isEmpty());
218
219         Assert.assertTrue(this.switchManager.getNodeConnectorProp
220                 (nc, Capabilities.CapabilitiesPropName)
221                 .equals(new Capabilities
222                         (CapabilitiesType.FLOW_STATS_CAPABILITY.getValue())));
223         Assert.assertTrue(this.switchManager.getNodeConnectorProp
224                 (nc, Bandwidth.BandwidthPropName)
225                 .equals(new Bandwidth (Bandwidth.BW1Gbps)));
226         Assert.assertTrue(this.switchManager.getNodeConnectorProp
227                 (nc, State.StatePropName)
228                 .equals(new State (State.EDGE_UP)));
229     }
230 }