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 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;
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();
// check if parameters are null.
if(paramsMap == null || paramsMap.isEmpty()) {
- resultObj.put("reason", "csarid and vnfmid are null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.REASON, "csarid and vnfmid are null.");
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
return resultObj;
}
String vnfdid = "";
if(null == csarid || "".equals(csarid)) {
- resultObj.put("reason", "csarid is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.REASON, "csarid is null.");
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
return resultObj;
}
if(null == vnfmid || "".equals(vnfmid)) {
- resultObj.put("reason", "vnfmid is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ 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("retCode").toString()) == Constant.HTTP_OK) {
- LOG.info("get CSAR info successful.", csarobj.get("retCode"));
+ 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("retCode"));
- resultObj.put("reason", csarobj.get("reason").toString());
- resultObj.put("retCode", Constant.REST_FAIL);
+ 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 = new JSONObject();
- csarTempObj = vnfpkg.getJSONObject("template");
+ 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 + System.getProperty(Constant.FILE_SEPARATOR) + csarfilename);
+ JSONObject downloadObject = downloadCsar(downloadUri, csarfilepath + csarfilename);
- if(Integer.valueOf(downloadObject.get("retCode").toString()) != Constant.REST_SUCCESS) {
- LOG.error("download CSAR fail.", downloadObject.get("retCode"));
- resultObj.put("reason", downloadObject.get("reason").toString());
- resultObj.put("retCode", downloadObject.get("retCode").toString());
+ 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("retCode"));
+ LOG.info("download CSAR successful." + downloadObject.get(Constant.RETCODE));
// unzip csar package to location.
- JSONObject unzipObject =
- unzipCSAR(csarfilepath + System.getProperty(Constant.FILE_SEPARATOR) + csarfilename, csarfilepath);
+ JSONObject unzipObject = unzipCSAR(csarfilepath + csarfilename, csarfilepath);
- if(Integer.valueOf(unzipObject.get("retCode").toString()) != Constant.REST_SUCCESS) {
- LOG.error("unzip CSAR fail.", unzipObject.get("retCode"));
- resultObj.put("reason", unzipObject.get("reason").toString());
- resultObj.put("retCode", unzipObject.get("retCode").toString());
+ 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("retCode"));
+ 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
- JSONObject vnfmObject = getVnfmConnInfo(vnfmMap);
- if(Integer.valueOf(vnfmObject.get("retCode").toString()) != Constant.HTTP_OK) {
- LOG.error("get Vnfm Connection Info fail.", vnfmObject.get("retCode"));
- resultObj.put("reason", vnfmObject.get("reason").toString());
- resultObj.put("retCode", vnfmObject.get("retCode").toString());
+ // 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("retCode"));
+ LOG.info("get Vnfm Connection Info successful.", vnfmObject.get(Constant.RETCODE));
String vnfmUrl = vnfmObject.getString("url");
- String userName = vnfmObject.getString("userName");
- String password = vnfmObject.getString("password");
+ 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("userName", userName);
- connObject.put("password", password);
+ 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("reason", "connect fail.");
+ resultObj.put(Constant.REASON, "connect fail.");
return resultObj;
}
LOG.info("get Access Session successful.");
}
// return values
- resultObj.put("retCode", Constant.HTTP_OK);
+ resultObj.put(Constant.RETCODE, Constant.HTTP_OK);
resultObj.put("vnfdId", vnfdid);
resultObj.put("vnfdVersion", vnfdVersion);
resultObj.put("planName", planName);
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("reason", "RestfulResponse is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ 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("retCode", Constant.HTTP_OK);
+ 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("reason", "ESR return fail.");
+ resultObj.put(Constant.REASON, "ESR return fail.");
}
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
return resultObj;
}
JSONObject resultObj = new JSONObject();
if(null == csarid || "".equals(csarid)) {
- resultObj.put("reason", "csarid is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.REASON, "csarid is null.");
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
return resultObj;
}
JSONObject resultObj = new JSONObject();
if(url == null || "".equals(url)) {
- resultObj.put("reason", "url is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.REASON, "url is null.");
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
return resultObj;
}
if(filePath == null || "".equals(filePath)) {
- resultObj.put("reason", "downloadUrl filePath is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ 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("reason", "download csar file successfully.");
- resultObj.put("retCode", Constant.REST_SUCCESS);
+ resultObj.put(Constant.REASON, "download csar file successfully.");
+ resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
} else {
- resultObj.put("reason", "download csar file failed.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.REASON, "download csar file failed.");
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
}
return resultObj;
}
JSONArray resArray = new JSONArray();
if(url == null || url.equals("")) {
- url = "http://127.0.0.1:31943";
+ url = "http://" + Constant.LOCAL_HOST + ":31943";
}
// get vim_id
} else {
LOG.error("uploadVNFPackage get allcloud failed, code:" + statusCode + " re:" + result);
resultObj.put(Constant.RETCODE, statusCode);
- resultObj.put("reason", "get allcloud failed. code:" + statusCode + " re:" + result);
+ 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("reason", "get allcloud failed and JSONException." + e.getMessage());
+ 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("reason", "get allcloud failed and VnfmException." + e.getMessage());
+ 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("reason", "get allcloud failed and IOException." + e.getMessage());
+ resultObj.put(Constant.REASON, "get allcloud failed and IOException." + e.getMessage());
return resultObj;
}
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("reason", "upload VNF package failed and JSONException." + e.getMessage());
+ 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("reason", "upload VNF package failed and VnfmException." + e.getMessage());
+ 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("reason", "upload VNF package failed and IOException." + e.getMessage());
+ resultObj.put(Constant.REASON, "upload VNF package failed and IOException." + e.getMessage());
return resultObj;
}
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("reason", "get vnfd version failed and JSONException." + e.getMessage());
+ 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("reason", "get vnfd version failed and VnfmException." + e.getMessage());
+ 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("reason", "get vnfd version failed and IOException." + e.getMessage());
+ resultObj.put(Constant.REASON, "get vnfd version failed and IOException." + e.getMessage());
return resultObj;
}
return resultObj;
} else {
LOG.error("uploadVNFPackage get VNFDPlanInfo failed, code:" + statusCode + " re:" + result);
resultObj.put(Constant.RETCODE, statusCode);
- resultObj.put("reason", "get VNFDPlanInfo failed. code:" + statusCode + " re:" + result);
+ 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("reason", "get VNFDPlanInfo failed and JSONException." + e.getMessage());
+ 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("reason", "get VNFDPlanInfo failed and VnfmException." + e.getMessage());
+ 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("reason", "get VNFDPlanInfo failed and IOException." + e.getMessage());
+ resultObj.put(Constant.REASON, "get VNFDPlanInfo failed and IOException." + e.getMessage());
return resultObj;
}
return resultObj;
* @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("reason", "fileName is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.REASON, "fileName is null.");
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
return resultObj;
}
if(filePath == null || "".equals(filePath)) {
- resultObj.put("reason", "unzipCSAR filePath is null.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ 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("reason", "unzip csar file successfully.");
- resultObj.put("retCode", Constant.REST_SUCCESS);
+ resultObj.put(Constant.REASON, "unzip csar file successfully.");
+ resultObj.put(Constant.RETCODE, Constant.REST_SUCCESS);
} else {
- resultObj.put("reason", "unzip csar file failed.");
- resultObj.put("retCode", Constant.REST_FAIL);
+ resultObj.put(Constant.REASON, "unzip csar file failed.");
+ resultObj.put(Constant.RETCODE, Constant.REST_FAIL);
}
return resultObj;
}