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.
19 import java.io.IOException;
20 import java.text.SimpleDateFormat;
21 import java.util.List;
22 import java.util.Locale;
24 import java.util.Properties;
25 import java.util.TimeZone;
26 import java.util.UUID;
28 import org.apache.http.HttpStatus;
29 import org.onap.vtp.error.VTPError;
30 import org.onap.vtp.error.VTPError.VTPException;
31 import org.open.infc.grpc.Output;
32 import org.open.infc.grpc.Result;
33 import org.open.infc.grpc.client.OpenInterfaceGrpcClient;
34 import org.open.infc.grpc.client.OpenRemoteCli;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
41 public class VTPResource {
43 protected static final Logger LOG = LoggerFactory.getLogger(VTPResource.class);
45 protected static String VTP_TEST_CENTER_IP; // NOSONAR
46 protected static int VTP_TEST_CENTER_PORT; // NOSONAR
47 protected static String VTP_ARTIFACT_STORE; // NOSONAR
48 protected static String VTP_EXECUTION_TEMP_STORE; // NOSONAR
49 protected static int VTP_EXECUTION_GRPC_TIMEOUT; // NOSONAR
51 protected static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US); // NOSONAR
54 dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
56 Properties prp = new Properties();
58 prp.load(VTPResource.class.getClassLoader().getResourceAsStream("vtp.properties"));
59 VTP_TEST_CENTER_IP = prp.getProperty("vtp.grpc.server");
60 VTP_TEST_CENTER_PORT = Integer.parseInt(prp.getProperty("vtp.grpc.port"));
61 VTP_ARTIFACT_STORE = prp.getProperty("vtp.artifact.store");
62 VTP_EXECUTION_TEMP_STORE = prp.getProperty("vtp.file.store");
63 VTP_EXECUTION_GRPC_TIMEOUT = Integer.parseInt(prp.getProperty("vtp.grpc.timeout")) * 1000 ;
64 } catch (Exception e) { // NOSONAR
65 LOG.error(e.getMessage());
69 protected Result makeRpc(List <String> args) throws VTPException {
70 return this.makeRpc(args, VTP_EXECUTION_GRPC_TIMEOUT);
73 protected Result makeRpc(List <String> args, int timeout) throws VTPException {
75 String requestId = UUID.randomUUID().toString();
77 result = new OpenRemoteCli(
82 } catch(OpenInterfaceGrpcClient.OpenInterfaceGrpcTimeoutExecption e) {
83 LOG.info("Timed out.", e);
84 throw new VTPException(
85 new VTPError().setHttpStatus(HttpStatus.SC_GATEWAY_TIMEOUT).setMessage("Timed out. Please use request-id to track the progress.").setCode(VTPError.TIMEOUT));
86 } catch (Exception e) {
87 LOG.info("Exception occurs.", e);
88 throw new VTPException(new VTPError().setMessage(e.getMessage()));
91 if (result.getExitCode() != 0) {
92 throw new VTPException(
93 new VTPError().setMessage(result.getOutput()));
99 public static String getStorePath() {
100 return VTP_ARTIFACT_STORE;
103 protected JsonNode makeRpcAndGetJson(List<String> args) throws VTPException, IOException {
104 return this.makeRpcAndGetJson(args, VTP_EXECUTION_GRPC_TIMEOUT);
107 protected JsonNode makeRpcAndGetJson(List<String> args, int timeout) throws VTPException, IOException {
108 Result result = this.makeRpc(args, timeout);
109 ObjectMapper mapper = new ObjectMapper();
110 return mapper.readTree(result.getOutput());
113 protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonNode argsJsonNode) throws VTPException {
114 return this.makeRpc(scenario, requestId, profile, testCase, argsJsonNode, VTP_EXECUTION_GRPC_TIMEOUT);
117 protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonNode argsJsonNode, int timeout) throws VTPException {
118 Output output = null;
119 ObjectMapper mapper = new ObjectMapper();
120 Map <String, String> args = mapper.convertValue(argsJsonNode, Map.class);
122 output = new OpenRemoteCli(
124 VTP_TEST_CENTER_PORT,
126 requestId).invoke(scenario, profile, testCase, args);
127 } catch(OpenInterfaceGrpcClient.OpenInterfaceGrpcTimeoutExecption e) {
128 LOG.info("Timed out.", e);
129 throw new VTPException(
130 new VTPError().setHttpStatus(HttpStatus.SC_GATEWAY_TIMEOUT).setMessage("Timed out. Please use request-id to track the progress.").setCode(VTPError.TIMEOUT));
131 } catch (Exception e) {
132 LOG.info("Exception occurs", e);
133 throw new VTPException(
134 new VTPError().setMessage(e.getMessage()));