Merge "Should not allow nodes to have same node name"
[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", "topologymanager").versionAsInProject(),
114                 mavenBundle("org.opendaylight.controller", "hosttracker").versionAsInProject(),
115                 mavenBundle("org.opendaylight.controller", "forwardingrulesmanager").versionAsInProject(),
116                 mavenBundle("org.opendaylight.controller", "statisticsmanager").versionAsInProject(),
117                 mavenBundle("org.opendaylight.controller",
118                         "switchmanager.implementation").versionAsInProject(),
119                 mavenBundle("org.jboss.spec.javax.transaction",
120                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
121                 mavenBundle("org.apache.commons", "commons-lang3")
122                         .versionAsInProject(),
123                 mavenBundle("org.apache.felix",
124                         "org.apache.felix.dependencymanager")
125                         .versionAsInProject(), junitBundles());
126     }
127
128     private String stateToString(int state) {
129         switch (state) {
130         case Bundle.ACTIVE:
131             return "ACTIVE";
132         case Bundle.INSTALLED:
133             return "INSTALLED";
134         case Bundle.RESOLVED:
135             return "RESOLVED";
136         case Bundle.UNINSTALLED:
137             return "UNINSTALLED";
138         default:
139             return "Not CONVERTED";
140         }
141     }
142
143     @Before
144     public void areWeReady() {
145         assertNotNull(bc);
146         boolean debugit = false;
147         Bundle b[] = bc.getBundles();
148         for (int i = 0; i < b.length; i++) {
149             int state = b[i].getState();
150             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
151                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
152                         + stateToString(state));
153                 debugit = true;
154             }
155         }
156         if (debugit) {
157             log.debug("Do some debugging because some bundle is "
158                     + "unresolved");
159         }
160
161         // Assert if true, if false we are good to go!
162         assertFalse(debugit);
163
164         // Now lets create a hosttracker for testing purpose
165         ServiceReference s = bc.getServiceReference(ISwitchManager.class
166                 .getName());
167         if (s != null) {
168             this.switchManager = (ISwitchManager) bc.getService(s);
169         }
170
171         // If StatisticsManager is null, cannot run tests.
172         assertNotNull(this.switchManager);
173     }
174
175     @Test
176     public void testNodeProp() throws UnknownHostException {
177         assertNotNull(this.switchManager);
178
179         Node node;
180         try {
181             node = new Node("STUB", new Integer(0xCAFE));
182         } catch (ConstructionException e) {
183             // test failed if node cannot be created.
184             node = null;
185             Assert.assertTrue(false);
186         }
187         Map<String, Property> propMap = this.switchManager.getNodeProps(node);
188         Assert.assertFalse(propMap.isEmpty());
189
190         Assert.assertTrue(this.switchManager.getNodeProp(node,
191                 Capabilities.CapabilitiesPropName).equals(
192                 new Capabilities(3)));
193         Assert.assertTrue(this.switchManager.getNodeProp(node,
194                 Actions.ActionsPropName).equals(new Actions(2)));
195         Assert.assertTrue(this.switchManager.getNodeProp(node,
196                 Buffers.BuffersPropName).equals(new Buffers(1)));
197         Assert.assertTrue(this.switchManager.getNodeProp(node,
198                 TimeStamp.TimeStampPropName).equals(
199                 new TimeStamp(100000L, "connectedSince")));
200     }
201
202     @Test
203     public void testNodeConnectorProp() throws UnknownHostException {
204         assertNotNull(this.switchManager);
205         Node node;
206         NodeConnector nc;
207         try {
208             node = new Node("STUB", 0xCAFE);
209             nc = new NodeConnector("STUB", 0xCAFE, node);
210         } catch (ConstructionException e) {
211             node = null;
212             nc = null;
213             Assert.assertTrue(false);
214         }
215         Map<String, Property> propMap = this.switchManager
216                 .getNodeConnectorProps(nc);
217         Assert.assertFalse(propMap.isEmpty());
218
219         Assert.assertTrue(this.switchManager.getNodeConnectorProp(nc,
220                 Capabilities.CapabilitiesPropName).equals(
221                 new Capabilities(CapabilitiesType.FLOW_STATS_CAPABILITY
222                         .getValue())));
223         Assert.assertTrue(this.switchManager.getNodeConnectorProp(nc,
224                 Bandwidth.BandwidthPropName).equals(
225                 new Bandwidth(Bandwidth.BW1Gbps)));
226         Assert.assertTrue(this.switchManager.getNodeConnectorProp(nc,
227                 State.StatePropName).equals(new State(State.EDGE_UP)));
228     }
229 }