Merge "Replace LOGPATH with LOGSUFFIX"
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / util / MSModelUtils.java
index f8b8b52..1a62ccc 100644 (file)
@@ -96,6 +96,7 @@ public class MSModelUtils {
        public static final String STRING="string";
        public static final String INTEGER="integer";
        public static final String LIST="list";
+       public static final String MAP="map";
        public static final String DEFAULT=".default";
        public static final String MANYFALSE=":MANY-false";
        public static final String MANYTRUE=":MANY-true";
@@ -738,12 +739,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);
@@ -756,12 +763,16 @@ public class MSModelUtils {
                
        } catch (IOException e) {
                logger.error(e);
+       }catch(ParserException e){
+               logger.error(e);
+               return e.getMessage();
        }
-       
+          
+       return null;
        } 
        
        @SuppressWarnings("unchecked")
-       public LinkedHashMap<String, String> load(String fileName) throws IOException { 
+       public LinkedHashMap<String, String> load(String fileName) throws IOException,ParserException { 
                File newConfiguration = new File(fileName);
                StringBuilder orderInfo = new StringBuilder("[");
                Yaml yaml = new Yaml();
@@ -770,6 +781,8 @@ public class MSModelUtils {
                        yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is); 
                } catch (FileNotFoundException e) {
                        logger.error(e);
+               }catch(Exception e){
+                       throw new ParserException("Invalid TOSCA Model format. Please make sure it is a valid YAML file");
                }
 
                StringBuilder sb = new StringBuilder(); 
@@ -777,6 +790,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 +816,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) { 
@@ -985,7 +1081,7 @@ public class MSModelUtils {
                                        attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
                                        dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());         
                                }
-                               else if(typeValue != null && typeValue.equalsIgnoreCase(LIST)){
+                               else if(LIST.equalsIgnoreCase(typeValue) || MAP.equalsIgnoreCase(typeValue)){
                                        logger.info("requiredValue is:"+ requiredValue);
                                        String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
                                        String listValue=map.get(findList);
@@ -1001,19 +1097,31 @@ public class MSModelUtils {
                                                }//Its string
                                                else{
                                                        StringBuilder stringListItems= new StringBuilder();
-                                                       stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+REQUIREDVALUE+requiredValue +MANYFALSE);
+                                                       if(LIST.equalsIgnoreCase(typeValue)){
+                                                           stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":required-"+requiredValue +":MANY-false");
+                                                       }else if( MAP.equalsIgnoreCase(typeValue)){
+                                                               stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":required-"+requiredValue +":MANY-true");
+                                                       }
                                                        dataMapForJson.put(uniqueDataKey, stringListItems.toString());
-                                                       dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
+                                                       boolean isConstraintsFound = false;
                                                        for(int i=0;i<10;i++){
                                                                String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
                                                                logger.info("findConstraints => " + findConstraints);
                                                                String constraintsValue=map.get(findConstraints);
                                                                logger.info("constraintsValue => " + constraintsValue);
-                                                               if(constraintsValue==null){
+                                                               if((constraintsValue==null || constraintsValue.isEmpty()) && i==0){ //if no constraints at all ( index i as 0 can tell this )
+                                                                       isConstraintsFound = false;
+                                                                       //if type is list but no constraints
+                                                                       String newValue = dataMapForJson.get(uniqueDataKey).replace("MANY-false", "MANY-true"); 
+                                                                       newValue = newValue.replace(uniqueDataKeySplit[1].toUpperCase()+":", "");       
+                                                                       dataMapForJson.put(uniqueDataKey, newValue);
                                                                        break;
-                                                               }
-                                                               else{
-                                                                       logger.info("constraintsValue => " + constraintsValue);
+                                                               } else{
+                                                                       isConstraintsFound = true;
+                                                                       if(i == 0){ // only need to add one time for the same attribute
+                                                                          dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
+                                                                       }
+
                                                                        if(constraintsValue.contains("=")){
                                                                                constraintsValue = constraintsValue.replace("=", "equal-sign");
                                                                        }
@@ -1021,9 +1129,12 @@ public class MSModelUtils {
                                                                        dataListBuffer.append(constraintsValue+",");
                                                                }
                                                        }
-                                                       dataListBuffer.append("]#");
-                                                       logger.info(dataListBuffer);
+                                                       if(isConstraintsFound){                                                 
+                                                           dataListBuffer.append("]#");
+                                                       }
                                                }
+                                       }else{
+                                               logger.info("entry_schema.type is not defined correctly");
                                        }
                                }
                                else{
@@ -1043,7 +1154,7 @@ public class MSModelUtils {
        }
        
        
-       LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String,String> map){
+       LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String,String> map) throws ParserException{
                LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= new LinkedHashMap <>();
                for(String uniqueKey: uniqueKeys){
                        LinkedHashMap<String,String> hm;
@@ -1055,7 +1166,11 @@ public class MSModelUtils {
                                                hm = mapKey.get(uniqueKey);
                                                String keyStr= key.substring(key.lastIndexOf('.')+1);
                                                String valueStr= map.get(key);
-                                               if(("type").equals(keyStr)){
+                                               if("type".equalsIgnoreCase(keyStr) && ((key.contains("entry_schema.0.type") || key.contains("entry_schema.type") && valueStr.contains("policy.data.")))){
+                                                  throw new ParserException("For using user defined object type, Please make sure no space between 'type:' and object " + valueStr );
+                                                       
+                                               }       
+                                               if("type".equals(keyStr)){
                                                        if(!key.contains("entry_schema"))
                                                        {
                                                                hm.put(keyStr,valueStr);
@@ -1068,6 +1183,9 @@ public class MSModelUtils {
                                                hm = new LinkedHashMap <>();
                                                String keyStr= key.substring(key.lastIndexOf('.')+1);
                                                String valueStr= map.get(key);
+                                               if(key.contains(".objective.")){                                                        
+                                                       throw new ParserException("Attribute objective is a key word. Please use a different name");
+                                               }
                                                if(("type").equals(keyStr)){
                                                        if(!key.contains("entry_schema"))
                                                        {