2 * Copyright 2016 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.openo.nfvo.jujuvnfmadapter.common.servicetoken;
19 import java.util.HashMap;
22 import org.openo.baseservice.remoteservice.exception.ServiceException;
23 import org.openo.baseservice.roa.util.restclient.Restful;
24 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
25 import org.openo.baseservice.roa.util.restclient.RestfulOptions;
26 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
27 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
33 * Juju VNFM restful utility class.<br>
38 * @version NFVO 0.5 Sep 12, 2016
40 public final class JujuVnfmRestfulUtil {
42 public static final String GET_TYPE = "get";
44 public static final String ADD_TYPE = "add";
46 public static final String POST_TYPE = "post";
48 public static final String PUT_TYPE = "put";
50 public static final String DEL_TYPE = "delete";
52 public static final String METHOD_TYPE = "methodType";
54 public static final int ERROR_STATUS_CODE = -1;
56 public static final String CONTENT_TYPE = "Content-type";
58 public static final String APPLICATION = "application/json";
60 public static final String HEADER_AUTH_TOKEN = "X-Auth-Token";
62 private static final Logger LOG = LoggerFactory.getLogger(JujuVnfmRestfulUtil.class);
64 private JujuVnfmRestfulUtil() {
70 * Get Vim response content.<br>
73 * @param restParametes
78 public static String getVimResponseContent(String url, RestfulParametes restParametes, String type) {
79 Map<String, Object> resMap = getVimResponseContent(url, restParametes, null, type);
81 return resMap.get("responseContent").toString();
87 * Get Vim response content.<br>
90 * @param restParametes
96 public static Map<String, Object> getVimResponseContent(String url, RestfulParametes restParametes,
97 RestfulOptions opt, String type) {
98 Map<String, Object> resMap = new HashMap<>(2);
101 Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
102 RestfulResponse rsp = null;
104 if (GET_TYPE.equals(type)) {
105 rsp = rest.get(url, restParametes, opt);
106 } else if (ADD_TYPE.equals(type)) {
107 rsp = rest.post(url, restParametes, opt);
108 } else if (PUT_TYPE.equals(type)) {
109 rsp = rest.put(url, restParametes, opt);
110 } else if (DEL_TYPE.equals(type)) {
111 rsp = rest.delete(url, restParametes, opt);
114 resMap.put("responseContent", rsp.getResponseContent());
115 resMap.put("statusCode", rsp.getStatus());
118 LOG.info("get response data success!");
119 } catch (ServiceException e) {
120 LOG.error("get response data catch exception {}.", e);
128 * Get Vim response result.<br>
135 public static RestfulResponse getVimResponseResult(String url, String type) {
136 RestfulParametes restParametes = new RestfulParametes();
137 return getVimResponseResult(url, restParametes, type);
142 * Get Vim response result.<br>
145 * @param restParametes
150 public static RestfulResponse getVimResponseResult(String url, RestfulParametes restParametes, String type) {
151 return vimRestfulResponse(url, restParametes, null, type);
156 * Get Vim response.<br>
159 * @param restParametes
165 public static RestfulResponse vimRestfulResponse(String url, RestfulParametes restParametes, RestfulOptions opt,
167 RestfulResponse rsp = null;
169 Map<String, String> headerMap = new HashMap<>(2);
170 headerMap.put(CONTENT_TYPE, APPLICATION);
171 restParametes.setHeaderMap(headerMap);
172 Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
175 if (GET_TYPE.equals(type)) {
176 rsp = rest.get(url, restParametes, opt);
177 } else if (ADD_TYPE.equals(type)) {
178 rsp = rest.post(url, restParametes, opt);
179 } else if (PUT_TYPE.equals(type)) {
180 rsp = rest.put(url, restParametes, opt);
181 } else if (DEL_TYPE.equals(type)) {
182 rsp = rest.delete(url, restParametes, opt);
185 LOG.info("get response data success!");
186 } catch (ServiceException e) {
187 LOG.error("get response data catch ServiceException {}.", e);
194 * Get remote response.<br>
198 * @param domainTokens
203 public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens,
205 String url = paramsMap.get("url");
206 String methodType = paramsMap.get(METHOD_TYPE);
207 String path = paramsMap.get("path");
209 RestfulResponse rsp = null;
211 String sslOptionFile = "";
213 String restClientFile = "restclient.json";
216 sslOptionFile = "ssl.nfvo.properties";
220 rest = HttpRestHelp.getRestInstance(sslOptionFile, restClientFile, isHttps);
222 RestfulOptions opt = new RestfulOptions();
223 String[] strs = path.split("(http(s)?://)|:");
225 opt.setHost(strs[1]);
226 opt.setPort(Integer.parseInt(strs[2]));
228 RestfulParametes restfulParametes = new RestfulParametes();
229 Map<String, String> headerMap = new HashMap<>(3);
230 headerMap.put(CONTENT_TYPE, APPLICATION);
231 headerMap.put(HEADER_AUTH_TOKEN, domainTokens);
232 restfulParametes.setHeaderMap(headerMap);
233 restfulParametes.setRawData(params);
236 if (GET_TYPE.equalsIgnoreCase(methodType)) {
237 rsp = rest.get(url, restfulParametes, opt);
238 } else if (POST_TYPE.equalsIgnoreCase(methodType)) {
239 rsp = rest.post(url, restfulParametes, opt);
240 } else if (PUT_TYPE.equalsIgnoreCase(methodType)) {
241 rsp = rest.put(url, restfulParametes, opt);
242 } else if (DEL_TYPE.equalsIgnoreCase(methodType)) {
243 rsp = rest.delete(url, restfulParametes, opt);
246 } catch (ServiceException e) {
247 LOG.error("function=restfulResponse, get restful response catch exception {}", e);
254 * Get remote response.<br>
261 public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
262 if(null == paramsMap){
265 String url = paramsMap.get("url");
266 String methodType = paramsMap.get(METHOD_TYPE);
268 RestfulResponse rsp = null;
269 Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
272 RestfulParametes restfulParametes = new RestfulParametes();
273 Map<String, String> headerMap = new HashMap<>(3);
274 headerMap.put(CONTENT_TYPE, APPLICATION);
275 restfulParametes.setHeaderMap(headerMap);
276 restfulParametes.setRawData(params);
279 if (GET_TYPE.equalsIgnoreCase(methodType)) {
280 rsp = rest.get(url, restfulParametes);
281 } else if (POST_TYPE.equalsIgnoreCase(methodType)) {
282 rsp = rest.post(url, restfulParametes);
283 } else if (PUT_TYPE.equalsIgnoreCase(methodType)) {
284 rsp = rest.put(url, restfulParametes);
285 } else if (DEL_TYPE.equalsIgnoreCase(methodType)) {
286 rsp = rest.delete(url, restfulParametes);
289 } catch (ServiceException e) {
290 LOG.error("function=getRemoteResponse, get restful response catch exception {}", e);
297 * Generate parameter map.<br>
306 public static Map<String, String> generateParametesMap(String url, String methodType, String path,
308 Map<String, String> paramsMap = new HashMap<>(6);
309 paramsMap.put("url", url);
310 paramsMap.put(METHOD_TYPE, methodType);
311 paramsMap.put("path", path);
312 paramsMap.put("authMode", authMode);