238e7ad77f35d4e255027675313d749c30a744e6
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / openo / nfvo / jujuvnfmadapter / common / servicetoken / JujuVnfmRestfulUtil.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.nfvo.jujuvnfmadapter.common.servicetoken;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
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;
30
31 /**
32  * 
33  * Juju VNFM restful utility class.<br>
34  * <p>
35  * </p>
36  * 
37  * @author
38  * @version     NFVO 0.5  Sep 12, 2016
39  */
40 public final class JujuVnfmRestfulUtil {
41
42     public static final String GET_TYPE = "get";
43
44     public static final String ADD_TYPE = "add";
45
46     public static final String POST_TYPE = "post";
47
48     public static final String PUT_TYPE = "put";
49
50     public static final String DEL_TYPE = "delete";
51
52     public static final String METHOD_TYPE = "methodType";
53
54     public static final int ERROR_STATUS_CODE = -1;
55
56     public static final String CONTENT_TYPE = "Content-type";
57
58     public static final String APPLICATION = "application/json";
59
60     public static final String HEADER_AUTH_TOKEN = "X-Auth-Token";
61
62     private static final Logger LOG = LoggerFactory.getLogger(JujuVnfmRestfulUtil.class);
63
64     private JujuVnfmRestfulUtil() {
65         // constructor
66     }
67
68     /**
69      * 
70      * Get Vim response content.<br>
71      * 
72      * @param url
73      * @param restParametes
74      * @param type
75      * @return
76      * @since  NFVO 0.5
77      */
78     public static String getVimResponseContent(String url, RestfulParametes restParametes, String type) {
79         Map<String, Object> resMap = getVimResponseContent(url, restParametes, null, type);
80
81         return resMap.get("responseContent").toString();
82
83     }
84
85     /**
86      * 
87      * Get Vim response content.<br>
88      * 
89      * @param url
90      * @param restParametes
91      * @param opt
92      * @param type
93      * @return
94      * @since  NFVO 0.5
95      */
96     public static Map<String, Object> getVimResponseContent(String url, RestfulParametes restParametes,
97             RestfulOptions opt, String type) {
98         Map<String, Object> resMap = new HashMap<>(2);
99
100         try {
101             Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
102             RestfulResponse rsp = null;
103             if (rest != 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);
112                 }
113                 if (null != rsp) {
114                     resMap.put("responseContent", rsp.getResponseContent());
115                     resMap.put("statusCode", rsp.getStatus());
116                 }
117             }
118             LOG.info("get response data success!");
119         } catch (ServiceException e) {
120             LOG.error("get response data catch exception {}.", e);
121         }
122
123         return resMap;
124     }
125
126     /**
127      * 
128      * Get Vim response result.<br>
129      * 
130      * @param url
131      * @param type
132      * @return
133      * @since  NFVO 0.5
134      */
135     public static RestfulResponse getVimResponseResult(String url, String type) {
136         RestfulParametes restParametes = new RestfulParametes();
137         return getVimResponseResult(url, restParametes, type);
138     }
139
140     /**
141      * 
142      * Get Vim response result.<br>
143      * 
144      * @param url
145      * @param restParametes
146      * @param type
147      * @return
148      * @since  NFVO 0.5
149      */
150     public static RestfulResponse getVimResponseResult(String url, RestfulParametes restParametes, String type) {
151         return vimRestfulResponse(url, restParametes, null, type);
152     }
153
154     /**
155      * 
156      * Get Vim response.<br>
157      * 
158      * @param url
159      * @param restParametes
160      * @param opt
161      * @param type
162      * @return
163      * @since  NFVO 0.5
164      */
165     public static RestfulResponse vimRestfulResponse(String url, RestfulParametes restParametes, RestfulOptions opt,
166             String type) {
167         RestfulResponse rsp = null;
168         try {
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);
173
174             if (rest != null) {
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);
183                 }
184             }
185             LOG.info("get response data success!");
186         } catch (ServiceException e) {
187             LOG.error("get response data catch ServiceException {}.", e);
188         }
189         return rsp;
190     }
191
192     /**
193      * 
194      * Get remote response.<br>
195      * 
196      * @param paramsMap
197      * @param params
198      * @param domainTokens
199      * @param isHttps
200      * @return
201      * @since  NFVO 0.5
202      */
203     public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens,
204             boolean isHttps) {
205         String url = paramsMap.get("url");
206         String methodType = paramsMap.get(METHOD_TYPE);
207         String path = paramsMap.get("path");
208
209         RestfulResponse rsp = null;
210         Restful rest = null;
211         String sslOptionFile = "";
212         try {
213             String restClientFile = "restclient.json";
214
215             if (isHttps) {
216                 sslOptionFile = "ssl.nfvo.properties";
217
218             }
219
220             rest = HttpRestHelp.getRestInstance(sslOptionFile, restClientFile, isHttps);
221
222             RestfulOptions opt = new RestfulOptions();
223             String[] strs = path.split("(http(s)?://)|:");
224
225             opt.setHost(strs[1]);
226             opt.setPort(Integer.parseInt(strs[2]));
227
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);
234
235             if (rest != null) {
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);
244                 }
245             }
246         } catch (ServiceException e) {
247             LOG.error("function=restfulResponse, get restful response catch exception {}", e);
248         }
249         return rsp;
250     }
251
252     /**
253      * 
254      * Get remote response.<br>
255      * 
256      * @param paramsMap
257      * @param params
258      * @return
259      * @since  NFVO 0.5
260      */
261     public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
262         if(null == paramsMap){
263             return null;
264         }
265         String url = paramsMap.get("url");
266         String methodType = paramsMap.get(METHOD_TYPE);
267
268         RestfulResponse rsp = null;
269         Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
270         try {
271
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);
277
278             if (rest != null) {
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);
287                 }
288             }
289         } catch (ServiceException e) {
290             LOG.error("function=getRemoteResponse, get restful response catch exception {}", e);
291         }
292         return rsp;
293     }
294     
295     /**
296      * 
297      * Generate parameter map.<br>
298      * 
299      * @param url
300      * @param methodType
301      * @param path
302      * @param authMode
303      * @return
304      * @since  NFVO 0.5
305      */
306     public static Map<String, String> generateParametesMap(String url, String methodType, String path,
307             String authMode) {
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);
313         return paramsMap;
314     }
315 }