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