Merge "Added default output directory to CodeGeneratorImpl in maven-sal-api-gen-plugi...
[controller.git] / opendaylight / switchmanager / integrationtest / src / test / java / org / opendaylight / controller / switchmanager / internal / SwitchManagerIT.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.switchmanager.internal;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
14 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
15 import static org.ops4j.pax.exam.CoreOptions.options;
16 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
17 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
18
19 import java.net.UnknownHostException;
20 import java.util.Map;
21
22 import javax.inject.Inject;
23
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.opendaylight.controller.sal.core.Actions;
29 import org.opendaylight.controller.sal.core.Bandwidth;
30 import org.opendaylight.controller.sal.core.Buffers;
31 import org.opendaylight.controller.sal.core.Capabilities;
32 import org.opendaylight.controller.sal.core.Capabilities.CapabilitiesType;
33 import org.opendaylight.controller.sal.core.ConstructionException;
34 import org.opendaylight.controller.sal.core.Node;
35 import org.opendaylight.controller.sal.core.NodeConnector;
36 import org.opendaylight.controller.sal.core.Property;
37 import org.opendaylight.controller.sal.core.State;
38 import org.opendaylight.controller.sal.core.TimeStamp;
39 import org.opendaylight.controller.switchmanager.ISwitchManager;
40 import org.ops4j.pax.exam.Option;
41 import org.ops4j.pax.exam.junit.Configuration;
42 import org.ops4j.pax.exam.junit.PaxExam;
43 import org.ops4j.pax.exam.util.PathUtils;
44 import org.osgi.framework.Bundle;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.ServiceReference;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 @RunWith(PaxExam.class)
51 public class SwitchManagerIT {
52     private final Logger log = LoggerFactory.getLogger(SwitchManagerIT.class);
53     // get the OSGI bundle context
54     @Inject
55     private BundleContext bc;
56
57     private ISwitchManager switchManager = null;
58
59     // Configure the OSGi container
60     @Configuration
61     public Option[] config() {
62         return options(
63                 systemProperty("logback.configurationFile").value(
64                         "file:" + PathUtils.getBaseDir()
65                                 + "/src/test/resources/logback.xml"),
66                 // To start OSGi console for inspection remotely
67                 systemProperty("osgi.console").value("2401"),
68                 // Set the systemPackages (used by clustering)
69                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
70                 // List framework bundles
71                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
72                         "1.0.0.v20120522-1841"),
73                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
74                         "1.0.400.v20120522-2049"),
75                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
76                         "3.3.100.v20120522-1822"),
77                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
78                         "1.4.0.v20120522-1841"),
79                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
80                         "0.8.0.v201108120515"),
81                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
82                         "0.8.0.v201108120515"),
83                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
84                         "0.8.0.v201110170705"),
85                 // List logger bundles
86                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
87                 mavenBundle("org.slf4j", "log4j-over-slf4j")
88                         .versionAsInProject(),
89                 mavenBundle("ch.qos.logback", "logback-core")
90                         .versionAsInProject(),
91                 mavenBundle("ch.qos.logback", "logback-classic")
92                         .versionAsInProject(),
93                 mavenBundle("org.opendaylight.controller", "clustering.stub")
94                         .versionAsInProject(),
95                 mavenBundle("org.opendaylight.controller", "configuration")
96                         .versionAsInProject(),
97                 mavenBundle("org.opendaylight.controller",
98                         "configuration.implementation").versionAsInProject(),
99                 mavenBundle("org.opendaylight.controller", "containermanager")
100                         .versionAsInProject(),
101                 mavenBundle("org.opendaylight.controller",
102                         "containermanager.implementation").versionAsInProject(),
103                 mavenBundle("org.opendaylight.controller",
104                         "clustering.services").versionAsInProject(),
105                 mavenBundle("org.opendaylight.controller", "sal")
106                         .versionAsInProject(),
107                 mavenBundle("org.opendaylight.controller", "sal.implementation")
108                         .versionAsInProject(),
109                 mavenBundle("org.opendaylight.controller",
110                                 "protocol_plugins.stub").versionAsInProject(),
111                 mavenBundle("org.opendaylight.controller", "switchmanager")
112                         .versionAsInProject(),
113                 mavenBundle("org.opendaylight.controller",
114                         "switchmanager.implementation").versionAsInProject(),
115                 mavenBundle("org.jboss.spec.javax.transaction",
116                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
117                 mavenBundle("org.apache.commons", "commons-lang3")
118                         .versionAsInProject(),
119                 mavenBundle("org.apache.felix",
120                         "org.apache.felix.dependencymanager")
121                         .versionAsInProject(), junitBundles());
122     }
123
124     private String stateToString(int state) {
125         switch (state) {
126         case Bundle.ACTIVE:
127             return "ACTIVE";
128         case Bundle.INSTALLED:
129             return "INSTALLED";
130         case Bundle.RESOLVED:
131             return "RESOLVED";
132         case Bundle.UNINSTALLED:
133             return "UNINSTALLED";
134         default:
135             return "Not CONVERTED";
136         }
137     }
138
139     @Before
140     public void areWeReady() {
141         assertNotNull(bc);
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:"
148                         + stateToString(state));
149                 debugit = true;
150             }
151         }
152         if (debugit) {
153             log.debug("Do some debugging because some bundle is "
154                     + "unresolved");
155         }
156
157         // Assert if true, if false we are good to go!
158         assertFalse(debugit);
159
160         // Now lets create a hosttracker for testing purpose
161         ServiceReference s = bc.getServiceReference(ISwitchManager.class
162                 .getName());
163         if (s != null) {
164             this.switchManager = (ISwitchManager) bc.getService(s);
165         }
166
167         // If StatisticsManager is null, cannot run tests.
168         assertNotNull(this.switchManager);
169     }
170
171     @Test
172     public void testNodeProp() throws UnknownHostException {
173         assertNotNull(this.switchManager);
174
175         Node node;
176         try {
177             node = new Node("STUB", new Integer(0xCAFE));
178         } catch (ConstructionException e) {
179             // test failed if node cannot be created.
180             node = null;
181             Assert.assertTrue(false);
182         }
183         Map<String, Property> propMap = this.switchManager.getNodeProps(node);
184         Assert.assertFalse(propMap.isEmpty());
185
186         Assert.assertTrue(this.switchManager.getNodeProp(node,
187                 Capabilities.CapabilitiesPropName).equals(
188                 new Capabilities(3)));
189         Assert.assertTrue(this.switchManager.getNodeProp(node,
190                 Actions.ActionsPropName).equals(new Actions(2)));
191         Assert.assertTrue(this.switchManager.getNodeProp(node,
192                 Buffers.BuffersPropName).equals(new Buffers(1)));
193         Assert.assertTrue(this.switchManager.getNodeProp(node,
194                 TimeStamp.TimeStampPropName).equals(
195                 new TimeStamp(100000L, "connectedSince")));
196     }
197
198     @Test
199     public void testNodeConnectorProp() throws UnknownHostException {
200         assertNotNull(this.switchManager);
201         Node node;
202         NodeConnector nc;
203         try {
204             node = new Node("STUB", 0xCAFE);
205             nc = new NodeConnector("STUB", 0xCAFE, node);
206         } catch (ConstructionException e) {
207             node = null;
208             nc = null;
209             Assert.assertTrue(false);
210         }
211         Map<String, Property> propMap = this.switchManager
212                 .getNodeConnectorProps(nc);
213         Assert.assertFalse(propMap.isEmpty());
214
215         Assert.assertTrue(this.switchManager.getNodeConnectorProp(nc,
216                 Capabilities.CapabilitiesPropName).equals(
217                 new Capabilities(CapabilitiesType.FLOW_STATS_CAPABILITY
218                         .getValue())));
219         Assert.assertTrue(this.switchManager.getNodeConnectorProp(nc,
220                 Bandwidth.BandwidthPropName).equals(
221                 new Bandwidth(Bandwidth.BW1Gbps)));
222         Assert.assertTrue(this.switchManager.getNodeConnectorProp(nc,
223                 State.StatePropName).equals(new State(State.EDGE_UP)));
224     }
225 }