3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.northboundtest.unittestsuite.internal;
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
17 import org.eclipse.osgi.framework.console.CommandInterpreter;
18 import org.eclipse.osgi.framework.console.CommandProvider;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.FrameworkUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * This java class provides the osgi console with the commands for running the unit test scripts for the API3
30 public class API3UnitTest implements CommandProvider {
31 private static Logger log = LoggerFactory
32 .getLogger(API3UnitTest.class);
34 private static final String python = "/usr/bin/python";
37 * Function called by the dependency manager when all the required
38 * dependencies are satisfied
42 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
44 bundleContext.registerService(CommandProvider.class.getName(), this,
49 * Function called by the dependency manager when at least one
50 * dependency become unsatisfied or when the component is shutting
51 * down because for example bundle is being stopped.
58 * Function called by dependency manager after "init ()" is called
59 * and after the services provided by the class are registered in
60 * the service registry
67 * Function called by the dependency manager before the services
68 * exported by the component are unregistered, this will be
69 * followed by a "destroy ()" calls
76 public String getHelp() {
77 StringBuffer help = new StringBuffer();
78 help.append("---API3 Unit Test---\n");
80 .append("\t api3ut - run the python script for the specified northbound module\n");
81 help.append("\t GET <uri>");
82 help.append("\t PUT <uri> data1==x1 data2==x2 ...");
83 help.append("\t POST <uri> data1==x1 data2==x2 ...");
84 help.append("\t DELETE <uri>");
85 return help.toString();
88 public void _api3ut(CommandInterpreter ci) {
89 boolean custom = false;
93 module = ci.nextArgument();
99 if (module.equals("custom")) {
100 target = ci.nextArgument();
102 } else if (module.equals("flows")) {
103 target = "flowsUnitTest.py";
104 } else if (module.equals("subnets")) {
105 target = "subnetsUnitTest.py";
106 } else if (module.equals("hosts")) {
107 target = "hostsUnitTest.py";
108 } else if (module.equals("slices")) {
109 target = "slicesUnitTest.py";
110 } else if (module.equals("tif")) {
111 target = "tifUnitTest.py";
113 ci.println("ERROR: Coming soon");
116 if (target != null) {
117 executeScript(target, custom);
121 private void printUsage(CommandInterpreter ci) {
122 ci.println("Usage: api3ut [<module> | custom <target>]");
124 .println("<module>: [flows, hosts, subnets, slices, tif] (You need python-httplib2 installed)");
125 ci.println("<target>: your linux script (w/ absolute path)");
128 private void printStream(InputStream stream) throws IOException {
130 BufferedReader reader = new BufferedReader(
131 new InputStreamReader(stream));
133 while ((line = reader.readLine()) != null) {
134 System.out.println(line);
138 public void executeScript(String target, boolean custom)
139 throws RuntimeException {
140 String script = (custom) ? target : "SCRIPTS/python/" + target;
142 Runtime runTime = Runtime.getRuntime();
143 Process process = runTime.exec(python + " " + script);
144 printStream(process.getInputStream());
145 printStream(process.getErrorStream());
146 } catch (Exception e) {
147 System.out.println("Exception!");
152 public void _GET(CommandInterpreter ci) {
153 parseRestRequest("GET", ci);
156 public void _PUT(CommandInterpreter ci) {
157 parseRestRequest("PUT", ci);
160 public void _DELETE(CommandInterpreter ci) {
161 parseRestRequest("DELETE", ci);
164 public void _POST(CommandInterpreter ci) {
165 parseRestRequest("POST", ci);
168 private void parseRestRequest(String action, CommandInterpreter ci) {
169 String uri, resource;
170 StringBuffer resources = new StringBuffer(" ");
172 uri = ci.nextArgument();
178 resource = ci.nextArgument();
179 while (resource != null) {
180 resources.append(resource);
181 resources.append(" ");
182 resource = ci.nextArgument();
185 executeRestCall(action, uri, resources.toString());
189 private void executeRestCall(String action, String uri, String resources) {
190 String script = "SCRIPTS/python/rest_call.py";
193 Runtime runTime = Runtime.getRuntime();
194 Process process = runTime.exec(python + " " + script + " " + action
195 + " " + uri + " " + resources);
196 printStream(process.getInputStream());
197 printStream(process.getErrorStream());
198 } catch (Exception e) {
199 System.out.println("Exception!");
204 private void printRestUsage(CommandInterpreter ci) {
205 ci.println("Usage: GET/PUT/POST/DELETE <uri> [<resources>]");
206 ci.println("<uri>: ex: slices/red or slices/red/flowspecs");
208 .println("<resources>: resource==<value>,... ex: switchId==2 port==3-7");