summaryrefslogtreecommitdiffstats
path: root/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/servicetoken/VnfmRestfulUtil.java
blob: b7c9c7adab7e74b1990060a8e3a2961d85ff2c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.Restful;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulAsyncCallback;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulFactory;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulOptions;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulParametes;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.ServiceException;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * VNFM Restful Utility.
 * .</br>
 *
 * @author
 * @version VFC 1.0 Sep 10, 2016
 */
public final class VnfmRestfulUtil {

    public static final String TYPE_GET = "get";

    public static final String TYPE_ADD = "add";

    public static final String TYPE_POST = "post";

    public static final String TYPE_PUT = "put";

    public static final String TYPE_DEL = "delete";

    public static final int ERROR_STATUS_CODE = -1;

    private static final Logger LOG = LoggerFactory.getLogger(VnfmRestfulUtil.class);

    private VnfmRestfulUtil() {

    }

    /**
     * within our module, we support a default method to invoke
     *
     * @param path
     *            rest service url
     * @param methodNames
     *            [post, delete, put, get, asyncPost, asyncDelete, asyncPut,
     *            asyncGet]
     * @param bodyParam
     *            rest body msg
     * @return
     */
    @SuppressWarnings("unchecked")
    public static RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
        RestfulParametes restParametes = new RestfulParametes();
        Map<String, String> headerMap = new HashMap<>(2);
        headerMap.put(Constant.CONTENT_TYPE, Constant.APPLICATION);
        restParametes.setHeaderMap(headerMap);

        if(Constant.GET.equals(methodNames) || Constant.DELETE.equals(methodNames)) {
            if(null != bodyParam) {
                Map<String, String> vnfParamsMap = new HashMap<>(Constant.DEFAULT_COLLECTION_SIZE);
                if(path.contains("?")) {
                    String[] vnfUtlList = path.split("\\?");
                    String[] vnfParams = vnfUtlList[1].split("&");
                    int paramsSize = vnfParams.length;

                    for(int i = 0; i < paramsSize; i++) {
                        vnfParamsMap.put(vnfParams[i].split("=")[0], vnfParams[i].split("=")[1]);
                    }
                }

                String vnfParamKey = null;
                Iterator<String> nameItr = bodyParam.keys();
                while(nameItr.hasNext()) {
                    vnfParamKey = nameItr.next();
                    vnfParamsMap.put(vnfParamKey, bodyParam.get(vnfParamKey).toString());

                }
                LOG.warn("method is GET or DEL,and paramsMap = " + vnfParamsMap);
                restParametes.setParamMap(vnfParamsMap);
            }
        } else {
            restParametes.setRawData(bodyParam == null ? null : bodyParam.toString());
        }
        return getRestRes(methodNames, path, restParametes);
    }

    /**
     * encapsulate the java reflect exception
     *
     * @param methodName
     *            Restful's method
     * @param objects
     *            method param array
     * @return
     */
    private static boolean isAnyNull(Object... objects) {
        for(int i = 0; i < objects.length; i++) {
            if(objects[i] == null) {
                return true;
            }
        }
        return false;

    }

    private static Class<?>[] formArray(Object[] objects) {
        Class<?>[] vnfClasses = new Class[objects.length];
        for(int i = 0; i < objects.length; i++) {
            vnfClasses[i] = objects[i].getClass();
        }
        return vnfClasses;

    }

    /**
     * Helps to invoke http methods Restful
     * <br>
     *
     * @param methodName String
     * @param objects String
     * @return
     * @since VFC 1.0
     */
    public static RestfulResponse getRestRes(String methodName, Object... objects) {
        Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
        try {
            if(isAnyNull(objects, rest)) {
                return null;
            }

            Class<?>[] vnfClasses = formArray(objects);

            if(methodName.startsWith("async")) {
                vnfClasses[vnfClasses.length - 1] = RestfulAsyncCallback.class;
            }

            Class<?> rtType = methodName.startsWith("async") ? void.class : RestfulResponse.class;
            MethodType mt = MethodType.methodType(rtType, vnfClasses);
            Object reuslt = MethodHandles.lookup().findVirtual(rest.getClass(), methodName, mt).bindTo(rest)
                    .invokeWithArguments(objects);
            if(reuslt != null) {
                return (RestfulResponse)reuslt;
            }
            LOG.warn("function=getRestRes, msg: invoke Restful async {} method which return type is Void.", methodName);
            return null;
        } catch(ReflectiveOperationException e) {
            LOG.error("function=getRestRes, msg=error occurs, e={}.", e);
        } catch(ServiceException e) {

            LOG.error("function=getRestRes, msg=ServiceException occurs, status={}", e.getHttpCode());
            LOG.error("function=getRestRes, msg=ServiceException occurs, reason={}.", e.getCause().getMessage());
            LOG.error("function=getRestRes, msg=ServiceException occurs, e={}.", e);
            RestfulResponse response = new RestfulResponse();
            response.setStatus(e.getHttpCode());
            response.setResponseJson(e.getCause().getMessage());
            return response;

        } catch(Throwable e) { // NOSONAR
            try {
                throw (VnfmException)new VnfmException().initCause(e.getCause());
            } catch(VnfmException e1) {
                LOG.error("function=getRestRes, msg=VnfmException occurs, e={},e1={}.", e1, e);
            }

        }
        return null;
    }

    /**
     * Helps to send Request to vnfm.
     * <br>
     *
     * @param path String
     * @param methodName String
     * @param paraJson JSONObject
     * @return
     * @since VFC 1.0
     */
    public static JSONObject sendReqToApp(String path, String methodName, JSONObject paraJson) {
        JSONObject retJson = new JSONObject();
        retJson.put(Constant.RETCODE, Constant.REST_FAIL);
        String abPath = null;
        String vnfmId = null;
        if(paraJson != null && paraJson.containsKey("vnfmInfo")) {
            JSONObject vnfmObj = paraJson.getJSONObject("vnfmInfo");
            vnfmId = vnfmObj.getString("id");
        } else {
            abPath = path;
        }
        LOG.warn("function=sendReqToApp, msg=url to send to app is: " + abPath);

        RestfulResponse restfulResponse = VnfmRestfulUtil.getRestResByDefault(path, methodName, paraJson);
        if(restfulResponse == null || abPath == null) {
            LOG.error("function=sendReqToApp, msg=data from app is null");
            retJson.put("data", "get null result");
        } else if(restfulResponse.getStatus() == Constant.HTTP_OK) {
            JSONObject object = JSONObject.fromObject(restfulResponse.getResponseContent());
            if(!abPath.contains("vnfdmgr/v1")) {
                LOG.warn("function=sendReqToApp, msg=result from app is: " + object.toString());
            }
            if(object.getInt(Constant.RETCODE) == Constant.REST_SUCCESS) {
                retJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
                retJson.put("data", withVnfmIdSuffix(vnfmId, object.get("data")));
                return retJson;
            } else {
                retJson.put(Constant.RETCODE, Constant.REST_FAIL);
                if(object.containsKey("msg")) {
                    retJson.put("data", object.getString("msg"));
                    return retJson;
                } else {
                    return object;
                }
            }
        } else {
            LOG.error("function=sendReqToApp, msg=status from app is: " + restfulResponse.getStatus());
            LOG.error("function=sendReqToApp, msg=result from app is: " + restfulResponse.getResponseContent());
            retJson.put("data", "send to app get error status: " + restfulResponse.getStatus());
        }
        return retJson;
    }

    /**
     * append suffix to result with vnfmId
     *
     * @param vnfmId
     * @param dataJson
     * @return
     */
    private static Object withVnfmIdSuffix(String vnfmId, Object dataJson) {
        Object result = new Object();
        if(vnfmId == null) {
            return dataJson;
        }

        if(dataJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject)dataJson;
            jsonObject.put("vnfmId", vnfmId);
            result = jsonObject;
        } else if(dataJson instanceof JSONArray) {
            JSONArray dataArray = (JSONArray)dataJson;
            JSONArray resultArray = new JSONArray();

            for(Object obj : dataArray) {
                JSONObject jsonObject = JSONObject.fromObject(obj);
                jsonObject.put("vnfmId", vnfmId);
                resultArray.add(jsonObject);
            }
            result = resultArray;
        }
        return result;
    }

    /**
     * Make request and get HTTP response
     * <br>
     *
     * @param paramsMap Map<String, String>
     * @param params String
     * @param domainTokens String
     * @param isNfvoApp boolean
     * @return
     * @since VFC 1.0
     */
    public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params, String domainTokens,
            boolean isNfvoApp) {
        String utilUrl = paramsMap.get("url");
        String utilMethodType = paramsMap.get("methodType");
        String utilPath = paramsMap.get("path");
        String authMode = paramsMap.get("authMode");

        RestfulResponse rsp = null;
        Restful rest = null;
        String sslOptionFile = "";
        try {
            String restClientFile = "restclient.json";

            if(isNfvoApp) {
                sslOptionFile = "ssl.nfvo.properties";
            } else {
                sslOptionFile = "ssl.vcmm.properties";
            }

            LOG.warn("function=getRemoteResponse,AuthenticationMode=" + authMode);

            rest = HttpRestfulHelp.getRestInstance(sslOptionFile, restClientFile);

            RestfulOptions opt = new RestfulOptions();
            String[] strs = utilPath.split("(http(s)?://)|:");

            opt.setHost(strs[1]);
            opt.setPort(Integer.parseInt(strs[2]));

            RestfulParametes restfulParametes = new RestfulParametes();
            Map<String, String> headerMap = new HashMap<>(3);
            headerMap.put(Constant.CONTENT_TYPE, Constant.APPLICATION);
            headerMap.put(Constant.HEADER_AUTH_TOKEN, domainTokens);
            restfulParametes.setHeaderMap(headerMap);
            restfulParametes.setRawData(params);

            if(rest != null) {
                if(TYPE_GET.equalsIgnoreCase(utilMethodType)) {
                    rsp = rest.get(utilUrl, restfulParametes, opt);
                } else if(TYPE_POST.equalsIgnoreCase(utilMethodType)) {
                    rsp = rest.post(utilUrl, restfulParametes, opt);
                } else if(TYPE_PUT.equalsIgnoreCase(utilMethodType)) {
                    rsp = rest.put(utilUrl, restfulParametes, opt);
                } else if(TYPE_DEL.equalsIgnoreCase(utilMethodType)) {
                    rsp = rest.delete(utilUrl, restfulParametes, opt);
                }
            }
        } catch(ServiceException e) {
            LOG.error("function=restfulResponse, get restful response catch exception {}", e);
        }
        return rsp;
    }

    /**
     * Make request and get HTTP response
     * <br>
     *
     * @param url String
     * @param methodType String
     * @param params String
     * @return
     * @since VFC 1.0
     */
    public static RestfulResponse getRemoteResponse(String url, String methodType, String params) {
        RestfulResponse rsp = null;
        Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
        try {

            RestfulParametes restfulParametes = new RestfulParametes();
            Map<String, String> headerMap = new HashMap<>(3);
            headerMap.put(Constant.CONTENT_TYPE, Constant.APPLICATION);
            restfulParametes.setHeaderMap(headerMap);
            if(params != null) {
                restfulParametes.setRawData(params);
            }

            if(rest != null) {
                if(TYPE_GET.equalsIgnoreCase(methodType)) {
                    rsp = rest.get(url, restfulParametes);
                } else if(TYPE_POST.equalsIgnoreCase(methodType)) {
                    rsp = rest.post(url, restfulParametes);
                } else if(TYPE_PUT.equalsIgnoreCase(methodType)) {
                    rsp = rest.put(url, restfulParametes);
                } else if(TYPE_DEL.equalsIgnoreCase(methodType)) {
                    rsp = rest.delete(url, restfulParametes);
                }
            }
        } catch(ServiceException e) {
            LOG.error("function=getRemoteResponse, get restful response catch exception {}", e);
        }
        return rsp;
    }

    public static RestfulResponse getRemoteResponse(String url, String methodType, Map<String, String> headerMap,
            String params) {
        RestfulResponse rsp = null;
        Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
        try {

            RestfulParametes restfulParametes = new RestfulParametes();
            restfulParametes.setHeaderMap(headerMap);
            if(params != null) {
                restfulParametes.setRawData(params);
            }

            if(rest != null) {
                if(TYPE_GET.equalsIgnoreCase(methodType)) {
                    rsp = rest.get(url, restfulParametes);
                } else if(TYPE_POST.equalsIgnoreCase(methodType)) {
                    rsp = rest.post(url, restfulParametes);
                } else if(TYPE_PUT.equalsIgnoreCase(methodType)) {
                    rsp = rest.put(url, restfulParametes);
                } else if(TYPE_DEL.equalsIgnoreCase(methodType)) {
                    rsp = rest.delete(url, restfulParametes);
                }
            }
        } catch(ServiceException e) {
            LOG.error("function=getRemoteResponse, get restful response catch exception {}", e);
        }
        return rsp;
    }

    /**
     * Make Params map<br>
     *
     * @param url String
     * @param methodType String
     * @param path String
     * @param authMode String
     * @return
     * @since VFC 1.0
     */
    public static Map<String, String> generateParamsMap(String url, String methodType, String path, String authMode) {
        Map<String, String> utilParamsMap = new HashMap<>(6);
        utilParamsMap.put("url", url);
        utilParamsMap.put("methodType", methodType);
        utilParamsMap.put("path", path);
        utilParamsMap.put("authMode", authMode);
        return utilParamsMap;
    }

}