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.google.gson.internal.LinkedTreeMap;
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;
52 @Api(tags = { "VNF Test Platform" })
53 public class VTPResource {
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 {
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();
68 if (result.getExitCode() != 0) {
69 return Response.serverError().entity(result.getOutput()).build();
72 return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
75 @Path("/tests/{testName}/run")
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());
89 List<String> cmdArgsList = new ArrayList<>();
90 for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) {
91 cmdArgsList.add(defaultArg);
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());
100 result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{}));
101 } catch (Exception e) {
102 return Response.serverError().build();
105 if (result.getExitCode() != 0) {
106 return Response.serverError().entity(result.getOutput()).build();
109 return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();