2 * Copyright 2018 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vnfsdk.marketplace.resource;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map.Entry;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
35 import org.apache.commons.io.IOUtils;
36 import org.eclipse.jetty.http.HttpStatus;
37 import org.onap.vnfsdk.marketplace.common.ToolUtil;
38 import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException;
39 import org.open.infc.grpc.Result;
40 import org.open.infc.grpc.client.OpenRemoteCli;
42 import com.fasterxml.jackson.databind.JsonNode;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.fasterxml.jackson.databind.node.ObjectNode;
45 import com.google.gson.internal.LinkedTreeMap;
47 import io.swagger.annotations.Api;
48 import io.swagger.annotations.ApiOperation;
49 import io.swagger.annotations.ApiParam;
50 import io.swagger.annotations.ApiResponse;
51 import io.swagger.annotations.ApiResponses;
55 @Api(tags = { "VNF Test Platform" })
56 public class VTPResource {
59 @ApiOperation(value = "VTP Test cases", response = String.class)
60 @Produces(MediaType.APPLICATION_JSON)
61 @ApiResponses(value = {
62 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) })
63 public Response listTests() throws IOException, MarketplaceResourceException {
66 result = OpenRemoteCli.run(new String[] { "-P", "open-cli", "schema-list", "--product", "onap-vtp", "--format", "json" });
67 } catch (Exception e) {
68 return Response.serverError().build();
71 if (result.getExitCode() != 0) {
72 return Response.serverError().entity(result.getOutput()).build();
75 return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
78 @Path("/tests/{testName}/run")
80 @ApiOperation(value = "Run VTP testcase")
81 @Consumes(MediaType.APPLICATION_JSON)
82 @Produces(MediaType.APPLICATION_JSON)
83 @ApiResponses(value = {
84 @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Test case not found", response = String.class),
85 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "VTP internal failure", response = String.class) })
86 public Response runTest(@ApiParam(value = "test Name") @PathParam("testName") String testName,
87 @Context HttpServletRequest request)
88 throws IOException, MarketplaceResourceException {
89 String details = IOUtils.toString(request.getInputStream());
92 List<String> cmdArgsList = new ArrayList<>();
93 for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) {
94 cmdArgsList.add(defaultArg);
97 LinkedTreeMap<String, String> cmdArgs = ToolUtil.fromJson(details, LinkedTreeMap.class);
98 for (Entry<String, String> arg : cmdArgs.entrySet()) {
99 cmdArgsList.add("--" + arg.getKey());
100 cmdArgsList.add(arg.getValue());
103 result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{}));
104 } catch (Exception e) {
105 return Response.serverError().build();
108 if (result.getExitCode() != 0) {
109 return Response.serverError().entity(result.getOutput()).build();
112 ObjectMapper mapper = new ObjectMapper();
113 JsonNode resultJson = mapper.readTree(result.getOutput());
115 ((ObjectNode)resultJson).put("build_tag", System.getenv("BUILD_TAG"));
117 return Response.ok(resultJson.toString(), MediaType.APPLICATION_JSON).build();