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.HttpURLConnection;
23 import java.net.MalformedURLException;
25 import java.net.URLConnection;
26 import java.nio.charset.Charset;
27 import java.util.Arrays;
29 import org.apache.commons.codec.binary.Base64;
31 import org.codehaus.jettison.json.JSONArray;
32 import org.codehaus.jettison.json.JSONObject;
33 import org.codehaus.jettison.json.JSONTokener;
35 import org.opendaylight.controller.usermanager.IUserManager;
37 @RunWith(PaxExam.class)
38 public class NorthboundIntegrationTest {
39 private Logger log = LoggerFactory
40 .getLogger(NorthboundIntegrationTest.class);
41 // get the OSGI bundle context
43 private BundleContext bc;
44 private IUserManager users = null;
46 private String stateToString(int state) {
50 case Bundle.INSTALLED:
54 case Bundle.UNINSTALLED:
57 return "Not CONVERTED";
62 public void areWeReady() {
64 boolean debugit = false;
65 Bundle b[] = bc.getBundles();
66 for (int i = 0; i < b.length; i++) {
67 int state = b[i].getState();
68 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
69 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
70 + stateToString(state));
75 log.debug("Do some debugging because some bundle is "
78 // Assert if true, if false we are good to go!
81 ServiceReference r = bc.getServiceReference(IUserManager.class
84 this.users = (IUserManager) bc.getService(r);
86 // If UserManager is null, cannot login to run tests.
87 assertNotNull(this.users);
91 // static variable to pass response code from getJsonResult()
92 private static Integer httpResponseCode = null;
94 private String getJsonResult(String restUrl) {
95 return getJsonResult(restUrl, "GET");
98 private String getJsonResult(String restUrl, String method) {
99 // initialize response code to indicate error
100 httpResponseCode = 400;
103 URL url = new URL(restUrl);
105 this.users.getAuthorizationList();
106 this.users.authenticate("admin", "admin");
107 String authString = "admin:admin";
108 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
109 String authStringEnc = new String(authEncBytes);
111 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
112 connection.setRequestMethod(method);
113 connection.setRequestProperty("Authorization", "Basic "
115 connection.setRequestProperty("Content-Type", "application/json");
116 connection.setRequestProperty("Accept", "application/json");
118 connection.connect();
119 connection.getContentType();
121 // Response code for success should be 2xx
122 httpResponseCode = connection.getResponseCode();
124 InputStream is = connection.getInputStream();
125 BufferedReader rd = new BufferedReader(new InputStreamReader(is,
126 Charset.forName("UTF-8")));
127 StringBuilder sb = new StringBuilder();
129 while ((cp = rd.read()) != -1) {
130 sb.append((char) cp);
133 connection.disconnect();
134 return sb.toString();
135 } catch (Exception e) {
142 public void testStatistics() {
144 System.out.println("Starting Statistics JAXB client.");
146 String baseURL = "http://127.0.0.1:8080/controller/nb/v2/statistics/default/";
148 String actionTypes[] = { "drop", "loopback", "flood", "floodAll",
149 "controller", "swPath", "hwPath", "output", "setDlSrc",
150 "setDlDst", "setDlType", "setVlanId", "setVlanPcp",
151 "setVlanCfi", "popVlan", "pushVlan", "setNwSrc",
152 "setNwDst", "setNwTos", "setTpSrc", "setTpDst" };
153 String result = getJsonResult(baseURL + "flowstats");
154 JSONTokener jt = new JSONTokener(result);
155 JSONObject json = new JSONObject(jt);
156 JSONObject flowStatistics = json.getJSONObject("flowStatistics");
157 JSONObject node = flowStatistics.getJSONObject("node");
158 // test that node was returned properly
159 Assert.assertTrue(node.getInt("@id") == 0xCAFE);
160 Assert.assertTrue(node.getString("@type").equals("STUB"));
162 // test that flow statistics results are correct
163 JSONArray flowStats = flowStatistics.getJSONArray("flowStat");
164 for (int i = 0; i < flowStats.length(); i++) {
166 JSONObject flowStat = flowStats.getJSONObject(i);
167 Assert.assertTrue(flowStat.getInt("tableId") == 1);
168 Assert.assertTrue(flowStat.getInt("durationSeconds") == 40);
169 Assert.assertTrue(flowStat.getInt("durationNanoseconds") == 400);
170 Assert.assertTrue(flowStat.getInt("packetCount") == 200);
171 Assert.assertTrue(flowStat.getInt("byteCount") == 100);
173 // test that flow information is correct
174 JSONObject flow = flowStat.getJSONObject("flow");
175 Assert.assertTrue(flow.getInt("priority") == 3500);
176 Assert.assertTrue(flow.getInt("idleTimeout") == 1000);
177 Assert.assertTrue(flow.getInt("hardTimeout") == 2000);
178 Assert.assertTrue(flow.getInt("id") == 12345);
180 JSONObject match = (flow.getJSONObject("match")
181 .getJSONObject("matchField"));
182 Assert.assertTrue(match.getString("type").equals("NW_DST"));
183 Assert.assertTrue(match.getString("value").equals("1.1.1.1"));
185 JSONObject act = flow.getJSONObject("actions");
186 Assert.assertTrue(act.getString("@type").equals(actionTypes[i]));
188 if (act.getString("@type").equals("output")) {
189 JSONObject port = act.getJSONObject("port");
190 JSONObject port_node = port.getJSONObject("node");
191 Assert.assertTrue(port.getInt("@id") == 51966);
192 Assert.assertTrue(port.getString("@type").equals("STUB"));
193 Assert.assertTrue(port_node.getInt("@id") == 51966);
194 Assert.assertTrue(port_node.getString("@type").equals(
198 if (act.getString("@type").equals("setDlSrc")) {
199 byte srcMatch[] = { (byte) 5, (byte) 4, (byte) 3, (byte) 2,
201 String src = act.getString("address");
202 byte srcBytes[] = new byte[5];
203 srcBytes[0] = Byte.parseByte(src.substring(0, 2));
204 srcBytes[1] = Byte.parseByte(src.substring(2, 4));
205 srcBytes[2] = Byte.parseByte(src.substring(4, 6));
206 srcBytes[3] = Byte.parseByte(src.substring(6, 8));
207 srcBytes[4] = Byte.parseByte(src.substring(8, 10));
208 Assert.assertTrue(Arrays.equals(srcBytes, srcMatch));
211 if (act.getString("@type").equals("setDlDst")) {
212 byte dstMatch[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4,
214 String dst = act.getString("address");
215 byte dstBytes[] = new byte[5];
216 dstBytes[0] = Byte.parseByte(dst.substring(0, 2));
217 dstBytes[1] = Byte.parseByte(dst.substring(2, 4));
218 dstBytes[2] = Byte.parseByte(dst.substring(4, 6));
219 dstBytes[3] = Byte.parseByte(dst.substring(6, 8));
220 dstBytes[4] = Byte.parseByte(dst.substring(8, 10));
221 Assert.assertTrue(Arrays.equals(dstBytes, dstMatch));
223 if (act.getString("@type").equals("setDlType"))
224 Assert.assertTrue(act.getInt("dlType") == 10);
225 if (act.getString("@type").equals("setVlanId"))
226 Assert.assertTrue(act.getInt("vlanId") == 2);
227 if (act.getString("@type").equals("setVlanPcp"))
228 Assert.assertTrue(act.getInt("pcp") == 3);
229 if (act.getString("@type").equals("setVlanCfi"))
230 Assert.assertTrue(act.getInt("cfi") == 1);
232 if (act.getString("@type").equals("setNwSrc"))
233 Assert.assertTrue(act.getString("address")
235 if (act.getString("@type").equals("setNwDst"))
236 Assert.assertTrue(act.getString("address")
239 if (act.getString("@type").equals("pushVlan")) {
240 int head = act.getInt("VlanHeader");
241 // parsing vlan header
242 int id = head & 0xfff;
243 int cfi = (head >> 12) & 0x1;
244 int pcp = (head >> 13) & 0x7;
245 int tag = (head >> 16) & 0xffff;
246 Assert.assertTrue(id == 1234);
247 Assert.assertTrue(cfi == 1);
248 Assert.assertTrue(pcp == 1);
249 Assert.assertTrue(tag == 0x8100);
251 if (act.getString("@type").equals("setNwTos"))
252 Assert.assertTrue(act.getInt("tos") == 16);
253 if (act.getString("@type").equals("setTpSrc"))
254 Assert.assertTrue(act.getInt("port") == 4201);
255 if (act.getString("@type").equals("setTpDst"))
256 Assert.assertTrue(act.getInt("port") == 8080);
259 // for /controller/nb/v2/statistics/default/portstats
260 result = getJsonResult(baseURL + "portstats");
261 jt = new JSONTokener(result);
262 json = new JSONObject(jt);
263 JSONObject portStatistics = json.getJSONObject("portStatistics");
264 JSONObject node2 = portStatistics.getJSONObject("node");
265 // test that node was returned properly
266 Assert.assertTrue(node2.getInt("@id") == 0xCAFE);
267 Assert.assertTrue(node2.getString("@type").equals("STUB"));
269 // test that port statistic results are correct
270 JSONObject portStat = portStatistics.getJSONObject("portStat");
271 Assert.assertTrue(portStat.getInt("receivePackets") == 250);
272 Assert.assertTrue(portStat.getInt("transmitPackets") == 500);
273 Assert.assertTrue(portStat.getInt("receiveBytes") == 1000);
274 Assert.assertTrue(portStat.getInt("transmitBytes") == 5000);
275 Assert.assertTrue(portStat.getInt("receiveDrops") == 2);
276 Assert.assertTrue(portStat.getInt("transmitDrops") == 50);
277 Assert.assertTrue(portStat.getInt("receiveErrors") == 3);
278 Assert.assertTrue(portStat.getInt("transmitErrors") == 10);
279 Assert.assertTrue(portStat.getInt("receiveFrameError") == 5);
280 Assert.assertTrue(portStat.getInt("receiveOverRunError") == 6);
281 Assert.assertTrue(portStat.getInt("receiveCrcError") == 1);
282 Assert.assertTrue(portStat.getInt("collisionCount") == 4);
284 // String result = getJsonResult(baseURL+"flowstats/STUB/51966");
285 // System.out.println(result);
287 } catch (Exception e) {
288 // Got an unexpected exception
289 Assert.assertTrue(false);
294 // Configure the OSGi container
296 public Option[] config() {
299 systemProperty("logback.configurationFile").value(
300 "file:" + PathUtils.getBaseDir()
301 + "/src/test/resources/logback.xml"),
302 // To start OSGi console for inspection remotely
303 systemProperty("osgi.console").value("2401"),
304 systemProperty("org.eclipse.gemini.web.tomcat.config.path")
305 .value(PathUtils.getBaseDir()
306 + "/src/test/resources/tomcat-server.xml"),
308 // setting default level. Jersey bundles will need to be started
310 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
312 // Set the systemPackages (used by clustering)
313 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
314 mavenBundle("javax.servlet", "servlet-api", "2.5"),
316 mavenBundle("org.slf4j", "jcl-over-slf4j", "1.7.2"),
317 mavenBundle("org.slf4j", "slf4j-api", "1.7.2"),
318 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
319 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
320 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
321 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
322 mavenBundle("org.apache.felix",
323 "org.apache.felix.dependencymanager", "3.1.0"),
325 // the plugin stub to get data for the tests
326 mavenBundle("org.opendaylight.controller",
327 "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
329 // List all the opendaylight modules
330 mavenBundle("org.opendaylight.controller", "security",
331 "0.4.0-SNAPSHOT").noStart(),
332 mavenBundle("org.opendaylight.controller", "sal",
334 mavenBundle("org.opendaylight.controller",
335 "sal.implementation", "0.4.0-SNAPSHOT"),
336 mavenBundle("org.opendaylight.controller", "statisticsmanager",
338 mavenBundle("org.opendaylight.controller",
339 "statisticsmanager.implementation", "0.4.0-SNAPSHOT"),
340 mavenBundle("org.opendaylight.controller", "containermanager",
342 mavenBundle("org.opendaylight.controller",
343 "containermanager.implementation", "0.4.0-SNAPSHOT"),
344 mavenBundle("org.opendaylight.controller",
345 "forwardingrulesmanager", "0.4.0-SNAPSHOT"),
346 mavenBundle("org.opendaylight.controller",
347 "forwardingrulesmanager.implementation",
349 mavenBundle("org.opendaylight.controller", "arphandler",
351 mavenBundle("org.opendaylight.controller",
352 "clustering.services", "0.4.0-SNAPSHOT"),
353 mavenBundle("org.opendaylight.controller",
354 "clustering.services-implementation", "0.4.0-SNAPSHOT"),
355 mavenBundle("org.opendaylight.controller", "switchmanager",
357 mavenBundle("org.opendaylight.controller",
358 "switchmanager.implementation", "0.4.0-SNAPSHOT"),
359 mavenBundle("org.opendaylight.controller", "configuration",
361 mavenBundle("org.opendaylight.controller",
362 "configuration.implementation", "0.4.0-SNAPSHOT"),
363 mavenBundle("org.opendaylight.controller", "hosttracker",
365 mavenBundle("org.opendaylight.controller",
366 "hosttracker.implementation", "0.4.0-SNAPSHOT"),
367 mavenBundle("org.opendaylight.controller", "arphandler",
369 mavenBundle("org.opendaylight.controller",
370 "routing.dijkstra_implementation", "0.4.0-SNAPSHOT"),
371 mavenBundle("org.opendaylight.controller", "topologymanager",
374 mavenBundle("org.opendaylight.controller", "usermanager",
376 mavenBundle("org.opendaylight.controller", "logging.bridge",
378 mavenBundle("org.opendaylight.controller", "clustering.test",
381 mavenBundle("org.opendaylight.controller",
382 "forwarding.staticrouting", "0.4.0-SNAPSHOT"),
384 // Northbound bundles
385 mavenBundle("org.opendaylight.controller",
386 "commons.northbound", "0.4.0-SNAPSHOT"),
387 mavenBundle("org.opendaylight.controller",
388 "forwarding.staticrouting.northbound", "0.4.0-SNAPSHOT"),
389 mavenBundle("org.opendaylight.controller",
390 "statistics.northbound", "0.4.0-SNAPSHOT"),
391 mavenBundle("org.opendaylight.controller",
392 "topology.northbound", "0.4.0-SNAPSHOT"),
393 mavenBundle("org.opendaylight.controller",
394 "hosttracker.northbound", "0.4.0-SNAPSHOT"),
395 mavenBundle("org.opendaylight.controller",
396 "switchmanager.northbound", "0.4.0-SNAPSHOT"),
397 mavenBundle("org.opendaylight.controller",
398 "flowprogrammer.northbound", "0.4.0-SNAPSHOT"),
399 mavenBundle("org.opendaylight.controller",
400 "subnets.northbound", "0.4.0-SNAPSHOT"),
402 mavenBundle("org.codehaus.jackson", "jackson-mapper-asl",
404 mavenBundle("org.codehaus.jackson", "jackson-core-asl", "1.9.8"),
405 mavenBundle("org.codehaus.jackson", "jackson-jaxrs", "1.9.8"),
406 mavenBundle("org.codehaus.jettison", "jettison", "1.3.3"),
408 mavenBundle("commons-io", "commons-io", "2.3"),
410 mavenBundle("commons-fileupload", "commons-fileupload", "1.2.2"),
412 mavenBundle("equinoxSDK381", "javax.servlet",
413 "3.0.0.v201112011016"),
414 mavenBundle("equinoxSDK381", "javax.servlet.jsp",
415 "2.2.0.v201112011158"),
416 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
417 "1.4.0.v20120522-1841"),
418 mavenBundle("orbit", "javax.xml.rpc", "1.1.0.v201005080400"),
419 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
420 "1.0.400.v20120522-2049"),
421 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
422 "3.3.100.v20120522-1822"),
423 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
424 "0.8.0.v201108120515"),
425 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
426 "0.8.0.v201108120515"),
427 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
428 "0.8.0.v201110170705"),
429 mavenBundle("equinoxSDK381", "org.eclipse.equinox.cm",
430 "1.0.400.v20120522-1841"),
431 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
432 "1.0.0.v20120522-1841"),
433 mavenBundle("equinoxSDK381", "org.eclipse.equinox.launcher",
434 "1.3.0.v20120522-1813"),
436 mavenBundle("geminiweb", "org.eclipse.gemini.web.core",
438 mavenBundle("geminiweb", "org.eclipse.gemini.web.extender",
440 mavenBundle("geminiweb", "org.eclipse.gemini.web.tomcat",
442 mavenBundle("geminiweb",
443 "org.eclipse.virgo.kernel.equinox.extensions",
444 "3.6.0.RELEASE").noStart(),
445 mavenBundle("geminiweb", "org.eclipse.virgo.util.common",
447 mavenBundle("geminiweb", "org.eclipse.virgo.util.io",
449 mavenBundle("geminiweb", "org.eclipse.virgo.util.math",
451 mavenBundle("geminiweb", "org.eclipse.virgo.util.osgi",
453 mavenBundle("geminiweb",
454 "org.eclipse.virgo.util.osgi.manifest", "3.6.0.RELEASE"),
455 mavenBundle("geminiweb",
456 "org.eclipse.virgo.util.parser.manifest",
459 mavenBundle("org.apache.felix",
460 "org.apache.felix.dependencymanager", "3.1.0"),
461 mavenBundle("org.apache.felix",
462 "org.apache.felix.dependencymanager.shell", "3.0.1"),
464 mavenBundle("com.google.code.gson", "gson", "2.1"),
465 mavenBundle("org.jboss.spec.javax.transaction",
466 "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
467 mavenBundle("org.apache.felix", "org.apache.felix.fileinstall",
469 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
470 mavenBundle("commons-codec", "commons-codec"),
471 mavenBundle("virgomirror",
472 "org.eclipse.jdt.core.compiler.batch",
473 "3.8.0.I20120518-2145"),
474 mavenBundle("eclipselink", "javax.persistence",
475 "2.0.4.v201112161009"),
477 mavenBundle("orbit", "javax.activation", "1.1.0.v201211130549"),
478 mavenBundle("orbit", "javax.annotation", "1.1.0.v201209060031"),
479 mavenBundle("orbit", "javax.ejb", "3.1.1.v201204261316"),
480 mavenBundle("orbit", "javax.el", "2.2.0.v201108011116"),
481 mavenBundle("orbit", "javax.mail.glassfish",
482 "1.4.1.v201108011116"),
483 mavenBundle("orbit", "javax.xml.rpc", "1.1.0.v201005080400"),
484 mavenBundle("orbit", "org.apache.catalina",
485 "7.0.32.v201211201336"),
486 // these are bundle fragments that can't be started on its own
487 mavenBundle("orbit", "org.apache.catalina.ha",
488 "7.0.32.v201211201952").noStart(),
489 mavenBundle("orbit", "org.apache.catalina.tribes",
490 "7.0.32.v201211201952").noStart(),
491 mavenBundle("orbit", "org.apache.coyote",
492 "7.0.32.v201211201952").noStart(),
493 mavenBundle("orbit", "org.apache.jasper",
494 "7.0.32.v201211201952").noStart(),
496 mavenBundle("orbit", "org.apache.el", "7.0.32.v201211081135"),
497 mavenBundle("orbit", "org.apache.juli.extras",
498 "7.0.32.v201211081135"),
499 mavenBundle("orbit", "org.apache.tomcat.api",
500 "7.0.32.v201211081135"),
501 mavenBundle("orbit", "org.apache.tomcat.util",
502 "7.0.32.v201211201952").noStart(),
503 mavenBundle("orbit", "javax.servlet.jsp.jstl",
504 "1.2.0.v201105211821"),
505 mavenBundle("orbit", "javax.servlet.jsp.jstl.impl",
506 "1.2.0.v201210211230"),
508 mavenBundle("org.ops4j.pax.exam", "pax-exam-container-native"),
509 mavenBundle("org.ops4j.pax.exam", "pax-exam-junit4"),
510 mavenBundle("org.ops4j.pax.exam", "pax-exam-link-mvn"),
511 mavenBundle("org.ops4j.pax.url", "pax-url-aether"),
513 mavenBundle("org.springframework", "org.springframework.asm",
515 mavenBundle("org.springframework", "org.springframework.aop",
517 mavenBundle("org.springframework",
518 "org.springframework.context", "3.1.3.RELEASE"),
519 mavenBundle("org.springframework",
520 "org.springframework.context.support", "3.1.3.RELEASE"),
521 mavenBundle("org.springframework", "org.springframework.core",
523 mavenBundle("org.springframework", "org.springframework.beans",
525 mavenBundle("org.springframework",
526 "org.springframework.expression", "3.1.3.RELEASE"),
527 mavenBundle("org.springframework", "org.springframework.web",
530 mavenBundle("org.aopalliance",
531 "com.springsource.org.aopalliance", "1.0.0"),
532 mavenBundle("org.springframework",
533 "org.springframework.web.servlet", "3.1.3.RELEASE"),
534 mavenBundle("org.springframework.security",
535 "spring-security-config", "3.1.3.RELEASE"),
536 mavenBundle("org.springframework.security",
537 "spring-security-core", "3.1.3.RELEASE"),
538 mavenBundle("org.springframework.security",
539 "spring-security-web", "3.1.3.RELEASE"),
540 mavenBundle("org.springframework.security",
541 "spring-security-taglibs", "3.1.3.RELEASE"),
542 mavenBundle("org.springframework",
543 "org.springframework.transaction", "3.1.3.RELEASE"),
545 mavenBundle("org.ow2.chameleon.management", "chameleon-mbeans",
547 mavenBundle("org.opendaylight.controller.thirdparty",
548 "net.sf.jung2", "2.0.1-SNAPSHOT"),
549 mavenBundle("org.opendaylight.controller.thirdparty",
550 "com.sun.jersey.jersey-servlet", "1.17-SNAPSHOT"),
552 // Jersey needs to be started before the northbound application
553 // bundles, using a lower start level
554 mavenBundle("com.sun.jersey", "jersey-client", "1.17"),
555 mavenBundle("com.sun.jersey", "jersey-server", "1.17")
557 mavenBundle("com.sun.jersey", "jersey-core", "1.17")
559 mavenBundle("com.sun.jersey", "jersey-json", "1.17")
560 .startLevel(2), junitBundles());