summaryrefslogtreecommitdiffstats
path: root/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java
blob: e3bc1b291a7fb42a9999c4f9fe945afe61f39699 (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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
 * 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.service.adapter.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.DownloadCsarManager;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmUtil;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.SystemEnvVariablesFactory;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VNFRestfulUtil;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.inf.IResourceManager;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.ConnectMgrVnfm;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.HttpRequests;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Resource Manager adapter class.
 * .</br>
 *
 * @author
 * @version VFC 1.0 Sep 13, 2016
 */
public class AdapterResourceManager implements IResourceManager {

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

    private static final String VNFD_FILE_PATH = "vnfd_file_path";

    @Override
    public JSONObject uploadVNFPackage(JSONObject vnfpkg, Map<String, String> paramsMap) {
        JSONObject resultObj = new JSONObject();
        String vnfDescriptorId = paramsMap.get("vnfDescriptorId");
        try {
            // if upper layer do not provide vnfpackage info,then get the
            // vnfpackage info from JSON file.
            if(vnfpkg == null || vnfpkg.isEmpty()) {
                String vnfPkgInfo = readVfnPkgInfoFromJson();
                JSONObject vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
                vnfpkg = vnfpkgJson.getJSONObject(vnfDescriptorId);
            }
        } catch(IOException e) {
            LOG.error("function=uploadVNFPackage", e);
        }

        // check if parameters are null.
        if(paramsMap == null || paramsMap.isEmpty()) {
            resultObj.put(Constant.REASON, "csarid and vnfmid are null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }

        String csarid = paramsMap.get("csarid");
        String vnfmid = paramsMap.get("vnfmid");
        String vnfdid = "";

        if(null == csarid || "".equals(csarid)) {
            resultObj.put(Constant.REASON, "csarid is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }
        if(null == vnfmid || "".equals(vnfmid)) {
            resultObj.put(Constant.REASON, "vnfmid is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }

        // obtain CSAR package info
        JSONObject csarobj = getVnfmCsarInfo(csarid);
        String downloadUri = "";
        if(Integer.valueOf(csarobj.get(Constant.RETCODE).toString()) == Constant.HTTP_OK) {
            LOG.info("get CSAR info successful.", csarobj.get(Constant.RETCODE));
            downloadUri = csarobj.getString("downloadUri");
        } else {
            LOG.error("get CSAR info fail.", csarobj.get(Constant.RETCODE));
            resultObj.put(Constant.REASON, csarobj.get(Constant.REASON).toString());
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }
        JSONObject csarTempObj = vnfpkg.getJSONObject("template");
        String csarfilepath = csarTempObj.getString("csar_file_path");
        String csarfilename = csarTempObj.getString("csar_file_name");

        // download csar package and save in location.
        JSONObject downloadObject = downloadCsar(downloadUri, csarfilepath + csarfilename);

        if(Integer.valueOf(downloadObject.get(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) {
            LOG.error("download CSAR fail." + downloadObject.get(Constant.RETCODE));
            resultObj.put(Constant.REASON, downloadObject.get(Constant.REASON).toString());
            resultObj.put(Constant.RETCODE, downloadObject.get(Constant.RETCODE).toString());
            return resultObj;
        }
        LOG.info("download CSAR successful." + downloadObject.get(Constant.RETCODE));

        // unzip csar package to location.
        JSONObject unzipObject = unzipCSAR(csarfilepath + csarfilename, csarfilepath);

        if(Integer.valueOf(unzipObject.get(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) {
            LOG.error("unzip CSAR fail.", unzipObject.get(Constant.RETCODE));
            resultObj.put(Constant.REASON, unzipObject.get(Constant.REASON).toString());
            resultObj.put(Constant.RETCODE, unzipObject.get(Constant.RETCODE).toString());
            return resultObj;
        }
        LOG.info("unzip CSAR successful.", unzipObject.get(Constant.RETCODE));

        // upload vnfd to ftps server
        JSONObject uploadResJson = uploadCsar(csarTempObj, csarfilepath);
        LOG.info("upload Csar result: {}.", uploadResJson);

        Map<String, String> vnfmMap = new HashMap<>();
        vnfmMap.put("url", String.format(UrlConstant.REST_VNFMINFO_GET, vnfmid));
        vnfmMap.put("methodType", Constant.GET);

        // get VNFM connection info
        // getVnfmConnInfo(vnfmMap)
        JSONObject vnfmObject = VnfmUtil.getVnfmById(vnfmid);
        if(Integer.valueOf(vnfmObject.get(Constant.RETCODE).toString()) != Constant.HTTP_OK) {
            LOG.error("get Vnfm Connection Info fail.", vnfmObject.get(Constant.RETCODE));
            resultObj.put(Constant.REASON, vnfmObject.get(Constant.REASON).toString());
            resultObj.put(Constant.RETCODE, vnfmObject.get(Constant.RETCODE).toString());
            return resultObj;
        }
        LOG.info("get Vnfm Connection Info successful.", vnfmObject.get(Constant.RETCODE));

        String vnfmUrl = vnfmObject.getString("url");
        String userName = vnfmObject.getString(Constant.USERNAME);
        String password = vnfmObject.getString(Constant.PASSWORD);

        // build VNFM connection and get token
        ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();

        JSONObject connObject = new JSONObject();
        connObject.put("url", vnfmUrl);
        connObject.put(Constant.USERNAME, userName);
        connObject.put(Constant.PASSWORD, password);
        if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, Constant.CERTIFICATE)) {
            LOG.error("get Access Session fail.");
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "connect fail.");
            return resultObj;
        }
        LOG.info("get Access Session successful.");
        String connToken = mgrVcmm.getAccessSession();

        // get vim_id
        JSONObject cloudObject = getAllCloud(vnfmUrl, connToken);
        String vimId = "";

        if(!cloudObject.isEmpty() && cloudObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
            LOG.info("get all cloud successful.", cloudObject.get(Constant.RETCODE));
            vimId = cloudObject.getString("vim_id");
        } else {
            LOG.error("get all cloud fail.", cloudObject.get(Constant.RETCODE));
            return cloudObject;
        }

        // upload VNF package
        csarTempObj.put("vim_id", vimId);
        vnfpkg.put("template", csarTempObj);
        LOG.info("vnfpkg: " + vnfpkg);

        JSONObject uploadPkgObject = upload(vnfpkg, vnfmUrl, connToken);
        LOG.info("uploadPkgObject:" + uploadPkgObject);
        if(!uploadPkgObject.isEmpty() && uploadPkgObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
            LOG.info("upload vnf package info successful.", uploadPkgObject.get(Constant.RETCODE));
            vnfdid = uploadPkgObject.getString("id");
        }

        if(vnfdid == null || "".equals(vnfdid.trim())) {
            JSONObject vnfdConf = readVnfdIdInfoFromJson();
            LOG.info("vnfdConf=" + vnfdConf);
            if(vnfdConf.containsKey(vnfDescriptorId)) {
                vnfdid = vnfdConf.getString(vnfDescriptorId);
            }
        }
        LOG.info("set vnfdId=" + vnfdid);

        // get vnfd version
        String vnfdVersion = "";

        JSONObject vnfdVerObject =
                getVnfdVersion(vnfmUrl, String.format(UrlConstant.URL_VNFDINFO_GET, vnfdid), connToken);
        LOG.info("vnfdVerObject:" + vnfdVerObject);
        if(!vnfdVerObject.isEmpty() && vnfdVerObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
            LOG.info("get vnfd version successful.", vnfdVerObject.get(Constant.RETCODE));
            JSONArray verArr = vnfdVerObject.getJSONArray("templates");
            JSONObject verTmpObj = verArr.getJSONObject(0);

            vnfdVersion = verTmpObj.getString("vnfdVersion");
        } else {
            LOG.error("get vnfd version fail.", vnfdVerObject.get(Constant.RETCODE));
            return vnfdVerObject;
        }

        // get vnfd plan info
        String planName = "";
        String planId = "";

        JSONObject vnfdPlanInfo = getVNFDPlanInfo(vnfmUrl, vnfdid, connToken);
        LOG.info("vnfdPlanInfo:" + vnfdPlanInfo);
        JSONObject inputsObj = new JSONObject();
        if(!vnfdPlanInfo.isEmpty() && vnfdPlanInfo.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
            LOG.info("get vnfd plan info successful.", vnfdPlanInfo.get(Constant.RETCODE));
            JSONObject planTmpObj = vnfdPlanInfo.getJSONObject("template");
            String templateName = planTmpObj.getString("template_name").trim();
            JSONArray topoTmpObj = planTmpObj.getJSONArray("topology_template");

            JSONObject planObj = topoTmpObj.getJSONObject(0);
            if("VNFD_vUGW".equals(templateName)) {
                for(int i = 0; i < topoTmpObj.size(); i++) {
                    String name = topoTmpObj.getJSONObject(i).getString("plan_name").trim();
                    if("Normal_E9K".equals(name)) {
                        planObj = topoTmpObj.getJSONObject(i);
                    }
                }
            }

            planName = planObj.getString("plan_name");
            planId = planObj.getString("plan_id");
            if(planObj.containsKey("inputs")) {
                JSONArray inputs = planObj.getJSONArray("inputs");
                for(int i = 0; i < inputs.size(); i++) {
                    JSONObject obj = inputs.getJSONObject(i);
                    obj.put("value", obj.getString("default"));
                }
                inputsObj.put("inputs", inputs);
                inputsObj.put("External_network", new JSONArray());
            }
        } else {
            LOG.error("get vnfd plan info fail.", vnfdPlanInfo.get(Constant.RETCODE));
            return vnfdPlanInfo;
        }

        // return values
        resultObj.put(Constant.RETCODE, Constant.HTTP_OK);
        resultObj.put("vnfdId", vnfdid);
        resultObj.put("vnfdVersion", vnfdVersion);
        resultObj.put("planName", planName);
        resultObj.put("planId", planId);
        resultObj.put("parameters", inputsObj);
        LOG.info("resultObj:" + resultObj.toString());

        return resultObj;
    }

    private JSONObject uploadCsar(JSONObject vnfpkg, String csarfilepath) {
        LOG.info("vnfpkg: " + vnfpkg + "csarfilepath:" + csarfilepath);
        JSONObject resJson = new JSONObject();

        boolean flag = false;
        FTPSClient ftpClient = new FTPSClient();
        try {
            ftpClient.connect(vnfpkg.getString("ftp_server_ip"), 21);
            ftpClient.login(vnfpkg.getString("ftp_username"), vnfpkg.getString("ftp_password"));
            int replyCode = ftpClient.getReplyCode();
            LOG.info("replyCode: " + replyCode);
            if(!FTPReply.isPositiveCompletion(replyCode)) {
                resJson.put("message", "Connect ftps server failed!");
                return resJson;
            }

            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.makeDirectory(vnfpkg.getString(VNFD_FILE_PATH));
            LOG.info("makeDirectory: " + ftpClient.makeDirectory(vnfpkg.getString(VNFD_FILE_PATH)));
            ftpClient.changeWorkingDirectory(vnfpkg.getString(VNFD_FILE_PATH));
            LOG.info("changeWorkingDirectory: " + ftpClient.changeWorkingDirectory(vnfpkg.getString(VNFD_FILE_PATH)));
            String vnfdPath = csarfilepath + "Artifacts/Deployment/OTHER/";
            LOG.info("vnfd_file_name: " + vnfdPath + vnfpkg.getString("vnfd_file_name"));
            InputStream inputStream = new FileInputStream(new File(vnfdPath + vnfpkg.getString("vnfd_file_name")));
            flag = ftpClient.storeFile(vnfpkg.getString("vnfd_file_name"), inputStream);
            if(flag) {
                resJson.put("message", "upload Csar success!");
            }
            inputStream.close();
            ftpClient.logout();
        } catch(Exception e) {
            LOG.error("Exception: " + e);
        } finally {
            if(ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch(IOException e) {
                    LOG.error("IOException: " + e);
                }
            }
        }
        return resJson;
    }

    private JSONObject sendRequest(Map<String, String> paramsMap) {
        JSONObject resultObj = new JSONObject();
        RestfulResponse rsp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
        if(null == rsp) {
            LOG.error("function=sendRequest,  RestfulResponse is null");
            resultObj.put(Constant.REASON, "RestfulResponse is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }
        String resultCreate = rsp.getResponseContent();

        if(rsp.getStatus() == Constant.HTTP_OK) {
            LOG.warn("function=sendRequest, msg= status={}, result={}.", rsp.getStatus(), resultCreate);
            resultObj = JSONObject.fromObject(resultCreate);
            resultObj.put(Constant.RETCODE, Constant.HTTP_OK);
            return resultObj;
        } else {
            LOG.error("function=sendRequest, msg=ESR return fail,status={}, result={}.", rsp.getStatus(), resultCreate);
            resultObj.put(Constant.REASON, "ESR return fail.");
        }
        resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
        return resultObj;
    }

    @Override
    public JSONObject getVnfmCsarInfo(String csarid) {
        JSONObject resultObj = new JSONObject();

        if(null == csarid || "".equals(csarid)) {
            resultObj.put(Constant.REASON, "csarid is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }

        Map<String, String> paramsMap = new HashMap<String, String>();

        paramsMap.put("url", String.format(UrlConstant.REST_CSARINFO_GET, csarid));
        paramsMap.put("methodType", Constant.GET);

        return this.sendRequest(paramsMap);
    }

    @Override
    public JSONObject downloadCsar(String url, String filePath) {
        JSONObject resultObj = new JSONObject();

        if(url == null || "".equals(url)) {
            resultObj.put(Constant.REASON, "url is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }
        if(filePath == null || "".equals(filePath)) {
            resultObj.put(Constant.REASON, "downloadUrl filePath is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }

        String status = DownloadCsarManager.download(url, filePath);

        if(Constant.DOWNLOADCSAR_SUCCESS.equals(status)) {
            resultObj.put(Constant.REASON, "download csar file successfully.");
            resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
        } else {
            resultObj.put(Constant.REASON, "download csar file failed.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
        }
        return resultObj;
    }

    @Override
    public JSONObject getAllCloud(String url, String conntoken) {
        JSONObject resultObj = new JSONObject();
        JSONArray resArray = new JSONArray();

        if(url == null || url.equals("")) {
            url = "http://" + Constant.LOCAL_HOST + ":31943";
        }

        // get vim_id
        HttpMethod httpMethodCloud = null;
        try {
            httpMethodCloud =
                    new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(url.trim(), UrlConstant.URL_ALLCLOUD_NEW_GET)
                            .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).setParams("").get().execute();

            int statusCode = httpMethodCloud.getStatusCode();

            String result = httpMethodCloud.getResponseBodyAsString();
            LOG.info(result);
            if(statusCode == HttpStatus.SC_OK) {
                JSONObject vimInfo = JSONObject.fromObject(result);
                resArray = vimInfo.getJSONArray("vim_info");
                resultObj = resArray.getJSONObject(0);
                resultObj.put(Constant.RETCODE, statusCode);
            } else {
                LOG.error("uploadVNFPackage get allcloud failed, code:" + statusCode + " re:" + result);
                resultObj.put(Constant.RETCODE, statusCode);
                resultObj.put(Constant.REASON, "get allcloud failed. code:" + statusCode + " re:" + result);
                return resultObj;
            }
        } catch(JSONException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud JSONException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get allcloud failed and JSONException." + e.getMessage());
            return resultObj;
        } catch(VnfmException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud VnfmException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get allcloud failed and VnfmException." + e.getMessage());
            return resultObj;
        } catch(IOException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud IOException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get allcloud failed and IOException." + e.getMessage());
            return resultObj;
        }
        return resultObj;
    }

    /**
     * Upload vnfpackage<br>
     *
     * @param vnfpackage
     * @param vnfmurl
     * @param conntoken
     * @return
     * @since VFC 1.0
     */
    public JSONObject upload(JSONObject vnfpackage, String vnfmurl, String conntoken) {
        JSONObject resultObj = new JSONObject();
        HttpMethod httpMethodVnf = null;

        try {
            httpMethodVnf = new HttpRequests.Builder(Constant.CERTIFICATE)
                    .setUrl(vnfmurl.trim(), UrlConstant.URL_VNFPACKAGE_POST).setParams(vnfpackage.toString())
                    .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).post().execute();

            int statusCodeUp = httpMethodVnf.getStatusCode();

            String resultUp = httpMethodVnf.getResponseBodyAsString();

            if(statusCodeUp == HttpStatus.SC_CREATED || statusCodeUp == HttpStatus.SC_OK) {
                LOG.info("uploadVNFPackage upload VNF package successful, code:" + statusCodeUp + " re:" + resultUp);
                resultObj = JSONObject.fromObject(resultUp);
                resultObj.put(Constant.RETCODE, statusCodeUp);
            } else {
                LOG.error("uploadVNFPackage upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
                resultObj.put(Constant.RETCODE, statusCodeUp);
                resultObj.put("data", "upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
                return resultObj;
            }
        } catch(JSONException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package JSONException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "upload VNF package failed and JSONException." + e.getMessage());
            return resultObj;
        } catch(VnfmException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package VnfmException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "upload VNF package failed and VnfmException." + e.getMessage());
            return resultObj;
        } catch(IOException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package IOException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "upload VNF package failed and IOException." + e.getMessage());
            return resultObj;
        }
        return resultObj;
    }

    /**
     * Find vnfd version.<br>
     *
     * @param prefixUrl
     * @param serviceUrl
     * @return
     * @since VFC 1.0
     */
    public JSONObject getVnfdVersion(String prefixUrl, String serviceUrl, String conntoken) {
        JSONObject resultObj = new JSONObject();
        HttpMethod httpMethodVnfd = null;
        try {
            httpMethodVnfd = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(prefixUrl.trim(), serviceUrl)
                    .setParams("").addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();

            int statusCodeVnfd = httpMethodVnfd.getStatusCode();

            String resultVnfd = httpMethodVnfd.getResponseBodyAsString();
            LOG.info("getVnfdVersion result:" + resultVnfd);
            if(statusCodeVnfd == HttpStatus.SC_OK) {
                resultObj = JSONObject.fromObject(resultVnfd);
                resultObj.put(Constant.RETCODE, statusCodeVnfd);
            } else {
                LOG.error("uploadVNFPackage vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
                resultObj.put(Constant.RETCODE, statusCodeVnfd);
                resultObj.put("data", "get vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
                return resultObj;
            }
        } catch(JSONException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version JSONException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get vnfd version failed and JSONException." + e.getMessage());
            return resultObj;
        } catch(VnfmException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version VnfmException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get vnfd version failed and VnfmException." + e.getMessage());
            return resultObj;
        } catch(IOException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version IOException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get vnfd version failed and IOException." + e.getMessage());
            return resultObj;
        }
        return resultObj;
    }

    /**
     * Find VNFM connection information.<br>
     *
     * @param paramsMap
     * @return
     * @since VFC 1.0
     */
    public JSONObject getVnfmConnInfo(Map<String, String> paramsMap) {
        return this.sendRequest(paramsMap);
    }

    @Override
    public JSONObject getVNFDPlanInfo(String url, String vnfdid, String conntoken) {
        JSONObject resultObj = new JSONObject();

        HttpMethod httpMethodPlan = null;
        try {
            httpMethodPlan = new HttpRequests.Builder(Constant.CERTIFICATE)
                    .setUrl(url.trim(), String.format(UrlConstant.URL_VNFDPLANINFO_GET, vnfdid)).setParams("")
                    .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();

            int statusCode = httpMethodPlan.getStatusCode();

            String result = httpMethodPlan.getResponseBodyAsString();
            LOG.info("getVNFDPlanInfo result=" + result);
            if(statusCode == HttpStatus.SC_OK) {
                resultObj = JSONObject.fromObject(result);
                resultObj.put(Constant.RETCODE, statusCode);
            } else {
                LOG.error("uploadVNFPackage get VNFDPlanInfo failed, code:" + statusCode + " re:" + result);
                resultObj.put(Constant.RETCODE, statusCode);
                resultObj.put(Constant.REASON, "get VNFDPlanInfo failed. code:" + statusCode + " re:" + result);
                return resultObj;
            }
        } catch(JSONException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo JSONException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and JSONException." + e.getMessage());
            return resultObj;
        } catch(VnfmException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo VnfmException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and VnfmException." + e.getMessage());
            return resultObj;
        } catch(IOException e) {
            LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo IOException e={}.", e);
            resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
            resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and IOException." + e.getMessage());
            return resultObj;
        }
        return resultObj;
    }

    /**
     * Get VNF package information.<br>
     *
     * @return
     * @throws IOException
     * @since VFC 1.0
     */
    public static String readVfnPkgInfoFromJson() throws IOException {
        InputStream ins = null;
        BufferedInputStream bins = null;
        String fileContent = "";

        String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
                + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
                + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + Constant.VNFPKGINFO;

        try {
            ins = new FileInputStream(fileName);
            bins = new BufferedInputStream(ins);

            byte[] contentByte = new byte[ins.available()];
            int num = bins.read(contentByte);

            if(num > 0) {
                fileContent = new String(contentByte);
            }
        } catch(FileNotFoundException e) {
            LOG.error(fileName + "is not found!", e);
        } finally {
            if(ins != null) {
                ins.close();
            }
            if(bins != null) {
                bins.close();
            }
        }

        return fileContent;
    }

    private static JSONObject readVnfdIdInfoFromJson() {
        JSONObject jsonObject = new JSONObject();
        InputStream ins = null;
        BufferedInputStream bins = null;
        String fileContent = "";

        String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
                + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
                + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfd_ids.json";

        try {
            ins = new FileInputStream(fileName);
            bins = new BufferedInputStream(ins);

            byte[] contentByte = new byte[ins.available()];
            int num = bins.read(contentByte);

            if(num > 0) {
                fileContent = new String(contentByte);
            }
            if(fileContent != null) {
                jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds");
            }
            ins.close();
            bins.close();
        } catch(Exception e) {
            LOG.error(fileName + " read error!", e);
        } finally {

        }

        return jsonObject;
    }

    /*
     * unzip CSAR packge
     * @param fileName filePath
     * @return
     */
    public JSONObject unzipCSAR(String fileName, String filePath) {
        LOG.info("fileName: " + fileName + ", filePath: " + filePath);
        JSONObject resultObj = new JSONObject();

        if(fileName == null || "".equals(fileName)) {
            resultObj.put(Constant.REASON, "fileName is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }
        if(filePath == null || "".equals(filePath)) {
            resultObj.put(Constant.REASON, "unzipCSAR filePath is null.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
            return resultObj;
        }

        int status = DownloadCsarManager.unzipCSAR(fileName, filePath);

        if(Constant.UNZIP_SUCCESS == status) {
            resultObj.put(Constant.REASON, "unzip csar file successfully.");
            resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
        } else {
            resultObj.put(Constant.REASON, "unzip csar file failed.");
            resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
        }
        return resultObj;
    }

}