2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common;
19 import java.io.IOException;
20 import java.lang.invoke.MethodHandle;
21 import java.lang.invoke.MethodHandles;
22 import java.lang.invoke.MethodType;
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;
32 import net.sf.json.JSONObject;
40 * @version NFVO 0.5 Aug 25, 2016
42 public final class ResultRequestUtil {
44 private static final Logger LOG = LoggerFactory.getLogger(ResultRequestUtil.class);
46 private ResultRequestUtil() throws VnfmException {
47 throw new VnfmException("can't be instanced.");
58 * [get, put, delete, post]
60 * raw data with json format, if <code>methodName</code> is get
61 * or delete, fill it with null
65 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson) {
66 JSONObject resultJson = new JSONObject();
68 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
70 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject)) {
71 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
72 resultJson.put("data", "connect fail.");
76 HttpMethod httpMethod = 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]);
88 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
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);
103 if(httpMethod != null) {
104 httpMethod.releaseConnection();
108 if(httpMethod == null) {
109 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
110 resultJson.put("data", "get connection error");
124 * [get, put, delete, post]
126 * raw data with json format, if <code>methodName</code> is get
127 * or delete, fill it with null
131 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson,
133 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
135 JSONObject resultJson = new JSONObject();
137 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
139 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, authModel)) {
140 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
141 resultJson.put("data", "connect fail.");
145 HttpMethod httpMethod = null;
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]);
157 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
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);
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);
179 if(httpMethod != null) {
180 httpMethod.releaseConnection();
184 if(httpMethod == null) {
185 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
186 resultJson.put("data", "get connection error");
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);
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);
212 if(httpMethodToken != null) {
213 httpMethodToken.releaseConnection();
229 public static JSONObject callSouth(JSONObject vnfmObject, String path, String methodName, String paramsJson,
231 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
233 JSONObject resultJson = new JSONObject();
235 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
237 if(Constant.HTTP_OK != mgrVcmm.connectSouth(vnfmObject, authModel)) {
238 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
239 resultJson.put("data", "connect fail.");
243 HttpMethod httpMethod = null;
246 String result = null;
247 String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
248 LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
249 mgrVcmm.getAccessSession());
250 HttpRequests.Builder builder =
251 new HttpRequests.Builder(authModel).addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
252 .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
253 MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
255 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
257 builder = (HttpRequests.Builder)mt.invoke();
258 httpMethod = builder.execute();
259 result = httpMethod.getResponseBodyAsString();
260 LOG.warn("function=call, msg=response status is {}. result is {}", httpMethod.getStatusCode(), result);
261 resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
262 resultJson.put("data", result);
264 // logout delete tokens
265 String token = mgrVcmm.getAccessSession();
266 String oldUrl = vnfmObject.getString("url").trim();
267 String newUrl = oldUrl.replaceAll("30001", "30000");
268 String user = vnfmObject.getString("userName");
269 removeV3Tokens(newUrl, token, user);
270 } catch(IOException e) {
271 LOG.info("function=call, msg=IOException, e is {}", e);
272 } catch(ReflectiveOperationException e) {
273 LOG.info("function=call, msg=ReflectiveOperationException, e is {}", e);
274 } catch(Throwable e) {
275 LOG.info("function=call, msg=Throwable, e is {}", e);
277 if(httpMethod != null) {
278 httpMethod.releaseConnection();
282 if(httpMethod == null) {
283 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
284 resultJson.put("data", "get connection error");
298 private static void removeV3Tokens(String vnfmUrl, String token, String user) {
299 HttpMethod httpMethodToken = null;
300 String tokenUrl = String.format(ParamConstants.CSM_AUTH_CONNECT_SOUTH_DISCONNECT, user);
301 LOG.info("removeTokens tokenUrl=" + tokenUrl);
303 httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
304 .setParams("").addHeader("X-Auth-Token", token).delete().execute();
305 int statusCode = httpMethodToken.getStatusCode();
306 String result = httpMethodToken.getResponseBodyAsString();
307 LOG.info("removeTokens int=" + statusCode + ", result=" + result);
308 } catch(IOException e) {
309 LOG.info("function=call, msg=IOException, e is {}", e);
310 } catch(Throwable e) {
311 LOG.info("function=call, msg=Throwable, e is {}", e);
313 if(httpMethodToken != null) {
314 httpMethodToken.releaseConnection();