1 package org.opendaylight.controller.northbound.integrationtest;
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;
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.MalformedURLException;
24 import java.net.URLConnection;
25 import java.nio.charset.Charset;
26 import java.util.Arrays;
28 import org.apache.commons.codec.binary.Base64;
30 import org.codehaus.jettison.json.JSONArray;
31 import org.codehaus.jettison.json.JSONObject;
32 import org.codehaus.jettison.json.JSONTokener;
34 import org.opendaylight.controller.usermanager.IUserManager;
36 @RunWith(PaxExam.class)
37 public class NorthboundIntegrationTest {
38 private Logger log = LoggerFactory
39 .getLogger(NorthboundIntegrationTest.class);
40 // get the OSGI bundle context
42 private BundleContext bc;
43 private IUserManager users = null;
45 private String stateToString(int state) {
49 case Bundle.INSTALLED:
53 case Bundle.UNINSTALLED:
56 return "Not CONVERTED";
61 public void areWeReady() {
63 boolean debugit = false;
64 Bundle b[] = bc.getBundles();
65 for (int i = 0; i < b.length; i++) {
66 int state = b[i].getState();
67 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
68 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
69 + stateToString(state));
74 log.debug("Do some debugging because some bundle is "
77 // Assert if true, if false we are good to go!
80 ServiceReference r = bc.getServiceReference(IUserManager.class
83 this.users = (IUserManager) bc.getService(r);
85 // If UserManager is null, cannot login to run tests.
86 assertNotNull(this.users);
90 private String getJsonResult(String restUrl) {
92 URL url = new URL(restUrl);
94 this.users.getAuthorizationList();
95 this.users.authenticate("admin", "admin");
96 String authString = "admin:admin";
97 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
98 String authStringEnc = new String(authEncBytes);
99 URLConnection connection = url.openConnection();
100 connection.setRequestProperty("Authorization", "Basic "
103 connection.setRequestProperty("Content-Type", "application/json");
104 connection.setRequestProperty("Accept", "application/json");
106 connection.connect();
107 connection.getContentType();
108 InputStream is = connection.getInputStream();
109 // InputStream is = connection.getInputStream();
110 BufferedReader rd = new BufferedReader(new InputStreamReader(is,
111 Charset.forName("UTF-8")));
112 StringBuilder sb = new StringBuilder();
114 while ((cp = rd.read()) != -1) {
115 sb.append((char) cp);
118 return sb.toString();
119 } catch (Exception e) {
125 public void testStatistics() {
127 System.out.println("Starting Statistics JAXB client.");
129 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/statistics/default/";
131 String actionTypes[] = { "drop", "loopback", "flood", "floodAll",
132 "controller", "swPath", "hwPath", "output", "setDlSrc",
133 "setDlDst", "setDlType", "setVlanId", "setVlanPcp",
134 "setVlanCfi", "popVlan", "pushVlan", "setNwSrc",
135 "setNwDst", "setNwTos", "setTpSrc", "setTpDst" };
136 String result = getJsonResult(baseURL + "flowstats");
137 JSONTokener jt = new JSONTokener(result);
138 JSONObject json = new JSONObject(jt);
139 JSONObject flowStatistics = json.getJSONObject("flowStatistics");
140 JSONObject node = flowStatistics.getJSONObject("node");
141 // test that node was returned properly
142 Assert.assertTrue(node.getInt("@id") == 0xCAFE);
143 Assert.assertTrue(node.getString("@type").equals("STUB"));
145 // test that flow statistics results are correct
146 JSONArray flowStats = flowStatistics.getJSONArray("flowStat");
147 for (int i = 0; i < flowStats.length(); i++) {
149 JSONObject flowStat = flowStats.getJSONObject(i);
150 Assert.assertTrue(flowStat.getInt("tableId") == 1);
151 Assert.assertTrue(flowStat.getInt("durationSeconds") == 40);
152 Assert.assertTrue(flowStat.getInt("durationNanoseconds") == 400);
153 Assert.assertTrue(flowStat.getInt("packetCount") == 200);
154 Assert.assertTrue(flowStat.getInt("byteCount") == 100);
156 // test that flow information is correct
157 JSONObject flow = flowStat.getJSONObject("flow");
158 Assert.assertTrue(flow.getInt("priority") == 3500);
159 Assert.assertTrue(flow.getInt("idleTimeout") == 1000);
160 Assert.assertTrue(flow.getInt("hardTimeout") == 2000);
161 Assert.assertTrue(flow.getInt("id") == 12345);
163 JSONObject match = (flow.getJSONObject("match")
164 .getJSONObject("matchField"));
165 Assert.assertTrue(match.getString("type").equals("NW_DST"));
166 Assert.assertTrue(match.getString("value").equals("1.1.1.1"));
168 JSONObject act = flow.getJSONObject("actions");
169 Assert.assertTrue(act.getString("@type").equals(actionTypes[i]));
171 if (act.getString("@type").equals("output")) {
172 JSONObject port = act.getJSONObject("port");
173 JSONObject port_node = port.getJSONObject("node");
174 Assert.assertTrue(port.getInt("@id") == 51966);
175 Assert.assertTrue(port.getString("@type").equals("STUB"));
176 Assert.assertTrue(port_node.getInt("@id") == 51966);
177 Assert.assertTrue(port_node.getString("@type").equals(
181 if (act.getString("@type").equals("setDlSrc")) {
182 byte srcMatch[] = { (byte) 5, (byte) 4, (byte) 3, (byte) 2,
184 String src = act.getString("address");
185 byte srcBytes[] = new byte[5];
186 srcBytes[0] = Byte.parseByte(src.substring(0, 2));
187 srcBytes[1] = Byte.parseByte(src.substring(2, 4));
188 srcBytes[2] = Byte.parseByte(src.substring(4, 6));
189 srcBytes[3] = Byte.parseByte(src.substring(6, 8));
190 srcBytes[4] = Byte.parseByte(src.substring(8, 10));
191 Assert.assertTrue(Arrays.equals(srcBytes, srcMatch));
194 if (act.getString("@type").equals("setDlDst")) {
195 byte dstMatch[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4,
197 String dst = act.getString("address");
198 byte dstBytes[] = new byte[5];
199 dstBytes[0] = Byte.parseByte(dst.substring(0, 2));
200 dstBytes[1] = Byte.parseByte(dst.substring(2, 4));
201 dstBytes[2] = Byte.parseByte(dst.substring(4, 6));
202 dstBytes[3] = Byte.parseByte(dst.substring(6, 8));
203 dstBytes[4] = Byte.parseByte(dst.substring(8, 10));
204 Assert.assertTrue(Arrays.equals(dstBytes, dstMatch));
206 if (act.getString("@type").equals("setDlType"))
207 Assert.assertTrue(act.getInt("dlType") == 10);
208 if (act.getString("@type").equals("setVlanId"))
209 Assert.assertTrue(act.getInt("vlanId") == 2);
210 if (act.getString("@type").equals("setVlanPcp"))
211 Assert.assertTrue(act.getInt("pcp") == 3);
212 if (act.getString("@type").equals("setVlanCfi"))
213 Assert.assertTrue(act.getInt("cfi") == 1);
215 if (act.getString("@type").equals("setNwSrc"))
216 Assert.assertTrue(act.getString("address")
218 if (act.getString("@type").equals("setNwDst"))
219 Assert.assertTrue(act.getString("address")
222 if (act.getString("@type").equals("pushVlan")) {
223 int head = act.getInt("VlanHeader");
224 // parsing vlan header
225 int id = head & 0xfff;
226 int cfi = (head >> 12) & 0x1;
227 int pcp = (head >> 13) & 0x7;
228 int tag = (head >> 16) & 0xffff;
229 Assert.assertTrue(id == 1234);
230 Assert.assertTrue(cfi == 1);
231 Assert.assertTrue(pcp == 1);
232 Assert.assertTrue(tag == 0x8100);
234 if (act.getString("@type").equals("setNwTos"))
235 Assert.assertTrue(act.getInt("tos") == 16);
236 if (act.getString("@type").equals("setTpSrc"))
237 Assert.assertTrue(act.getInt("port") == 4201);
238 if (act.getString("@type").equals("setTpDst"))
239 Assert.assertTrue(act.getInt("port") == 8080);
242 // for /controller/nb/v2/statistics/default/portstats
243 result = getJsonResult(baseURL + "portstats");
244 jt = new JSONTokener(result);
245 json = new JSONObject(jt);
246 JSONObject portStatistics = json.getJSONObject("portStatistics");
247 JSONObject node2 = portStatistics.getJSONObject("node");
248 // test that node was returned properly
249 Assert.assertTrue(node2.getInt("@id") == 0xCAFE);
250 Assert.assertTrue(node2.getString("@type").equals("STUB"));
252 // test that port statistic results are correct
253 JSONObject portStat = portStatistics.getJSONObject("portStat");
254 Assert.assertTrue(portStat.getInt("receivePackets") == 250);
255 Assert.assertTrue(portStat.getInt("transmitPackets") == 500);
256 Assert.assertTrue(portStat.getInt("receiveBytes") == 1000);
257 Assert.assertTrue(portStat.getInt("transmitBytes") == 5000);
258 Assert.assertTrue(portStat.getInt("receiveDrops") == 2);
259 Assert.assertTrue(portStat.getInt("transmitDrops") == 50);
260 Assert.assertTrue(portStat.getInt("receiveErrors") == 3);
261 Assert.assertTrue(portStat.getInt("transmitErrors") == 10);
262 Assert.assertTrue(portStat.getInt("receiveFrameError") == 5);
263 Assert.assertTrue(portStat.getInt("receiveOverRunError") == 6);
264 Assert.assertTrue(portStat.getInt("receiveCrcError") == 1);
265 Assert.assertTrue(portStat.getInt("collisionCount") == 4);
267 // String result = getJsonResult(baseURL+"flowstats/STUB/51966");
268 // System.out.println(result);
270 } catch (Exception e) {
271 // Got an unexpected exception
272 Assert.assertTrue(false);
277 // Configure the OSGi container
279 public Option[] config() {
282 systemProperty("logback.configurationFile").value(
283 "file:" + PathUtils.getBaseDir()
284 + "/src/test/resources/logback.xml"),
285 // To start OSGi console for inspection remotely
286 systemProperty("osgi.console").value("2401"),
287 systemProperty("org.eclipse.gemini.web.tomcat.config.path")
288 .value(PathUtils.getBaseDir()
289 + "/src/test/resources/tomcat-server.xml"),
291 // setting default level. Jersey bundles will need to be started
293 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
295 // Set the systemPackages (used by clustering)
296 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
297 mavenBundle("javax.servlet", "servlet-api", "2.5"),
299 mavenBundle("org.slf4j", "jcl-over-slf4j", "1.7.2"),
300 mavenBundle("org.slf4j", "slf4j-api", "1.7.2"),
301 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
302 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
303 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
304 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
305 mavenBundle("org.apache.felix",
306 "org.apache.felix.dependencymanager", "3.1.0"),
308 // the plugin stub to get data for the tests
309 mavenBundle("org.opendaylight.controller",
310 "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
312 // List all the opendaylight modules
313 mavenBundle("org.opendaylight.controller", "security",
314 "0.4.0-SNAPSHOT").noStart(),
315 mavenBundle("org.opendaylight.controller", "sal",
317 mavenBundle("org.opendaylight.controller",
318 "sal.implementation", "0.4.0-SNAPSHOT"),
319 mavenBundle("org.opendaylight.controller", "statisticsmanager",
321 mavenBundle("org.opendaylight.controller",
322 "statisticsmanager.implementation", "0.4.0-SNAPSHOT"),
323 mavenBundle("org.opendaylight.controller", "containermanager",
325 mavenBundle("org.opendaylight.controller",
326 "containermanager.implementation", "0.4.0-SNAPSHOT"),
327 mavenBundle("org.opendaylight.controller",
328 "forwardingrulesmanager", "0.4.0-SNAPSHOT"),
329 mavenBundle("org.opendaylight.controller",
330 "forwardingrulesmanager.implementation",
332 mavenBundle("org.opendaylight.controller", "arphandler",
334 mavenBundle("org.opendaylight.controller",
335 "clustering.services", "0.4.0-SNAPSHOT"),
336 mavenBundle("org.opendaylight.controller",
337 "clustering.services-implementation", "0.4.0-SNAPSHOT"),
338 mavenBundle("org.opendaylight.controller", "switchmanager",
340 mavenBundle("org.opendaylight.controller",
341 "switchmanager.implementation", "0.4.0-SNAPSHOT"),
342 mavenBundle("org.opendaylight.controller", "configuration",
344 mavenBundle("org.opendaylight.controller",
345 "configuration.implementation", "0.4.0-SNAPSHOT"),
346 mavenBundle("org.opendaylight.controller", "hosttracker",
348 mavenBundle("org.opendaylight.controller",
349 "hosttracker.implementation", "0.4.0-SNAPSHOT"),
350 mavenBundle("org.opendaylight.controller", "arphandler",
352 mavenBundle("org.opendaylight.controller",
353 "routing.dijkstra_implementation", "0.4.0-SNAPSHOT"),
354 mavenBundle("org.opendaylight.controller", "topologymanager",
357 mavenBundle("org.opendaylight.controller", "usermanager",
359 mavenBundle("org.opendaylight.controller", "logging.bridge",
361 mavenBundle("org.opendaylight.controller", "clustering.test",
364 mavenBundle("org.opendaylight.controller",
365 "forwarding.staticrouting", "0.4.0-SNAPSHOT"),
367 // Northbound bundles
368 mavenBundle("org.opendaylight.controller",
369 "commons.northbound", "0.4.0-SNAPSHOT"),
370 mavenBundle("org.opendaylight.controller",
371 "forwarding.staticrouting.northbound", "0.4.0-SNAPSHOT"),
372 mavenBundle("org.opendaylight.controller",
373 "statistics.northbound", "0.4.0-SNAPSHOT"),
374 mavenBundle("org.opendaylight.controller",
375 "topology.northbound", "0.4.0-SNAPSHOT"),
376 mavenBundle("org.opendaylight.controller",
377 "hosttracker.northbound", "0.4.0-SNAPSHOT"),
378 mavenBundle("org.opendaylight.controller",
379 "switchmanager.northbound", "0.4.0-SNAPSHOT"),
380 mavenBundle("org.opendaylight.controller",
381 "flowprogrammer.northbound", "0.4.0-SNAPSHOT"),
382 mavenBundle("org.opendaylight.controller",
383 "subnets.northbound", "0.4.0-SNAPSHOT"),
385 mavenBundle("org.codehaus.jackson", "jackson-mapper-asl",
387 mavenBundle("org.codehaus.jackson", "jackson-core-asl", "1.9.8"),
388 mavenBundle("org.codehaus.jackson", "jackson-jaxrs", "1.9.8"),
389 mavenBundle("org.codehaus.jettison", "jettison", "1.3.3"),
391 mavenBundle("commons-io", "commons-io", "2.3"),
393 mavenBundle("commons-fileupload", "commons-fileupload", "1.2.2"),
395 mavenBundle("equinoxSDK381", "javax.servlet",
396 "3.0.0.v201112011016"),
397 mavenBundle("equinoxSDK381", "javax.servlet.jsp",
398 "2.2.0.v201112011158"),
399 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
400 "1.4.0.v20120522-1841"),
401 mavenBundle("orbit", "javax.xml.rpc", "1.1.0.v201005080400"),
402 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
403 "1.0.400.v20120522-2049"),
404 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
405 "3.3.100.v20120522-1822"),
406 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
407 "0.8.0.v201108120515"),
408 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
409 "0.8.0.v201108120515"),
410 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
411 "0.8.0.v201110170705"),
412 mavenBundle("equinoxSDK381", "org.eclipse.equinox.cm",
413 "1.0.400.v20120522-1841"),
414 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
415 "1.0.0.v20120522-1841"),
416 mavenBundle("equinoxSDK381", "org.eclipse.equinox.launcher",
417 "1.3.0.v20120522-1813"),
419 mavenBundle("geminiweb", "org.eclipse.gemini.web.core",
421 mavenBundle("geminiweb", "org.eclipse.gemini.web.extender",
423 mavenBundle("geminiweb", "org.eclipse.gemini.web.tomcat",
425 mavenBundle("geminiweb",
426 "org.eclipse.virgo.kernel.equinox.extensions",
427 "3.6.0.RELEASE").noStart(),
428 mavenBundle("geminiweb", "org.eclipse.virgo.util.common",
430 mavenBundle("geminiweb", "org.eclipse.virgo.util.io",
432 mavenBundle("geminiweb", "org.eclipse.virgo.util.math",
434 mavenBundle("geminiweb", "org.eclipse.virgo.util.osgi",
436 mavenBundle("geminiweb",
437 "org.eclipse.virgo.util.osgi.manifest", "3.6.0.RELEASE"),
438 mavenBundle("geminiweb",
439 "org.eclipse.virgo.util.parser.manifest",
442 mavenBundle("org.apache.felix",
443 "org.apache.felix.dependencymanager", "3.1.0"),
444 mavenBundle("org.apache.felix",
445 "org.apache.felix.dependencymanager.shell", "3.0.1"),
447 mavenBundle("com.google.code.gson", "gson", "2.1"),
448 mavenBundle("org.jboss.spec.javax.transaction",
449 "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
450 mavenBundle("org.apache.felix", "org.apache.felix.fileinstall",
452 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
453 mavenBundle("commons-codec", "commons-codec"),
454 mavenBundle("virgomirror",
455 "org.eclipse.jdt.core.compiler.batch",
456 "3.8.0.I20120518-2145"),
457 mavenBundle("eclipselink", "javax.persistence",
458 "2.0.4.v201112161009"),
460 mavenBundle("orbit", "javax.activation", "1.1.0.v201211130549"),
461 mavenBundle("orbit", "javax.annotation", "1.1.0.v201209060031"),
462 mavenBundle("orbit", "javax.ejb", "3.1.1.v201204261316"),
463 mavenBundle("orbit", "javax.el", "2.2.0.v201108011116"),
464 mavenBundle("orbit", "javax.mail.glassfish",
465 "1.4.1.v201108011116"),
466 mavenBundle("orbit", "javax.xml.rpc", "1.1.0.v201005080400"),
467 mavenBundle("orbit", "org.apache.catalina",
468 "7.0.32.v201211201336"),
469 // these are bundle fragments that can't be started on its own
470 mavenBundle("orbit", "org.apache.catalina.ha",
471 "7.0.32.v201211201952").noStart(),
472 mavenBundle("orbit", "org.apache.catalina.tribes",
473 "7.0.32.v201211201952").noStart(),
474 mavenBundle("orbit", "org.apache.coyote",
475 "7.0.32.v201211201952").noStart(),
476 mavenBundle("orbit", "org.apache.jasper",
477 "7.0.32.v201211201952").noStart(),
479 mavenBundle("orbit", "org.apache.el", "7.0.32.v201211081135"),
480 mavenBundle("orbit", "org.apache.juli.extras",
481 "7.0.32.v201211081135"),
482 mavenBundle("orbit", "org.apache.tomcat.api",
483 "7.0.32.v201211081135"),
484 mavenBundle("orbit", "org.apache.tomcat.util",
485 "7.0.32.v201211201952").noStart(),
486 mavenBundle("orbit", "javax.servlet.jsp.jstl",
487 "1.2.0.v201105211821"),
488 mavenBundle("orbit", "javax.servlet.jsp.jstl.impl",
489 "1.2.0.v201210211230"),
491 mavenBundle("org.ops4j.pax.exam", "pax-exam-container-native"),
492 mavenBundle("org.ops4j.pax.exam", "pax-exam-junit4"),
493 mavenBundle("org.ops4j.pax.exam", "pax-exam-link-mvn"),
494 mavenBundle("org.ops4j.pax.url", "pax-url-aether"),
496 mavenBundle("org.springframework", "org.springframework.asm",
498 mavenBundle("org.springframework", "org.springframework.aop",
500 mavenBundle("org.springframework",
501 "org.springframework.context", "3.1.3.RELEASE"),
502 mavenBundle("org.springframework",
503 "org.springframework.context.support", "3.1.3.RELEASE"),
504 mavenBundle("org.springframework", "org.springframework.core",
506 mavenBundle("org.springframework", "org.springframework.beans",
508 mavenBundle("org.springframework",
509 "org.springframework.expression", "3.1.3.RELEASE"),
510 mavenBundle("org.springframework", "org.springframework.web",
513 mavenBundle("org.aopalliance",
514 "com.springsource.org.aopalliance", "1.0.0"),
515 mavenBundle("org.springframework",
516 "org.springframework.web.servlet", "3.1.3.RELEASE"),
517 mavenBundle("org.springframework.security",
518 "spring-security-config", "3.1.3.RELEASE"),
519 mavenBundle("org.springframework.security",
520 "spring-security-core", "3.1.3.RELEASE"),
521 mavenBundle("org.springframework.security",
522 "spring-security-web", "3.1.3.RELEASE"),
523 mavenBundle("org.springframework.security",
524 "spring-security-taglibs", "3.1.3.RELEASE"),
525 mavenBundle("org.springframework",
526 "org.springframework.transaction", "3.1.3.RELEASE"),
528 mavenBundle("org.ow2.chameleon.management", "chameleon-mbeans",
530 mavenBundle("org.opendaylight.controller.thirdparty",
531 "net.sf.jung2", "2.0.1-SNAPSHOT"),
532 mavenBundle("org.opendaylight.controller.thirdparty",
533 "com.sun.jersey.jersey-servlet", "1.17-SNAPSHOT"),
535 // Jersey needs to be started before the northbound application
536 // bundles, using a lower start level
537 mavenBundle("com.sun.jersey", "jersey-client", "1.17"),
538 mavenBundle("com.sun.jersey", "jersey-server", "1.17")
540 mavenBundle("com.sun.jersey", "jersey-core", "1.17")
542 mavenBundle("com.sun.jersey", "jersey-json", "1.17")
543 .startLevel(2), junitBundles());