diff options
5 files changed, 63 insertions, 20 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index b773f1fbbc..fff9f0bf1c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -271,13 +271,13 @@ public final class ImportUtils { } private static void setPropertyConstraints(Map<String, Object> propertyValue, PropertyDefinition property) { - List<PropertyConstraint> constraints = getPropertyConstraints(propertyValue, property.getType()); + List<PropertyConstraint> constraints = getPropertyConstraints(propertyValue, property.getType(), property.getSchema()); if (CollectionUtils.isNotEmpty(constraints)) { property.setConstraints(constraints); } } - private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, final String propertyType) { + private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, final String propertyType, final SchemaDefinition schema) { final List<Object> propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue); if (CollectionUtils.isEmpty(propertyFieldConstraints)) { return Collections.emptyList(); @@ -287,7 +287,7 @@ public final class ImportUtils { }.getType(); final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create(); for (final Object constraintJson : propertyFieldConstraints) { - final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson); + final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson, schema); if (propertyConstraint != null) { constraintList.add(propertyConstraint); } @@ -308,7 +308,7 @@ public final class ImportUtils { return constraints; } - private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson) { + private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson, SchemaDefinition schema) { PropertyConstraint propertyConstraint; try { propertyConstraint = gson.fromJson(gson.toJson(constraintJson), constraintType); @@ -317,7 +317,7 @@ public final class ImportUtils { } if (propertyConstraint instanceof ValidValuesConstraint) { try { - ((ValidValuesConstraint) propertyConstraint).validateType(propertyType); + ((ValidValuesConstraint) propertyConstraint).validateType(propertyType, schema); boolean valid = ((ValidValuesConstraint) propertyConstraint).validateValueType(propertyType); if (!valid) { ((ValidValuesConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java index 6c820a1af2..966f13c952 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java @@ -140,8 +140,8 @@ public class PropertyValueConstraintValidationUtil { if (isPropertyNotMappedAsInput(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) { for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) { try { - propertyConstraint.initialize(toscaType); - propertyConstraint.validate(toscaType, propertyDefinition.getValue()); + propertyConstraint.initialize(toscaType, propertyDefinition.getSchema()); + propertyConstraint.validate(toscaType, propertyDefinition.getSchema(), propertyDefinition.getValue()); } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) { errorMessages.add(propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition))); } catch (IllegalArgumentException ie) { 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 { |