296be72a131c078a98cb725541af6f7bdc7e2875
[controller.git] / opendaylight / topologymanager / integrationtest / src / test / java / org / opendaylight / controller / topologymanager / TopologyManagerIT.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.topologymanager;
10
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.opendaylight.controller.protocol_plugins.stub.internal.TopologyServices;
15 import org.opendaylight.controller.sal.core.*;
16 import org.opendaylight.controller.sal.topology.IPluginInTopologyService;
17 import org.ops4j.pax.exam.Option;
18 import org.ops4j.pax.exam.junit.Configuration;
19 import org.ops4j.pax.exam.junit.PaxExam;
20 import org.ops4j.pax.exam.util.PathUtils;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.ServiceReference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import javax.inject.Inject;
28 import java.util.HashSet;
29 import java.util.Set;
30
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotNull;
33 import static org.ops4j.pax.exam.CoreOptions.*;
34
35 @RunWith(PaxExam.class)
36 public class TopologyManagerIT {
37     private Logger log = LoggerFactory
38             .getLogger(TopologyManagerIT.class);
39     // get the OSGI bundle context
40     @Inject
41     private BundleContext bc;
42
43     private ITopologyManager manager = null;
44
45     // Configure the OSGi container
46     @Configuration
47     public Option[] config() {
48         return options(
49                 //
50                 systemProperty("logback.configurationFile").value(
51                         "file:" + PathUtils.getBaseDir()
52                                 + "/src/test/resources/logback.xml"),
53                 // To start OSGi console for inspection remotely
54                 systemProperty("osgi.console").value("2401"),
55                 // Set the systemPackages (used by clustering)
56                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
57                 // List framework bundles
58                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console").versionAsInProject(),
59                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
60                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
61                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
62                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
63                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
64                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
65                 // List logger bundles
66                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
67                 mavenBundle("org.slf4j", "log4j-over-slf4j")
68                         .versionAsInProject(),
69                 mavenBundle("ch.qos.logback", "logback-core")
70                         .versionAsInProject(),
71                 mavenBundle("ch.qos.logback", "logback-classic")
72                         .versionAsInProject(),
73                 // needed by statisticsmanager
74                 mavenBundle("org.opendaylight.controller", "containermanager")
75                     .versionAsInProject(),
76                 mavenBundle("org.opendaylight.controller", "containermanager.it.implementation")
77                     .versionAsInProject(),
78                 mavenBundle("org.opendaylight.controller", "clustering.services")
79                     .versionAsInProject(),
80                 mavenBundle("org.opendaylight.controller", "clustering.stub")
81                     .versionAsInProject(),
82                 // needed by forwardingrulesmanager
83                 mavenBundle("org.opendaylight.controller", "configuration")
84                     .versionAsInProject(),
85                 mavenBundle("org.opendaylight.controller", "configuration.implementation")
86                     .versionAsInProject(),
87                 mavenBundle("org.opendaylight.controller", "hosttracker")
88                     .versionAsInProject(),
89
90                 // List all the bundles on which the test case depends
91                 mavenBundle("org.opendaylight.controller", "sal")
92                     .versionAsInProject(),
93                 mavenBundle("org.opendaylight.controller", "sal.implementation")
94                     .versionAsInProject(),
95                 mavenBundle("org.opendaylight.controller", "protocol_plugins.stub")
96                     .versionAsInProject(),
97                 mavenBundle("org.opendaylight.controller", "switchmanager")
98                     .versionAsInProject(),
99                 mavenBundle("org.opendaylight.controller", "switchmanager.implementation")
100                     .versionAsInProject(),
101                 mavenBundle("org.opendaylight.controller", "statisticsmanager")
102                     .versionAsInProject(),
103                 mavenBundle("org.opendaylight.controller", "statisticsmanager.implementation")
104                     .versionAsInProject(),
105                 mavenBundle("org.opendaylight.controller", "forwardingrulesmanager")
106                     .versionAsInProject(),
107
108                 // needed by hosttracker
109                 mavenBundle("org.opendaylight.controller", "topologymanager")
110                         .versionAsInProject(),
111                 mavenBundle("org.jboss.spec.javax.transaction",
112                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
113                 mavenBundle("org.apache.commons", "commons-lang3")
114                         .versionAsInProject(),
115                 mavenBundle("org.apache.felix",
116                         "org.apache.felix.dependencymanager")
117                         .versionAsInProject(), junitBundles());
118     }
119
120     private String stateToString(int state) {
121         switch (state) {
122         case Bundle.ACTIVE:
123             return "ACTIVE";
124         case Bundle.INSTALLED:
125             return "INSTALLED";
126         case Bundle.RESOLVED:
127             return "RESOLVED";
128         case Bundle.UNINSTALLED:
129             return "UNINSTALLED";
130         default:
131             return "Not CONVERTED";
132         }
133     }
134
135     @Before
136     public void areWeReady() {
137         assertNotNull(bc);
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:"
144                         + stateToString(state));
145                 debugit = true;
146             }
147         }
148         if (debugit) {
149             log.debug("Do some debugging because some bundle is "
150                     + "unresolved");
151         }
152
153         // Assert if true, if false we are good to go!
154         assertFalse(debugit);
155
156         ServiceReference r = bc.getServiceReference(ITopologyManager.class
157                 .getName());
158         if (r != null) {
159             this.manager = (ITopologyManager) bc.getService(r);
160         }
161         // If StatisticsManager is null, cannot run tests.
162         assertNotNull(this.manager);
163
164     }
165
166
167     /**
168      *  This test verifies that the isInternal method of the TopologyManager returns true when a node is internal and
169      *  not otherwise
170      *
171      *  To make a node interval we add a node using the plugin interface (TopologyServices.addEdge) this is to ensure
172      *  that when TopologyManager sees the edge is via it's dependency on the SAL ITopologyService.
173      *
174      * @throws Exception
175      */
176     @Test
177     public void testIsInternal() throws Exception{
178         Node node1 = new Node("STUB", 0xCAFE);
179         Node node2 = new Node("STUB", 0XFACE);
180
181         NodeConnector head = new NodeConnector("STUB", node1.getID(), node1);
182         NodeConnector tail = new NodeConnector("STUB", node2.getID(), node2);
183
184         assert(this.manager.isInternal(head));
185
186         Set<Property> properties = new HashSet<Property>();
187
188         ServiceReference r = bc.getServiceReference(IPluginInTopologyService.class
189                 .getName());
190         TopologyServices topologyServices = null;
191         if (r != null) {
192             if(bc.getService(r) instanceof TopologyServices) {
193                 topologyServices = (TopologyServices) bc.getService(r);
194             } else {
195                 throw new RuntimeException("topology service registered is not from the stub plugin implementation");
196             }
197         }
198
199         topologyServices.addEdge(new Edge(tail, head), properties, UpdateType.ADDED);
200
201         assert(this.manager.isInternal(head));
202     }
203
204 }