Merge "When ports are added to a container, construct its topology immediately based...
[controller.git] / opendaylight / forwardingrulesmanager / integrationtest / src / test / java / org / opendaylight / controller / forwardingrulesmanager / internal / ForwardingRulesManagerIT.java
1 package org.opendaylight.controller.forwardingrulesmanager.internal;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
6 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
7 import static org.ops4j.pax.exam.CoreOptions.options;
8 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
9 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import javax.inject.Inject;
17
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
23 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
24 import org.opendaylight.controller.sal.action.Action;
25 import org.opendaylight.controller.sal.action.Drop;
26 import org.opendaylight.controller.sal.core.ConstructionException;
27 import org.opendaylight.controller.sal.core.Node;
28 import org.opendaylight.controller.sal.flowprogrammer.Flow;
29 import org.opendaylight.controller.sal.match.Match;
30 import org.opendaylight.controller.sal.match.MatchType;
31 import org.opendaylight.controller.sal.utils.NodeCreator;
32 import org.opendaylight.controller.sal.utils.Status;
33 import org.opendaylight.controller.sal.utils.StatusCode;
34 import org.ops4j.pax.exam.Option;
35 import org.ops4j.pax.exam.junit.Configuration;
36 import org.ops4j.pax.exam.junit.PaxExam;
37 import org.ops4j.pax.exam.util.PathUtils;
38 import org.osgi.framework.Bundle;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceReference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 @RunWith(PaxExam.class)
45 public class ForwardingRulesManagerIT {
46     private Logger log = LoggerFactory.getLogger(ForwardingRulesManagerIT.class);
47     // get the OSGI bundle context
48     @Inject
49     private BundleContext bc;
50
51     private IForwardingRulesManager manager = null;
52
53     // Configure the OSGi container
54     @Configuration
55     public Option[] config() {
56         return options(
57                 //
58                 systemProperty("logback.configurationFile").value(
59                         "file:" + PathUtils.getBaseDir()
60                                 + "/src/test/resources/logback.xml"),
61                 // To start OSGi console for inspection remotely
62                 systemProperty("osgi.console").value("2401"),
63                 // Set the systemPackages (used by clustering)
64                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
65                 // List framework bundles
66                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
67                         "1.0.0.v20120522-1841"),
68                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
69                         "1.0.400.v20120522-2049"),
70                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
71                         "3.3.100.v20120522-1822"),
72                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
73                         "1.4.0.v20120522-1841"),
74                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
75                         "0.8.0.v201108120515"),
76                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
77                         "0.8.0.v201108120515"),
78                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
79                         "0.8.0.v201110170705"),
80                 // List logger bundles
81                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
82                 mavenBundle("org.slf4j", "log4j-over-slf4j")
83                         .versionAsInProject(),
84                 mavenBundle("ch.qos.logback", "logback-core")
85                         .versionAsInProject(),
86                 mavenBundle("ch.qos.logback", "logback-classic")
87                         .versionAsInProject(),
88                 // List all the bundles on which the test case depends
89                 mavenBundle("org.opendaylight.controller", "sal")
90                         .versionAsInProject(),
91                 mavenBundle("org.opendaylight.controller", "sal.implementation")
92                         .versionAsInProject(),
93
94                 mavenBundle("org.opendaylight.controller",
95                         "protocol_plugins.stub").versionAsInProject(),
96
97                 mavenBundle("org.opendaylight.controller", "containermanager")
98                         .versionAsInProject(),
99                 mavenBundle("org.opendaylight.controller",
100                         "containermanager.implementation").versionAsInProject(),
101
102                 mavenBundle("org.opendaylight.controller",
103                         "forwardingrulesmanager").versionAsInProject(),
104
105                 mavenBundle("org.opendaylight.controller",
106                         "forwardingrulesmanager.implementation")
107                         .versionAsInProject(),
108
109                 mavenBundle("org.opendaylight.controller",
110                         "clustering.services").versionAsInProject(),
111                 mavenBundle("org.opendaylight.controller", "clustering.stub")
112                         .versionAsInProject(),
113                 mavenBundle("org.opendaylight.controller", "switchmanager")
114                         .versionAsInProject(),
115                 mavenBundle("org.opendaylight.controller", "statisticsmanager").versionAsInProject(),
116                 mavenBundle("org.opendaylight.controller",
117                         "switchmanager.implementation").versionAsInProject(),
118                 mavenBundle("org.opendaylight.controller", "configuration")
119                         .versionAsInProject(),
120                 mavenBundle("org.opendaylight.controller",
121                         "configuration.implementation").versionAsInProject(),
122                 mavenBundle("org.opendaylight.controller", "hosttracker")
123                         .versionAsInProject(),
124                 mavenBundle("org.opendaylight.controller",
125                         "hosttracker.implementation").versionAsInProject(),
126                 mavenBundle("org.opendaylight.controller",
127                         "connectionmanager.implementation").versionAsInProject(),
128                 mavenBundle("org.opendaylight.controller",
129                         "connectionmanager").versionAsInProject(),
130                 mavenBundle("org.opendaylight.controller",
131                         "sal.connection").versionAsInProject(),
132                 mavenBundle("org.opendaylight.controller",
133                         "sal.connection.implementation").versionAsInProject(),
134
135                 // needed by hosttracker
136                 mavenBundle("org.opendaylight.controller", "topologymanager")
137                         .versionAsInProject(),
138
139                 mavenBundle("org.jboss.spec.javax.transaction",
140                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
141                 mavenBundle("org.apache.commons", "commons-lang3")
142                         .versionAsInProject(),
143                 mavenBundle("org.apache.felix",
144                         "org.apache.felix.dependencymanager")
145                         .versionAsInProject(), junitBundles());
146     }
147
148     private String stateToString(int state) {
149         switch (state) {
150         case Bundle.ACTIVE:
151             return "ACTIVE";
152         case Bundle.INSTALLED:
153             return "INSTALLED";
154         case Bundle.RESOLVED:
155             return "RESOLVED";
156         case Bundle.UNINSTALLED:
157             return "UNINSTALLED";
158         default:
159             return "Not CONVERTED";
160         }
161     }
162
163     @Before
164     public void areWeReady() {
165         assertNotNull(bc);
166         boolean debugit = false;
167         Bundle b[] = bc.getBundles();
168         for (Bundle element : b) {
169             int state = element.getState();
170             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
171                 log.debug("Bundle:" + element.getSymbolicName() + " state:"
172                         + stateToString(state));
173                 debugit = true;
174             }
175         }
176         if (debugit) {
177             log.debug("Do some debugging because some bundle is "
178                     + "unresolved");
179         }
180
181         // Assert if true, if false we are good to go!
182         assertFalse(debugit);
183
184         ServiceReference r = bc.getServiceReference(IForwardingRulesManager.class.getName());
185         if (r != null) {
186             this.manager = (IForwardingRulesManager) bc.getService(r);
187         }
188         // If StatisticsManager is null, cannot run tests.
189         assertNotNull(this.manager);
190
191     }
192
193     @Test
194     public void testFlowEntries() {
195         Flow flow = new Flow();
196
197         Match match = new Match();
198         try {
199             match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
200         } catch (UnknownHostException e) {
201         }
202         flow.setMatch(match);
203         Action action = new Drop();
204
205         List<Action> actions = new ArrayList<Action>();
206         actions.add(action);
207         flow.setActions(actions);
208         Node node;
209         try {
210             // Must use a node published by the stub protocol plugin else
211             // connection manager will not report it as a local node
212             node = new Node("STUB", 51966);
213             FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
214             Status stat = manager.installFlowEntry(fe);
215
216             Assert.assertTrue(stat.getCode() == StatusCode.SUCCESS);
217         } catch (ConstructionException e) {
218             // Got a failure while allocating the node
219             Assert.assertTrue(false);
220         }
221     }
222 }