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);
208 * @throws VnfmException
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);
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);
224 if(httpMethodToken != null) {
225 httpMethodToken.releaseConnection();
241 public static JSONObject callSouth(JSONObject vnfmObject, String path, String methodName, String paramsJson,
243 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
245 JSONObject resultJson = new JSONObject();
247 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
249 if(Constant.HTTP_OK != mgrVcmm.connectSouth(vnfmObject, authModel)) {
250 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
251 resultJson.put("data", CONNECT_FAIL);
255 HttpMethod httpMethod = null;
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);
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]);
270 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
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);
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);
290 if(httpMethod != null) {
291 httpMethod.releaseConnection();
295 if(httpMethod == null) {
296 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
297 resultJson.put("data", CONNECTION_ERROR);
309 * @throws VnfmException
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);
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);
325 if(httpMethodToken != null) {
326 httpMethodToken.releaseConnection();