Merge "Migrate to gson"
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vtp / scenario / VTPScenarioResource.java
1 /**
2  * Copyright 2018 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vtp.scenario;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import javax.ws.rs.GET;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.QueryParam;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32
33 import org.eclipse.jetty.http.HttpStatus;
34 import org.onap.vtp.VTPResource;
35 import org.onap.vtp.error.VTPError;
36 import org.onap.vtp.error.VTPError.VTPException;
37 import org.onap.vtp.scenario.model.VTPTestCase;
38 import org.onap.vtp.scenario.model.VTPTestScenario;
39 import org.onap.vtp.scenario.model.VTPTestSuite;
40 import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseInput;
41 import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseList;
42 import org.onap.vtp.scenario.model.VTPTestCase.VTPTestCaseOutput;
43 import org.onap.vtp.scenario.model.VTPTestScenario.VTPTestScenarioList;
44 import org.onap.vtp.scenario.model.VTPTestSuite.VTPTestSuiteList;
45
46 import com.google.gson.JsonArray;
47 import com.google.gson.JsonElement;
48 import com.google.gson.JsonObject;
49
50 import io.swagger.annotations.Api;
51 import io.swagger.annotations.ApiOperation;
52 import io.swagger.annotations.ApiParam;
53 import io.swagger.annotations.ApiResponse;
54 import io.swagger.annotations.ApiResponses;
55
56 @Path("/vtp")
57 @Api(tags = {"VTP Scenario"})
58 public class VTPScenarioResource extends VTPResource{
59     private static final String DESCRIPTION = "description";
60     public VTPTestScenarioList listTestScenariosHandler() throws VTPException {
61         List<String> args = new ArrayList<>();
62
63         args.addAll(Arrays.asList(new String[] {
64                 "--product", "open-cli", "product-list", "--format", "json"
65                 }));
66
67         JsonElement results = null;
68         try {
69             results = this.makeRpcAndGetJson(args);
70         } catch (IOException e) {
71             LOG.error("IOException occurs",e);
72         }
73
74         VTPTestScenarioList list = new VTPTestScenarioList();
75
76         if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
77             JsonArray resultsArray = results.getAsJsonArray();
78                 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
79                     JsonElement jsonElement = it.next();
80                     JsonObject n = jsonElement.getAsJsonObject();
81                     if (n.entrySet().iterator().hasNext()) {
82                         String name = n.get("product").getAsString();
83
84                         if ("open-cli".equalsIgnoreCase(name))
85                             continue;
86
87                         list.getScenarios().add(new VTPTestScenario().setName(name).setDescription(
88                                 n.get(DESCRIPTION).getAsString()));
89                     }
90             }
91         }
92
93         return list;
94     }
95
96     @Path("/scenarios")
97     @GET
98     @ApiOperation(tags = "VTP Scenario", value = " List available test scenarios", response = VTPTestScenario.class, responseContainer = "List")
99     @Produces(MediaType.APPLICATION_JSON)
100     @ApiResponses(value = {
101             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
102                     message = "Failed to perform the operation",
103                     response = VTPError.class) })
104     public Response listTestScenarios() throws VTPException {
105         return Response.ok(this.listTestScenariosHandler().getScenarios().toString(), MediaType.APPLICATION_JSON).build();
106     }
107
108     public VTPTestSuiteList listTestSutiesHandler(String scenario) throws VTPException {
109         List<String> args = new ArrayList<>();
110
111         args.addAll(Arrays.asList(new String[] {
112                 "--product", "open-cli", "service-list", "--product", scenario, "--format", "json"
113                 }));
114
115         JsonElement results = null;
116         try {
117             results = this.makeRpcAndGetJson(args);
118         } catch (IOException e) {
119             LOG.error("IOException occurs",e);
120         }
121
122         VTPTestSuiteList list = new VTPTestSuiteList();
123
124         if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
125             JsonArray resultsArray = results.getAsJsonArray();
126                 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
127                     JsonElement jsonElement = it.next();
128                     JsonObject n = jsonElement.getAsJsonObject();
129                     if (n.entrySet().iterator().hasNext()) {
130                         list.getSuites().add(new VTPTestSuite().setName(n.get("service").getAsString()).setDescription(
131                                 n.get(DESCRIPTION).getAsString()));
132                     }
133                 }
134         }
135
136         return list;
137     }
138
139     @Path("/scenarios/{scenario}/testsuites")
140     @GET
141     @ApiOperation(tags = "VTP Scenario",  value = " List available test suties in given scenario", response = VTPTestSuite.class, responseContainer = "List")
142     @Produces(MediaType.APPLICATION_JSON)
143     @ApiResponses(value = {
144             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
145                     message = "Failed to perform the operation",
146                     response = VTPError.class) })
147     public Response listTestSuties(
148             @ApiParam("Test scenario name") @PathParam("scenario") String scenario) throws VTPException {
149
150         return Response.ok(this.listTestSutiesHandler(scenario).getSuites().toString(), MediaType.APPLICATION_JSON).build();
151     }
152
153     public VTPTestCaseList listTestcasesHandler(String testSuiteName, String scenario) throws VTPException {
154         List<String> args = new ArrayList<>();
155
156         args.addAll(Arrays.asList(new String[] {
157                 "--product", "open-cli", "schema-list", "--product", scenario, "--format", "json"
158                 }));
159         if (testSuiteName != null) {
160             args.add("--service");
161             args.add(testSuiteName);
162         }
163
164         JsonElement results = null;
165         try {
166             results = this.makeRpcAndGetJson(args);
167         } catch (IOException e) {
168             LOG.error("IOException occurs",e);
169         }
170
171         VTPTestCaseList list = new VTPTestCaseList();
172
173         if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
174             JsonArray resultsArray = results.getAsJsonArray();
175                 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
176                     JsonElement jsonElement = it.next();
177                     JsonObject n = jsonElement.getAsJsonObject();
178                     if (n.entrySet().iterator().hasNext())
179                         list.getTestCases().add(
180                                 new VTPTestCase().setTestCaseName(
181                                         n.get("command").getAsString()).setTestSuiteName(
182                                                 n.get("service").getAsString()));
183                 }
184         }
185
186         return list;
187     }
188
189     @Path("/scenarios/{scenario}/testcases")
190     @GET
191     @ApiOperation(tags = "VTP Scenario", value = " List available test cases", response = VTPTestCase.class, responseContainer = "List")
192     @Produces(MediaType.APPLICATION_JSON)
193     @ApiResponses(value = {
194             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
195                     message = "Failed to perform the operation",
196                     response = VTPError.class) })
197     public Response listTestcases(
198              @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
199              @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName
200              ) throws VTPException {
201
202         return Response.ok(this.listTestcasesHandler(testSuiteName, scenario).getTestCases().toString(), MediaType.APPLICATION_JSON).build();
203     }
204
205     public VTPTestCase getTestcaseHandler(String scenario, String testSuiteName, String testCaseName) throws VTPException {
206         List<String> args = new ArrayList<>();
207         args.addAll(Arrays.asList(new String[] {
208                  "--product", "open-cli", "schema-show", "--product", scenario, "--service", testSuiteName, "--command", testCaseName , "--format", "json"
209                 }));
210         JsonElement results = null;
211         try {
212             results = this.makeRpcAndGetJson(args);
213         } catch (IOException e) {
214             LOG.error("IOException occurs",e);
215         }
216
217         JsonObject schema = results.getAsJsonObject().getAsJsonObject("schema");
218
219         VTPTestCase tc = new VTPTestCase();
220         tc.setTestCaseName(schema.get("name").getAsString());
221         tc.setDescription(schema.get(DESCRIPTION).getAsString());
222         tc.setTestSuiteName(schema.get("service").getAsString());
223         tc.setAuthor(schema.get("author").getAsString());
224         JsonElement inputsJson = schema.get("inputs");
225         if (inputsJson != null && inputsJson.isJsonArray()) {
226             for (final JsonElement jsonElement: inputsJson.getAsJsonArray()) {
227                 JsonObject inputJson  = jsonElement.getAsJsonObject();
228                 VTPTestCaseInput input = new VTPTestCaseInput();
229
230                 input.setName(inputJson.get("name").getAsString());
231                 input.setDescription(inputJson.get(DESCRIPTION).getAsString());
232                 input.setType(inputJson.get("type").getAsString());
233
234                 if (inputJson.get("is_optional") != null)
235                     input.setIsOptional(inputJson.get("is_optional").getAsBoolean());
236
237                 if (inputJson.get("default_value") != null)
238                     input.setDefaultValue(inputJson.get("default_value").getAsString());
239
240                 if (inputJson.get("metadata") != null)
241                     input.setMetadata(inputJson.get("metadata"));
242
243                 tc.getInputs().add(input);
244             }
245         }
246
247         JsonElement outputsJson = schema.get("outputs");
248         if (outputsJson != null && outputsJson.isJsonArray() && outputsJson.getAsJsonArray().size()>0) {
249             for (final JsonElement jsonElement: outputsJson.getAsJsonArray()) {
250                 JsonObject outputJson = jsonElement.getAsJsonObject();
251                 VTPTestCaseOutput output = new VTPTestCaseOutput();
252                 output.setName(outputJson.get("name").getAsString());
253                 output.setDescription(outputJson.get(DESCRIPTION).getAsString());
254                 output.setType(outputJson.get("type").getAsString());
255
256                 tc.getOutputs().add(output);
257             }
258         }
259
260         return tc;
261     }
262
263     @Path("/scenarios/{scenario}/testsuites/{testSuiteName}/testcases/{testCaseName}")
264     @GET
265     @ApiOperation(tags = "VTP Scenario",  value = "Retrieve test cases details like inputs outputs and test suite name", response = VTPTestCase.class)
266     @Produces(MediaType.APPLICATION_JSON)
267     @ApiResponses(value = {
268             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
269                     message = "Failed to perform the operation", response = VTPError.class),
270             @ApiResponse(code = HttpStatus.NOT_FOUND_404,
271                     message = "Test case does not exist", response = VTPError.class)})
272     public Response getTestcase(
273             @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
274             @ApiParam(value = "Test case name") @PathParam("testSuiteName") String testSuiteName,
275             @ApiParam(value = "Test case name") @PathParam("testCaseName") String testCaseName)
276                     throws VTPException {
277
278         return Response.ok(this.getTestcaseHandler(scenario, testSuiteName, testCaseName).toString(), MediaType.APPLICATION_JSON).build();
279     }
280 }