Make sure the streams are closed properly-adapter
[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
index 420d8eb..53e51de 100644 (file)
@@ -32,6 +32,7 @@ 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;
@@ -61,16 +62,17 @@ public class AdapterResourceManager implements IResourceManager {
     private static final String VNFD_FILE_PATH = "vnfd_file_path";
 
     @Override
-    public JSONObject uploadVNFPackage(JSONObject vnfpkg, Map<String, String> paramsMap) {
+    public JSONObject uploadVNFPackage(JSONObject vnfpkge, Map<String, String> paramsMap) {
         JSONObject resultObj = new JSONObject();
         String vnfDescriptorId = paramsMap.get("vnfDescriptorId");
+        JSONObject vnfpkgJson = new JSONObject();
         try {
             // if upper layer do not provide vnfpackage info,then get the
             // vnfpackage info from JSON file.
-            if(vnfpkg == null || vnfpkg.isEmpty()) {
+            if(vnfpkge == null || vnfpkge.isEmpty()) {
                 String vnfPkgInfo = readVfnPkgInfoFromJson();
-                JSONObject vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
-                vnfpkg = vnfpkgJson.getJSONObject(vnfDescriptorId);
+                vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
+
             }
         } catch(IOException e) {
             LOG.error("function=uploadVNFPackage", e);
@@ -101,15 +103,19 @@ public class AdapterResourceManager implements IResourceManager {
         // obtain CSAR package info
         JSONObject csarobj = getVnfmCsarInfo(csarid);
         String downloadUri = "";
+        String csarName = "";
         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");
+            downloadUri = csarobj.getJSONObject("packageInfo").getString("downloadUrl");
+            csarName = csarobj.getJSONObject("packageInfo").getString("csarName");
         } else {
             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;
         }
+        String vnfdName = transferFromCsar(csarName);
+        JSONObject vnfpkg = vnfpkgJson.getJSONObject(vnfdName);
         JSONObject csarTempObj = vnfpkg.getJSONObject("template");
         String csarfilepath = csarTempObj.getString("csar_file_path");
         String csarfilename = csarTempObj.getString("csar_file_name");
@@ -137,22 +143,17 @@ 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);
+        // 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(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(Constant.RETCODE));
+        // getVnfmConnInfo(vnfmMap)
+        JSONObject vnfmObject = VnfmUtil.getVnfmById(vnfmid);
+        LOG.info("get Vnfm Connection Info successful.");
 
         String vnfmUrl = vnfmObject.getString("url");
         String userName = vnfmObject.getString(Constant.USERNAME);
@@ -187,7 +188,7 @@ public class AdapterResourceManager implements IResourceManager {
         }
 
         // upload VNF package
-        csarTempObj.put("vim_id", vimId);
+        // csarTempObj.put("vim_id", vimId);
         vnfpkg.put("template", csarTempObj);
         LOG.info("vnfpkg: " + vnfpkg);
 
@@ -200,9 +201,9 @@ public class AdapterResourceManager implements IResourceManager {
 
         if(vnfdid == null || "".equals(vnfdid.trim())) {
             JSONObject vnfdConf = readVnfdIdInfoFromJson();
-            LOG.info("vnfdConf=" + vnfdConf);
-            if(vnfdConf.containsKey(vnfDescriptorId)) {
-                vnfdid = vnfdConf.getString(vnfDescriptorId);
+            LOG.info("vnfdName:" + vnfdName + ", vnfdConf=" + vnfdConf);
+            if(vnfdConf.containsKey(vnfdName)) {
+                vnfdid = vnfdConf.getString(vnfdName);
             }
         }
         LOG.info("set vnfdId=" + vnfdid);
@@ -275,6 +276,14 @@ public class AdapterResourceManager implements IResourceManager {
         return resultObj;
     }
 
+    private String transferFromCsar(String csarName) {
+        LOG.info("csarName: " + csarName);
+        if(csarName.toUpperCase().contains("HSS")) {
+            return "HSS";
+        }
+        return "";
+    }
+
     private JSONObject uploadCsar(JSONObject vnfpkg, String csarfilepath) {
         LOG.info("vnfpkg: " + vnfpkg + "csarfilepath:" + csarfilepath);
         JSONObject resJson = new JSONObject();
@@ -297,14 +306,17 @@ public class AdapterResourceManager implements IResourceManager {
             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/";
+            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!");
+            try(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!");
+                }
+            } catch(Exception e) {
+                LOG.error("Exception: " + e);
             }
-            inputStream.close();
+
             ftpClient.logout();
         } catch(Exception e) {
             LOG.error("Exception: " + e);
@@ -598,33 +610,23 @@ public class AdapterResourceManager implements IResourceManager {
      * @since VFC 1.0
      */
     public static String readVfnPkgInfoFromJson() throws IOException {
-        InputStream ins = null;
-        BufferedInputStream bins = null;
         String fileContent = "";
 
         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
                 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
                 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + Constant.VNFPKGINFO;
 
-        try {
-            ins = new FileInputStream(fileName);
-            bins = new BufferedInputStream(ins);
-
-            byte[] contentByte = new byte[ins.available()];
-            int num = bins.read(contentByte);
+        try (InputStream ins = new FileInputStream(fileName)) {
+            try(BufferedInputStream bins = new BufferedInputStream(ins)){
+                byte[] contentByte = new byte[ins.available()];
+                int num = bins.read(contentByte);
 
-            if(num > 0) {
-                fileContent = new String(contentByte);
+                if(num > 0) {
+                    fileContent = new String(contentByte);
+                }
             }
         } catch(FileNotFoundException e) {
             LOG.error(fileName + "is not found!", e);
-        } finally {
-            if(ins != null) {
-                ins.close();
-            }
-            if(bins != null) {
-                bins.close();
-            }
         }
 
         return fileContent;
@@ -632,35 +634,28 @@ public class AdapterResourceManager implements IResourceManager {
 
     private static JSONObject readVnfdIdInfoFromJson() {
         JSONObject jsonObject = new JSONObject();
-        InputStream ins = null;
-        BufferedInputStream bins = null;
+
         String fileContent = "";
 
         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
                 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
                 + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfd_ids.json";
 
-        try {
-            ins = new FileInputStream(fileName);
-            bins = new BufferedInputStream(ins);
-
-            byte[] contentByte = new byte[ins.available()];
-            int num = bins.read(contentByte);
+        try (InputStream ins = new FileInputStream(fileName)) {
+            try (BufferedInputStream bins = new BufferedInputStream(ins)){
+                byte[] contentByte = new byte[ins.available()];
+                int num = bins.read(contentByte);
 
-            if(num > 0) {
-                fileContent = new String(contentByte);
-            }
-            if(fileContent != null) {
-                jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds");
+                if(num > 0) {
+                    fileContent = new String(contentByte);
+                }
+                if(fileContent != null) {
+                    jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds");
+                }
             }
-            ins.close();
-            bins.close();
         } catch(Exception e) {
             LOG.error(fileName + " read error!", e);
-        } finally {
-
         }
-
         return jsonObject;
     }