Fix VNF instantiate problem
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / adapter / impl / AdapterResourceManager.java
1 /*
2  * Copyright 2016-2017 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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl;
18
19 import java.io.BufferedInputStream;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.commons.httpclient.HttpMethod;
28 import org.apache.commons.httpclient.HttpStatus;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.DownloadCsarManager;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
31 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
32 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.SystemEnvVariablesFactory;
33 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VNFRestfulUtil;
34 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.inf.IResourceManager;
35 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
36 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
37 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.ConnectMgrVnfm;
38 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.HttpRequests;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import net.sf.json.JSONArray;
43 import net.sf.json.JSONException;
44 import net.sf.json.JSONObject;
45
46 /**
47  * Resource Manager adapter class.
48  * .</br>
49  *
50  * @author
51  * @version VFC 1.0 Sep 13, 2016
52  */
53 public class AdapterResourceManager implements IResourceManager {
54
55     private static final Logger LOG = LoggerFactory.getLogger(AdapterResourceManager.class);
56
57     @Override
58     public JSONObject uploadVNFPackage(JSONObject vnfpkg, Map<String, String> paramsMap) {
59         JSONObject resultObj = new JSONObject();
60         String vnfDescriptorId = paramsMap.get("vnfDescriptorId");
61         try {
62             // if upper layer do not provide vnfpackage info,then get the
63             // vnfpackage info from JSON file.
64             if(vnfpkg == null || vnfpkg.isEmpty()) {
65                 String vnfPkgInfo = readVfnPkgInfoFromJson();
66                 JSONObject vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
67                 vnfpkg = vnfpkgJson.getJSONObject(vnfDescriptorId);
68             }
69         } catch(IOException e) {
70             LOG.error("function=uploadVNFPackage", e);
71         }
72
73         // check if parameters are null.
74         if(paramsMap == null || paramsMap.isEmpty()) {
75             resultObj.put("reason", "csarid and vnfmid are null.");
76             resultObj.put("retCode", Constant.REST_FAIL);
77             return resultObj;
78         }
79
80         String csarid = paramsMap.get("csarid");
81         String vnfmid = paramsMap.get("vnfmid");
82         String vnfdid = "";
83
84         if(null == csarid || "".equals(csarid)) {
85             resultObj.put("reason", "csarid is null.");
86             resultObj.put("retCode", Constant.REST_FAIL);
87             return resultObj;
88         }
89         if(null == vnfmid || "".equals(vnfmid)) {
90             resultObj.put("reason", "vnfmid is null.");
91             resultObj.put("retCode", Constant.REST_FAIL);
92             return resultObj;
93         }
94
95         // obtain CSAR package info
96         JSONObject csarobj = getVnfmCsarInfo(csarid);
97         String downloadUri = "";
98         if(Integer.valueOf(csarobj.get("retCode").toString()) == Constant.HTTP_OK) {
99             LOG.info("get CSAR info successful.", csarobj.get("retCode"));
100             downloadUri = csarobj.getString("downloadUri");
101         } else {
102             LOG.error("get CSAR info fail.", csarobj.get("retCode"));
103             resultObj.put("reason", csarobj.get("reason").toString());
104             resultObj.put("retCode", Constant.REST_FAIL);
105             return resultObj;
106         }
107         JSONObject csarTempObj = new JSONObject();
108         csarTempObj = vnfpkg.getJSONObject("template");
109         String csarfilepath = csarTempObj.getString("csar_file_path");
110         String csarfilename = csarTempObj.getString("csar_file_name");
111
112         // download csar package and save in location.
113         JSONObject downloadObject =
114                 downloadCsar(downloadUri, csarfilepath + System.getProperty(Constant.FILE_SEPARATOR) + csarfilename);
115
116         if(Integer.valueOf(downloadObject.get("retCode").toString()) != Constant.REST_SUCCESS) {
117             LOG.error("download CSAR fail.", downloadObject.get("retCode"));
118             resultObj.put("reason", downloadObject.get("reason").toString());
119             resultObj.put("retCode", downloadObject.get("retCode").toString());
120             return resultObj;
121         }
122         LOG.info("download CSAR successful.", downloadObject.get("retCode"));
123
124         // unzip csar package to location.
125         JSONObject unzipObject =
126                 unzipCSAR(csarfilepath + System.getProperty(Constant.FILE_SEPARATOR) + csarfilename, csarfilepath);
127
128         if(Integer.valueOf(unzipObject.get("retCode").toString()) != Constant.REST_SUCCESS) {
129             LOG.error("unzip CSAR fail.", unzipObject.get("retCode"));
130             resultObj.put("reason", unzipObject.get("reason").toString());
131             resultObj.put("retCode", unzipObject.get("retCode").toString());
132             return resultObj;
133         }
134         LOG.info("unzip CSAR successful.", unzipObject.get("retCode"));
135
136         Map<String, String> vnfmMap = new HashMap<>();
137         vnfmMap.put("url", String.format(UrlConstant.REST_VNFMINFO_GET, vnfmid));
138         vnfmMap.put("methodType", Constant.GET);
139
140         // get VNFM connection info
141         JSONObject vnfmObject = getVnfmConnInfo(vnfmMap);
142         if(Integer.valueOf(vnfmObject.get("retCode").toString()) != Constant.HTTP_OK) {
143             LOG.error("get Vnfm Connection Info fail.", vnfmObject.get("retCode"));
144             resultObj.put("reason", vnfmObject.get("reason").toString());
145             resultObj.put("retCode", vnfmObject.get("retCode").toString());
146             return resultObj;
147         }
148         LOG.info("get Vnfm Connection Info successful.", vnfmObject.get("retCode"));
149
150         String vnfmUrl = vnfmObject.getString("url");
151         String userName = vnfmObject.getString("userName");
152         String password = vnfmObject.getString("password");
153
154         // build VNFM connection and get token
155         ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
156
157         JSONObject connObject = new JSONObject();
158         connObject.put("url", vnfmUrl);
159         connObject.put("userName", userName);
160         connObject.put("password", password);
161         if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, Constant.CERTIFICATE)) {
162             LOG.error("get Access Session fail.");
163             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
164             resultObj.put("reason", "connect fail.");
165             return resultObj;
166         }
167         LOG.info("get Access Session successful.");
168         String connToken = mgrVcmm.getAccessSession();
169
170         // get vim_id
171         JSONObject cloudObject = getAllCloud(vnfmUrl, connToken);
172         String vimId = "";
173
174         if(!cloudObject.isEmpty() && cloudObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
175             LOG.info("get all cloud successful.", cloudObject.get(Constant.RETCODE));
176             vimId = cloudObject.getString("vim_id");
177         } else {
178             LOG.error("get all cloud fail.", cloudObject.get(Constant.RETCODE));
179             return cloudObject;
180         }
181
182         // upload VNF package
183         csarTempObj.put("vim_id", vimId);
184         vnfpkg.put("template", csarTempObj);
185         LOG.info("vnfpkg: " + vnfpkg);
186
187         JSONObject uploadPkgObject = upload(vnfpkg, vnfmUrl, connToken);
188         LOG.info("uploadPkgObject:" + uploadPkgObject);
189         if(!uploadPkgObject.isEmpty() && uploadPkgObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
190             LOG.info("upload vnf package info successful.", uploadPkgObject.get(Constant.RETCODE));
191             vnfdid = uploadPkgObject.getString("id");
192         }
193
194         if(vnfdid == null || "".equals(vnfdid.trim())) {
195             JSONObject vnfdConf = readVnfdIdInfoFromJson();
196             LOG.info("vnfdConf=" + vnfdConf);
197             if(vnfdConf.containsKey(vnfDescriptorId)) {
198                 vnfdid = vnfdConf.getString(vnfDescriptorId);
199             }
200         }
201         LOG.info("set vnfdId=" + vnfdid);
202
203         // get vnfd version
204         String vnfdVersion = "";
205
206         JSONObject vnfdVerObject =
207                 getVnfdVersion(vnfmUrl, String.format(UrlConstant.URL_VNFDINFO_GET, vnfdid), connToken);
208         LOG.info("vnfdVerObject:" + vnfdVerObject);
209         if(!vnfdVerObject.isEmpty() && vnfdVerObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
210             LOG.info("get vnfd version successful.", vnfdVerObject.get(Constant.RETCODE));
211             JSONArray verArr = vnfdVerObject.getJSONArray("templates");
212             JSONObject verTmpObj = verArr.getJSONObject(0);
213
214             vnfdVersion = verTmpObj.getString("vnfdVersion");
215         } else {
216             LOG.error("get vnfd version fail.", vnfdVerObject.get(Constant.RETCODE));
217             return vnfdVerObject;
218         }
219
220         // get vnfd plan info
221         String planName = "";
222         String planId = "";
223
224         JSONObject vnfdPlanInfo = getVNFDPlanInfo(vnfmUrl, vnfdid, connToken);
225         LOG.info("vnfdPlanInfo:" + vnfdPlanInfo);
226         JSONObject inputsObj = new JSONObject();
227         if(!vnfdPlanInfo.isEmpty() && vnfdPlanInfo.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
228             LOG.info("get vnfd plan info successful.", vnfdPlanInfo.get(Constant.RETCODE));
229             JSONObject planTmpObj = vnfdPlanInfo.getJSONObject("template");
230             String templateName = planTmpObj.getString("template_name").trim();
231             JSONArray topoTmpObj = planTmpObj.getJSONArray("topology_template");
232
233             JSONObject planObj = topoTmpObj.getJSONObject(0);
234             if("VNFD_vUGW".equals(templateName)) {
235                 for(int i = 0; i < topoTmpObj.size(); i++) {
236                     String name = topoTmpObj.getJSONObject(i).getString("plan_name").trim();
237                     if("Normal_E9K".equals(name)) {
238                         planObj = topoTmpObj.getJSONObject(i);
239                     }
240                 }
241             }
242
243             planName = planObj.getString("plan_name");
244             planId = planObj.getString("plan_id");
245             if(planObj.containsKey("inputs")) {
246                 JSONArray inputs = planObj.getJSONArray("inputs");
247                 for(int i = 0; i < inputs.size(); i++) {
248                     JSONObject obj = inputs.getJSONObject(i);
249                     obj.put("value", obj.getString("default"));
250                 }
251                 inputsObj.put("inputs", inputs);
252                 inputsObj.put("External_network", new JSONArray());
253             }
254         } else {
255             LOG.error("get vnfd plan info fail.", vnfdPlanInfo.get(Constant.RETCODE));
256             return vnfdPlanInfo;
257         }
258
259         // return values
260         resultObj.put("retCode", Constant.HTTP_OK);
261         resultObj.put("vnfdId", vnfdid);
262         resultObj.put("vnfdVersion", vnfdVersion);
263         resultObj.put("planName", planName);
264         resultObj.put("planId", planId);
265         resultObj.put("parameters", inputsObj);
266         LOG.info("resultObj:" + resultObj.toString());
267
268         return resultObj;
269     }
270
271     private JSONObject sendRequest(Map<String, String> paramsMap) {
272         JSONObject resultObj = new JSONObject();
273         RestfulResponse rsp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
274         if(null == rsp) {
275             LOG.error("function=sendRequest,  RestfulResponse is null");
276             resultObj.put("reason", "RestfulResponse is null.");
277             resultObj.put("retCode", Constant.REST_FAIL);
278             return resultObj;
279         }
280         String resultCreate = rsp.getResponseContent();
281
282         if(rsp.getStatus() == Constant.HTTP_OK) {
283             LOG.warn("function=sendRequest, msg= status={}, result={}.", rsp.getStatus(), resultCreate);
284             resultObj = JSONObject.fromObject(resultCreate);
285             resultObj.put("retCode", Constant.HTTP_OK);
286             return resultObj;
287         } else {
288             LOG.error("function=sendRequest, msg=ESR return fail,status={}, result={}.", rsp.getStatus(), resultCreate);
289             resultObj.put("reason", "ESR return fail.");
290         }
291         resultObj.put("retCode", Constant.REST_FAIL);
292         return resultObj;
293     }
294
295     @Override
296     public JSONObject getVnfmCsarInfo(String csarid) {
297         JSONObject resultObj = new JSONObject();
298
299         if(null == csarid || "".equals(csarid)) {
300             resultObj.put("reason", "csarid is null.");
301             resultObj.put("retCode", Constant.REST_FAIL);
302             return resultObj;
303         }
304
305         Map<String, String> paramsMap = new HashMap<String, String>();
306
307         paramsMap.put("url", String.format(UrlConstant.REST_CSARINFO_GET, csarid));
308         paramsMap.put("methodType", Constant.GET);
309
310         return this.sendRequest(paramsMap);
311     }
312
313     @Override
314     public JSONObject downloadCsar(String url, String filePath) {
315         JSONObject resultObj = new JSONObject();
316
317         if(url == null || "".equals(url)) {
318             resultObj.put("reason", "url is null.");
319             resultObj.put("retCode", Constant.REST_FAIL);
320             return resultObj;
321         }
322         if(filePath == null || "".equals(filePath)) {
323             resultObj.put("reason", "downloadUrl filePath is null.");
324             resultObj.put("retCode", Constant.REST_FAIL);
325             return resultObj;
326         }
327
328         String status = DownloadCsarManager.download(url, filePath);
329
330         if(Constant.DOWNLOADCSAR_SUCCESS.equals(status)) {
331             resultObj.put("reason", "download csar file successfully.");
332             resultObj.put("retCode", Constant.REST_SUCCESS);
333         } else {
334             resultObj.put("reason", "download csar file failed.");
335             resultObj.put("retCode", Constant.REST_FAIL);
336         }
337         return resultObj;
338     }
339
340     @Override
341     public JSONObject getAllCloud(String url, String conntoken) {
342         JSONObject resultObj = new JSONObject();
343         JSONArray resArray = new JSONArray();
344
345         if(url == null || url.equals("")) {
346             url = "http://127.0.0.1:31943";
347         }
348
349         // get vim_id
350         HttpMethod httpMethodCloud = null;
351         try {
352             httpMethodCloud =
353                     new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(url.trim(), UrlConstant.URL_ALLCLOUD_NEW_GET)
354                             .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).setParams("").get().execute();
355
356             int statusCode = httpMethodCloud.getStatusCode();
357
358             String result = httpMethodCloud.getResponseBodyAsString();
359             LOG.info(result);
360             if(statusCode == HttpStatus.SC_OK) {
361                 JSONObject vimInfo = JSONObject.fromObject(result);
362                 resArray = vimInfo.getJSONArray("vim_info");
363                 resultObj = resArray.getJSONObject(0);
364                 resultObj.put(Constant.RETCODE, statusCode);
365             } else {
366                 LOG.error("uploadVNFPackage get allcloud failed, code:" + statusCode + " re:" + result);
367                 resultObj.put(Constant.RETCODE, statusCode);
368                 resultObj.put("reason", "get allcloud failed. code:" + statusCode + " re:" + result);
369                 return resultObj;
370             }
371         } catch(JSONException e) {
372             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud JSONException e={}.", e);
373             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
374             resultObj.put("reason", "get allcloud failed and JSONException." + e.getMessage());
375             return resultObj;
376         } catch(VnfmException e) {
377             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud VnfmException e={}.", e);
378             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
379             resultObj.put("reason", "get allcloud failed and VnfmException." + e.getMessage());
380             return resultObj;
381         } catch(IOException e) {
382             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud IOException e={}.", e);
383             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
384             resultObj.put("reason", "get allcloud failed and IOException." + e.getMessage());
385             return resultObj;
386         }
387         return resultObj;
388     }
389
390     /**
391      * Upload vnfpackage<br>
392      *
393      * @param vnfpackage
394      * @param vnfmurl
395      * @param conntoken
396      * @return
397      * @since VFC 1.0
398      */
399     public JSONObject upload(JSONObject vnfpackage, String vnfmurl, String conntoken) {
400         JSONObject resultObj = new JSONObject();
401         HttpMethod httpMethodVnf = null;
402
403         try {
404             httpMethodVnf = new HttpRequests.Builder(Constant.CERTIFICATE)
405                     .setUrl(vnfmurl.trim(), UrlConstant.URL_VNFPACKAGE_POST).setParams(vnfpackage.toString())
406                     .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).post().execute();
407
408             int statusCodeUp = httpMethodVnf.getStatusCode();
409
410             String resultUp = httpMethodVnf.getResponseBodyAsString();
411
412             if(statusCodeUp == HttpStatus.SC_CREATED || statusCodeUp == HttpStatus.SC_OK) {
413                 LOG.info("uploadVNFPackage upload VNF package successful, code:" + statusCodeUp + " re:" + resultUp);
414                 resultObj = JSONObject.fromObject(resultUp);
415                 resultObj.put(Constant.RETCODE, statusCodeUp);
416             } else {
417                 LOG.error("uploadVNFPackage upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
418                 resultObj.put(Constant.RETCODE, statusCodeUp);
419                 resultObj.put("data", "upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
420                 return resultObj;
421             }
422         } catch(JSONException e) {
423             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package JSONException e={}.", e);
424             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
425             resultObj.put("reason", "upload VNF package failed and JSONException." + e.getMessage());
426             return resultObj;
427         } catch(VnfmException e) {
428             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package VnfmException e={}.", e);
429             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
430             resultObj.put("reason", "upload VNF package failed and VnfmException." + e.getMessage());
431             return resultObj;
432         } catch(IOException e) {
433             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package IOException e={}.", e);
434             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
435             resultObj.put("reason", "upload VNF package failed and IOException." + e.getMessage());
436             return resultObj;
437         }
438         return resultObj;
439     }
440
441     /**
442      * Find vnfd version.<br>
443      *
444      * @param prefixUrl
445      * @param serviceUrl
446      * @return
447      * @since VFC 1.0
448      */
449     public JSONObject getVnfdVersion(String prefixUrl, String serviceUrl, String conntoken) {
450         JSONObject resultObj = new JSONObject();
451         HttpMethod httpMethodVnfd = null;
452         try {
453             httpMethodVnfd = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(prefixUrl.trim(), serviceUrl)
454                     .setParams("").addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();
455
456             int statusCodeVnfd = httpMethodVnfd.getStatusCode();
457
458             String resultVnfd = httpMethodVnfd.getResponseBodyAsString();
459             LOG.info("getVnfdVersion result:" + resultVnfd);
460             if(statusCodeVnfd == HttpStatus.SC_OK) {
461                 resultObj = JSONObject.fromObject(resultVnfd);
462                 resultObj.put(Constant.RETCODE, statusCodeVnfd);
463             } else {
464                 LOG.error("uploadVNFPackage vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
465                 resultObj.put(Constant.RETCODE, statusCodeVnfd);
466                 resultObj.put("data", "get vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
467                 return resultObj;
468             }
469         } catch(JSONException e) {
470             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version JSONException e={}.", e);
471             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
472             resultObj.put("reason", "get vnfd version failed and JSONException." + e.getMessage());
473             return resultObj;
474         } catch(VnfmException e) {
475             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version VnfmException e={}.", e);
476             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
477             resultObj.put("reason", "get vnfd version failed and VnfmException." + e.getMessage());
478             return resultObj;
479         } catch(IOException e) {
480             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version IOException e={}.", e);
481             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
482             resultObj.put("reason", "get vnfd version failed and IOException." + e.getMessage());
483             return resultObj;
484         }
485         return resultObj;
486     }
487
488     /**
489      * Find VNFM connection information.<br>
490      *
491      * @param paramsMap
492      * @return
493      * @since VFC 1.0
494      */
495     public JSONObject getVnfmConnInfo(Map<String, String> paramsMap) {
496         return this.sendRequest(paramsMap);
497     }
498
499     @Override
500     public JSONObject getVNFDPlanInfo(String url, String vnfdid, String conntoken) {
501         JSONObject resultObj = new JSONObject();
502
503         HttpMethod httpMethodPlan = null;
504         try {
505             httpMethodPlan = new HttpRequests.Builder(Constant.CERTIFICATE)
506                     .setUrl(url.trim(), String.format(UrlConstant.URL_VNFDPLANINFO_GET, vnfdid)).setParams("")
507                     .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();
508
509             int statusCode = httpMethodPlan.getStatusCode();
510
511             String result = httpMethodPlan.getResponseBodyAsString();
512             LOG.info("getVNFDPlanInfo result=" + result);
513             if(statusCode == HttpStatus.SC_OK) {
514                 resultObj = JSONObject.fromObject(result);
515                 resultObj.put(Constant.RETCODE, statusCode);
516             } else {
517                 LOG.error("uploadVNFPackage get VNFDPlanInfo failed, code:" + statusCode + " re:" + result);
518                 resultObj.put(Constant.RETCODE, statusCode);
519                 resultObj.put("reason", "get VNFDPlanInfo failed. code:" + statusCode + " re:" + result);
520                 return resultObj;
521             }
522         } catch(JSONException e) {
523             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo JSONException e={}.", e);
524             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
525             resultObj.put("reason", "get VNFDPlanInfo failed and JSONException." + e.getMessage());
526             return resultObj;
527         } catch(VnfmException e) {
528             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo VnfmException e={}.", e);
529             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
530             resultObj.put("reason", "get VNFDPlanInfo failed and VnfmException." + e.getMessage());
531             return resultObj;
532         } catch(IOException e) {
533             LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo IOException e={}.", e);
534             resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
535             resultObj.put("reason", "get VNFDPlanInfo failed and IOException." + e.getMessage());
536             return resultObj;
537         }
538         return resultObj;
539     }
540
541     /**
542      * Get VNF package information.<br>
543      *
544      * @return
545      * @throws IOException
546      * @since VFC 1.0
547      */
548     public static String readVfnPkgInfoFromJson() throws IOException {
549         InputStream ins = null;
550         BufferedInputStream bins = null;
551         String fileContent = "";
552
553         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
554                 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
555                 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + Constant.VNFPKGINFO;
556
557         try {
558             ins = new FileInputStream(fileName);
559             bins = new BufferedInputStream(ins);
560
561             byte[] contentByte = new byte[ins.available()];
562             int num = bins.read(contentByte);
563
564             if(num > 0) {
565                 fileContent = new String(contentByte);
566             }
567         } catch(FileNotFoundException e) {
568             LOG.error(fileName + "is not found!", e);
569         } finally {
570             if(ins != null) {
571                 ins.close();
572             }
573             if(bins != null) {
574                 bins.close();
575             }
576         }
577
578         return fileContent;
579     }
580
581     private static JSONObject readVnfdIdInfoFromJson() {
582         JSONObject jsonObject = new JSONObject();
583         InputStream ins = null;
584         BufferedInputStream bins = null;
585         String fileContent = "";
586
587         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
588                 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
589                 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfd_ids.json";
590
591         try {
592             ins = new FileInputStream(fileName);
593             bins = new BufferedInputStream(ins);
594
595             byte[] contentByte = new byte[ins.available()];
596             int num = bins.read(contentByte);
597
598             if(num > 0) {
599                 fileContent = new String(contentByte);
600             }
601             if(fileContent != null) {
602                 jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds");
603             }
604             ins.close();
605             bins.close();
606         } catch(Exception e) {
607             LOG.error(fileName + " read error!", e);
608         } finally {
609
610         }
611
612         return jsonObject;
613     }
614
615     /*
616      * unzip CSAR packge
617      * @param fileName filePath
618      * @return
619      */
620     public JSONObject unzipCSAR(String fileName, String filePath) {
621         JSONObject resultObj = new JSONObject();
622
623         if(fileName == null || "".equals(fileName)) {
624             resultObj.put("reason", "fileName is null.");
625             resultObj.put("retCode", Constant.REST_FAIL);
626             return resultObj;
627         }
628         if(filePath == null || "".equals(filePath)) {
629             resultObj.put("reason", "unzipCSAR filePath is null.");
630             resultObj.put("retCode", Constant.REST_FAIL);
631             return resultObj;
632         }
633
634         int status = DownloadCsarManager.unzipCSAR(fileName, filePath);
635
636         if(Constant.UNZIP_SUCCESS == status) {
637             resultObj.put("reason", "unzip csar file successfully.");
638             resultObj.put("retCode", Constant.REST_SUCCESS);
639         } else {
640             resultObj.put("reason", "unzip csar file failed.");
641             resultObj.put("retCode", Constant.REST_FAIL);
642         }
643         return resultObj;
644     }
645
646 }