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