modify Sonar Issue
[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.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 public class VTPResource {
42
43     protected static final Logger LOG = LoggerFactory.getLogger(VTPResource.class);
44
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
50
51     protected static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);  // NOSONAR
52
53     static {
54         dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
55
56         Properties prp = new Properties();
57         try {
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());
66         }
67     }
68
69     protected Result makeRpc(List <String> args) throws VTPException {
70         return this.makeRpc(args, VTP_EXECUTION_GRPC_TIMEOUT);
71     }
72
73     protected Result makeRpc(List <String> args, int timeout) throws VTPException {
74         Result result = null;
75         String requestId = UUID.randomUUID().toString();
76         try {
77             result = new OpenRemoteCli(
78                     VTP_TEST_CENTER_IP,
79                     VTP_TEST_CENTER_PORT,
80                     timeout,
81                     requestId).run(args);
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()));
89         }
90
91         if (result.getExitCode() != 0) {
92             throw new VTPException(
93                     new VTPError().setMessage(result.getOutput()));
94         }
95
96         return result;
97     }
98
99     public static String getStorePath() {
100         return VTP_ARTIFACT_STORE;
101     }
102
103     protected JsonNode makeRpcAndGetJson(List<String> args) throws VTPException, IOException {
104         return this.makeRpcAndGetJson(args, VTP_EXECUTION_GRPC_TIMEOUT);
105     }
106
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());
111     }
112
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);
115     }
116
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);
121         try {
122             output = new OpenRemoteCli(
123                     VTP_TEST_CENTER_IP,
124                     VTP_TEST_CENTER_PORT,
125                     timeout,
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()));
135         }
136
137         return output;
138     }
139 }