Northbound IntegrationTest
[controller.git] / opendaylight / northbound / integrationtest / src / test / java / org / opendaylight / controller / northbound / integrationtest / NorthboundIntegrationTest.java
1 package org.opendaylight.controller.northbound.integrationtest;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.osgi.framework.ServiceReference;
6 import org.osgi.framework.Bundle;
7 import javax.inject.Inject;
8 import org.junit.Assert;
9 import org.junit.Test;
10 import org.junit.Before;
11 import org.junit.runner.RunWith;
12 import org.ops4j.pax.exam.junit.PaxExam;
13 import org.osgi.framework.BundleContext;
14 import static org.junit.Assert.*;
15 import org.ops4j.pax.exam.junit.Configuration;
16 import static org.ops4j.pax.exam.CoreOptions.*;
17 import org.ops4j.pax.exam.Option;
18 import org.ops4j.pax.exam.util.PathUtils;
19 import java.io.BufferedReader;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.net.URL;
23 import java.net.URLConnection;
24 import java.nio.charset.Charset;
25 import org.apache.commons.codec.binary.Base64;
26
27 import org.codehaus.jettison.json.JSONObject;
28 import org.codehaus.jettison.json.JSONTokener;
29
30 import org.opendaylight.controller.usermanager.IUserManager;
31
32 @RunWith(PaxExam.class)
33 public class NorthboundIntegrationTest {
34     private Logger log = LoggerFactory
35             .getLogger(NorthboundIntegrationTest.class);
36     // get the OSGI bundle context
37     @Inject
38     private BundleContext bc;
39     private IUserManager users = null;
40
41     private String stateToString(int state) {
42         switch (state) {
43         case Bundle.ACTIVE:
44             return "ACTIVE";
45         case Bundle.INSTALLED:
46             return "INSTALLED";
47         case Bundle.RESOLVED:
48             return "RESOLVED";
49         case Bundle.UNINSTALLED:
50             return "UNINSTALLED";
51         default:
52             return "Not CONVERTED";
53         }
54     }
55
56     @Before
57     public void areWeReady() {
58         assertNotNull(bc);
59         boolean debugit = false;
60         Bundle b[] = bc.getBundles();
61         for (int i = 0; i < b.length; i++) {
62             int state = b[i].getState();
63             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
64                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
65                         + stateToString(state));
66                 debugit = true;
67             }
68         }
69         if (debugit) {
70             log.debug("Do some debugging because some bundle is "
71                     + "unresolved");
72         }
73         // Assert if true, if false we are good to go!
74         assertFalse(debugit);
75
76         ServiceReference r = bc.getServiceReference(IUserManager.class
77                 .getName());
78         if (r != null) {
79             this.users = (IUserManager) bc.getService(r);
80         }
81         // If UserManager is null, cannot login to run tests.
82         assertNotNull(this.users);
83
84     }
85
86     @Test
87     public void testStatistics() {
88
89         System.out.println("Starting Statistics JAXB client.");
90
91         String baseURL = "http://127.0.0.1:8080/controller/nb/v2/statistics/default/flowstats";
92         try {
93             URL url = new URL(baseURL);
94
95             this.users.getAuthorizationList();
96             this.users.authenticate("admin", "admin");
97             String authString = "admin:admin";
98             byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
99             String authStringEnc = new String(authEncBytes);
100             URLConnection connection = url.openConnection();
101             connection.setRequestProperty("Authorization", "Basic "
102                     + authStringEnc);
103
104             connection.setRequestProperty("Content-Type", "application/json");
105             connection.setRequestProperty("Accept", "application/json");
106
107             connection.connect();
108             connection.getContentType();
109             InputStream is = connection.getInputStream();
110             // InputStream is = connection.getInputStream();
111             BufferedReader rd = new BufferedReader(new InputStreamReader(is,
112                     Charset.forName("UTF-8")));
113             StringBuilder sb = new StringBuilder();
114             int cp;
115             while ((cp = rd.read()) != -1) {
116                 sb.append((char) cp);
117             }
118             is.close();
119             JSONTokener jt = new JSONTokener(sb.toString());
120             JSONObject json = new JSONObject(jt);
121             JSONObject flowStatistics = json.getJSONObject("flowStatistics");
122             JSONObject node = flowStatistics.getJSONObject("node");
123             // test that node was returned properly
124             Assert.assertTrue(node.getInt("@id") == 0xCAFE);
125             Assert.assertTrue(node.getString("@type").equals("STUB"));
126
127             // test that flow statistics results are correct
128             JSONObject flowStat = flowStatistics.getJSONObject("flowStat");
129             Assert.assertTrue(flowStat.getInt("tableId") == 1);
130             Assert.assertTrue(flowStat.getInt("durationSeconds") == 40);
131             Assert.assertTrue(flowStat.getInt("durationNanoseconds") == 400);
132             Assert.assertTrue(flowStat.getInt("packetCount") == 200);
133             Assert.assertTrue(flowStat.getInt("byteCount") == 100);
134
135             // test that flow information is correct
136             JSONObject flow = flowStat.getJSONObject("flow");
137             Assert.assertTrue(flow.getInt("priority") == 3500);
138             Assert.assertTrue(flow.getInt("idleTimeout") == 1000);
139             Assert.assertTrue(flow.getInt("hardTimeout") == 2000);
140             Assert.assertTrue(flow.getInt("id") == 12345);
141
142             JSONObject match = (flow.getJSONObject("match")
143                     .getJSONObject("matchField"));
144             Assert.assertTrue(match.getString("type").equals("NW_DST"));
145             Assert.assertTrue(match.getString("value").equals("1.1.1.1"));
146
147             Assert.assertTrue(flow.getJSONObject("actions").getString("@type")
148                     .equals("drop"));
149
150         } catch (Exception e) {
151             // Got an unexpected exception
152             Assert.assertTrue(false);
153
154         }
155     }
156
157     // Configure the OSGi container
158     @Configuration
159     public Option[] config() {
160         return options(
161                 //
162                 systemProperty("logback.configurationFile").value(
163                         "file:" + PathUtils.getBaseDir()
164                                 + "/src/test/resources/logback.xml"),
165                 // To start OSGi console for inspection remotely
166                 systemProperty("osgi.console").value("2401"),
167                 systemProperty("org.eclipse.gemini.web.tomcat.config.path")
168                         .value(PathUtils.getBaseDir()
169                                 + "/src/test/resources/tomcat-server.xml"),
170
171                 // setting default level. Jersey bundles will need to be started
172                 // earlier.
173                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
174
175                 // Set the systemPackages (used by clustering)
176                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
177                 mavenBundle("javax.servlet", "servlet-api", "2.5"),
178
179                 mavenBundle("org.slf4j", "jcl-over-slf4j", "1.7.2"),
180                 mavenBundle("org.slf4j", "slf4j-api", "1.7.2"),
181                 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
182                 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
183                 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
184                 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
185                 mavenBundle("org.apache.felix",
186                         "org.apache.felix.dependencymanager", "3.1.0"),
187
188                 // the plugin stub to get data for the tests
189                 mavenBundle("org.opendaylight.controller",
190                         "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
191
192                 // List all the opendaylight modules
193                 mavenBundle("org.opendaylight.controller", "security",
194                         "0.4.0-SNAPSHOT").noStart(),
195                 mavenBundle("org.opendaylight.controller", "sal",
196                         "0.5.0-SNAPSHOT"),
197                 mavenBundle("org.opendaylight.controller",
198                         "sal.implementation", "0.4.0-SNAPSHOT"),
199                 mavenBundle("org.opendaylight.controller", "statisticsmanager",
200                         "0.4.0-SNAPSHOT"),
201                 mavenBundle("org.opendaylight.controller",
202                         "statisticsmanager.implementation", "0.4.0-SNAPSHOT"),
203                 mavenBundle("org.opendaylight.controller", "containermanager",
204                         "0.4.0-SNAPSHOT"),
205                 mavenBundle("org.opendaylight.controller",
206                         "containermanager.implementation", "0.4.0-SNAPSHOT"),
207                 mavenBundle("org.opendaylight.controller",
208                         "forwardingrulesmanager", "0.4.0-SNAPSHOT"),
209                 mavenBundle("org.opendaylight.controller",
210                         "forwardingrulesmanager.implementation",
211                         "0.4.0-SNAPSHOT"),
212                 mavenBundle("org.opendaylight.controller", "arphandler",
213                         "0.4.0-SNAPSHOT"),
214                 mavenBundle("org.opendaylight.controller",
215                         "clustering.services", "0.4.0-SNAPSHOT"),
216                 mavenBundle("org.opendaylight.controller",
217                         "clustering.services-implementation", "0.4.0-SNAPSHOT"),
218                 mavenBundle("org.opendaylight.controller", "switchmanager",
219                         "0.4.0-SNAPSHOT"),
220                 mavenBundle("org.opendaylight.controller",
221                         "switchmanager.implementation", "0.4.0-SNAPSHOT"),
222                 mavenBundle("org.opendaylight.controller", "configuration",
223                         "0.4.0-SNAPSHOT"),
224                 mavenBundle("org.opendaylight.controller",
225                         "configuration.implementation", "0.4.0-SNAPSHOT"),
226                 mavenBundle("org.opendaylight.controller", "hosttracker",
227                         "0.4.0-SNAPSHOT"),
228                 mavenBundle("org.opendaylight.controller",
229                         "hosttracker.implementation", "0.4.0-SNAPSHOT"),
230                 mavenBundle("org.opendaylight.controller", "arphandler",
231                         "0.4.0-SNAPSHOT"),
232                 mavenBundle("org.opendaylight.controller",
233                         "routing.dijkstra_implementation", "0.4.0-SNAPSHOT"),
234                 mavenBundle("org.opendaylight.controller", "topologymanager",
235                         "0.4.0-SNAPSHOT"),
236
237                 mavenBundle("org.opendaylight.controller", "usermanager",
238                         "0.4.0-SNAPSHOT"),
239                 mavenBundle("org.opendaylight.controller", "logging.bridge",
240                         "0.4.0-SNAPSHOT"),
241                 mavenBundle("org.opendaylight.controller", "clustering.test",
242                         "0.4.0-SNAPSHOT"),
243
244                 mavenBundle("org.opendaylight.controller",
245                         "forwarding.staticrouting", "0.4.0-SNAPSHOT"),
246
247                 // Northbound bundles
248                 mavenBundle("org.opendaylight.controller",
249                         "commons.northbound", "0.4.0-SNAPSHOT"),
250                 mavenBundle("org.opendaylight.controller",
251                         "forwarding.staticrouting.northbound", "0.4.0-SNAPSHOT"),
252                 mavenBundle("org.opendaylight.controller",
253                         "statistics.northbound", "0.4.0-SNAPSHOT"),
254                 mavenBundle("org.opendaylight.controller",
255                         "topology.northbound", "0.4.0-SNAPSHOT"),
256                 mavenBundle("org.opendaylight.controller",
257                         "hosttracker.northbound", "0.4.0-SNAPSHOT"),
258                 mavenBundle("org.opendaylight.controller",
259                         "switchmanager.northbound", "0.4.0-SNAPSHOT"),
260                 mavenBundle("org.opendaylight.controller",
261                         "flowprogrammer.northbound", "0.4.0-SNAPSHOT"),
262                 mavenBundle("org.opendaylight.controller",
263                         "subnets.northbound", "0.4.0-SNAPSHOT"),
264
265                 mavenBundle("org.codehaus.jackson", "jackson-mapper-asl",
266                         "1.9.8"),
267                 mavenBundle("org.codehaus.jackson", "jackson-core-asl", "1.9.8"),
268                 mavenBundle("org.codehaus.jackson", "jackson-jaxrs", "1.9.8"),
269                 mavenBundle("org.codehaus.jettison", "jettison", "1.3.3"),
270
271                 mavenBundle("commons-io", "commons-io", "2.3"),
272
273                 mavenBundle("commons-fileupload", "commons-fileupload", "1.2.2"),
274
275                 mavenBundle("equinoxSDK381", "javax.servlet",
276                         "3.0.0.v201112011016"),
277                 mavenBundle("equinoxSDK381", "javax.servlet.jsp",
278                         "2.2.0.v201112011158"),
279                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
280                         "1.4.0.v20120522-1841"),
281                 mavenBundle("orbit", "javax.xml.rpc", "1.1.0.v201005080400"),
282                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
283                         "1.0.400.v20120522-2049"),
284                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
285                         "3.3.100.v20120522-1822"),
286                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
287                         "0.8.0.v201108120515"),
288                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
289                         "0.8.0.v201108120515"),
290                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
291                         "0.8.0.v201110170705"),
292                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.cm",
293                         "1.0.400.v20120522-1841"),
294                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
295                         "1.0.0.v20120522-1841"),
296                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.launcher",
297                         "1.3.0.v20120522-1813"),
298
299                 mavenBundle("geminiweb", "org.eclipse.gemini.web.core",
300                         "2.2.0.RELEASE"),
301                 mavenBundle("geminiweb", "org.eclipse.gemini.web.extender",
302                         "2.2.0.RELEASE"),
303                 mavenBundle("geminiweb", "org.eclipse.gemini.web.tomcat",
304                         "2.2.0.RELEASE"),
305                 mavenBundle("geminiweb",
306                         "org.eclipse.virgo.kernel.equinox.extensions",
307                         "3.6.0.RELEASE").noStart(),
308                 mavenBundle("geminiweb", "org.eclipse.virgo.util.common",
309                         "3.6.0.RELEASE"),
310                 mavenBundle("geminiweb", "org.eclipse.virgo.util.io",
311                         "3.6.0.RELEASE"),
312                 mavenBundle("geminiweb", "org.eclipse.virgo.util.math",
313                         "3.6.0.RELEASE"),
314                 mavenBundle("geminiweb", "org.eclipse.virgo.util.osgi",
315                         "3.6.0.RELEASE"),
316                 mavenBundle("geminiweb",
317                         "org.eclipse.virgo.util.osgi.manifest", "3.6.0.RELEASE"),
318                 mavenBundle("geminiweb",
319                         "org.eclipse.virgo.util.parser.manifest",
320                         "3.6.0.RELEASE"),
321
322                 mavenBundle("org.apache.felix",
323                         "org.apache.felix.dependencymanager", "3.1.0"),
324                 mavenBundle("org.apache.felix",
325                         "org.apache.felix.dependencymanager.shell", "3.0.1"),
326
327                 mavenBundle("com.google.code.gson", "gson", "2.1"),
328                 mavenBundle("org.jboss.spec.javax.transaction",
329                         "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
330                 mavenBundle("org.apache.felix", "org.apache.felix.fileinstall",
331                         "3.1.6"),
332                 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
333                 mavenBundle("commons-codec", "commons-codec"),
334                 mavenBundle("virgomirror",
335                         "org.eclipse.jdt.core.compiler.batch",
336                         "3.8.0.I20120518-2145"),
337                 mavenBundle("eclipselink", "javax.persistence",
338                         "2.0.4.v201112161009"),
339
340                 mavenBundle("orbit", "javax.activation", "1.1.0.v201211130549"),
341                 mavenBundle("orbit", "javax.annotation", "1.1.0.v201209060031"),
342                 mavenBundle("orbit", "javax.ejb", "3.1.1.v201204261316"),
343                 mavenBundle("orbit", "javax.el", "2.2.0.v201108011116"),
344                 mavenBundle("orbit", "javax.mail.glassfish",
345                         "1.4.1.v201108011116"),
346                 mavenBundle("orbit", "javax.xml.rpc", "1.1.0.v201005080400"),
347                 mavenBundle("orbit", "org.apache.catalina",
348                         "7.0.32.v201211201336"),
349                 // these are bundle fragments that can't be started on its own
350                 mavenBundle("orbit", "org.apache.catalina.ha",
351                         "7.0.32.v201211201952").noStart(),
352                 mavenBundle("orbit", "org.apache.catalina.tribes",
353                         "7.0.32.v201211201952").noStart(),
354                 mavenBundle("orbit", "org.apache.coyote",
355                         "7.0.32.v201211201952").noStart(),
356                 mavenBundle("orbit", "org.apache.jasper",
357                         "7.0.32.v201211201952").noStart(),
358
359                 mavenBundle("orbit", "org.apache.el", "7.0.32.v201211081135"),
360                 mavenBundle("orbit", "org.apache.juli.extras",
361                         "7.0.32.v201211081135"),
362                 mavenBundle("orbit", "org.apache.tomcat.api",
363                         "7.0.32.v201211081135"),
364                 mavenBundle("orbit", "org.apache.tomcat.util",
365                         "7.0.32.v201211201952").noStart(),
366                 mavenBundle("orbit", "javax.servlet.jsp.jstl",
367                         "1.2.0.v201105211821"),
368                 mavenBundle("orbit", "javax.servlet.jsp.jstl.impl",
369                         "1.2.0.v201210211230"),
370
371                 mavenBundle("org.ops4j.pax.exam", "pax-exam-container-native"),
372                 mavenBundle("org.ops4j.pax.exam", "pax-exam-junit4"),
373                 mavenBundle("org.ops4j.pax.exam", "pax-exam-link-mvn"),
374                 mavenBundle("org.ops4j.pax.url", "pax-url-aether"),
375
376                 mavenBundle("org.springframework", "org.springframework.asm",
377                         "3.1.3.RELEASE"),
378                 mavenBundle("org.springframework", "org.springframework.aop",
379                         "3.1.3.RELEASE"),
380                 mavenBundle("org.springframework",
381                         "org.springframework.context", "3.1.3.RELEASE"),
382                 mavenBundle("org.springframework",
383                         "org.springframework.context.support", "3.1.3.RELEASE"),
384                 mavenBundle("org.springframework", "org.springframework.core",
385                         "3.1.3.RELEASE"),
386                 mavenBundle("org.springframework", "org.springframework.beans",
387                         "3.1.3.RELEASE"),
388                 mavenBundle("org.springframework",
389                         "org.springframework.expression", "3.1.3.RELEASE"),
390                 mavenBundle("org.springframework", "org.springframework.web",
391                         "3.1.3.RELEASE"),
392
393                 mavenBundle("org.aopalliance",
394                         "com.springsource.org.aopalliance", "1.0.0"),
395                 mavenBundle("org.springframework",
396                         "org.springframework.web.servlet", "3.1.3.RELEASE"),
397                 mavenBundle("org.springframework.security",
398                         "spring-security-config", "3.1.3.RELEASE"),
399                 mavenBundle("org.springframework.security",
400                         "spring-security-core", "3.1.3.RELEASE"),
401                 mavenBundle("org.springframework.security",
402                         "spring-security-web", "3.1.3.RELEASE"),
403                 mavenBundle("org.springframework.security",
404                         "spring-security-taglibs", "3.1.3.RELEASE"),
405                 mavenBundle("org.springframework",
406                         "org.springframework.transaction", "3.1.3.RELEASE"),
407
408                 mavenBundle("org.ow2.chameleon.management", "chameleon-mbeans",
409                         "1.0.0"),
410                 mavenBundle("org.opendaylight.controller.thirdparty",
411                         "net.sf.jung2", "2.0.1-SNAPSHOT"),
412                 mavenBundle("org.opendaylight.controller.thirdparty",
413                         "com.sun.jersey.jersey-servlet", "1.17-SNAPSHOT"),
414
415                 // Jersey needs to be started before the northbound application
416                 // bundles, using a lower start level
417                 mavenBundle("com.sun.jersey", "jersey-client", "1.17"),
418                 mavenBundle("com.sun.jersey", "jersey-server", "1.17")
419                         .startLevel(2),
420                 mavenBundle("com.sun.jersey", "jersey-core", "1.17")
421                         .startLevel(2),
422                 mavenBundle("com.sun.jersey", "jersey-json", "1.17")
423                         .startLevel(2), junitBundles());
424     }
425 }