fix vulnerabilties issue: commons-httpclient
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / ResultRequestUtil.java
1 /*
2  * Copyright 2016-2017 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.vfc.nfvo.vnfm.svnfm.vnfmadapter.common;
18
19 import java.io.IOException;
20 import java.lang.invoke.MethodHandle;
21 import java.lang.invoke.MethodHandles;
22 import java.lang.invoke.MethodType;
23
24 import org.apache.http.client.methods.HttpPost;
25
26 import org.apache.commons.httpclient.HttpMethod;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.ConnectMgrVnfm;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.HttpRequests;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import net.sf.json.JSONObject;
35
36 /**
37  * <br/>
38  * <p>
39  * </p>
40  *
41  * @author
42  * @version VFC 1.0 Aug 25, 2016
43  */
44 public final class ResultRequestUtil {
45
46     private static final Logger LOG = LoggerFactory.getLogger(ResultRequestUtil.class);
47
48     private static final String CONNECT_FAIL = "connect fail.";
49
50     private static final String RESPONSE_STATUS = "function=call, msg=response status is {}. result is {}";
51
52     private static final String IOEXCEPTION = "function=call, msg=IOException, e is {}";
53
54     private static final String REFLECTIVEOPERATIONEXCEPTION =
55             "function=call, msg=ReflectiveOperationException, e is {}";
56
57     private static final String THROWABLE = "function=call, msg=Throwable, e is {}";
58
59     private static final String CONNECTION_ERROR = "get connection error";
60
61     private ResultRequestUtil() throws VnfmException {
62         throw new VnfmException("can't be instanced.");
63     }
64
65     /**
66      * common method
67      * <br/>
68      *
69      * @param vnfmObject
70      * @param path
71      *            url defined
72      * @param methodName
73      *            [get, put, delete, post]
74      * @param paramsJson
75      *            raw data with json format, if <code>methodName</code> is get
76      *            or delete, fill it with null
77      * @return
78      * @since VFC 1.0
79      */
80     public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson) {
81         JSONObject resultJson = new JSONObject();
82
83         ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
84
85         if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject)) {
86             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
87             resultJson.put("data", CONNECT_FAIL);
88             return resultJson;
89         }
90
91         HttpMethod httpMethod = null;
92         try {
93
94             String result = null;
95             String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
96             LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
97                     mgrVcmm.getAccessSession());
98             HttpRequests.Builder builder = new HttpRequests.Builder(Constant.ANONYMOUS)
99                     .addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
100                     .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
101             MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
102             MethodHandle mt =
103                     MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
104
105             builder = (HttpRequests.Builder)mt.invoke();
106             httpMethod = builder.execute();
107             result = httpMethod.getResponseBodyAsString();
108             LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
109             resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
110             resultJson.put("data", result);
111         } catch(IOException e) {
112             LOG.info(IOEXCEPTION, e);
113         } catch(ReflectiveOperationException e) {
114             LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
115         } catch(Throwable e) {
116             LOG.info(THROWABLE, e);
117         } finally {
118             if(httpMethod != null) {
119                 httpMethod.releaseConnection();
120             }
121         }
122
123         if(httpMethod == null) {
124             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
125             resultJson.put("data", CONNECTION_ERROR);
126         }
127
128         return resultJson;
129     }
130 /*
131     private static JSONObject doPost(JSONObject vnfmObj, String path, String paramsJson, String authModel){
132         return "aa";
133     }
134 */
135     /**
136      * common method
137      * <br/>
138      *
139      * @param vnfmObject
140      * @param path
141      *            url defined
142      * @param methodName
143      *            [get, put, delete, post]
144      * @param paramsJson
145      *            raw data with json format, if <code>methodName</code> is get
146      *            or delete, fill it with null
147      * @return
148      * @since VFC 1.0
149      */
150     public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson,
151             String authModel) {
152         LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
153                 + vnfmObject);
154         JSONObject resultJson = new JSONObject();
155
156         ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
157
158         if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, authModel)) {
159             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
160             resultJson.put("data", CONNECT_FAIL);
161             return resultJson;
162         }
163
164         HttpMethod httpMethod = null;
165         try {
166
167             String result = null;
168             String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
169             LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
170                     mgrVcmm.getAccessSession());
171             HttpRequests.Builder builder =
172                     new HttpRequests.Builder(authModel).addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
173                             .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
174             MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
175             MethodHandle mt =
176                     MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
177
178             builder = (HttpRequests.Builder)mt.invoke();
179             httpMethod = builder.execute();
180             result = httpMethod.getResponseBodyAsString();
181             LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
182             resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
183             resultJson.put("data", result);
184
185             // logout delete tokens
186             String token = mgrVcmm.getAccessSession();
187             String roaRand = mgrVcmm.getRoaRand();
188             String vnfmUrl = vnfmObject.getString("url");
189             String user = vnfmObject.getString(Constant.USERNAME);
190             removeTokens(vnfmUrl, token, roaRand, user);
191         } catch(IOException e) {
192             LOG.info(IOEXCEPTION, e);
193         } catch(ReflectiveOperationException e) {
194             LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
195         } catch(Throwable e) {
196             LOG.info(THROWABLE, e);
197         } finally {
198             if(httpMethod != null) {
199                 httpMethod.releaseConnection();
200             }
201         }
202
203         if(httpMethod == null) {
204             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
205             resultJson.put("data", CONNECTION_ERROR);
206         }
207
208         return resultJson;
209     }
210
211     /**
212      * <br>
213      * 
214      * @throws VnfmException
215      * @since VFC 1.0
216      */
217     private static void removeTokens(String vnfmUrl, String token, String roaRand, String user) throws VnfmException {
218         HttpMethod httpMethodToken = null;
219         String tokenUrl = String.format(ParamConstants.CSM_AUTH_DISCONNECT, user, roaRand);
220         LOG.info("removeTokens tokenUrl=" + tokenUrl);
221         try {
222             httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
223                     .setParams("").addHeader(Constant.X_AUTH_TOKEN, token).delete().execute();
224             int statusCode = httpMethodToken.getStatusCode();
225             String result = httpMethodToken.getResponseBodyAsString();
226             LOG.info("removeTokens int=" + statusCode + ", result=" + result);
227         } catch(IOException e) {
228             LOG.info(IOEXCEPTION, e);
229         } finally {
230             if(httpMethodToken != null) {
231                 httpMethodToken.releaseConnection();
232             }
233         }
234     }
235
236     /**
237      * <br>
238      * 
239      * @param vnfmObject
240      * @param path
241      * @param methodName
242      * @param paramsJson
243      * @param authModel
244      * @return
245      * @since VFC 1.0
246      */
247     public static JSONObject callSouth(JSONObject vnfmObject, String path, String methodName, String paramsJson,
248             String authModel) {
249         LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
250                 + vnfmObject);
251         JSONObject resultJson = new JSONObject();
252
253         ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
254
255         if(Constant.HTTP_OK != mgrVcmm.connectSouth(vnfmObject, authModel)) {
256             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
257             resultJson.put("data", CONNECT_FAIL);
258             return resultJson;
259         }
260
261         HttpMethod httpMethod = null;
262         try {
263
264             String result = null;
265             String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
266             String oldUrl = vnfmObject.getString("url").trim();
267             String newUrl = oldUrl.replaceAll("30001", "30000");
268             LOG.info("function=callSouth, msg=url is {}, session is {}", newUrl + vnfPath, mgrVcmm.getAccessSession());
269             LOG.info("function=callSouth, paramsJson is {}", paramsJson);
270
271             HttpRequests.Builder builder =
272                     new HttpRequests.Builder(authModel).addHeader(Constant.X_AUTH_TOKEN, mgrVcmm.getAccessSession())
273                             .setUrl(newUrl, vnfPath).setParams(paramsJson);
274             MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
275             MethodHandle mt =
276                     MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
277
278             builder = (HttpRequests.Builder)mt.invoke();
279             httpMethod = builder.execute();
280             result = httpMethod.getResponseBodyAsString();
281             LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
282             resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
283             resultJson.put("data", result);
284
285             // logout delete tokens
286             String token = mgrVcmm.getAccessSession();
287             String user = vnfmObject.getString(Constant.USERNAME);
288             removeV3Tokens(newUrl, token, user);
289         } catch(IOException e) {
290             LOG.info(IOEXCEPTION, e);
291         } catch(ReflectiveOperationException e) {
292             LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
293         } catch(Throwable e) {
294             LOG.info(THROWABLE, e);
295         } finally {
296             if(httpMethod != null) {
297                 httpMethod.releaseConnection();
298             }
299         }
300
301         if(httpMethod == null) {
302             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
303             resultJson.put("data", CONNECTION_ERROR);
304         }
305
306         return resultJson;
307     }
308
309     /**
310      * <br>
311      * 
312      * @param vnfmUrl
313      * @param token
314      * @param user
315      * @throws VnfmException
316      * @since VFC 1.0
317      */
318     private static void removeV3Tokens(String vnfmUrl, String token, String user) throws VnfmException {
319         HttpMethod httpMethodToken = null;
320         String tokenUrl = String.format(ParamConstants.CSM_AUTH_CONNECT_SOUTH_DISCONNECT, user);
321         LOG.info("removeTokens tokenUrl=" + tokenUrl);
322         try {
323             httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
324                     .setParams("").addHeader(Constant.X_AUTH_TOKEN, token).delete().execute();
325             int statusCode = httpMethodToken.getStatusCode();
326             String result = httpMethodToken.getResponseBodyAsString();
327             LOG.info("removeTokens int=" + statusCode + ", result=" + result);
328         } catch(IOException e) {
329             LOG.info(IOEXCEPTION, e);
330         } finally {
331             if(httpMethodToken != null) {
332                 httpMethodToken.releaseConnection();
333             }
334         }
335     }
336 }