1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/**
* Copyright 2018 Huawei Technologies Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.vnfsdk.marketplace.resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.onap.vnfsdk.marketplace.common.ToolUtil;
import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException;
import org.open.infc.grpc.Result;
import org.open.infc.grpc.client.OpenRemoteCli;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.internal.LinkedTreeMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
@Path("/vtp")
@Api(tags = { "VNF Test Platform" })
public class VTPResource {
@Path("/tests")
@GET
@ApiOperation(value = "VTP Test cases", response = String.class)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
@ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) })
public Response listTests() throws IOException, MarketplaceResourceException {
Result result = null;
try {
result = OpenRemoteCli.run(new String[] { "-P", "open-cli", "schema-list", "--product", "onap-vtp", "--format", "json" });
} catch (Exception e) {
return Response.serverError().build();
}
if (result.getExitCode() != 0) {
return Response.serverError().entity(result.getOutput()).build();
}
return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
}
@Path("/tests/{testName}/run")
@POST
@ApiOperation(value = "Run VTP testcase")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
@ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Test case not found", response = String.class),
@ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "VTP internal failure", response = String.class) })
public Response runTest(@ApiParam(value = "test Name") @PathParam("testName") String testName,
@Context HttpServletRequest request)
throws IOException, MarketplaceResourceException {
String details = IOUtils.toString(request.getInputStream());
Result result = null;
try {
List<String> cmdArgsList = new ArrayList<>();
for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) {
cmdArgsList.add(defaultArg);
}
LinkedTreeMap<String, String> cmdArgs = ToolUtil.fromJson(details, LinkedTreeMap.class);
for (Entry<String, String> arg : cmdArgs.entrySet()) {
cmdArgsList.add("--" + arg.getKey());
cmdArgsList.add(arg.getValue());
}
result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{}));
} catch (Exception e) {
return Response.serverError().build();
}
if (result.getExitCode() != 0) {
return Response.serverError().entity(result.getOutput()).build();
}
ObjectMapper mapper = new ObjectMapper();
JsonNode resultJson = mapper.readTree(result.getOutput());
((ObjectNode)resultJson).put("build_tag", System.getenv("BUILD_TAG"));
JsonNode results = resultJson.get("results");
if (results != null && results.isArray()) {
ArrayNode resultsArray = (ArrayNode)results;
if (resultsArray.size() >= 0) {
String error = resultsArray.get(0).get("error").asText();
((ObjectNode)resultJson).put("criteria", "SUCCESS".equalsIgnoreCase(error) ? "PASS" : "FAILED");
}
}
return Response.ok(resultJson.toString(), MediaType.APPLICATION_JSON).build();
}
}
|