cbf8f39c7f02c2d7f72a85aeeb41d54e29ea4221
[vfc/nfvo/driver/vnfm/svnfm.git] /
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      * @since VFC 1.0
209      */
210     private static void removeTokens(String vnfmUrl, String token, String roaRand, String user) {
211         HttpMethod httpMethodToken = null;
212         String tokenUrl = String.format(ParamConstants.CSM_AUTH_DISCONNECT, user, roaRand);
213         LOG.info("removeTokens tokenUrl=" + tokenUrl);
214         try {
215             httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
216                     .setParams("").addHeader(Constant.X_AUTH_TOKEN, token).delete().execute();
217             int statusCode = httpMethodToken.getStatusCode();
218             String result = httpMethodToken.getResponseBodyAsString();
219             LOG.info("removeTokens int=" + statusCode + ", result=" + result);
220         } catch(IOException e) {
221             LOG.info(IOEXCEPTION, e);
222         } catch(Throwable e) {
223             LOG.info(THROWABLE, e);
224         } finally {
225             if(httpMethodToken != null) {
226                 httpMethodToken.releaseConnection();
227             }
228         }
229     }
230
231     /**
232      * <br>
233      * 
234      * @param vnfmObject
235      * @param path
236      * @param methodName
237      * @param paramsJson
238      * @param authModel
239      * @return
240      * @since VFC 1.0
241      */
242     public static JSONObject callSouth(JSONObject vnfmObject, String path, String methodName, String paramsJson,
243             String authModel) {
244         LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
245                 + vnfmObject);
246         JSONObject resultJson = new JSONObject();
247
248         ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
249
250         if(Constant.HTTP_OK != mgrVcmm.connectSouth(vnfmObject, authModel)) {
251             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
252             resultJson.put("data", CONNECT_FAIL);
253             return resultJson;
254         }
255
256         HttpMethod httpMethod = null;
257         try {
258
259             String result = null;
260             String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
261             String oldUrl = vnfmObject.getString("url").trim();
262             String newUrl = oldUrl.replaceAll("30001", "30000");
263             LOG.info("function=callSouth, msg=url is {}, session is {}", newUrl + vnfPath, mgrVcmm.getAccessSession());
264             LOG.info("function=callSouth, paramsJson is {}", paramsJson);
265
266             HttpRequests.Builder builder =
267                     new HttpRequests.Builder(authModel).addHeader(Constant.X_AUTH_TOKEN, mgrVcmm.getAccessSession())
268                             .setUrl(newUrl, vnfPath).setParams(paramsJson);
269             MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
270             MethodHandle mt =
271                     MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
272
273             builder = (HttpRequests.Builder)mt.invoke();
274             httpMethod = builder.execute();
275             result = httpMethod.getResponseBodyAsString();
276             LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
277             resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
278             resultJson.put("data", result);
279
280             // logout delete tokens
281             String token = mgrVcmm.getAccessSession();
282             String user = vnfmObject.getString(Constant.USERNAME);
283             removeV3Tokens(newUrl, token, user);
284         } catch(IOException e) {
285             LOG.info(IOEXCEPTION, e);
286         } catch(ReflectiveOperationException e) {
287             LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
288         } catch(Throwable e) {
289             LOG.info(THROWABLE, e);
290         } finally {
291             if(httpMethod != null) {
292                 httpMethod.releaseConnection();
293             }
294         }
295
296         if(httpMethod == null) {
297             resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
298             resultJson.put("data", CONNECTION_ERROR);
299         }
300
301         return resultJson;
302     }
303
304     /**
305      * <br>
306      * 
307      * @param vnfmUrl
308      * @param token
309      * @param user
310      * @since VFC 1.0
311      */
312     private static void removeV3Tokens(String vnfmUrl, String token, String user) {
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         } catch(Throwable e) {
325             LOG.info(THROWABLE, e);
326         } finally {
327             if(httpMethodToken != null) {
328                 httpMethodToken.releaseConnection();
329             }
330         }
331     }
332 }