Merge "Add More Validations on TOSCA Model Format"
authorJorge Hernandez <jh1730@att.com>
Wed, 4 Apr 2018 15:56:29 +0000 (15:56 +0000)
committerGerrit Code Review <gerrit@onap.org>
Wed, 4 Apr 2018 15:56:29 +0000 (15:56 +0000)
ONAP-REST/src/main/java/org/onap/policy/rest/util/MSModelUtils.java
POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java
POLICY-SDK-APP/src/main/webapp/app/policyApp/controller/dictionaryController/MSModelsDictController.js

index f8b8b52..91e7626 100644 (file)
@@ -738,12 +738,18 @@ public class MSModelUtils {
     /*
      * For TOSCA Model
      */
-       public void parseTosca (String fileName){
+       public String parseTosca (String fileName){
                LinkedHashMap<String,String> map= new LinkedHashMap<>();
     
        try {
                        map=load(fileName);
                        
+                       if(map != null){
+                               if(map.get("error") != null){
+                                       return map.get("error");
+                               }
+                       }
+                       
                        parseDataAndPolicyNodes(map);
                        
                        LinkedHashMap<String,String> dataMapForJson=parseDataNodes(map);
@@ -757,7 +763,8 @@ public class MSModelUtils {
        } catch (IOException e) {
                logger.error(e);
        }
-       
+          
+       return null;
        } 
        
        @SuppressWarnings("unchecked")
@@ -777,6 +784,13 @@ public class MSModelUtils {
                if (yamlMap == null) { 
                        return settings; 
                } 
+                               
+               String message = validations(yamlMap);  
+               
+               if(message != null){
+                       settings.put("error", message);
+                       return settings;                                        
+               }
                
                findNode(yamlMap);
                
@@ -796,7 +810,83 @@ public class MSModelUtils {
                List<String> path = new ArrayList <>(); 
                serializeMap(settings, sb, path, yamlMap); 
                return settings; 
-       } 
+       }
+       
+       @SuppressWarnings("unchecked")
+       private String validations(@SuppressWarnings("rawtypes") LinkedHashMap yamlMap){
+               
+               boolean isNoteTypeFound = false;
+               boolean isDataTypeFound = false;
+               boolean isToscaVersionKeyFound = false;
+               boolean isToscaVersionValueFound = false;
+               @SuppressWarnings("rawtypes")
+               Map m1 = new HashMap();
+               short order =0;
+               if(yamlMap != null){
+                       // Get a set of the entries
+                    @SuppressWarnings("rawtypes")
+                       Set set = yamlMap.entrySet();                 
+                    // Get an iterator
+                    @SuppressWarnings("rawtypes")
+                       Iterator i = set.iterator();                  
+                     // Display elements
+                    while(i.hasNext()) {
+                        @SuppressWarnings("rawtypes")                   
+                                Map.Entry me = (Map.Entry)i.next();
+                        
+                        if("tosca_definitions_version".equals(me.getKey())){
+                                isToscaVersionKeyFound = true;
+                                order++;
+                                m1.put("tosca_definitions_version", order);
+                        }
+                        
+                        if("tosca_simple_yaml_1_0_0".equals(me.getValue())){
+                                isToscaVersionValueFound = true;
+                        }
+
+                        if("node_types".equals(me.getKey())){
+                                isNoteTypeFound = true;
+                                order++;
+                                m1.put("node_types", order);
+                        }
+                        
+                        if("data_types".equals(me.getKey())){
+                                isDataTypeFound = true;
+                                order++;
+                                m1.put("data_types", order);
+                        }
+
+                    }
+                    
+                
+                if(!isDataTypeFound){
+                        return "data_types are missing or invalid.";
+                }  
+                
+                if(!isToscaVersionKeyFound || !isToscaVersionValueFound){
+                        return "tosca_definitions_version is missing or invalid.";
+                }  
+                
+                if(!isNoteTypeFound){
+                        return "node_types are missing or invalid.";
+                }  
+                
+                short version = (short) m1.get("tosca_definitions_version");
+                
+                if(version > 1 ){
+                       return "tosca_definitions_version should be defined first.";
+                }
+                
+                short data = (short) m1.get("data_types");
+                short node = (short) m1.get("node_types");
+                if(node > data){
+                       return "node_types should be defined before data_types.";                        
+                }               
+                
+               }
+               
+               return null;
+       }
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) { 
index 0f315a3..c4c8220 100644 (file)
@@ -1177,7 +1177,17 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        fileList = listModelFiles(this.directory);
                }else if (yml==true){
                        
-                       msMLUtils.parseTosca(this.newFile);
+                       errorMsg = msMLUtils.parseTosca(this.newFile);
+                       if(errorMsg != null){
+                               PrintWriter out = response.getWriter();                         
+                               response.setCharacterEncoding("UTF-8");
+                               response.setContentType("application / json");
+                               request.setCharacterEncoding("UTF-8");
+                               JSONObject j = new JSONObject();
+                               j.put("errorMsg", errorMsg);
+                           out.write(j.toString());
+                           return;
+                       }
                        
                }else {
                        File file = new File(this.newFile);
index 3165b1b..262b2ad 100644 (file)
@@ -36,9 +36,16 @@ app.controller('editMSModelController' ,  function ($scope, $modalInstance, mess
     $scope.editMSmodelName = message.microServiceModelsDictionaryData;
 
     $scope.uploadFile = function(files) {
+               valid = true;
        var extn = files[0].name.substr(files[0].name.lastIndexOf('.')+1);
        if(extn == 'zip' || extn == 'xmi'||  extn == 'yml'){
-               valid = true;
+               if(extn == 'yml'){
+                       if(!files[0].name.includes("-v")){
+                               Notification.error("File name should contain -v, such as myModel-v123.yml");
+                               valid = false;
+                               return;
+                       }
+               }
                var fd = new FormData();
                fd.append("file", files[0]);
                $http.post("ms_dictionary/set_MSModelData", fd, {
@@ -46,7 +53,7 @@ app.controller('editMSModelController' ,  function ($scope, $modalInstance, mess
                        headers: {'Content-Type': undefined },
                        transformRequest: angular.identity
                }).success(function(data){
-                       if(data.errorMsg != undefined){
+                       if(data.errorMsg != undefined || data.errorMsg != null){
                                Notification.error(data.errorMsg);
                                valid = false;
                                return;
@@ -62,7 +69,7 @@ app.controller('editMSModelController' ,  function ($scope, $modalInstance, mess
                 }
             }).error( );
        }else{
-               Notification.error("Micro Service Model Upload file should ends with .zip or .xmi extension");
+               Notification.error("Micro Service Model Upload file should ends with .zip .yml or .xmi extension");
                valid = false;
        }