Merge "update release version"
[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.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.node.ArrayNode;
48
49 import io.swagger.annotations.Api;
50 import io.swagger.annotations.ApiOperation;
51 import io.swagger.annotations.ApiParam;
52 import io.swagger.annotations.ApiResponse;
53 import io.swagger.annotations.ApiResponses;
54
55 @Path("/vtp")
56 @Api(tags = {"VTP Scenario"})
57 public class VTPScenarioResource extends VTPResource{
58     private static final String DESCRIPTION = "description";
59     public VTPTestScenarioList listTestScenariosHandler() throws VTPException {
60         List<String> args = new ArrayList<>();
61
62         args.addAll(Arrays.asList(new String[] {
63                 "--product", "open-cli", "product-list", "--format", "json"
64                 }));
65
66         JsonNode results = null;
67         try {
68             results = this.makeRpcAndGetJson(args);
69         } catch (IOException e) {
70             LOG.error("IOException occurs",e);
71         }
72
73         VTPTestScenarioList list = new VTPTestScenarioList();
74
75         if (results != null && results.isArray()) {
76             ArrayNode resultsArray = (ArrayNode)results;
77             if (resultsArray.size() >= 0) {
78                 for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) {
79                     JsonNode n = it.next();
80                     if (n.elements().hasNext()) {
81                         String name = n.get("product").asText();
82
83                         if ("open-cli".equalsIgnoreCase(name))
84                             continue;
85
86                         list.getScenarios().add(new VTPTestScenario().setName(name).setDescription(
87                                 n.get(DESCRIPTION).asText()));
88                     }
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         JsonNode 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.isArray()) {
125             ArrayNode resultsArray = (ArrayNode)results;
126             if (resultsArray.size() >= 0) {
127                 for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) {
128                     JsonNode n = it.next();
129                     if (n.elements().hasNext()) {
130                         list.getSuites().add(new VTPTestSuite().setName(n.get("service").asText()).setDescription(
131                                 n.get(DESCRIPTION).asText()));
132                     }
133                 }
134             }
135         }
136
137         return list;
138     }
139
140     @Path("/scenarios/{scenario}/testsuites")
141     @GET
142     @ApiOperation(tags = "VTP Scenario",  value = " List available test suties in given scenario", response = VTPTestSuite.class, responseContainer = "List")
143     @Produces(MediaType.APPLICATION_JSON)
144     @ApiResponses(value = {
145             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
146                     message = "Failed to perform the operation",
147                     response = VTPError.class) })
148     public Response listTestSuties(
149             @ApiParam("Test scenario name") @PathParam("scenario") String scenario) throws VTPException {
150
151         return Response.ok(this.listTestSutiesHandler(scenario).getSuites().toString(), MediaType.APPLICATION_JSON).build();
152     }
153
154     public VTPTestCaseList listTestcasesHandler(String testSuiteName, String scenario) throws VTPException {
155         List<String> args = new ArrayList<>();
156
157         args.addAll(Arrays.asList(new String[] {
158                 "--product", "open-cli", "schema-list", "--product", scenario, "--format", "json"
159                 }));
160         if (testSuiteName != null) {
161             args.add("--service");
162             args.add(testSuiteName);
163         }
164
165         JsonNode results = null;
166         try {
167             results = this.makeRpcAndGetJson(args);
168         } catch (IOException e) {
169             LOG.error("IOException occurs",e);
170         }
171
172         VTPTestCaseList list = new VTPTestCaseList();
173
174         if (results != null && results.isArray()) {
175             ArrayNode resultsArray = (ArrayNode)results;
176             if (resultsArray.size() >= 0) {
177                 for (Iterator<JsonNode> it = resultsArray.iterator(); it.hasNext();) {
178                     JsonNode n = it.next();
179                     if (n.elements().hasNext())
180                         list.getTestCases().add(
181                                 new VTPTestCase().setTestCaseName(
182                                         n.get("command").asText()).setTestSuiteName(
183                                                 n.get("service").asText()));
184                 }
185             }
186         }
187
188         return list;
189     }
190
191     @Path("/scenarios/{scenario}/testcases")
192     @GET
193     @ApiOperation(tags = "VTP Scenario", value = " List available test cases", response = VTPTestCase.class, responseContainer = "List")
194     @Produces(MediaType.APPLICATION_JSON)
195     @ApiResponses(value = {
196             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
197                     message = "Failed to perform the operation",
198                     response = VTPError.class) })
199     public Response listTestcases(
200              @ApiParam("Test scenario name") @PathParam("scenario") String scenario,
201              @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName
202              ) throws VTPException {
203
204         return Response.ok(this.listTestcasesHandler(testSuiteName, scenario).getTestCases().toString(), MediaType.APPLICATION_JSON).build();
205     }
206
207     public VTPTestCase getTestcaseHandler(String scenario, String testSuiteName, String testCaseName) throws VTPException {
208         List<String> args = new ArrayList<>();
209         args.addAll(Arrays.asList(new String[] {
210                  "--product", "open-cli", "schema-show", "--product", scenario, "--service", testSuiteName, "--command", testCaseName , "--format", "json"
211                 }));
212         JsonNode results = null;
213         try {
214             results = this.makeRpcAndGetJson(args);
215         } catch (IOException e) {
216             LOG.error("IOException occurs",e);
217         }
218
219         JsonNode schema = results.get("schema");
220
221         VTPTestCase tc = new VTPTestCase();
222         tc.setTestCaseName(schema.get("name").asText());
223         tc.setDescription(schema.get(DESCRIPTION).asText());
224         tc.setTestSuiteName(schema.get("service").asText());
225         tc.setAuthor(schema.get("author").asText());
226         JsonNode inputsJson = schema.get("inputs");
227         if (inputsJson != null && inputsJson.isArray()) {
228             for (final JsonNode inputJson: inputsJson) {
229                 VTPTestCaseInput input = new VTPTestCaseInput();
230
231                 input.setName(inputJson.get("name").asText());
232                 input.setDescription(inputJson.get(DESCRIPTION).asText());
233                 input.setType(inputJson.get("type").asText());
234
235                 if (inputJson.get("is_optional") != null)
236                     input.setIsOptional(inputJson.get("is_optional").asBoolean());
237
238                 if (inputJson.get("default_value") != null)
239                     input.setDefaultValue(inputJson.get("default_value").asText());
240
241                 if (inputJson.get("metadata") != null)
242                     input.setMetadata(inputJson.get("metadata"));
243
244                 tc.getInputs().add(input);
245             }
246         }
247
248         JsonNode outputsJson = schema.get("outputs");
249         if (outputsJson != null && outputsJson.isArray()) {
250             for (final JsonNode outputJson: outputsJson) {
251                 VTPTestCaseOutput output = new VTPTestCaseOutput();
252                 output.setName(outputJson.get("name").asText());
253                 output.setDescription(outputJson.get(DESCRIPTION).asText());
254                 output.setType(outputJson.get("type").asText());
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 }