Code Improvements-Vnfsdk-refrepo sonar issue fixes
[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     private static final String PRODUCT_ARG="--product";
61     private static final String OPEN_CLI="open-cli";
62     private static final String FORMAT="--format";
63     private static final String IO_EXCEPTION_OCCURS ="IOException occurs";
64     private static final String SERVICE="service";
65     public VTPTestScenarioList listTestScenariosHandler() throws VTPException {
66         List<String> args = new ArrayList<>();
67
68         args.addAll(Arrays.asList(
69                 PRODUCT_ARG, OPEN_CLI, "product-list", FORMAT, "json"
70                 ));
71
72
73         JsonElement results = null;
74         try {
75             results = this.makeRpcAndGetJson(args);
76         } catch (IOException e) {
77             LOG.error(IO_EXCEPTION_OCCURS,e);
78         }
79
80         VTPTestScenarioList list = new VTPTestScenarioList();
81
82         if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
83             JsonArray resultsArray = results.getAsJsonArray();
84                 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
85                     JsonElement jsonElement = it.next();
86                     JsonObject n = jsonElement.getAsJsonObject();
87                     if (n.entrySet().iterator().hasNext()) {
88                         String name = n.get("product").getAsString();
89
90                         if (OPEN_CLI.equalsIgnoreCase(name))
91                             continue;
92
93                         list.getScenarios().add(new VTPTestScenario().setName(name).setDescription(
94                                 n.get(DESCRIPTION).getAsString()));
95                     }
96             }
97         }
98
99         return list;
100     }
101
102     @Path("/scenarios")
103     @GET
104     @ApiOperation(tags = "VTP Scenario", value = " List available test scenarios", response = VTPTestScenario.class, responseContainer = "List")
105     @Produces(MediaType.APPLICATION_JSON)
106     @ApiResponses(value = {
107             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
108                     message = "Failed to perform the operation",
109                     response = VTPError.class) })
110     public Response listTestScenarios() throws VTPException {
111         return Response.ok(this.listTestScenariosHandler().getScenarios().toString(), MediaType.APPLICATION_JSON).build();
112     }
113
114     public VTPTestSuiteList listTestSutiesHandler(String scenario) throws VTPException {
115         List<String> args = new ArrayList<>();
116
117         args.addAll(Arrays.asList(
118                 PRODUCT_ARG, OPEN_CLI, "service-list", PRODUCT_ARG, scenario, FORMAT, "json"
119                 ));
120
121         JsonElement results = null;
122         try {
123             results = this.makeRpcAndGetJson(args);
124         } catch (IOException e) {
125             LOG.error(IO_EXCEPTION_OCCURS,e);
126         }
127
128         VTPTestSuiteList list = new VTPTestSuiteList();
129
130         if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
131             JsonArray resultsArray = results.getAsJsonArray();
132                 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
133                     JsonElement jsonElement = it.next();
134                     JsonObject n = jsonElement.getAsJsonObject();
135                     if (n.entrySet().iterator().hasNext()) {
136                         list.getSuites().add(new VTPTestSuite().setName(n.get(SERVICE).getAsString()).setDescription(
137                                 n.get(DESCRIPTION).getAsString()));
138                     }
139                 }
140         }
141
142         return list;
143     }
144
145     @Path("/scenarios/{scenario}/testsuites")
146     @GET
147     @ApiOperation(tags = "VTP Scenario",  value = " List available test suties in given scenario", response = VTPTestSuite.class, responseContainer = "List")
148     @Produces(MediaType.APPLICATION_JSON)
149     @ApiResponses(value = {
150             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
151                     message = "Failed to perform the operation",
152                     response = VTPError.class) })
153     public Response listTestSuties(
154             @ApiParam("Test scenario name") @PathParam("scenario") String scenario) throws VTPException {
155
156         return Response.ok(this.listTestSutiesHandler(scenario).getSuites().toString(), MediaType.APPLICATION_JSON).build();
157     }
158
159     public VTPTestCaseList listTestcasesHandler(String testSuiteName, String scenario) throws VTPException {
160         List<String> args = new ArrayList<>();
161
162         args.addAll(Arrays.asList(
163                 PRODUCT_ARG, OPEN_CLI, "schema-list", PRODUCT_ARG, scenario, FORMAT, "json"
164                 ));
165         if (testSuiteName != null) {
166             args.add("--service");
167             args.add(testSuiteName);
168         }
169
170         JsonElement results = null;
171         try {
172             results = this.makeRpcAndGetJson(args);
173         } catch (IOException e) {
174             LOG.error(IO_EXCEPTION_OCCURS,e);
175         }
176
177         VTPTestCaseList list = new VTPTestCaseList();
178
179         if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) {
180             JsonArray resultsArray = results.getAsJsonArray();
181                 for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
182                     JsonElement jsonElement = it.next();
183                     JsonObject n = jsonElement.getAsJsonObject();
184                     if (n.entrySet().iterator().hasNext())
185                         list.getTestCases().add(
186                                 new VTPTestCase().setTestCaseName(
187                                         n.get("command").getAsString()).setTestSuiteName(
188                                                 n.get(SERVICE).getAsString()));
189                 }
190         }
191
192         return list;
193     }
194
195     @Path("/scenarios/{scenario}/testcases")
196     @GET
197     @ApiOperation(tags = "VTP Scenario", value = " List available test cases", response = VTPTestCase.class, responseContainer = "List")
198     @Produces(MediaType.APPLICATION_JSON)
199     @ApiResponses(value = {
200             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
201                     message = "Failed to perform the operation",
202                     response = VTPError.class) })
203     public Response listTestcases(
204              @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
205              @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName
206              ) throws VTPException {
207
208         return Response.ok(this.listTestcasesHandler(testSuiteName, scenario).getTestCases().toString(), MediaType.APPLICATION_JSON).build();
209     }
210
211     public VTPTestCase getTestcaseHandler(String scenario, String testSuiteName, String testCaseName) throws VTPException {
212         List<String> args = new ArrayList<>();
213         args.addAll(Arrays.asList(
214                 PRODUCT_ARG, OPEN_CLI, "schema-show", PRODUCT_ARG, scenario, "--service", testSuiteName, "--command", testCaseName , FORMAT, "json"
215                 ));
216         JsonElement results = null;
217         try {
218             results = this.makeRpcAndGetJson(args);
219         } catch (IOException e) {
220             LOG.error(IO_EXCEPTION_OCCURS,e);
221         }
222
223         JsonObject schema = results.getAsJsonObject().getAsJsonObject("schema");
224
225         VTPTestCase tc = new VTPTestCase();
226         tc.setTestCaseName(schema.get("name").getAsString());
227         tc.setDescription(schema.get(DESCRIPTION).getAsString());
228         tc.setTestSuiteName(schema.get(SERVICE).getAsString());
229         tc.setAuthor(schema.get("author").getAsString());
230         JsonElement inputsJson = schema.get("inputs");
231         if (inputsJson != null && inputsJson.isJsonArray()) {
232             for (final JsonElement jsonElement: inputsJson.getAsJsonArray()) {
233                 JsonObject inputJson  = jsonElement.getAsJsonObject();
234                 VTPTestCaseInput input = new VTPTestCaseInput();
235
236                 input.setName(inputJson.get("name").getAsString());
237                 input.setDescription(inputJson.get(DESCRIPTION).getAsString());
238                 input.setType(inputJson.get("type").getAsString());
239
240                 if (inputJson.get("is_optional") != null)
241                     input.setIsOptional(inputJson.get("is_optional").getAsBoolean());
242
243                 if (inputJson.get("default_value") != null)
244                     input.setDefaultValue(inputJson.get("default_value").getAsString());
245
246                 if (inputJson.get("metadata") != null)
247                     input.setMetadata(inputJson.get("metadata"));
248
249                 tc.getInputs().add(input);
250             }
251         }
252
253         JsonElement outputsJson = schema.get("outputs");
254         if (outputsJson != null && outputsJson.isJsonArray() && outputsJson.getAsJsonArray().size()>0) {
255             for (final JsonElement jsonElement: outputsJson.getAsJsonArray()) {
256                 JsonObject outputJson = jsonElement.getAsJsonObject();
257                 VTPTestCaseOutput output = new VTPTestCaseOutput();
258                 output.setName(outputJson.get("name").getAsString());
259                 output.setDescription(outputJson.get(DESCRIPTION).getAsString());
260                 output.setType(outputJson.get("type").getAsString());
261
262                 tc.getOutputs().add(output);
263             }
264         }
265
266         return tc;
267     }
268
269     @Path("/scenarios/{scenario}/testsuites/{testSuiteName}/testcases/{testCaseName}")
270     @GET
271     @ApiOperation(tags = "VTP Scenario",  value = "Retrieve test cases details like inputs outputs and test suite name", response = VTPTestCase.class)
272     @Produces(MediaType.APPLICATION_JSON)
273     @ApiResponses(value = {
274             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
275                     message = "Failed to perform the operation", response = VTPError.class),
276             @ApiResponse(code = HttpStatus.NOT_FOUND_404,
277                     message = "Test case does not exist", response = VTPError.class)})
278     public Response getTestcase(
279             @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
280             @ApiParam(value = "Test case name") @PathParam("testSuiteName") String testSuiteName,
281             @ApiParam(value = "Test case name") @PathParam("testCaseName") String testCaseName)
282                     throws VTPException {
283
284         return Response.ok(this.getTestcaseHandler(scenario, testSuiteName, testCaseName).toString(), MediaType.APPLICATION_JSON).build();
285     }
286 }