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.google.gson.Gson;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
41 import com.google.gson.reflect.TypeToken;
43 public class VTPResource {
45 protected static final Logger LOG = LoggerFactory.getLogger(VTPResource.class);
46 private static Gson gson = new Gson();
48 protected static String VTP_TEST_CENTER_IP; // NOSONAR
49 protected static int VTP_TEST_CENTER_PORT; // NOSONAR
50 protected static String VTP_ARTIFACT_STORE; // NOSONAR
51 protected static String VTP_EXECUTION_TEMP_STORE; // NOSONAR
52 protected static int VTP_EXECUTION_GRPC_TIMEOUT; // NOSONAR
54 protected static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US); // NOSONAR
57 dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
59 Properties prp = new Properties();
61 prp.load(VTPResource.class.getClassLoader().getResourceAsStream("vtp.properties"));
62 VTP_TEST_CENTER_IP = prp.getProperty("vtp.grpc.server");
63 VTP_TEST_CENTER_PORT = Integer.parseInt(prp.getProperty("vtp.grpc.port"));
64 VTP_ARTIFACT_STORE = prp.getProperty("vtp.artifact.store");
65 VTP_EXECUTION_TEMP_STORE = prp.getProperty("vtp.file.store");
66 VTP_EXECUTION_GRPC_TIMEOUT = Integer.parseInt(prp.getProperty("vtp.grpc.timeout")) * 1000 ;
67 } catch (Exception e) { // NOSONAR
68 LOG.error(e.getMessage());
72 protected Result makeRpc(List <String> args) throws VTPException {
73 return this.makeRpc(args, VTP_EXECUTION_GRPC_TIMEOUT);
76 protected Result makeRpc(List <String> args, int timeout) throws VTPException {
78 String requestId = UUID.randomUUID().toString();
80 result = new OpenRemoteCli(
85 } catch(OpenInterfaceGrpcClient.OpenInterfaceGrpcTimeoutExecption e) {
86 LOG.info("Timed out.", e);
87 throw new VTPException(
88 new VTPError().setHttpStatus(HttpStatus.SC_GATEWAY_TIMEOUT).setMessage("Timed out. Please use request-id to track the progress.").setCode(VTPError.TIMEOUT));
89 } catch (Exception e) {
90 LOG.info("Exception occurs.", e);
91 throw new VTPException(new VTPError().setMessage(e.getMessage()));
94 if (result.getExitCode() != 0) {
95 throw new VTPException(
96 new VTPError().setMessage(result.getOutput()));
102 public static String getStorePath() {
103 return VTP_ARTIFACT_STORE;
106 protected JsonElement makeRpcAndGetJson(List<String> args) throws VTPException, IOException {
107 return this.makeRpcAndGetJson(args, VTP_EXECUTION_GRPC_TIMEOUT);
110 protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPException {
111 Result result = this.makeRpc(args, timeout);
112 JsonParser jsonParser = new JsonParser();
113 return jsonParser.parse(result.getOutput());
116 protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPException {
117 return this.makeRpc(scenario, requestId, profile, testCase, argsJsonNode, VTP_EXECUTION_GRPC_TIMEOUT);
120 protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode, int timeout) throws VTPException {
121 Output output = null;
122 Map <String, String> args = gson.fromJson(argsJsonNode, new TypeToken<Map<String,String>>(){}.getType());
124 output = new OpenRemoteCli(
126 VTP_TEST_CENTER_PORT,
128 requestId).invoke(scenario, profile, testCase, args);
129 } catch(OpenInterfaceGrpcClient.OpenInterfaceGrpcTimeoutExecption e) {
130 LOG.info("Timed out.", e);
131 throw new VTPException(
132 new VTPError().setHttpStatus(HttpStatus.SC_GATEWAY_TIMEOUT).setMessage("Timed out. Please use request-id to track the progress.").setCode(VTPError.TIMEOUT));
133 } catch (Exception e) {
134 LOG.info("Exception occurs", e);
135 throw new VTPException(
136 new VTPError().setMessage(e.getMessage()));