6c8b127fceb4a56b7bb0fda262ad3c5accbc4639
[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.google.gson.internal.LinkedTreeMap;
43
44 import io.swagger.annotations.Api;
45 import io.swagger.annotations.ApiOperation;
46 import io.swagger.annotations.ApiParam;
47 import io.swagger.annotations.ApiResponse;
48 import io.swagger.annotations.ApiResponses;
49
50
51 @Path("/vtp")
52 @Api(tags = { "VNF Test Platform" })
53 public class VTPResource {
54     @Path("/tests")
55     @GET
56     @ApiOperation(value = "VTP Test cases", response = String.class)
57     @Produces(MediaType.APPLICATION_JSON)
58     @ApiResponses(value = {
59             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) })
60     public Response listTests() throws IOException, MarketplaceResourceException {
61         Result result = null;
62         try {
63             result = OpenRemoteCli.run(new String[] { "-P", "open-cli", "schema-list", "--product", "onap-vtp", "--format", "json" });
64         } catch (Exception e) {
65             return Response.serverError().build();
66         }
67
68         if (result.getExitCode() != 0) {
69             return Response.serverError().entity(result.getOutput()).build();
70         }
71
72         return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
73     }
74
75     @Path("/tests/{testName}/run")
76     @POST
77     @ApiOperation(value = "Run VTP testcase")
78     @Consumes(MediaType.APPLICATION_JSON)
79     @Produces(MediaType.APPLICATION_JSON)
80     @ApiResponses(value = {
81             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Test case not found", response = String.class),
82             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "VTP internal failure", response = String.class) })
83     public Response runTest(@ApiParam(value = "test Name") @PathParam("testName") String testName,
84             @Context HttpServletRequest request)
85             throws IOException, MarketplaceResourceException {
86         String details = IOUtils.toString(request.getInputStream());
87         Result result = null;
88         try {
89             List<String> cmdArgsList = new ArrayList<>();
90             for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) {
91                 cmdArgsList.add(defaultArg);
92             }
93
94             LinkedTreeMap<String, String> cmdArgs = ToolUtil.fromJson(details, LinkedTreeMap.class);
95             for (Entry<String, String> arg : cmdArgs.entrySet()) {
96                 cmdArgsList.add("--" + arg.getKey());
97                 cmdArgsList.add(arg.getValue());
98             }
99
100             result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{}));
101         } catch (Exception e) {
102             return Response.serverError().build();
103         }
104
105         if (result.getExitCode() != 0) {
106             return Response.serverError().entity(result.getOutput()).build();
107         }
108
109         return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
110     }
111 }