diff options
Diffstat (limited to 'catalog-model/src/main')
3 files changed, 56 insertions, 13 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java index 12a79ec90e..df961acb6e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model; import com.fasterxml.jackson.annotation.JsonIgnore; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; @@ -28,12 +29,12 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolatio import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; public interface PropertyConstraint { - - void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException; - + + void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException; + void validate(Object propertyValue) throws ConstraintViolationException; - void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException; + void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException; @JsonIgnore ConstraintType getConstraintType(); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java index 43bed0af67..b8e2e26660 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java @@ -20,6 +20,8 @@ package org.openecomp.sdc.be.model.tosca.constraints; import java.util.Arrays; + +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; @@ -32,7 +34,11 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint { private static final String INVALID_VALUE_ERROR_MESSAGE = "Unsupported value provided for %s property supported value type is %s."; @Override - public void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException { + public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException { + validate(toscaType, propertyTextValue); + } + + protected void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException { try { validate(toscaType.convert(propertyTextValue)); } catch (ApplicationVersionException e) { @@ -51,8 +57,12 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint { return String.format(INVALID_VALUE_ERROR_MESSAGE, propertyName, toscaType.getType()); } - @Override - public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + protected void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { //Initialization not needed for few constraints for now might be needed in future } + + @Override + public void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException { + initialize(propertyType); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java index 1fd1adf015..4970808948 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java @@ -21,8 +21,13 @@ package org.openecomp.sdc.be.model.tosca.constraints; import static java.util.stream.Collectors.toList; +import java.util.Collection; +import java.util.Collections; + +import com.fasterxml.jackson.core.type.TypeReference; import com.google.common.collect.Sets; import java.util.List; +import java.util.Map; import java.util.Set; import javax.validation.constraints.NotNull; import lombok.EqualsAndHashCode; @@ -30,6 +35,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -53,26 +59,27 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { public ValidValuesConstraint(List<Object> validValues) { this.validValues = validValues; } - + @Override - public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + public void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = getValuesType(propertyType, schema); validValuesTyped = Sets.newHashSet(); if (validValues == null) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">"); } for (Object value : validValues) { - if (!propertyType.isValidValue(String.valueOf(value))) { + if (!toscaType.isValidValue(String.valueOf(value))) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">"); } else { - validValuesTyped.add(propertyType.convert(String.valueOf(value))); + validValuesTyped.add(toscaType.convert(String.valueOf(value))); } } } - public void validateType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - ToscaType toscaType = ToscaType.getToscaType(propertyType); + public void validateType(String propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = getValuesType(ToscaType.getToscaType(propertyType), schema); if (toscaType == null) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid values <" + validValues.toString() + PROPERTY_TYPE_IS + propertyType + ">"); @@ -88,6 +95,10 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { } } } + + private ToscaType getValuesType(ToscaType propertyType, SchemaDefinition schema) { + return ToscaType.isCollectionType(propertyType.getType()) ? ToscaType.getToscaType(schema.getProperty().getType()) : propertyType; + } @Override public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException { @@ -100,6 +111,27 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { } } } + + @Override + public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException { + try { + Collection<Object> valuesToValidate; + if (ToscaType.LIST == toscaType) { + valuesToValidate = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {}); + } else if (ToscaType.MAP == toscaType) { + final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {}); + valuesToValidate = map.values(); + } else { + valuesToValidate = Collections.singleton(propertyTextValue); + } + ToscaType valuesType = getValuesType(toscaType, schema); + for (final Object value: valuesToValidate) { + validate(valuesType, value.toString()); + } + } catch (ConstraintValueDoNotMatchPropertyTypeException exception) { + throw new ConstraintViolationException("Value cannot be parsed to a list", exception); + } + } @Override public void validate(Object propertyValue) throws ConstraintViolationException { |