OpenDaylight Controller functional modules.
[controller.git] / opendaylight / 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
22 /**
23  * This java class provides the osgi console with the commands for running the unit test scripts for the API3
24  *
25  *
26  *
27  */
28 public class API3UnitTest implements CommandProvider {
29     private static final String python = "/usr/bin/python";
30
31     /**
32      * Function called by the dependency manager when all the required
33      * dependencies are satisfied
34      *
35      */
36     void init() {
37         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
38                 .getBundleContext();
39         bundleContext.registerService(CommandProvider.class.getName(), this,
40                 null);
41     }
42
43     /**
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.
47      *
48      */
49     void destroy() {
50     }
51
52     /**
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
56      *
57      */
58     void start() {
59     }
60
61     /**
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
65      *
66      */
67     void stop() {
68     }
69
70     @Override
71     public String getHelp() {
72         StringBuffer help = new StringBuffer();
73         help.append("---API3 Unit Test---\n");
74         help
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();
81     }
82
83     public void _api3ut(CommandInterpreter ci) {
84         boolean custom = false;
85         String target = null;
86         String module = null;
87
88         module = ci.nextArgument();
89         if (module == null) {
90             printUsage(ci);
91             return;
92         }
93
94         if (module.equals("custom")) {
95             target = ci.nextArgument();
96             custom = true;
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";
107         } else {
108             ci.println("ERROR: Coming soon");
109         }
110
111         if (target != null) {
112             executeScript(target, custom);
113         }
114     }
115
116     private void printUsage(CommandInterpreter ci) {
117         ci.println("Usage: api3ut [<module> | custom <target>]");
118         ci
119                 .println("<module>: [flows, hosts, subnets, slices, tif] (You need python-httplib2 installed)");
120         ci.println("<target>: your linux script (w/ absolute path)");
121     }
122
123     private void printStream(InputStream stream) throws IOException {
124         String line;
125         BufferedReader reader = new BufferedReader(
126                 new InputStreamReader(stream));
127
128         while ((line = reader.readLine()) != null) {
129             System.out.println(line);
130         }
131     }
132
133     public void executeScript(String target, boolean custom)
134             throws RuntimeException {
135         String script = (custom) ? target : "SCRIPTS/python/" + target;
136         try {
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!");
143             e.printStackTrace();
144         }
145     }
146
147     public void _GET(CommandInterpreter ci) {
148         parseRestRequest("GET", ci);
149     }
150
151     public void _PUT(CommandInterpreter ci) {
152         parseRestRequest("PUT", ci);
153     }
154
155     public void _DELETE(CommandInterpreter ci) {
156         parseRestRequest("DELETE", ci);
157     }
158
159     public void _POST(CommandInterpreter ci) {
160         parseRestRequest("POST", ci);
161     }
162
163     private void parseRestRequest(String action, CommandInterpreter ci) {
164         String uri, resource;
165         StringBuffer resources = new StringBuffer(" ");
166
167         uri = ci.nextArgument();
168         if (uri == null) {
169             printRestUsage(ci);
170             return;
171         }
172
173         resource = ci.nextArgument();
174         while (resource != null) {
175             resources.append(resource);
176             resources.append(" ");
177             resource = ci.nextArgument();
178         }
179
180         executeRestCall(action, uri, resources.toString());
181
182     }
183
184     private void executeRestCall(String action, String uri, String resources) {
185         String script = "SCRIPTS/python/rest_call.py";
186
187         try {
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!");
195             e.printStackTrace();
196         }
197     }
198
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");
202         ci
203                 .println("<resources>: resource==<value>,... ex: switchId==2 port==3-7");
204     }
205 }