Fixed TopologyIT to use @Inject
[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     @Inject
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     /**
136      *  This test verifies that the isInternal method of the TopologyManager returns true when a node is internal and
137      *  not otherwise
138      *
139      *  To make a node interval we add a node using the plugin interface (TopologyServices.addEdge) this is to ensure
140      *  that when TopologyManager sees the edge is via it's dependency on the SAL ITopologyService.
141      *
142      * @throws Exception
143      */
144     @Test
145     public void testIsInternal() throws Exception{
146         Node node1 = new Node("STUB", 0xCAFE);
147         Node node2 = new Node("STUB", 0XFACE);
148
149         NodeConnector head = new NodeConnector("STUB", node1.getID(), node1);
150         NodeConnector tail = new NodeConnector("STUB", node2.getID(), node2);
151
152         assert(this.manager.isInternal(head));
153
154         Set<Property> properties = new HashSet<Property>();
155
156         ServiceReference r = bc.getServiceReference(IPluginInTopologyService.class
157                 .getName());
158         TopologyServices topologyServices = null;
159         if (r != null) {
160             if(bc.getService(r) instanceof TopologyServices) {
161                 topologyServices = (TopologyServices) bc.getService(r);
162             } else {
163                 throw new RuntimeException("topology service registered is not from the stub plugin implementation");
164             }
165         }
166
167         topologyServices.addEdge(new Edge(tail, head), properties, UpdateType.ADDED);
168
169         assert(this.manager.isInternal(head));
170     }
171
172 }