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;
23 * This java class provides the osgi console with the commands for running the unit test scripts for the API3
28 public class API3UnitTest implements CommandProvider {
29 private static final String python = "/usr/bin/python";
32 * Function called by the dependency manager when all the required
33 * dependencies are satisfied
37 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
39 bundleContext.registerService(CommandProvider.class.getName(), this,
44 * Function called by the dependency manager when at least one
45 * dependency become unsatisfied or when the component is shutting
46 * down because for example bundle is being stopped.
53 * Function called by dependency manager after "init ()" is called
54 * and after the services provided by the class are registered in
55 * the service registry
62 * Function called by the dependency manager before the services
63 * exported by the component are unregistered, this will be
64 * followed by a "destroy ()" calls
71 public String getHelp() {
72 StringBuffer help = new StringBuffer();
73 help.append("---API3 Unit Test---\n");
75 .append("\t api3ut - run the python script for the specified northbound module\n");
76 help.append("\t GET <uri>");
77 help.append("\t PUT <uri> data1==x1 data2==x2 ...");
78 help.append("\t POST <uri> data1==x1 data2==x2 ...");
79 help.append("\t DELETE <uri>");
80 return help.toString();
83 public void _api3ut(CommandInterpreter ci) {
84 boolean custom = false;
88 module = ci.nextArgument();
94 if (module.equals("custom")) {
95 target = ci.nextArgument();
97 } else if (module.equals("flows")) {
98 target = "flowsUnitTest.py";
99 } else if (module.equals("subnets")) {
100 target = "subnetsUnitTest.py";
101 } else if (module.equals("hosts")) {
102 target = "hostsUnitTest.py";
103 } else if (module.equals("slices")) {
104 target = "slicesUnitTest.py";
105 } else if (module.equals("tif")) {
106 target = "tifUnitTest.py";
108 ci.println("ERROR: Coming soon");
111 if (target != null) {
112 executeScript(target, custom);
116 private void printUsage(CommandInterpreter ci) {
117 ci.println("Usage: api3ut [<module> | custom <target>]");
119 .println("<module>: [flows, hosts, subnets, slices, tif] (You need python-httplib2 installed)");
120 ci.println("<target>: your linux script (w/ absolute path)");
123 private void printStream(InputStream stream) throws IOException {
125 BufferedReader reader = new BufferedReader(
126 new InputStreamReader(stream));
128 while ((line = reader.readLine()) != null) {
129 System.out.println(line);
133 public void executeScript(String target, boolean custom)
134 throws RuntimeException {
135 String script = (custom) ? target : "SCRIPTS/python/" + target;
137 Runtime runTime = Runtime.getRuntime();
138 Process process = runTime.exec(python + " " + script);
139 printStream(process.getInputStream());
140 printStream(process.getErrorStream());
141 } catch (Exception e) {
142 System.out.println("Exception!");
147 public void _GET(CommandInterpreter ci) {
148 parseRestRequest("GET", ci);
151 public void _PUT(CommandInterpreter ci) {
152 parseRestRequest("PUT", ci);
155 public void _DELETE(CommandInterpreter ci) {
156 parseRestRequest("DELETE", ci);
159 public void _POST(CommandInterpreter ci) {
160 parseRestRequest("POST", ci);
163 private void parseRestRequest(String action, CommandInterpreter ci) {
164 String uri, resource;
165 StringBuffer resources = new StringBuffer(" ");
167 uri = ci.nextArgument();
173 resource = ci.nextArgument();
174 while (resource != null) {
175 resources.append(resource);
176 resources.append(" ");
177 resource = ci.nextArgument();
180 executeRestCall(action, uri, resources.toString());
184 private void executeRestCall(String action, String uri, String resources) {
185 String script = "SCRIPTS/python/rest_call.py";
188 Runtime runTime = Runtime.getRuntime();
189 Process process = runTime.exec(python + " " + script + " " + action
190 + " " + uri + " " + resources);
191 printStream(process.getInputStream());
192 printStream(process.getErrorStream());
193 } catch (Exception e) {
194 System.out.println("Exception!");
199 private void printRestUsage(CommandInterpreter ci) {
200 ci.println("Usage: GET/PUT/POST/DELETE <uri> [<resources>]");
201 ci.println("<uri>: ex: slices/red or slices/red/flowspecs");
203 .println("<resources>: resource==<value>,... ex: switchId==2 port==3-7");