From 963ed36db35fd5e302f9a73661b2dab96339b433 Mon Sep 17 00:00:00 2001 From: luxin Date: Tue, 26 Sep 2017 09:15:35 +0800 Subject: Upload vnf package to ftps server Change-Id: Ic9aefbabae6c613834ac3fde1fa018c8f1e587bd Issue-Id:VFC-452 Signed-off-by: luxin --- .../vnfmadapter/VnfmadapterService/service/pom.xml | 35 ++++++----- .../adapter/impl/AdapterResourceManager.java | 69 +++++++++++++++++++--- 2 files changed, 81 insertions(+), 23 deletions(-) (limited to 'huawei/vnfmadapter/VnfmadapterService/service') diff --git a/huawei/vnfmadapter/VnfmadapterService/service/pom.xml b/huawei/vnfmadapter/VnfmadapterService/service/pom.xml index dfd285b4..2fbfe3d0 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/pom.xml +++ b/huawei/vnfmadapter/VnfmadapterService/service/pom.xml @@ -27,21 +27,26 @@ war - - org.powermock - powermock-core - 1.5.4 - - - org.powermock - powermock-api-mockito - 1.5.4 - - - org.powermock - powermock-module-junit4 - 1.5.4 - + + org.powermock + powermock-core + 1.5.4 + + + org.powermock + powermock-api-mockito + 1.5.4 + + + org.powermock + powermock-module-junit4 + 1.5.4 + + + commons-net + commons-net + 3.6 + commons-io commons-io diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java index 6b33750c..64e852e4 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java @@ -17,6 +17,7 @@ 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; @@ -26,6 +27,9 @@ import java.util.Map; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPReply; +import org.apache.commons.net.ftp.FTPSClient; import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.DownloadCsarManager; import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException; import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse; @@ -54,6 +58,8 @@ public class AdapterResourceManager implements IResourceManager { private static final Logger LOG = LoggerFactory.getLogger(AdapterResourceManager.class); + private static final String VNFD_FILE_PATH = "vnfd_file_path"; + @Override public JSONObject uploadVNFPackage(JSONObject vnfpkg, Map paramsMap) { JSONObject resultObj = new JSONObject(); @@ -104,26 +110,23 @@ public class AdapterResourceManager implements IResourceManager { 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(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) { - LOG.error("download CSAR fail.", downloadObject.get(Constant.RETCODE)); + LOG.error("download CSAR fail." + downloadObject.get(Constant.RETCODE)); resultObj.put(Constant.REASON, downloadObject.get(Constant.REASON).toString()); resultObj.put(Constant.RETCODE, downloadObject.get(Constant.RETCODE).toString()); return resultObj; } - LOG.info("download CSAR successful.", downloadObject.get(Constant.RETCODE)); + 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(Constant.RETCODE).toString()) != Constant.REST_SUCCESS) { LOG.error("unzip CSAR fail.", unzipObject.get(Constant.RETCODE)); @@ -133,6 +136,10 @@ public class AdapterResourceManager implements IResourceManager { } 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 vnfmMap = new HashMap<>(); vnfmMap.put("url", String.format(UrlConstant.REST_VNFMINFO_GET, vnfmid)); vnfmMap.put("methodType", Constant.GET); @@ -268,6 +275,51 @@ public class AdapterResourceManager implements IResourceManager { 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/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 paramsMap) { JSONObject resultObj = new JSONObject(); RestfulResponse rsp = VNFRestfulUtil.getRemoteResponse(paramsMap, ""); @@ -618,6 +670,7 @@ public class AdapterResourceManager implements IResourceManager { * @return */ public JSONObject unzipCSAR(String fileName, String filePath) { + LOG.info("fileName: " + fileName + ", filePath: " + filePath); JSONObject resultObj = new JSONObject(); if(fileName == null || "".equals(fileName)) { -- cgit 1.2.3-korg