summaryrefslogtreecommitdiffstats
path: root/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/RestfulUtil.java
blob: e8718fafc2face7f5961128ce3721feae58a63bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.adapters.vfc.util;

import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.openecomp.mso.adapters.vfc.model.RestfulResponse;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoAlarmLogger;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.properties.MsoPropertiesException;
import org.openecomp.mso.properties.MsoPropertiesFactory;

/**
 * <br>
 * <p>
 * </p>
 * utility to invoke restclient
 * 
 * @author
 * @version ONAP Amsterdam Release 2017-9-6
 */
public class RestfulUtil {

    /**
     * Log service
     */
    private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);

    private static final MsoAlarmLogger ALARMLOGGER = new MsoAlarmLogger();

    private static final int DEFAULT_TIME_OUT = 60000;

    private static final MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();

    public static String getMsbHost() {
        String msbIp = "10.229.32.131";
        String msbPort = "8090";
        try {
            msbIp = msoPropertiesFactory.getMsoJavaProperties("MSO_PROP_TOPOLOGY").getProperty("msb-ip",
                    "10.229.32.131");
            msbPort = msoPropertiesFactory.getMsoJavaProperties("MSO_PROP_TOPOLOGY").getProperty("msb-port", "8099");

        } catch(MsoPropertiesException e) {
            LOGGER.error(MessageEnum.RA_NS_EXC, "VFC", "", MsoLogger.ErrorCode.AvailabilityError,
                    "Get msb properties failed");
            e.printStackTrace();
        }
        return "http://" + msbIp + ":" + msbPort;
    }

    private RestfulUtil() {

    }

    public static RestfulResponse send(String url, String methodType, String content) {
        String msbUrl = getMsbHost() + url;
        LOGGER.info(MessageEnum.RA_NS_EXC, msbUrl, "VFC", "");
        LOGGER.debug("VFC Request Body:\n" + content);

        HttpRequestBase method = null;
        HttpResponse httpResponse = null;

        try {
            int timeout = DEFAULT_TIME_OUT;

            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
                    .setConnectionRequestTimeout(timeout).build();

            HttpClient client = HttpClientBuilder.create().build();

            if("POST".equals(methodType.toUpperCase())) {
                HttpPost httpPost = new HttpPost(msbUrl);
                httpPost.setConfig(requestConfig);
                httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
                method = httpPost;
            } else if("PUT".equals(methodType.toUpperCase())) {
                HttpPut httpPut = new HttpPut(msbUrl);
                httpPut.setConfig(requestConfig);
                httpPut.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
                method = httpPut;
            } else if("GET".equals(methodType.toUpperCase())) {
                HttpGet httpGet = new HttpGet(msbUrl);
                httpGet.setConfig(requestConfig);
                method = httpGet;
            } else if("DELETE".equals(methodType.toUpperCase())) {
                HttpDelete httpDelete = new HttpDelete(msbUrl);
                httpDelete.setConfig(requestConfig);
                method = httpDelete;
            }

            // now VFC have no auth
            // String userCredentials =
            // SDNCAdapterProperties.getEncryptedProperty(Constants.SDNC_AUTH_PROP,
            // Constants.DEFAULT_SDNC_AUTH, Constants.ENCRYPTION_KEY);
            // String authorization = "Basic " +
            // DatatypeConverter.printBase64Binary(userCredentials.getBytes());
            // method.setHeader("Authorization", authorization);

            httpResponse = client.execute(method);

            String responseContent = null;
            if(httpResponse.getEntity() != null) {
                responseContent = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
            }

            int statusCode = httpResponse.getStatusLine().getStatusCode();
            String statusMessage = httpResponse.getStatusLine().getReasonPhrase();

            LOGGER.debug("VFC Response: " + statusCode + " " + statusMessage
                    + (responseContent == null ? "" : System.lineSeparator() + responseContent));

            if(httpResponse.getStatusLine().getStatusCode() >= 300) {
                String errMsg = "VFC returned " + statusCode + " " + statusMessage;
                logError(errMsg);
                return createResponse(statusCode, errMsg);
            }

            httpResponse = null;

            if(null != method) {
                method.reset();
            } else {
                LOGGER.debug("method is NULL:");
            }

            method = null;

            LOGGER.info(MessageEnum.RA_RESPONSE_FROM_SDNC, responseContent, "VFC", "");
            return createResponse(statusCode, responseContent);

        } catch(SocketTimeoutException e) {
            String errMsg = "Request to VFC timed out";
            logError(errMsg, e);
            return createResponse(HttpURLConnection.HTTP_CLIENT_TIMEOUT, errMsg);

        } catch(ConnectTimeoutException e) {
            String errMsg = "Request to VFC timed out";
            logError(errMsg, e);
            return createResponse(HttpURLConnection.HTTP_CLIENT_TIMEOUT, errMsg);

        } catch(Exception e) {
            String errMsg = "Error processing request to VFC";
            logError(errMsg, e);
            return createResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, errMsg);

        } finally {
            if(httpResponse != null) {
                try {
                    EntityUtils.consume(httpResponse.getEntity());
                } catch(Exception e) {
                    LOGGER.debug("Exception :", e);
                }
            }

            if(method != null) {
                try {
                    method.reset();
                } catch(Exception e) {
                    LOGGER.debug("Exception :", e);
                }
            }
        }
    }

    private static void logError(String errMsg, Throwable t) {
        LOGGER.error(MessageEnum.RA_NS_EXC, "VFC", "", MsoLogger.ErrorCode.AvailabilityError, errMsg, t);
        ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, errMsg);
    }

    private static void logError(String errMsg) {
        LOGGER.error(MessageEnum.RA_NS_EXC, "VFC", "", MsoLogger.ErrorCode.AvailabilityError, errMsg);
        ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, errMsg);
    }

    private static RestfulResponse createResponse(int statusCode, String content) {
        RestfulResponse rsp = new RestfulResponse();
        rsp.setStatus(statusCode);
        rsp.setResponseContent(content);
        return rsp;
    }

    /**
     * @param request
     * @return
     */
    // public static String getRequestBody(HttpServletRequest request) {
    // String body = null;
    // StringBuilder stringBuilder = new StringBuilder();
    // BufferedReader bufferedReader = null;
    // try {
    // InputStream inputStream = request.getInputStream();
    // if (inputStream != null) {
    // bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    // char[] charBuffer = new char[128];
    // int bytesRead = -1;
    // while ((bytesRead = bufferedReader.read(charBuffer)) > 0)
    // stringBuilder.append(charBuffer, 0, bytesRead);
    // }
    // } catch (IOException ex) {
    // LOGGER.error(MessageEnum.RA_NS_EXC, "VFC", "", MsoLogger.ErrorCode.AvailabilityError,
    // "read inputStream buffer catch exception:", ex);
    // } finally {
    // if (bufferedReader != null) {
    // try {
    // bufferedReader.close();
    // } catch (IOException ex) {
    // LOGGER.error(MessageEnum.RA_NS_EXC, "VFC", "", MsoLogger.ErrorCode.AvailabilityError,
    // "close buffer catch exception:", ex);
    // }
    // }
    // }
    //
    // body = stringBuilder.toString();
    // return body;
    // }

}