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