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;
23 import org.apache.commons.httpclient.HttpMethod;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.ConnectMgrVnfm;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.HttpRequests;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import net.sf.json.JSONObject;
39 * @version VFC 1.0 Aug 25, 2016
41 public final class ResultRequestUtil {
43 private static final Logger LOG = LoggerFactory.getLogger(ResultRequestUtil.class);
45 private static final String CONNECT_FAIL = "connect fail.";
47 private static final String RESPONSE_STATUS = "function=call, msg=response status is {}. result is {}";
49 private static final String IOEXCEPTION = "function=call, msg=IOException, e is {}";
51 private static final String REFLECTIVEOPERATIONEXCEPTION =
52 "function=call, msg=ReflectiveOperationException, e is {}";
54 private static final String THROWABLE = "function=call, msg=Throwable, e is {}";
56 private static final String CONNECTION_ERROR = "get connection error";
58 private ResultRequestUtil() throws VnfmException {
59 throw new VnfmException("can't be instanced.");
70 * [get, put, delete, post]
72 * raw data with json format, if <code>methodName</code> is get
73 * or delete, fill it with null
77 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson) {
78 JSONObject resultJson = new JSONObject();
80 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
82 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject)) {
83 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
84 resultJson.put("data", CONNECT_FAIL);
88 HttpMethod httpMethod = null;
92 String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
93 LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
94 mgrVcmm.getAccessSession());
95 HttpRequests.Builder builder = new HttpRequests.Builder(Constant.ANONYMOUS)
96 .addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
97 .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
98 MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
100 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
102 builder = (HttpRequests.Builder)mt.invoke();
103 httpMethod = builder.execute();
104 result = httpMethod.getResponseBodyAsString();
105 LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
106 resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
107 resultJson.put("data", result);
108 } catch(IOException e) {
109 LOG.info(IOEXCEPTION, e);
110 } catch(ReflectiveOperationException e) {
111 LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
112 } catch(Throwable e) {
113 LOG.info(THROWABLE, e);
115 if(httpMethod != null) {
116 httpMethod.releaseConnection();
120 if(httpMethod == null) {
121 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
122 resultJson.put("data", CONNECTION_ERROR);
128 private static JSONObject doPost(JSONObject vnfmObj, String path, String paramsJson, String authModel){
140 * [get, put, delete, post]
142 * raw data with json format, if <code>methodName</code> is get
143 * or delete, fill it with null
147 public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson,
149 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
151 JSONObject resultJson = new JSONObject();
153 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
155 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, authModel)) {
156 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
157 resultJson.put("data", CONNECT_FAIL);
161 HttpMethod httpMethod = null;
164 String result = null;
165 String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
166 LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath,
167 mgrVcmm.getAccessSession());
168 HttpRequests.Builder builder =
169 new HttpRequests.Builder(authModel).addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession())
170 .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson);
171 MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
173 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
175 builder = (HttpRequests.Builder)mt.invoke();
176 httpMethod = builder.execute();
177 result = httpMethod.getResponseBodyAsString();
178 LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
179 resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
180 resultJson.put("data", result);
182 // logout delete tokens
183 String token = mgrVcmm.getAccessSession();
184 String roaRand = mgrVcmm.getRoaRand();
185 String vnfmUrl = vnfmObject.getString("url");
186 String user = vnfmObject.getString(Constant.USERNAME);
187 removeTokens(vnfmUrl, token, roaRand, user);
188 } catch(IOException e) {
189 LOG.info(IOEXCEPTION, e);
190 } catch(ReflectiveOperationException e) {
191 LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
192 } catch(Throwable e) {
193 LOG.info(THROWABLE, e);
195 if(httpMethod != null) {
196 httpMethod.releaseConnection();
200 if(httpMethod == null) {
201 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
202 resultJson.put("data", CONNECTION_ERROR);
211 * @throws VnfmException
214 private static void removeTokens(String vnfmUrl, String token, String roaRand, String user) throws VnfmException {
215 HttpMethod httpMethodToken = null;
216 String tokenUrl = String.format(ParamConstants.CSM_AUTH_DISCONNECT, user, roaRand);
217 LOG.info("removeTokens tokenUrl=" + tokenUrl);
219 httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
220 .setParams("").addHeader(Constant.X_AUTH_TOKEN, token).delete().execute();
221 int statusCode = httpMethodToken.getStatusCode();
222 String result = httpMethodToken.getResponseBodyAsString();
223 LOG.info("removeTokens int=" + statusCode + ", result=" + result);
224 } catch(IOException e) {
225 LOG.info(IOEXCEPTION, e);
227 if(httpMethodToken != null) {
228 httpMethodToken.releaseConnection();
244 public static JSONObject callSouth(JSONObject vnfmObject, String path, String methodName, String paramsJson,
246 LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo="
248 JSONObject resultJson = new JSONObject();
250 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
252 if(Constant.HTTP_OK != mgrVcmm.connectSouth(vnfmObject, authModel)) {
253 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
254 resultJson.put("data", CONNECT_FAIL);
258 HttpMethod httpMethod = null;
261 String result = null;
262 String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path;
263 String oldUrl = vnfmObject.getString("url").trim();
264 String newUrl = oldUrl.replaceAll("30001", "30000");
265 LOG.info("function=callSouth, msg=url is {}, session is {}", newUrl + vnfPath, mgrVcmm.getAccessSession());
266 LOG.info("function=callSouth, paramsJson is {}", paramsJson);
268 HttpRequests.Builder builder =
269 new HttpRequests.Builder(authModel).addHeader(Constant.X_AUTH_TOKEN, mgrVcmm.getAccessSession())
270 .setUrl(newUrl, vnfPath).setParams(paramsJson);
271 MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]);
273 MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType).bindTo(builder);
275 builder = (HttpRequests.Builder)mt.invoke();
276 httpMethod = builder.execute();
277 result = httpMethod.getResponseBodyAsString();
278 LOG.warn(RESPONSE_STATUS, httpMethod.getStatusCode(), result);
279 resultJson.put(Constant.RETCODE, httpMethod.getStatusCode());
280 resultJson.put("data", result);
282 // logout delete tokens
283 String token = mgrVcmm.getAccessSession();
284 String user = vnfmObject.getString(Constant.USERNAME);
285 removeV3Tokens(newUrl, token, user);
286 } catch(IOException e) {
287 LOG.info(IOEXCEPTION, e);
288 } catch(ReflectiveOperationException e) {
289 LOG.info(REFLECTIVEOPERATIONEXCEPTION, e);
290 } catch(Throwable e) {
291 LOG.info(THROWABLE, e);
293 if(httpMethod != null) {
294 httpMethod.releaseConnection();
298 if(httpMethod == null) {
299 resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
300 resultJson.put("data", CONNECTION_ERROR);
312 * @throws VnfmException
315 private static void removeV3Tokens(String vnfmUrl, String token, String user) throws VnfmException {
316 HttpMethod httpMethodToken = null;
317 String tokenUrl = String.format(ParamConstants.CSM_AUTH_CONNECT_SOUTH_DISCONNECT, user);
318 LOG.info("removeTokens tokenUrl=" + tokenUrl);
320 httpMethodToken = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(vnfmUrl.trim(), tokenUrl)
321 .setParams("").addHeader(Constant.X_AUTH_TOKEN, token).delete().execute();
322 int statusCode = httpMethodToken.getStatusCode();
323 String result = httpMethodToken.getResponseBodyAsString();
324 LOG.info("removeTokens int=" + statusCode + ", result=" + result);
325 } catch(IOException e) {
326 LOG.info(IOEXCEPTION, e);
328 if(httpMethodToken != null) {
329 httpMethodToken.releaseConnection();