Support TOSCA functions in operation inputs
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / PropertyConvertor.java
index 90a5161..bb89d99 100644 (file)
@@ -43,6 +43,7 @@ import org.openecomp.sdc.be.model.PropertyConstraint;
 import org.openecomp.sdc.be.model.PropertyDefinition;
 import org.openecomp.sdc.be.model.Resource;
 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
+import org.openecomp.sdc.be.model.tosca.ToscaType;
 import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
@@ -52,7 +53,9 @@ import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
+import org.openecomp.sdc.be.model.tosca.constraints.PatternConstraint;
 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
+import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
@@ -69,6 +72,7 @@ import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessOrEqual;
 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessThan;
 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMaxLength;
 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMinLength;
+import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintPattern;
 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
 import org.openecomp.sdc.common.log.wrappers.Logger;
@@ -76,7 +80,6 @@ import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
 import org.springframework.stereotype.Service;
 import org.yaml.snakeyaml.Yaml;
 
-
 @Service
 public class PropertyConvertor {
 
@@ -90,9 +93,9 @@ public class PropertyConvertor {
             if (props != null) {
                 Map<String, ToscaProperty> properties = new HashMap<>();
                 // take only the properties of this resource
-                props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
-                    properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
-                });
+                props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property ->
+                    properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY))
+                );
                 if (!properties.isEmpty()) {
                     toscaNodeType.setProperties(properties);
                 }
@@ -127,45 +130,94 @@ public class PropertyConvertor {
             prop.setStatus(property.getStatus());
         }
         prop.setMetadata(property.getMetadata());
-        
-        List<ToscaPropertyConstraint> constraints = new ArrayList<>();
+
         if (CollectionUtils.isNotEmpty(property.getConstraints())) {
-            constraints = convertConstraints(property.getConstraints());
-            prop.setConstraints(constraints);
+            try {
+                prop.setConstraints(convertConstraints(property.getConstraints(), property.getType(), property.getSchemaType()));
+            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
+                log.error(e.getMessage());
+            }
         }
         return prop;
     }
-    
-    private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints) {
+
+    private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints, String propertyType, String schemaType)
+        throws ConstraintValueDoNotMatchPropertyTypeException {
         List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>();
-        for (PropertyConstraint constraint: constraints){
+        for (PropertyConstraint constraint : constraints) {
             if (constraint instanceof EqualConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintEqual(((EqualConstraint) constraint).getEqual()));
+                EqualConstraint equalConstraint = ((EqualConstraint) constraint);
+
+                if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
+                    equalConstraint.changeConstraintValueTypeTo(propertyType);
+                }
+
+                ToscaPropertyConstraintEqual prop = new ToscaPropertyConstraintEqual(equalConstraint.getEqual());
+                convertedConstraints.add(prop);
             }
             if (constraint instanceof GreaterThanConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintGreaterThan(((GreaterThanConstraint) constraint).getGreaterThan()));
+                GreaterThanConstraint greaterThanConstraint = ((GreaterThanConstraint) constraint);
+
+                if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
+                    greaterThanConstraint.changeConstraintValueTypeTo(propertyType);
+                }
+
+                ToscaPropertyConstraintGreaterThan prop = new ToscaPropertyConstraintGreaterThan(greaterThanConstraint.getGreaterThan());
+                convertedConstraints.add(prop);
             }
             if (constraint instanceof GreaterOrEqualConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintGreaterOrEqual(((GreaterOrEqualConstraint) constraint).getGreaterOrEqual()));
+                GreaterOrEqualConstraint greaterOrEqualConstraint = ((GreaterOrEqualConstraint) constraint);
+
+                if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
+                    greaterOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
+                }
+
+                ToscaPropertyConstraintGreaterOrEqual prop = new ToscaPropertyConstraintGreaterOrEqual(greaterOrEqualConstraint.getGreaterOrEqual());
+                convertedConstraints.add(prop);
             }
             if (constraint instanceof LessThanConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintLessThan(((LessThanConstraint) constraint).getLessThan()));
+                LessThanConstraint lessThanConstraint = ((LessThanConstraint) constraint);
+
+                if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
+                    lessThanConstraint.changeConstraintValueTypeTo(propertyType);
+                }
+
+                ToscaPropertyConstraintLessThan prop = new ToscaPropertyConstraintLessThan(lessThanConstraint.getLessThan());
+                convertedConstraints.add(prop);
             }
             if (constraint instanceof LessOrEqualConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintLessOrEqual(((LessOrEqualConstraint) constraint).getLessOrEqual()));
+                LessOrEqualConstraint lessOrEqualConstraint = ((LessOrEqualConstraint) constraint);
+
+                if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
+                    lessOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
+                }
+
+                ToscaPropertyConstraintLessOrEqual prop = new ToscaPropertyConstraintLessOrEqual(lessOrEqualConstraint.getLessOrEqual());
+                convertedConstraints.add(prop);
             }
             if (constraint instanceof InRangeConstraint) {
                 InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint;
-                List<String> range = new ArrayList<>();
-                range.add(inRangeConstraint.getRangeMinValue());
-                range.add(inRangeConstraint.getRangeMaxValue());
-                convertedConstraints.add(new ToscaPropertyConstraintInRange(range));
+
+                if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
+                    inRangeConstraint.changeConstraintValueTypeTo(propertyType);
+                }
+
+                convertedConstraints.add(new ToscaPropertyConstraintInRange(inRangeConstraint.getInRange()));
             }
             if (constraint instanceof ValidValuesConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintValidValues(((ValidValuesConstraint) constraint).getValidValues()));
+                ValidValuesConstraint validValues = ((ValidValuesConstraint) constraint);
+
+                if (isTypeMapOrList(propertyType) && doesTypeNeedConvertingToIntOrFloat(schemaType)) {
+                    validValues.changeConstraintValueTypeTo(schemaType);
+                } else if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
+                    validValues.changeConstraintValueTypeTo(propertyType);
+                }
+
+                List prop = validValues.getValidValues();
+                convertedConstraints.add(new ToscaPropertyConstraintValidValues(prop));
             }
             if (constraint instanceof LengthConstraint) {
-                convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength().toString()));
+                convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength()));
             }
             if (constraint instanceof MinLengthConstraint) {
                 convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength()));
@@ -173,10 +225,21 @@ public class PropertyConvertor {
             if (constraint instanceof MaxLengthConstraint) {
                 convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength()));
             }
+            if (constraint instanceof PatternConstraint) {
+                convertedConstraints.add(new ToscaPropertyConstraintPattern(((PatternConstraint) constraint).getPattern()));
+            }
         }
         return convertedConstraints;
     }
 
+    private boolean doesTypeNeedConvertingToIntOrFloat(String propertyType) {
+        return ToscaType.INTEGER.getType().equals(propertyType) || ToscaType.FLOAT.getType().equals(propertyType);
+    }
+
+    private boolean isTypeMapOrList (String type) {
+        return ToscaType.MAP.getType().equals(type) || ToscaType.LIST.getType().equals(type);
+    }
+
     public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
                                        boolean preserveEmptyValue) {
         String propertyType = property.getType();
@@ -251,13 +314,14 @@ public class PropertyConvertor {
                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
             } else {
                 convertedValue = mapConverterInst
-                    .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
+                    .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue,
+                        property.isToscaFunction());
             }
             return convertedValue;
-        
+
         } catch (JsonParseException e) {
             log.trace("{} not parsable as JSON. Convert as YAML instead", value);
-            return  new Yaml().load(value);
+            return new Yaml().load(value);
         } catch (Exception e) {
             log.debug("convertToToscaValue failed to parse json value :", e);
             return null;