685e009066eb3a219551181f1bd523f247adad9a
[vnfsdk/refrepo.git] /
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.vnfsdk.marketplace.resource;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map.Entry;
23
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;
34
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;
41
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;
46
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;
52
53
54 @Path("/vtp")
55 @Api(tags = { "VNF Test Platform" })
56 public class VTPResource {
57     @Path("/tests")
58     @GET
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 {
64         Result result = null;
65         try {
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();
69         }
70
71         if (result.getExitCode() != 0) {
72             return Response.serverError().entity(result.getOutput()).build();
73         }
74
75         return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
76     }
77
78     @Path("/tests/{testName}/run")
79     @POST
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());
90         Result result = null;
91         try {
92             List<String> cmdArgsList = new ArrayList<>();
93             for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) {
94                 cmdArgsList.add(defaultArg);
95             }
96
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());
101             }
102
103             result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{}));
104         } catch (Exception e) {
105             return Response.serverError().build();
106         }
107
108         if (result.getExitCode() != 0) {
109             return Response.serverError().entity(result.getOutput()).build();
110         }
111
112         ObjectMapper mapper = new ObjectMapper();
113         JsonNode resultJson = mapper.readTree(result.getOutput());
114
115         ((ObjectNode)resultJson).put("build_tag", System.getenv("BUILD_TAG"));
116
117         return Response.ok(resultJson.toString(), MediaType.APPLICATION_JSON).build();
118     }
119 }