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.http.client.methods.HttpPost;
26 import org.apache.commons.httpclient.HttpMethod;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.ConnectMgrVnfm;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.HttpRequests;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import net.sf.json.JSONObject;
42 * @version VFC 1.0 Aug 25, 2016
44 public final class ResultRequestUtil {
46 private static final Logger LOG = LoggerFactory.getLogger(ResultRequestUtil.class);
48 private static final String CONNECT_FAIL = "connect fail.";
50 private static final String RESPONSE_STATUS = "function=call, msg=response status is {}. result is {}";
52 private static final String IOEXCEPTION = "function=call, msg=IOException, e is {}";
54 private static final String REFLECTIVEOPERATIONEXCEPTION =
55 "function=call, msg=ReflectiveOperationException, e is {}";
57 private static final String THROWABLE = "function=call, msg=Throwable, e is {}";
59 private static final String CONNECTION_ERROR = "get connection error";
61 private ResultRequestUtil() throws VnfmException {
62 throw new VnfmException("can't be instanced.");
73 * [get, put, delete, post]
75 * raw data with json format, if <code>methodName</code> is get
76 * or delete, fill it with null
80 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson) {
81 JSONObject resultJson = new JSONObject();
83 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
85 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject)) {
86 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
87 resultJson.put("data", CONNECT_FAIL);
91 HttpMethod httpMethod = null;
95 String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
96 LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
97 mgrVcmm.getAccessSession());
98 HttpRequests.Builder builder = new HttpRequests.Builder(Constant.ANONYMOUS)
99 .addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
100 .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
101 MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
103 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
105 builder = (HttpRequests.Builder)mt.invoke();
106 httpMethod = builder.execute();
107 result = httpMethod.getResponseBodyAsString();
108 LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
109 resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
110 resultJson.put("data", result);
111 } catch(IOException e) {
112 LOG.info(IOEXCEPTION, e);
113 } catch(ReflectiveOperationException e) {
114 LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
115 } catch(Throwable e) {
116 LOG.info(THROWABLE, e);
118 if(httpMethod != null) {
119 httpMethod.releaseConnection();
123 if(httpMethod == null) {
124 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
125 resultJson.put("data", CONNECTION_ERROR);
131 private static JSONObject doPost(JSONObject vnfmObj, String path, String paramsJson, String authModel){
143 * [get, put, delete, post]
145 * raw data with json format, if <code>methodName</code> is get
146 * or delete, fill it with null
150 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson,
152 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
154 JSONObject resultJson = new JSONObject();
156 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
158 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, authModel)) {
159 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
160 resultJson.put("data", CONNECT_FAIL);
164 HttpMethod httpMethod = null;
167 String result = null;
168 String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
169 LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
170 mgrVcmm.getAccessSession());
171 HttpRequests.Builder builder =
172 new HttpRequests.Builder(authModel).addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
173 .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
174 MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
176 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
178 builder = (HttpRequests.Builder)mt.invoke();
179 httpMethod = builder.execute();
180 result = httpMethod.getResponseBodyAsString();
181 LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
182 resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
183 resultJson.put("data", result);
185 // logout delete tokens
186 String token = mgrVcmm.getAccessSession();
187 String roaRand = mgrVcmm.getRoaRand();
188 String vnfmUrl = vnfmObject.getString("url");
189 String user = vnfmObject.getString(Constant.USERNAME);
190 removeTokens(vnfmUrl, token, roaRand, user);
191 } catch(IOException e) {
192 LOG.info(IOEXCEPTION, e);
193 } catch(ReflectiveOperationException e) {
194 LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
195 } catch(Throwable e) {
196 LOG.info(THROWABLE, e);
198 if(httpMethod != null) {
199 httpMethod.releaseConnection();
203 if(httpMethod == null) {
204 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
205 resultJson.put("data", CONNECTION_ERROR);
214 * @throws VnfmException
217 private static void removeTokens(String vnfmUrl, String token, String roaRand, String user) throws VnfmException {
218 HttpMethod httpMethodToken = null;
219 String tokenUrl = String.format(ParamConstants.CSM_AUTH_DISCONNECT, user, roaRand);
220 LOG.info("removeTokens tokenUrl=" + tokenUrl);
222 httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
223 .setParams("").addHeader(Constant.X_AUTH_TOKEN, token).delete().execute();
224 int statusCode = httpMethodToken.getStatusCode();
225 String result = httpMethodToken.getResponseBodyAsString();
226 LOG.info("removeTokens int=" + statusCode + ", result=" + result);
227 } catch(IOException e) {
228 LOG.info(IOEXCEPTION, e);
230 if(httpMethodToken != null) {
231 httpMethodToken.releaseConnection();
247 public static JSONObject callSouth(JSONObject vnfmObject, String path, String methodName, String paramsJson,
249 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
251 JSONObject resultJson = new JSONObject();
253 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
255 if(Constant.HTTP_OK != mgrVcmm.connectSouth(vnfmObject, authModel)) {
256 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
257 resultJson.put("data", CONNECT_FAIL);
261 HttpMethod httpMethod = null;
264 String result = null;
265 String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
266 String oldUrl = vnfmObject.getString("url").trim();
267 String newUrl = oldUrl.replaceAll("30001", "30000");
268 LOG.info("function=callSouth, msg=url is {}, session is {}", newUrl + vnfPath, mgrVcmm.getAccessSession());
269 LOG.info("function=callSouth, paramsJson is {}", paramsJson);
271 HttpRequests.Builder builder =
272 new HttpRequests.Builder(authModel).addHeader(Constant.X_AUTH_TOKEN, mgrVcmm.getAccessSession())
273 .setUrl(newUrl, vnfPath).setParams(paramsJson);
274 MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
276 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
278 builder = (HttpRequests.Builder)mt.invoke();
279 httpMethod = builder.execute();
280 result = httpMethod.getResponseBodyAsString();
281 LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
282 resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
283 resultJson.put("data", result);
285 // logout delete tokens
286 String token = mgrVcmm.getAccessSession();
287 String user = vnfmObject.getString(Constant.USERNAME);
288 removeV3Tokens(newUrl, token, user);
289 } catch(IOException e) {
290 LOG.info(IOEXCEPTION, e);
291 } catch(ReflectiveOperationException e) {
292 LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
293 } catch(Throwable e) {
294 LOG.info(THROWABLE, e);
296 if(httpMethod != null) {
297 httpMethod.releaseConnection();
301 if(httpMethod == null) {
302 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
303 resultJson.put("data", CONNECTION_ERROR);
315 * @throws VnfmException
318 private static void removeV3Tokens(String vnfmUrl, String token, String user) throws VnfmException {
319 HttpMethod httpMethodToken = null;
320 String tokenUrl = String.format(ParamConstants.CSM_AUTH_CONNECT_SOUTH_DISCONNECT, user);
321 LOG.info("removeTokens tokenUrl=" + tokenUrl);
323 httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
324 .setParams("").addHeader(Constant.X_AUTH_TOKEN, token).delete().execute();
325 int statusCode = httpMethodToken.getStatusCode();
326 String result = httpMethodToken.getResponseBodyAsString();
327 LOG.info("removeTokens int=" + statusCode + ", result=" + result);
328 } catch(IOException e) {
329 LOG.info(IOEXCEPTION, e);
331 if(httpMethodToken != null) {
332 httpMethodToken.releaseConnection();