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 VFC 1.0 Aug 25, 2016
42 public final class ResultRequestUtil {
44 private static final Logger LOG = LoggerFactory.getLogger(ResultRequestUtil.class);
46 private static final String CONNECT_FAIL = "connect fail.";
48 private static final String RESPONSE_STATUS = "function=call, msg=response status is {}. result is {}";
50 private static final String IOEXCEPTION = "function=call, msg=IOException, e is {}";
52 private static final String REFLECTIVEOPERATIONEXCEPTION =
53 "function=call, msg=ReflectiveOperationException, e is {}";
55 private static final String THROWABLE = "function=call, msg=Throwable, e is {}";
57 private static final String CONNECTION_ERROR = "get connection error";
59 private ResultRequestUtil() throws VnfmException {
60 throw new VnfmException("can't be instanced.");
71 * [get, put, delete, post]
73 * raw data with json format, if <code>methodName</code> is get
74 * or delete, fill it with null
78 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson) {
79 JSONObject resultJson = new JSONObject();
81 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
83 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject)) {
84 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
85 resultJson.put("data", CONNECT_FAIL);
89 HttpMethod httpMethod = 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]);
101 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
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);
116 if(httpMethod != null) {
117 httpMethod.releaseConnection();
121 if(httpMethod == null) {
122 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
123 resultJson.put("data", CONNECTION_ERROR);
137 * [get, put, delete, post]
139 * raw data with json format, if <code>methodName</code> is get
140 * or delete, fill it with null
144 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson,
146 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
148 JSONObject resultJson = new JSONObject();
150 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
152 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, authModel)) {
153 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
154 resultJson.put("data", CONNECT_FAIL);
158 HttpMethod httpMethod = null;
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]);
170 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
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);
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);
192 if(httpMethod != null) {
193 httpMethod.releaseConnection();
197 if(httpMethod == null) {
198 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
199 resultJson.put("data", CONNECTION_ERROR);
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);
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);
225 if(httpMethodToken != null) {
226 httpMethodToken.releaseConnection();
242 public static JSONObject callSouth(JSONObject vnfmObject, String path, String methodName, String paramsJson,
244 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
246 JSONObject resultJson = new JSONObject();
248 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
250 if(Constant.HTTP_OK != mgrVcmm.connectSouth(vnfmObject, authModel)) {
251 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
252 resultJson.put("data", CONNECT_FAIL);
256 HttpMethod httpMethod = null;
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);
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]);
271 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
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);
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);
291 if(httpMethod != null) {
292 httpMethod.releaseConnection();
296 if(httpMethod == null) {
297 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
298 resultJson.put("data", CONNECTION_ERROR);
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);
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);
327 if(httpMethodToken != null) {
328 httpMethodToken.releaseConnection();