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