2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Modifications Copyright (C) 2018.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.adapters.vfc.util;
26 import java.net.HttpURLConnection;
27 import java.net.SocketTimeoutException;
29 import javax.ws.rs.core.UriBuilder;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.client.config.RequestConfig;
34 import org.apache.http.client.methods.HttpDelete;
35 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.client.methods.HttpPost;
37 import org.apache.http.client.methods.HttpPut;
38 import org.apache.http.client.methods.HttpRequestBase;
39 import org.apache.http.conn.ConnectTimeoutException;
40 import org.apache.http.entity.ContentType;
41 import org.apache.http.entity.StringEntity;
42 import org.apache.http.impl.client.HttpClientBuilder;
43 import org.apache.http.util.EntityUtils;
44 import org.onap.so.adapters.vfc.model.RestfulResponse;
45 import org.onap.so.logger.MessageEnum;
46 import org.onap.so.logger.MsoAlarmLogger;
47 import org.onap.so.logger.MsoLogger;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.core.env.Environment;
50 import org.springframework.stereotype.Component;
56 * utility to invoke restclient
59 * @version ONAP Amsterdam Release 2017-9-6
62 public class RestfulUtil {
67 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, RestfulUtil.class);
69 private static final MsoAlarmLogger ALARMLOGGER = new MsoAlarmLogger();
71 private static final int DEFAULT_TIME_OUT = 60000;
73 private static final String ONAP_IP = "ONAP_IP";
75 private static final String DEFAULT_MSB_IP = "127.0.0.1";
77 private static final Integer DEFAULT_MSB_PORT = 80;
79 private static final String VFC_ADAPTER="VFC Adapter";
82 private Environment env;
84 public String getMsbHost() {
85 // MSB_IP will be set as ONAP_IP environment parameter in install flow.
86 String msbIp = System.getenv().get(ONAP_IP);
87 // if ONAP IP is not set. get it from config file.
88 if (null == msbIp || msbIp.isEmpty()) {
89 msbIp = env.getProperty("mso.msb-ip", DEFAULT_MSB_IP);
91 Integer msbPort = env.getProperty("mso.msb-port", Integer.class, DEFAULT_MSB_PORT);
93 String msbEndpoint = UriBuilder.fromPath("").host(msbIp).port(msbPort).scheme("http").build().toString();
94 LOGGER.debug("msbEndpoint in vfc adapter: " + msbEndpoint);
99 private RestfulUtil() {
103 public RestfulResponse send(String url, String methodType, String content) {
104 String msbUrl = getMsbHost() + url;
105 LOGGER.debug("Begin to sent message " + methodType +": " + msbUrl);
107 HttpRequestBase method = null;
108 HttpResponse httpResponse = null;
111 int timeout = DEFAULT_TIME_OUT;
113 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
114 .setConnectionRequestTimeout(timeout).build();
116 HttpClient client = HttpClientBuilder.create().build();
118 if("POST".equalsIgnoreCase(methodType)) {
119 HttpPost httpPost = new HttpPost(msbUrl);
120 httpPost.setConfig(requestConfig);
121 httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
123 } else if("PUT".equalsIgnoreCase(methodType)) {
124 HttpPut httpPut = new HttpPut(msbUrl);
125 httpPut.setConfig(requestConfig);
126 httpPut.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
128 } else if("GET".equalsIgnoreCase(methodType)) {
129 HttpGet httpGet = new HttpGet(msbUrl);
130 httpGet.setConfig(requestConfig);
132 } else if("DELETE".equalsIgnoreCase(methodType)) {
133 HttpDelete httpDelete = new HttpDelete(msbUrl);
134 httpDelete.setConfig(requestConfig);
138 // now VFC have no auth
139 // String userCredentials =
140 // SDNCAdapterProperties.getEncryptedProperty(Constants.SDNC_AUTH_PROP,
141 // Constants.DEFAULT_SDNC_AUTH, Constants.ENCRYPTION_KEY);
142 // String authorization = "Basic " +
143 // DatatypeConverter.printBase64Binary(userCredentials.getBytes());
144 // method.setHeader("Authorization", authorization);
146 httpResponse = client.execute(method);
148 String responseContent = null;
149 if(httpResponse.getEntity() != null) {
150 responseContent = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
153 int statusCode = httpResponse.getStatusLine().getStatusCode();
154 String statusMessage = httpResponse.getStatusLine().getReasonPhrase();
156 LOGGER.debug("VFC Response: " + statusCode + " " + statusMessage
157 + (responseContent == null ? "" : System.lineSeparator() + responseContent));
159 if(httpResponse.getStatusLine().getStatusCode() >= 300) {
160 String errMsg = "VFC returned " + statusCode + " " + statusMessage;
162 return createResponse(statusCode, errMsg);
170 LOGGER.debug("method is NULL:");
174 return createResponse(statusCode, responseContent);
176 } catch(SocketTimeoutException | ConnectTimeoutException e) {
177 String errMsg = "Request to VFC timed out";
179 return createResponse(HttpURLConnection.HTTP_CLIENT_TIMEOUT, errMsg);
181 } catch(Exception e) {
182 String errMsg = "Error processing request to VFC";
184 return createResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, errMsg);
187 if(httpResponse != null) {
189 EntityUtils.consume(httpResponse.getEntity());
190 } catch(Exception e) {
191 LOGGER.debug("Exception :", e);
198 } catch(Exception e) {
199 LOGGER.debug("Exception :", e);
205 private static void logError(String errMsg, Throwable t) {
206 LOGGER.error(MessageEnum.RA_NS_EXC, VFC_ADAPTER, "", MsoLogger.ErrorCode.AvailabilityError, errMsg, t);
207 ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, errMsg);
210 private static void logError(String errMsg) {
211 LOGGER.error(MessageEnum.RA_NS_EXC, VFC_ADAPTER, "", MsoLogger.ErrorCode.AvailabilityError, errMsg);
212 ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, errMsg);
215 private static RestfulResponse createResponse(int statusCode, String content) {
216 RestfulResponse rsp = new RestfulResponse();
217 rsp.setStatus(statusCode);
218 rsp.setResponseContent(content);