Merge "Added changes to save original model file"
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / CreateNewMicroServiceModel.java
index d6718ab..86455aa 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -57,7 +58,7 @@ public class CreateNewMicroServiceModel {
     private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
 
 
-    MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
+    private MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
 
     public CreateNewMicroServiceModel(String fileName, String serviceName, String string, String version) {
         super();
@@ -80,31 +81,8 @@ public class CreateNewMicroServiceModel {
             File directory = new File("ExtractDir" + File.separator + randomID);
             List<File> fileList = listModelFiles(directory.toString());
             //get all the files from a director
-            for (File file : fileList){
-                if (file.isFile()){
-                    int i = file.getName().lastIndexOf('.');
-                    String type = file.getName().substring(i+1);
-
-                    if(type != null && "yml".equalsIgnoreCase(type)){
-
-                        processYmlModel(file.toString(), modelName);
-
-                    }else{
-
-                        tempMap = utils.processEpackage(file.getAbsolutePath(), MODEL_TYPE.XMI);
-                        classMap.putAll(tempMap);
-                    }
-                }
-            }
-            cleanUpFile = "ExtractDir" + File.separator + randomID + ".zip";
-            try {
-                FileUtils.deleteDirectory(new File("ExtractDir" + File.separator + randomID));
-                FileUtils.deleteDirectory(new File(randomID));
-                File deleteFile = new File(cleanUpFile);
-                FileUtils.forceDelete(deleteFile);
-            } catch (IOException e) {
-                logger.error("Failed to unzip model file " + randomID, e);
-            }
+            processFiles(modelName, fileList);
+            doCleanUpFiles(randomID);
         }else {
             if(importFile.contains(".yml")){
 
@@ -122,6 +100,39 @@ public class CreateNewMicroServiceModel {
         }
     }
 
+    private void processFiles(String modelName, List<File> fileList) {
+        Map<String, MSAttributeObject> tempMap;
+        for (File file : fileList){
+            if (file.isFile()){
+                int i = file.getName().lastIndexOf('.');
+                String type = file.getName().substring(i+1);
+
+                if("yml".equalsIgnoreCase(type)){
+
+                    processYmlModel(file.toString(), modelName);
+
+                }else{
+
+                    tempMap = utils.processEpackage(file.getAbsolutePath(), MODEL_TYPE.XMI);
+                    classMap.putAll(tempMap);
+                }
+            }
+        }
+    }
+
+    private void doCleanUpFiles(String randomID) {
+        String cleanUpFile;
+        cleanUpFile = "ExtractDir" + File.separator + randomID + ".zip";
+        try {
+            FileUtils.deleteDirectory(new File("ExtractDir" + File.separator + randomID));
+            FileUtils.deleteDirectory(new File(randomID));
+            File deleteFile = new File(cleanUpFile);
+            FileUtils.forceDelete(deleteFile);
+        } catch (IOException e) {
+            logger.error("Failed to unzip model file " + randomID, e);
+        }
+    }
+
     private void processYmlModel(String fileName, String  modelName){
 
         try {
@@ -145,7 +156,7 @@ public class CreateNewMicroServiceModel {
             returnReferenceList.put(modelName, utils.getReferenceAttributes());
             msAttributes.setRefAttribute(returnReferenceList);
 
-            if(utils.getListConstraints()!=""){
+            if(!PolicyDBDao.isNullOrEmpty(utils.getListConstraints())){
                 LinkedHashMap<String, String> enumList =new LinkedHashMap<>();
                 String[] listArray=utils.getListConstraints().split("#");
                 for(String str:listArray){
@@ -322,7 +333,7 @@ public class CreateNewMicroServiceModel {
         Map<String, String> successMap = new HashMap<>();
         CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
         List<Object> result = dbConnection.getDataById(MicroServiceModels.class, "modelName:version", modelName+":"+version);
-        if(result.isEmpty()){
+        if(result == null || result.isEmpty()){
             MicroServiceModels model = new MicroServiceModels();
             model.setModelName(modelName);
             model.setVersion(version);