Move adsal into its own subdirectory.
[controller.git] / opendaylight / adsal / northboundtest / unit_test_suite / src / main / java / org / opendaylight / controller / northboundtest / unittestsuite / internal / API3UnitTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.northboundtest.unittestsuite.internal;
11
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16
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;
23
24 /**
25  * This java class provides the osgi console with the commands for running the unit test scripts for the API3
26  *
27  *
28  *
29  */
30 public class API3UnitTest implements CommandProvider {
31     private static Logger log = LoggerFactory
32          .getLogger(API3UnitTest.class);
33
34     private static final String python = "/usr/bin/python";
35
36     /**
37      * Function called by the dependency manager when all the required
38      * dependencies are satisfied
39      *
40      */
41     void init() {
42         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
43                 .getBundleContext();
44         bundleContext.registerService(CommandProvider.class.getName(), this,
45                 null);
46     }
47
48     /**
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.
52      *
53      */
54     void destroy() {
55     }
56
57     /**
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
61      *
62      */
63     void start() {
64     }
65
66     /**
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
70      *
71      */
72     void stop() {
73     }
74
75     @Override
76     public String getHelp() {
77         StringBuffer help = new StringBuffer();
78         help.append("---API3 Unit Test---\n");
79         help
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();
86     }
87
88     public void _api3ut(CommandInterpreter ci) {
89         boolean custom = false;
90         String target = null;
91         String module = null;
92
93         module = ci.nextArgument();
94         if (module == null) {
95             printUsage(ci);
96             return;
97         }
98
99         if (module.equals("custom")) {
100             target = ci.nextArgument();
101             custom = true;
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";
112         } else {
113             ci.println("ERROR: Coming soon");
114         }
115
116         if (target != null) {
117             executeScript(target, custom);
118         }
119     }
120
121     private void printUsage(CommandInterpreter ci) {
122         ci.println("Usage: api3ut [<module> | custom <target>]");
123         ci
124                 .println("<module>: [flows, hosts, subnets, slices, tif] (You need python-httplib2 installed)");
125         ci.println("<target>: your linux script (w/ absolute path)");
126     }
127
128     private void printStream(InputStream stream) throws IOException {
129         String line;
130         BufferedReader reader = new BufferedReader(
131                 new InputStreamReader(stream));
132
133         while ((line = reader.readLine()) != null) {
134             System.out.println(line);
135         }
136     }
137
138     public void executeScript(String target, boolean custom)
139             throws RuntimeException {
140         String script = (custom) ? target : "SCRIPTS/python/" + target;
141         try {
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!");
148             log.error("",e);
149         }
150     }
151
152     public void _GET(CommandInterpreter ci) {
153         parseRestRequest("GET", ci);
154     }
155
156     public void _PUT(CommandInterpreter ci) {
157         parseRestRequest("PUT", ci);
158     }
159
160     public void _DELETE(CommandInterpreter ci) {
161         parseRestRequest("DELETE", ci);
162     }
163
164     public void _POST(CommandInterpreter ci) {
165         parseRestRequest("POST", ci);
166     }
167
168     private void parseRestRequest(String action, CommandInterpreter ci) {
169         String uri, resource;
170         StringBuffer resources = new StringBuffer(" ");
171
172         uri = ci.nextArgument();
173         if (uri == null) {
174             printRestUsage(ci);
175             return;
176         }
177
178         resource = ci.nextArgument();
179         while (resource != null) {
180             resources.append(resource);
181             resources.append(" ");
182             resource = ci.nextArgument();
183         }
184
185         executeRestCall(action, uri, resources.toString());
186
187     }
188
189     private void executeRestCall(String action, String uri, String resources) {
190         String script = "SCRIPTS/python/rest_call.py";
191
192         try {
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!");
200             log.error("",e);
201         }
202     }
203
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");
207         ci
208                 .println("<resources>: resource==<value>,... ex: switchId==2 port==3-7");
209     }
210 }