Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vtp / VTPResource.java
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.vtp;
18
19 import java.io.IOException;
20 import java.text.SimpleDateFormat;
21 import java.util.List;
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.Properties;
25 import java.util.TimeZone;
26 import java.util.UUID;
27
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;
37
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;
42
43 public class VTPResource {
44
45     protected static final Logger LOG = LoggerFactory.getLogger(VTPResource.class);
46     private static Gson gson = new Gson();
47
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
53
54     protected static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);  // NOSONAR
55
56     static {
57         dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
58
59         Properties prp = new Properties();
60         try {
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());
69         }
70     }
71
72     protected Result makeRpc(List <String> args) throws VTPException {
73         return this.makeRpc(args, VTP_EXECUTION_GRPC_TIMEOUT);
74     }
75
76     protected Result makeRpc(List <String> args, int timeout) throws VTPException {
77         Result result = null;
78         String requestId = UUID.randomUUID().toString();
79         try {
80             result = new OpenRemoteCli(
81                     VTP_TEST_CENTER_IP,
82                     VTP_TEST_CENTER_PORT,
83                     timeout,
84                     requestId).run(args);
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()));
92         }
93
94         if (result.getExitCode() != 0) {
95             throw new VTPException(
96                     new VTPError().setMessage(result.getOutput()));
97         }
98
99         return result;
100     }
101
102     public static String getStorePath() {
103         return VTP_ARTIFACT_STORE;
104     }
105
106     protected JsonElement makeRpcAndGetJson(List<String> args) throws VTPException, IOException {
107         return this.makeRpcAndGetJson(args, VTP_EXECUTION_GRPC_TIMEOUT);
108     }
109
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());
114     }
115
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);
118     }
119
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());
123         try {
124             output = new OpenRemoteCli(
125                     VTP_TEST_CENTER_IP,
126                     VTP_TEST_CENTER_PORT,
127                     timeout,
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()));
137         }
138
139         return output;
140     }
141 }