Search in sources :

Example 16 with ConstraintViolationException

use of org.alien4cloud.tosca.exceptions.ConstraintViolationException in project alien4cloud by alien4cloud.

the class ConstraintPropertyService method checkDataTypePropertyConstraint.

private static void checkDataTypePropertyConstraint(String propertyName, Map<String, Object> complexPropertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    DataType dataType = ToscaContext.get(DataType.class, propertyDefinition.getType());
    if (dataType == null) {
        throw new ConstraintViolationException("Complex type " + propertyDefinition.getType() + " is not complex or it cannot be found in the archive nor in Alien");
    }
    for (Map.Entry<String, Object> complexPropertyValueEntry : complexPropertyValue.entrySet()) {
        if (!safe(dataType.getProperties()).containsKey(complexPropertyValueEntry.getKey())) {
            throw new ConstraintViolationException("Complex type <" + propertyDefinition.getType() + "> do not have nested property with name <" + complexPropertyValueEntry.getKey() + "> for property <" + propertyName + ">");
        } else {
            Object nestedPropertyValue = complexPropertyValueEntry.getValue();
            PropertyDefinition nestedPropertyDefinition = dataType.getProperties().get(complexPropertyValueEntry.getKey());
            checkPropertyConstraint(propertyName + "." + complexPropertyValueEntry.getKey(), nestedPropertyValue, nestedPropertyDefinition, missingPropertyConsumer);
        }
    }
    // check if the data type has required missing properties
    if (missingPropertyConsumer != null) {
        for (Map.Entry<String, PropertyDefinition> dataTypeDefinition : safe(dataType.getProperties()).entrySet()) {
            if (dataTypeDefinition.getValue().isRequired() && !complexPropertyValue.containsKey(dataTypeDefinition.getKey())) {
                // A required property is missing
                String missingPropertyName = propertyName + "." + dataTypeDefinition.getKey();
                missingPropertyConsumer.accept(missingPropertyName);
            }
        }
    }
}
Also used : DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Map(java.util.Map) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Aggregations

ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)16 ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)11 Map (java.util.Map)7 ConstraintTechnicalException (org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)6 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)5 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)4 Audit (alien4cloud.audit.annotation.Audit)3 NotFoundException (alien4cloud.exception.NotFoundException)3 Application (alien4cloud.model.application.Application)3 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)3 Location (alien4cloud.model.orchestrators.locations.Location)3 ConstraintUtil (alien4cloud.tosca.properties.constraints.ConstraintUtil)3 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)3 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)3 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 IntrospectionException (java.beans.IntrospectionException)2 DeploymentInputs (org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs)2 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)2 Topology (org.alien4cloud.tosca.model.templates.Topology)2