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 vnfpkg, Map<String, String> paramsMap) {
66 JSONObject resultObj = new JSONObject();
67 String vnfDescriptorId = paramsMap.get("vnfDescriptorId");
69 // if upper layer do not provide vnfpackage info,then get the
70 // vnfpackage info from JSON file.
71 if(vnfpkg == null || vnfpkg.isEmpty()) {
72 String vnfPkgInfo = readVfnPkgInfoFromJson();
73 JSONObject vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
74 vnfpkg = vnfpkgJson.getJSONObject(vnfDescriptorId);
76 } catch(IOException e) {
77 LOG.error("function=uploadVNFPackage", e);
80 // check if parameters are null.
81 if(paramsMap == null || paramsMap.isEmpty()) {
82 resultObj.put(Constant.REASON, "csarid and vnfmid are null.");
83 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
87 String csarid = paramsMap.get("csarid");
88 String vnfmid = paramsMap.get("vnfmid");
91 if(null == csarid || "".equals(csarid)) {
92 resultObj.put(Constant.REASON, "csarid is null.");
93 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
96 if(null == vnfmid || "".equals(vnfmid)) {
97 resultObj.put(Constant.REASON, "vnfmid is null.");
98 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
102 // obtain CSAR package info
103 JSONObject csarobj = getVnfmCsarInfo(csarid);
104 String downloadUri = "";
105 if(Integer.valueOf(csarobj.get(Constant.RETCODE).toString()) == Constant.HTTP_OK) {
106 LOG.info("get CSAR info successful.", csarobj.get(Constant.RETCODE));
107 downloadUri = csarobj.getString("downloadUri");
109 LOG.error("get CSAR info fail.", csarobj.get(Constant.RETCODE));
110 resultObj.put(Constant.REASON, csarobj.get(Constant.REASON).toString());
111 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
114 JSONObject csarTempObj = vnfpkg.getJSONObject("template");
115 String csarfilepath = csarTempObj.getString("csar_file_path");
116 String csarfilename = csarTempObj.getString("csar_file_name");
118 // download csar package and save in location.
119 JSONObject downloadObject = downloadCsar(downloadUri, csarfilepath + csarfilename);
121 if(Integer.valueOf(downloadObject.get(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) {
122 LOG.error("download CSAR fail." + downloadObject.get(Constant.RETCODE));
123 resultObj.put(Constant.REASON, downloadObject.get(Constant.REASON).toString());
124 resultObj.put(Constant.RETCODE, downloadObject.get(Constant.RETCODE).toString());
127 LOG.info("download CSAR successful." + downloadObject.get(Constant.RETCODE));
129 // unzip csar package to location.
130 JSONObject unzipObject = unzipCSAR(csarfilepath + csarfilename, csarfilepath);
132 if(Integer.valueOf(unzipObject.get(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) {
133 LOG.error("unzip CSAR fail.", unzipObject.get(Constant.RETCODE));
134 resultObj.put(Constant.REASON, unzipObject.get(Constant.REASON).toString());
135 resultObj.put(Constant.RETCODE, unzipObject.get(Constant.RETCODE).toString());
138 LOG.info("unzip CSAR successful.", unzipObject.get(Constant.RETCODE));
140 // upload vnfd to ftps server
141 JSONObject uploadResJson = uploadCsar(csarTempObj, csarfilepath);
142 LOG.info("upload Csar result: {}.", uploadResJson);
144 Map<String, String> vnfmMap = new HashMap<>();
145 vnfmMap.put("url", String.format(UrlConstant.REST_VNFMINFO_GET, vnfmid));
146 vnfmMap.put("methodType", Constant.GET);
148 // get VNFM connection info
149 // getVnfmConnInfo(vnfmMap)
150 JSONObject vnfmObject = VnfmUtil.getVnfmById(vnfmid);
151 if(Integer.valueOf(vnfmObject.get(Constant.RETCODE).toString()) != Constant.HTTP_OK) {
152 LOG.error("get Vnfm Connection Info fail.", vnfmObject.get(Constant.RETCODE));
153 resultObj.put(Constant.REASON, vnfmObject.get(Constant.REASON).toString());
154 resultObj.put(Constant.RETCODE, vnfmObject.get(Constant.RETCODE).toString());
157 LOG.info("get Vnfm Connection Info successful.", vnfmObject.get(Constant.RETCODE));
159 String vnfmUrl = vnfmObject.getString("url");
160 String userName = vnfmObject.getString(Constant.USERNAME);
161 String password = vnfmObject.getString(Constant.PASSWORD);
163 // build VNFM connection and get token
164 ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm();
166 JSONObject connObject = new JSONObject();
167 connObject.put("url", vnfmUrl);
168 connObject.put(Constant.USERNAME, userName);
169 connObject.put(Constant.PASSWORD, password);
170 if(Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, Constant.CERTIFICATE)) {
171 LOG.error("get Access Session fail.");
172 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
173 resultObj.put(Constant.REASON, "connect fail.");
176 LOG.info("get Access Session successful.");
177 String connToken = mgrVcmm.getAccessSession();
180 JSONObject cloudObject = getAllCloud(vnfmUrl, connToken);
183 if(!cloudObject.isEmpty() && cloudObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
184 LOG.info("get all cloud successful.", cloudObject.get(Constant.RETCODE));
185 vimId = cloudObject.getString("vim_id");
187 LOG.error("get all cloud fail.", cloudObject.get(Constant.RETCODE));
191 // upload VNF package
192 csarTempObj.put("vim_id", vimId);
193 vnfpkg.put("template", csarTempObj);
194 LOG.info("vnfpkg: " + vnfpkg);
196 JSONObject uploadPkgObject = upload(vnfpkg, vnfmUrl, connToken);
197 LOG.info("uploadPkgObject:" + uploadPkgObject);
198 if(!uploadPkgObject.isEmpty() && uploadPkgObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
199 LOG.info("upload vnf package info successful.", uploadPkgObject.get(Constant.RETCODE));
200 vnfdid = uploadPkgObject.getString("id");
203 if(vnfdid == null || "".equals(vnfdid.trim())) {
204 JSONObject vnfdConf = readVnfdIdInfoFromJson();
205 LOG.info("vnfdConf=" + vnfdConf);
206 if(vnfdConf.containsKey(vnfDescriptorId)) {
207 vnfdid = vnfdConf.getString(vnfDescriptorId);
210 LOG.info("set vnfdId=" + vnfdid);
213 String vnfdVersion = "";
215 JSONObject vnfdVerObject =
216 getVnfdVersion(vnfmUrl, String.format(UrlConstant.URL_VNFDINFO_GET, vnfdid), connToken);
217 LOG.info("vnfdVerObject:" + vnfdVerObject);
218 if(!vnfdVerObject.isEmpty() && vnfdVerObject.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
219 LOG.info("get vnfd version successful.", vnfdVerObject.get(Constant.RETCODE));
220 JSONArray verArr = vnfdVerObject.getJSONArray("templates");
221 JSONObject verTmpObj = verArr.getJSONObject(0);
223 vnfdVersion = verTmpObj.getString("vnfdVersion");
225 LOG.error("get vnfd version fail.", vnfdVerObject.get(Constant.RETCODE));
226 return vnfdVerObject;
229 // get vnfd plan info
230 String planName = "";
233 JSONObject vnfdPlanInfo = getVNFDPlanInfo(vnfmUrl, vnfdid, connToken);
234 LOG.info("vnfdPlanInfo:" + vnfdPlanInfo);
235 JSONObject inputsObj = new JSONObject();
236 if(!vnfdPlanInfo.isEmpty() && vnfdPlanInfo.get(Constant.RETCODE).equals(HttpStatus.SC_OK)) {
237 LOG.info("get vnfd plan info successful.", vnfdPlanInfo.get(Constant.RETCODE));
238 JSONObject planTmpObj = vnfdPlanInfo.getJSONObject("template");
239 String templateName = planTmpObj.getString("template_name").trim();
240 JSONArray topoTmpObj = planTmpObj.getJSONArray("topology_template");
242 JSONObject planObj = topoTmpObj.getJSONObject(0);
243 if("VNFD_vUGW".equals(templateName)) {
244 for(int i = 0; i < topoTmpObj.size(); i++) {
245 String name = topoTmpObj.getJSONObject(i).getString("plan_name").trim();
246 if("Normal_E9K".equals(name)) {
247 planObj = topoTmpObj.getJSONObject(i);
252 planName = planObj.getString("plan_name");
253 planId = planObj.getString("plan_id");
254 if(planObj.containsKey("inputs")) {
255 JSONArray inputs = planObj.getJSONArray("inputs");
256 for(int i = 0; i < inputs.size(); i++) {
257 JSONObject obj = inputs.getJSONObject(i);
258 obj.put("value", obj.getString("default"));
260 inputsObj.put("inputs", inputs);
261 inputsObj.put("External_network", new JSONArray());
264 LOG.error("get vnfd plan info fail.", vnfdPlanInfo.get(Constant.RETCODE));
269 resultObj.put(Constant.RETCODE, Constant.HTTP_OK);
270 resultObj.put("vnfdId", vnfdid);
271 resultObj.put("vnfdVersion", vnfdVersion);
272 resultObj.put("planName", planName);
273 resultObj.put("planId", planId);
274 resultObj.put("parameters", inputsObj);
275 LOG.info("resultObj:" + resultObj.toString());
280 private JSONObject uploadCsar(JSONObject vnfpkg, String csarfilepath) {
281 LOG.info("vnfpkg: " + vnfpkg + "csarfilepath:" + csarfilepath);
282 JSONObject resJson = new JSONObject();
284 boolean flag = false;
285 FTPSClient ftpClient = new FTPSClient();
287 ftpClient.connect(vnfpkg.getString("ftp_server_ip"), 21);
288 ftpClient.login(vnfpkg.getString("ftp_username"), vnfpkg.getString("ftp_password"));
289 int replyCode = ftpClient.getReplyCode();
290 LOG.info("replyCode: " + replyCode);
291 if(!FTPReply.isPositiveCompletion(replyCode)) {
292 resJson.put("message", "Connect ftps server failed!");
296 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
297 ftpClient.enterLocalPassiveMode();
298 ftpClient.makeDirectory(vnfpkg.getString(VNFD_FILE_PATH));
299 LOG.info("makeDirectory: " + ftpClient.makeDirectory(vnfpkg.getString(VNFD_FILE_PATH)));
300 ftpClient.changeWorkingDirectory(vnfpkg.getString(VNFD_FILE_PATH));
301 LOG.info("changeWorkingDirectory: " + ftpClient.changeWorkingDirectory(vnfpkg.getString(VNFD_FILE_PATH)));
302 String vnfdPath = csarfilepath + "Artifacts/Deployment/OTHER/";
303 LOG.info("vnfd_file_name: " + vnfdPath + vnfpkg.getString("vnfd_file_name"));
304 InputStream inputStream = new FileInputStream(new File(vnfdPath + vnfpkg.getString("vnfd_file_name")));
305 flag = ftpClient.storeFile(vnfpkg.getString("vnfd_file_name"), inputStream);
307 resJson.put("message", "upload Csar success!");
311 } catch(Exception e) {
312 LOG.error("Exception: " + e);
314 if(ftpClient.isConnected()) {
316 ftpClient.disconnect();
317 } catch(IOException e) {
318 LOG.error("IOException: " + e);
325 private JSONObject sendRequest(Map<String, String> paramsMap) {
326 JSONObject resultObj = new JSONObject();
327 RestfulResponse rsp = VNFRestfulUtil.getRemoteResponse(paramsMap, "");
329 LOG.error("function=sendRequest, RestfulResponse is null");
330 resultObj.put(Constant.REASON, "RestfulResponse is null.");
331 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
334 String resultCreate = rsp.getResponseContent();
336 if(rsp.getStatus() == Constant.HTTP_OK) {
337 LOG.warn("function=sendRequest, msg= status={}, result={}.", rsp.getStatus(), resultCreate);
338 resultObj = JSONObject.fromObject(resultCreate);
339 resultObj.put(Constant.RETCODE, Constant.HTTP_OK);
342 LOG.error("function=sendRequest, msg=ESR return fail,status={}, result={}.", rsp.getStatus(), resultCreate);
343 resultObj.put(Constant.REASON, "ESR return fail.");
345 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
350 public JSONObject getVnfmCsarInfo(String csarid) {
351 JSONObject resultObj = new JSONObject();
353 if(null == csarid || "".equals(csarid)) {
354 resultObj.put(Constant.REASON, "csarid is null.");
355 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
359 Map<String, String> paramsMap = new HashMap<String, String>();
361 paramsMap.put("url", String.format(UrlConstant.REST_CSARINFO_GET, csarid));
362 paramsMap.put("methodType", Constant.GET);
364 return this.sendRequest(paramsMap);
368 public JSONObject downloadCsar(String url, String filePath) {
369 JSONObject resultObj = new JSONObject();
371 if(url == null || "".equals(url)) {
372 resultObj.put(Constant.REASON, "url is null.");
373 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
376 if(filePath == null || "".equals(filePath)) {
377 resultObj.put(Constant.REASON, "downloadUrl filePath is null.");
378 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
382 String status = DownloadCsarManager.download(url, filePath);
384 if(Constant.DOWNLOADCSAR_SUCCESS.equals(status)) {
385 resultObj.put(Constant.REASON, "download csar file successfully.");
386 resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
388 resultObj.put(Constant.REASON, "download csar file failed.");
389 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
395 public JSONObject getAllCloud(String url, String conntoken) {
396 JSONObject resultObj = new JSONObject();
397 JSONArray resArray = new JSONArray();
399 if(url == null || url.equals("")) {
400 url = "http://" + Constant.LOCAL_HOST + ":31943";
404 HttpMethod httpMethodCloud = null;
407 new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(url.trim(), UrlConstant.URL_ALLCLOUD_NEW_GET)
408 .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).setParams("").get().execute();
410 int statusCode = httpMethodCloud.getStatusCode();
412 String result = httpMethodCloud.getResponseBodyAsString();
414 if(statusCode == HttpStatus.SC_OK) {
415 JSONObject vimInfo = JSONObject.fromObject(result);
416 resArray = vimInfo.getJSONArray("vim_info");
417 resultObj = resArray.getJSONObject(0);
418 resultObj.put(Constant.RETCODE, statusCode);
420 LOG.error("uploadVNFPackage get allcloud failed, code:" + statusCode + " re:" + result);
421 resultObj.put(Constant.RETCODE, statusCode);
422 resultObj.put(Constant.REASON, "get allcloud failed. code:" + statusCode + " re:" + result);
425 } catch(JSONException e) {
426 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud JSONException e={}.", e);
427 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
428 resultObj.put(Constant.REASON, "get allcloud failed and JSONException." + e.getMessage());
430 } catch(VnfmException e) {
431 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud VnfmException e={}.", e);
432 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
433 resultObj.put(Constant.REASON, "get allcloud failed and VnfmException." + e.getMessage());
435 } catch(IOException e) {
436 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get allcloud IOException e={}.", e);
437 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
438 resultObj.put(Constant.REASON, "get allcloud failed and IOException." + e.getMessage());
445 * Upload vnfpackage<br>
453 public JSONObject upload(JSONObject vnfpackage, String vnfmurl, String conntoken) {
454 JSONObject resultObj = new JSONObject();
455 HttpMethod httpMethodVnf = null;
458 httpMethodVnf = new HttpRequests.Builder(Constant.CERTIFICATE)
459 .setUrl(vnfmurl.trim(), UrlConstant.URL_VNFPACKAGE_POST).setParams(vnfpackage.toString())
460 .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).post().execute();
462 int statusCodeUp = httpMethodVnf.getStatusCode();
464 String resultUp = httpMethodVnf.getResponseBodyAsString();
466 if(statusCodeUp == HttpStatus.SC_CREATED || statusCodeUp == HttpStatus.SC_OK) {
467 LOG.info("uploadVNFPackage upload VNF package successful, code:" + statusCodeUp + " re:" + resultUp);
468 resultObj = JSONObject.fromObject(resultUp);
469 resultObj.put(Constant.RETCODE, statusCodeUp);
471 LOG.error("uploadVNFPackage upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
472 resultObj.put(Constant.RETCODE, statusCodeUp);
473 resultObj.put("data", "upload VNF package failed, code:" + statusCodeUp + " re:" + resultUp);
476 } catch(JSONException e) {
477 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package JSONException e={}.", e);
478 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
479 resultObj.put(Constant.REASON, "upload VNF package failed and JSONException." + e.getMessage());
481 } catch(VnfmException e) {
482 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package VnfmException e={}.", e);
483 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
484 resultObj.put(Constant.REASON, "upload VNF package failed and VnfmException." + e.getMessage());
486 } catch(IOException e) {
487 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage upload VNF package IOException e={}.", e);
488 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
489 resultObj.put(Constant.REASON, "upload VNF package failed and IOException." + e.getMessage());
496 * Find vnfd version.<br>
503 public JSONObject getVnfdVersion(String prefixUrl, String serviceUrl, String conntoken) {
504 JSONObject resultObj = new JSONObject();
505 HttpMethod httpMethodVnfd = null;
507 httpMethodVnfd = new HttpRequests.Builder(Constant.CERTIFICATE).setUrl(prefixUrl.trim(), serviceUrl)
508 .setParams("").addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();
510 int statusCodeVnfd = httpMethodVnfd.getStatusCode();
512 String resultVnfd = httpMethodVnfd.getResponseBodyAsString();
513 LOG.info("getVnfdVersion result:" + resultVnfd);
514 if(statusCodeVnfd == HttpStatus.SC_OK) {
515 resultObj = JSONObject.fromObject(resultVnfd);
516 resultObj.put(Constant.RETCODE, statusCodeVnfd);
518 LOG.error("uploadVNFPackage vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
519 resultObj.put(Constant.RETCODE, statusCodeVnfd);
520 resultObj.put("data", "get vnfd version failed, code:" + statusCodeVnfd + " re:" + resultVnfd);
523 } catch(JSONException e) {
524 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version JSONException e={}.", e);
525 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
526 resultObj.put(Constant.REASON, "get vnfd version failed and JSONException." + e.getMessage());
528 } catch(VnfmException e) {
529 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version VnfmException e={}.", e);
530 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
531 resultObj.put(Constant.REASON, "get vnfd version failed and VnfmException." + e.getMessage());
533 } catch(IOException e) {
534 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get vnfd version IOException e={}.", e);
535 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
536 resultObj.put(Constant.REASON, "get vnfd version failed and IOException." + e.getMessage());
543 * Find VNFM connection information.<br>
549 public JSONObject getVnfmConnInfo(Map<String, String> paramsMap) {
550 return this.sendRequest(paramsMap);
554 public JSONObject getVNFDPlanInfo(String url, String vnfdid, String conntoken) {
555 JSONObject resultObj = new JSONObject();
557 HttpMethod httpMethodPlan = null;
559 httpMethodPlan = new HttpRequests.Builder(Constant.CERTIFICATE)
560 .setUrl(url.trim(), String.format(UrlConstant.URL_VNFDPLANINFO_GET, vnfdid)).setParams("")
561 .addHeader(Constant.HEADER_AUTH_TOKEN, conntoken).get().execute();
563 int statusCode = httpMethodPlan.getStatusCode();
565 String result = httpMethodPlan.getResponseBodyAsString();
566 LOG.info("getVNFDPlanInfo result=" + result);
567 if(statusCode == HttpStatus.SC_OK) {
568 resultObj = JSONObject.fromObject(result);
569 resultObj.put(Constant.RETCODE, statusCode);
571 LOG.error("uploadVNFPackage get VNFDPlanInfo failed, code:" + statusCode + " re:" + result);
572 resultObj.put(Constant.RETCODE, statusCode);
573 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed. code:" + statusCode + " re:" + result);
576 } catch(JSONException e) {
577 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo JSONException e={}.", e);
578 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
579 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and JSONException." + e.getMessage());
581 } catch(VnfmException e) {
582 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo VnfmException e={}.", e);
583 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
584 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and VnfmException." + e.getMessage());
586 } catch(IOException e) {
587 LOG.error("function=uploadVNFPackage, msg=uploadVNFPackage get VNFDPlanInfo IOException e={}.", e);
588 resultObj.put(Constant.RETCODE, Constant.HTTP_INNERERROR);
589 resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and IOException." + e.getMessage());
596 * Get VNF package information.<br>
599 * @throws IOException
602 public static String readVfnPkgInfoFromJson() throws IOException {
603 InputStream ins = null;
604 BufferedInputStream bins = null;
605 String fileContent = "";
607 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
608 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
609 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + Constant.VNFPKGINFO;
612 ins = new FileInputStream(fileName);
613 bins = new BufferedInputStream(ins);
615 byte[] contentByte = new byte[ins.available()];
616 int num = bins.read(contentByte);
619 fileContent = new String(contentByte);
621 } catch(FileNotFoundException e) {
622 LOG.error(fileName + "is not found!", e);
635 private static JSONObject readVnfdIdInfoFromJson() {
636 JSONObject jsonObject = new JSONObject();
637 InputStream ins = null;
638 BufferedInputStream bins = null;
639 String fileContent = "";
641 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
642 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
643 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfd_ids.json";
646 ins = new FileInputStream(fileName);
647 bins = new BufferedInputStream(ins);
649 byte[] contentByte = new byte[ins.available()];
650 int num = bins.read(contentByte);
653 fileContent = new String(contentByte);
655 if(fileContent != null) {
656 jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds");
660 } catch(Exception e) {
661 LOG.error(fileName + " read error!", e);
671 * @param fileName filePath
674 public JSONObject unzipCSAR(String fileName, String filePath) {
675 LOG.info("fileName: " + fileName + ", filePath: " + filePath);
676 JSONObject resultObj = new JSONObject();
678 if(fileName == null || "".equals(fileName)) {
679 resultObj.put(Constant.REASON, "fileName is null.");
680 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
683 if(filePath == null || "".equals(filePath)) {
684 resultObj.put(Constant.REASON, "unzipCSAR filePath is null.");
685 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
689 int status = DownloadCsarManager.unzipCSAR(fileName, filePath);
691 if(Constant.UNZIP_SUCCESS == status) {
692 resultObj.put(Constant.REASON, "unzip csar file successfully.");
693 resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
695 resultObj.put(Constant.REASON, "unzip csar file failed.");
696 resultObj.put(Constant.RETCODE, Constant.REST_FAIL);