2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl;
19 import java.io.BufferedInputStream;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.HashMap;
28 import org.apache.commons.httpclient.HttpMethod;
29 import org.apache.commons.httpclient.HttpStatus;
30 import org.apache.commons.net.ftp.FTPClient;
31 import org.apache.commons.net.ftp.FTPReply;
32 import org.apache.commons.net.ftp.FTPSClient;
33 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.DownloadCsarManager;
34 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
35 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmUtil;
36 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
37 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.SystemEnvVariablesFactory;
38 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VNFRestfulUtil;
39 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.inf.IResourceManager;
40 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
41 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
42 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.ConnectMgrVnfm;
43 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.HttpRequests;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import net.sf.json.JSONArray;
48 import net.sf.json.JSONException;
49 import net.sf.json.JSONObject;
52 * Resource Manager adapter class.
56 * @version VFC 1.0 Sep 13, 2016
58 public class AdapterResourceManager implements IResourceManager {
60 private static final Logger LOG = LoggerFactory.getLogger(AdapterResourceManager.class);
62 private static final String VNFD_FILE_PATH = "vnfd_file_path";
65 public JSONObject uploadVNFPackage(JSONObject vnfpkge, Map<String, String> paramsMap) {
66 JSONObject resultObj = new JSONObject();
67 String vnfDescriptorId = paramsMap.get("vnfDescriptorId");
68 JSONObject vnfpkgJson = new JSONObject();
70 // if upper layer do not provide vnfpackage info,then get the
71 // vnfpackage info from JSON file.
72 if(vnfpkge == null || vnfpkge.isEmpty()) {
73 String vnfPkgInfo = readVfnPkgInfoFromJson();
74 vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
77 } catch(IOException e) {
78 LOG.error("function=uploadVNFPackage", e);
81 // check if parameters are null.
82 if(paramsMap == null || paramsMap.isEmpty()) {
83 resultObj.put(Constant.REASON, "csarid and vnfmid are null.");
84 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
88 String csarid = paramsMap.get("csarid");
89 String vnfmid = paramsMap.get("vnfmid");
92 if(null == csarid || "".equals(csarid)) {
93 resultObj.put(Constant.REASON, "csarid is null.");
94 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
97 if(null == vnfmid || "".equals(vnfmid)) {
98 resultObj.put(Constant.REASON, "vnfmid is null.");
99 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
103 // obtain CSAR package info
104 JSONObject csarobj = getVnfmCsarInfo(csarid);
105 String downloadUri = "";
106 String csarName = "";
107 if(Integer.valueOf(csarobj.get(Constant.RETCODE).toString()) == Constant.HTTP_OK) {
108 LOG.info("get CSAR info successful.", csarobj.get(Constant.RETCODE));
109 downloadUri = csarobj.getJSONObject("packageInfo").getString("downloadUrl");
110 csarName = csarobj.getJSONObject("packageInfo").getString("csarName");
112 LOG.error("get CSAR info fail.", csarobj.get(Constant.RETCODE));
113 resultObj.put(Constant.REASON, csarobj.get(Constant.REASON).toString());
114 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
117 String vnfdName = transferFromCsar(csarName);
118 JSONObject vnfpkg = vnfpkgJson.getJSONObject(vnfdName);
119 JSONObject csarTempObj = vnfpkg.getJSONObject("template");
120 String csarfilepath = csarTempObj.getString("csar_file_path");
121 String csarfilename = csarTempObj.getString("csar_file_name");
123 // download csar package and save in location.
124 JSONObject downloadObject = downloadCsar(downloadUri, csarfilepath + csarfilename);
126 if(Integer.valueOf(downloadObject.get(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) {
127 LOG.error("download CSAR fail." + downloadObject.get(Constant.RETCODE));
128 resultObj.put(Constant.REASON, downloadObject.get(Constant.REASON).toString());
129 resultObj.put(Constant.RETCODE, downloadObject.get(Constant.RETCODE).toString());
132 LOG.info("download CSAR successful." + downloadObject.get(Constant.RETCODE));
134 // unzip csar package to location.
135 JSONObject unzipObject = unzipCSAR(csarfilepath + csarfilename, csarfilepath);
137 if(Integer.valueOf(unzipObject.get(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) {
138 LOG.error("unzip CSAR fail.", unzipObject.get(Constant.RETCODE));
139 resultObj.put(Constant.REASON, unzipObject.get(Constant.REASON).toString());
140 resultObj.put(Constant.RETCODE, unzipObject.get(Constant.RETCODE).toString());
143 LOG.info("unzip CSAR successful.", unzipObject.get(Constant.RETCODE));
145 // upload vnfd to ftps server
146 // JSONObject uploadResJson = uploadCsar(csarTempObj, csarfilepath);
147 // LOG.info("upload Csar result: {}.", uploadResJson);
149 Map<String, String> vnfmMap = new HashMap<>();
150 vnfmMap.put("url", String.format(UrlConstant.REST_VNFMINFO_GET, vnfmid));
151 vnfmMap.put("methodType", Constant.GET);
153 // get VNFM connection info
154 // getVnfmConnInfo(vnfmMap)
155 JSONObject vnfmObject = VnfmUtil.getVnfmById(vnfmid);
156 LOG.info("get Vnfm Connection Info successful.");
158 String vnfmUrl = vnfmObject.getString("url");
159 String userName = vnfmObject.getString(Constant.USERNAME);
160 String password = vnfmObject.getString(Constant.PASSWORD);
162 // build VNFM connection and get token
163 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
165 JSONObject connObject = new JSONObject();
166 connObject.put("url", vnfmUrl);
167 connObject.put(Constant.USERNAME, userName);
168 connObject.put(Constant.PASSWORD, password);
169 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, Constant.CERTIFICATE)) {
170 LOG.error("get Access Session fail.");
171 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
172 resultObj.put(Constant.REASON, "connect fail.");
175 LOG.info("get Access Session successful.");
176 String connToken = mgrVcmm.getAccessSession();
179 JSONObject cloudObject = getAllCloud(vnfmUrl, connToken);
182 if(!cloudObject.isEmpty() && cloudObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
183 LOG.info("get all cloud successful.", cloudObject.get(Constant.RETCODE));
184 vimId = cloudObject.getString("vim_id");
186 LOG.error("get all cloud fail.", cloudObject.get(Constant.RETCODE));
190 // upload VNF package
191 // csarTempObj.put("vim_id", vimId);
192 vnfpkg.put("template", csarTempObj);
193 LOG.info("vnfpkg: " + vnfpkg);
195 JSONObject uploadPkgObject = upload(vnfpkg, vnfmUrl, connToken);
196 LOG.info("uploadPkgObject:" + uploadPkgObject);
197 if(!uploadPkgObject.isEmpty() && uploadPkgObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
198 LOG.info("upload vnf package info successful.", uploadPkgObject.get(Constant.RETCODE));
199 vnfdid = uploadPkgObject.getString("id");
202 if(vnfdid == null || "".equals(vnfdid.trim())) {
203 JSONObject vnfdConf = readVnfdIdInfoFromJson();
204 LOG.info("vnfdName:" + vnfdName + ", vnfdConf=" + vnfdConf);
205 if(vnfdConf.containsKey(vnfdName)) {
206 vnfdid = vnfdConf.getString(vnfdName);
209 LOG.info("set vnfdId=" + vnfdid);
212 String vnfdVersion = "";
214 JSONObject vnfdVerObject =
215 getVnfdVersion(vnfmUrl, String.format(UrlConstant.URL_VNFDINFO_GET, vnfdid), connToken);
216 LOG.info("vnfdVerObject:" + vnfdVerObject);
217 if(!vnfdVerObject.isEmpty() && vnfdVerObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
218 LOG.info("get vnfd version successful.", vnfdVerObject.get(Constant.RETCODE));
219 JSONArray verArr = vnfdVerObject.getJSONArray("templates");
220 JSONObject verTmpObj = verArr.getJSONObject(0);
222 vnfdVersion = verTmpObj.getString("vnfdVersion");
224 LOG.error("get vnfd version fail.", vnfdVerObject.get(Constant.RETCODE));
225 return vnfdVerObject;
228 // get vnfd plan info
229 String planName = "";
232 JSONObject vnfdPlanInfo = getVNFDPlanInfo(vnfmUrl, vnfdid, connToken);
233 LOG.info("vnfdPlanInfo:" + vnfdPlanInfo);
234 JSONObject inputsObj = new JSONObject();
235 if(!vnfdPlanInfo.isEmpty() && vnfdPlanInfo.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
236 LOG.info("get vnfd plan info successful.", vnfdPlanInfo.get(Constant.RETCODE));
237 JSONObject planTmpObj = vnfdPlanInfo.getJSONObject("template");
238 String templateName = planTmpObj.getString("template_name").trim();
239 JSONArray topoTmpObj = planTmpObj.getJSONArray("topology_template");
241 JSONObject planObj = topoTmpObj.getJSONObject(0);
242 if("VNFD_vUGW".equals(templateName)) {
243 for(int i = 0; i < topoTmpObj.size(); i++) {
244 String name = topoTmpObj.getJSONObject(i).getString("plan_name").trim();
245 if("Normal_E9K".equals(name)) {
246 planObj = topoTmpObj.getJSONObject(i);
251 planName = planObj.getString("plan_name");
252 planId = planObj.getString("plan_id");
253 if(planObj.containsKey("inputs")) {
254 JSONArray inputs = planObj.getJSONArray("inputs");
255 for(int i = 0; i < inputs.size(); i++) {
256 JSONObject obj = inputs.getJSONObject(i);
257 obj.put("value", obj.getString("default"));
259 inputsObj.put("inputs", inputs);
260 inputsObj.put("External_network", new JSONArray());
263 LOG.error("get vnfd plan info fail.", vnfdPlanInfo.get(Constant.RETCODE));
268 resultObj.put(Constant.RETCODE, Constant.HTTP_OK);
269 resultObj.put("vnfdId", vnfdid);
270 resultObj.put("vnfdVersion", vnfdVersion);
271 resultObj.put("planName", planName);
272 resultObj.put("planId", planId);
273 resultObj.put("parameters", inputsObj);
274 LOG.info("resultObj:" + resultObj.toString());
279 private String transferFromCsar(String csarName) {
280 LOG.info("csarName: " + csarName);
281 if(csarName.toUpperCase().contains("HSS")) {
287 private JSONObject uploadCsar(JSONObject vnfpkg, String csarfilepath) {
288 LOG.info("vnfpkg: " + vnfpkg + "csarfilepath:" + csarfilepath);
289 JSONObject resJson = new JSONObject();
291 boolean flag = false;
292 FTPSClient ftpClient = new FTPSClient();
294 ftpClient.connect(vnfpkg.getString("ftp_server_ip"), 21);
295 ftpClient.login(vnfpkg.getString("ftp_username"), vnfpkg.getString("ftp_password"));
296 int replyCode = ftpClient.getReplyCode();
297 LOG.info("replyCode: " + replyCode);
298 if(!FTPReply.isPositiveCompletion(replyCode)) {
299 resJson.put("message", "Connect ftps server failed!");
303 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
304 ftpClient.enterLocalPassiveMode();
305 ftpClient.makeDirectory(vnfpkg.getString(VNFD_FILE_PATH));
306 LOG.info("makeDirectory: " + ftpClient.makeDirectory(vnfpkg.getString(VNFD_FILE_PATH)));
307 ftpClient.changeWorkingDirectory(vnfpkg.getString(VNFD_FILE_PATH));
308 LOG.info("changeWorkingDirectory: " + ftpClient.changeWorkingDirectory(vnfpkg.getString(VNFD_FILE_PATH)));
309 String vnfdPath = csarfilepath + "Artifacts/Deployment/OTHER/";
310 LOG.info("vnfd_file_name: " + vnfdPath + vnfpkg.getString("vnfd_file_name"));
311 InputStream inputStream = new FileInputStream(new File(vnfdPath + vnfpkg.getString("vnfd_file_name")));
312 flag = ftpClient.storeFile(vnfpkg.getString("vnfd_file_name"), inputStream);
314 resJson.put("message", "upload Csar success!");
318 } catch(Exception e) {
319 LOG.error("Exception: " + e);
321 if(ftpClient.isConnected()) {
323 ftpClient.disconnect();
324 } catch(IOException e) {
325 LOG.error("IOException: " + e);
332 private JSONObject sendRequest(Map<String, String> paramsMap) {
333 JSONObject resultObj = new JSONObject();
334 RestfulResponse rsp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
336 LOG.error("function=sendRequest, RestfulResponse is null");
337 resultObj.put(Constant.REASON, "RestfulResponse is null.");
338 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
341 String resultCreate = rsp.getResponseContent();
343 if(rsp.getStatus() == Constant.HTTP_OK) {
344 LOG.warn("function=sendRequest, msg= status={}, result={}.", rsp.getStatus(), resultCreate);
345 resultObj = JSONObject.fromObject(resultCreate);
346 resultObj.put(Constant.RETCODE, Constant.HTTP_OK);
349 LOG.error("function=sendRequest, msg=ESR return fail,status={}, result={}.", rsp.getStatus(), resultCreate);
350 resultObj.put(Constant.REASON, "ESR return fail.");
352 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
357 public JSONObject getVnfmCsarInfo(String csarid) {
358 JSONObject resultObj = new JSONObject();
360 if(null == csarid || "".equals(csarid)) {
361 resultObj.put(Constant.REASON, "csarid is null.");
362 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
366 Map<String, String> paramsMap = new HashMap<String, String>();
368 paramsMap.put("url", String.format(UrlConstant.REST_CSARINFO_GET, csarid));
369 paramsMap.put("methodType", Constant.GET);
371 return this.sendRequest(paramsMap);
375 public JSONObject downloadCsar(String url, String filePath) {
376 JSONObject resultObj = new JSONObject();
378 if(url == null || "".equals(url)) {
379 resultObj.put(Constant.REASON, "url is null.");
380 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
383 if(filePath == null || "".equals(filePath)) {
384 resultObj.put(Constant.REASON, "downloadUrl filePath is null.");
385 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
389 String status = DownloadCsarManager.download(url, filePath);
391 if(Constant.DOWNLOADCSAR_SUCCESS.equals(status)) {
392 resultObj.put(Constant.REASON, "download csar file successfully.");
393 resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
395 resultObj.put(Constant.REASON, "download csar file failed.");
396 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
402 public JSONObject getAllCloud(String url, String conntoken) {
403 JSONObject resultObj = new JSONObject();
404 JSONArray resArray = new JSONArray();
406 if(url == null || url.equals("")) {
407 url = "http://" + Constant.LOCAL_HOST + ":31943";
411 HttpMethod httpMethodCloud = null;
414 new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(url.trim(), UrlConstant.URL_ALLCLOUD_NEW_GET)
415 .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).setParams("").get().execute();
417 int statusCode = httpMethodCloud.getStatusCode();
419 String result = httpMethodCloud.getResponseBodyAsString();
421 if(statusCode == HttpStatus.SC_OK) {
422 JSONObject vimInfo = JSONObject.fromObject(result);
423 resArray = vimInfo.getJSONArray("vim_info");
424 resultObj = resArray.getJSONObject(0);
425 resultObj.put(Constant.RETCODE, statusCode);
427 LOG.error("uploadVNFPackage get allcloud failed, code:" + statusCode + " re:" + result);
428 resultObj.put(Constant.RETCODE, statusCode);
429 resultObj.put(Constant.REASON, "get allcloud failed. code:" + statusCode + " re:" + result);
432 } catch(JSONException e) {
433 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud JSONException e={}.", e);
434 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
435 resultObj.put(Constant.REASON, "get allcloud failed and JSONException." + e.getMessage());
437 } catch(VnfmException e) {
438 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud VnfmException e={}.", e);
439 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
440 resultObj.put(Constant.REASON, "get allcloud failed and VnfmException." + e.getMessage());
442 } catch(IOException e) {
443 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud IOException e={}.", e);
444 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
445 resultObj.put(Constant.REASON, "get allcloud failed and IOException." + e.getMessage());
452 * Upload vnfpackage<br>
460 public JSONObject upload(JSONObject vnfpackage, String vnfmurl, String conntoken) {
461 JSONObject resultObj = new JSONObject();
462 HttpMethod httpMethodVnf = null;
465 httpMethodVnf = new HttpRequests.Builder(Constant.CERTIFICATE)
466 .setUrl(vnfmurl.trim(), UrlConstant.URL_VNFPACKAGE_POST).setParams(vnfpackage.toString())
467 .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).post().execute();
469 int statusCodeUp = httpMethodVnf.getStatusCode();
471 String resultUp = httpMethodVnf.getResponseBodyAsString();
473 if(statusCodeUp == HttpStatus.SC_CREATED || statusCodeUp == HttpStatus.SC_OK) {
474 LOG.info("uploadVNFPackage upload VNF package successful, code:" + statusCodeUp + " re:" + resultUp);
475 resultObj = JSONObject.fromObject(resultUp);
476 resultObj.put(Constant.RETCODE, statusCodeUp);
478 LOG.error("uploadVNFPackage upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
479 resultObj.put(Constant.RETCODE, statusCodeUp);
480 resultObj.put("data", "upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
483 } catch(JSONException e) {
484 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package JSONException e={}.", e);
485 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
486 resultObj.put(Constant.REASON, "upload VNF package failed and JSONException." + e.getMessage());
488 } catch(VnfmException e) {
489 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package VnfmException e={}.", e);
490 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
491 resultObj.put(Constant.REASON, "upload VNF package failed and VnfmException." + e.getMessage());
493 } catch(IOException e) {
494 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package IOException e={}.", e);
495 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
496 resultObj.put(Constant.REASON, "upload VNF package failed and IOException." + e.getMessage());
503 * Find vnfd version.<br>
510 public JSONObject getVnfdVersion(String prefixUrl, String serviceUrl, String conntoken) {
511 JSONObject resultObj = new JSONObject();
512 HttpMethod httpMethodVnfd = null;
514 httpMethodVnfd = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(prefixUrl.trim(), serviceUrl)
515 .setParams("").addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();
517 int statusCodeVnfd = httpMethodVnfd.getStatusCode();
519 String resultVnfd = httpMethodVnfd.getResponseBodyAsString();
520 LOG.info("getVnfdVersion result:" + resultVnfd);
521 if(statusCodeVnfd == HttpStatus.SC_OK) {
522 resultObj = JSONObject.fromObject(resultVnfd);
523 resultObj.put(Constant.RETCODE, statusCodeVnfd);
525 LOG.error("uploadVNFPackage vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
526 resultObj.put(Constant.RETCODE, statusCodeVnfd);
527 resultObj.put("data", "get vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
530 } catch(JSONException e) {
531 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version JSONException e={}.", e);
532 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
533 resultObj.put(Constant.REASON, "get vnfd version failed and JSONException." + e.getMessage());
535 } catch(VnfmException e) {
536 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version VnfmException e={}.", e);
537 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
538 resultObj.put(Constant.REASON, "get vnfd version failed and VnfmException." + e.getMessage());
540 } catch(IOException e) {
541 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version IOException e={}.", e);
542 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
543 resultObj.put(Constant.REASON, "get vnfd version failed and IOException." + e.getMessage());
550 * Find VNFM connection information.<br>
556 public JSONObject getVnfmConnInfo(Map<String, String> paramsMap) {
557 return this.sendRequest(paramsMap);
561 public JSONObject getVNFDPlanInfo(String url, String vnfdid, String conntoken) {
562 JSONObject resultObj = new JSONObject();
564 HttpMethod httpMethodPlan = null;
566 httpMethodPlan = new HttpRequests.Builder(Constant.CERTIFICATE)
567 .setUrl(url.trim(), String.format(UrlConstant.URL_VNFDPLANINFO_GET, vnfdid)).setParams("")
568 .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();
570 int statusCode = httpMethodPlan.getStatusCode();
572 String result = httpMethodPlan.getResponseBodyAsString();
573 LOG.info("getVNFDPlanInfo result=" + result);
574 if(statusCode == HttpStatus.SC_OK) {
575 resultObj = JSONObject.fromObject(result);
576 resultObj.put(Constant.RETCODE, statusCode);
578 LOG.error("uploadVNFPackage get VNFDPlanInfo failed, code:" + statusCode + " re:" + result);
579 resultObj.put(Constant.RETCODE, statusCode);
580 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed. code:" + statusCode + " re:" + result);
583 } catch(JSONException e) {
584 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo JSONException e={}.", e);
585 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
586 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and JSONException." + e.getMessage());
588 } catch(VnfmException e) {
589 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo VnfmException e={}.", e);
590 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
591 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and VnfmException." + e.getMessage());
593 } catch(IOException e) {
594 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo IOException e={}.", e);
595 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
596 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and IOException." + e.getMessage());
603 * Get VNF package information.<br>
606 * @throws IOException
609 public static String readVfnPkgInfoFromJson() throws IOException {
610 InputStream ins = null;
611 BufferedInputStream bins = null;
612 String fileContent = "";
614 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
615 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
616 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + Constant.VNFPKGINFO;
619 ins = new FileInputStream(fileName);
620 bins = new BufferedInputStream(ins);
622 byte[] contentByte = new byte[ins.available()];
623 int num = bins.read(contentByte);
626 fileContent = new String(contentByte);
628 } catch(FileNotFoundException e) {
629 LOG.error(fileName + "is not found!", e);
642 private static JSONObject readVnfdIdInfoFromJson() {
643 JSONObject jsonObject = new JSONObject();
644 InputStream ins = null;
645 BufferedInputStream bins = null;
646 String fileContent = "";
648 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
649 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
650 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfd_ids.json";
653 ins = new FileInputStream(fileName);
654 bins = new BufferedInputStream(ins);
656 byte[] contentByte = new byte[ins.available()];
657 int num = bins.read(contentByte);
660 fileContent = new String(contentByte);
662 if(fileContent != null) {
663 jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds");
667 } catch(Exception e) {
668 LOG.error(fileName + " read error!", e);
678 * @param fileName filePath
681 public JSONObject unzipCSAR(String fileName, String filePath) {
682 LOG.info("fileName: " + fileName + ", filePath: " + filePath);
683 JSONObject resultObj = new JSONObject();
685 if(fileName == null || "".equals(fileName)) {
686 resultObj.put(Constant.REASON, "fileName is null.");
687 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
690 if(filePath == null || "".equals(filePath)) {
691 resultObj.put(Constant.REASON, "unzipCSAR filePath is null.");
692 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
696 int status = DownloadCsarManager.unzipCSAR(fileName, filePath);
698 if(Constant.UNZIP_SUCCESS == status) {
699 resultObj.put(Constant.REASON, "unzip csar file successfully.");
700 resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
702 resultObj.put(Constant.REASON, "unzip csar file failed.");
703 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);