Search in sources :

Example 16 with PropertyConstraint

use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.

the class NodeFilterValidationService method validatePropertyFilters.

private List<Violations> validatePropertyFilters(Map<String, List<PropertyConstraint>> propertyFilters, Map<String, AbstractPropertyValue> propertyValues, Map<String, PropertyDefinition> propertyDefinitionMap, boolean skipInputs) {
    List<Violations> violations = Lists.newArrayList();
    for (Map.Entry<String, List<PropertyConstraint>> propertyEntry : propertyFilters.entrySet()) {
        Violations violation = new Violations(propertyEntry.getKey());
        List<NodeFilterConstraintViolation> violatedConstraints = Lists.newArrayList();
        violation.violatedConstraints = violatedConstraints;
        AbstractPropertyValue value = propertyValues.get(propertyEntry.getKey());
        String propertyValue = null;
        if (value == null) {
            propertyValue = null;
        } else if (value instanceof ScalarPropertyValue) {
            propertyValue = ((ScalarPropertyValue) value).getValue();
        } else {
            if (skipInputs) {
                continue;
            }
            if (FunctionEvaluator.isGetInput((FunctionPropertyValue) value)) {
                violation.relatedInput = ((FunctionPropertyValue) value).getElementNameToFetch();
            }
        }
        for (PropertyConstraint constraint : propertyEntry.getValue()) {
            if (!propertyDefinitionMap.containsKey(propertyEntry.getKey())) {
                continue;
            }
            // the constraint need to be initiazed with the type of the property (to check that actual value type matches the definition type).
            IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinitionMap.get(propertyEntry.getKey()).getType());
            try {
                constraint.initialize(toscaType);
                constraint.validate(toscaType, propertyValue);
            } catch (ConstraintViolationException e) {
                violatedConstraints.add(new NodeFilterConstraintViolation(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR, e.getMessage(), e.getConstraintInformation()));
            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
                violatedConstraints.add(new NodeFilterConstraintViolation(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR, e.getMessage(), null));
            }
        }
        if (!violatedConstraints.isEmpty()) {
            violations.add(violation);
        }
    }
    return violations;
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) NodeFilterConstraintViolation(alien4cloud.topology.task.NodeFilterConstraintViolation) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) Violations(alien4cloud.topology.task.NodeFilterToSatisfy.Violations) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 17 with PropertyConstraint

use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.

the class SuggestionService method postProcessSuggestionFromArchive.

public void postProcessSuggestionFromArchive(ParsingResult<ArchiveRoot> parsingResult) {
    ArchiveRoot archiveRoot = parsingResult.getResult();
    ParsingContext context = parsingResult.getContext();
    if (archiveRoot.hasToscaTopologyTemplate()) {
        Topology topology = archiveRoot.getTopology();
        Map<String, NodeTemplate> nodeTemplateMap = topology.getNodeTemplates();
        if (MapUtils.isEmpty(nodeTemplateMap)) {
            return;
        }
        for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodeTemplateMap.entrySet()) {
            NodeTemplate nodeTemplate = nodeTemplateEntry.getValue();
            String nodeName = nodeTemplateEntry.getKey();
            if (MapUtils.isNotEmpty(nodeTemplate.getProperties())) {
                checkProperties(nodeName, nodeTemplate.getProperties(), NodeType.class, nodeTemplate.getType(), context);
            }
            Map<String, Capability> capabilityMap = nodeTemplate.getCapabilities();
            if (MapUtils.isNotEmpty(capabilityMap)) {
                for (Map.Entry<String, Capability> capabilityEntry : capabilityMap.entrySet()) {
                    String capabilityName = capabilityEntry.getKey();
                    Capability capability = capabilityEntry.getValue();
                    if (MapUtils.isNotEmpty(capability.getProperties())) {
                        checkProperties(nodeName + ".capabilities." + capabilityName, capability.getProperties(), CapabilityType.class, capability.getType(), context);
                    }
                }
            }
            Map<String, RelationshipTemplate> relationshipTemplateMap = nodeTemplate.getRelationships();
            if (MapUtils.isNotEmpty(relationshipTemplateMap)) {
                for (Map.Entry<String, RelationshipTemplate> relationshipEntry : relationshipTemplateMap.entrySet()) {
                    String relationshipName = relationshipEntry.getKey();
                    RelationshipTemplate relationship = relationshipEntry.getValue();
                    if (MapUtils.isNotEmpty(relationship.getProperties())) {
                        checkProperties(nodeName + ".relationships." + relationshipName, relationship.getProperties(), RelationshipType.class, relationship.getType(), context);
                    }
                }
            }
        }
    }
    if (archiveRoot.hasToscaTypes()) {
        Map<String, NodeType> allNodeTypes = archiveRoot.getNodeTypes();
        if (MapUtils.isNotEmpty(allNodeTypes)) {
            for (Map.Entry<String, NodeType> nodeTypeEntry : allNodeTypes.entrySet()) {
                NodeType nodeType = nodeTypeEntry.getValue();
                if (nodeType.getRequirements() != null && !nodeType.getRequirements().isEmpty()) {
                    for (RequirementDefinition requirementDefinition : nodeType.getRequirements()) {
                        NodeFilter nodeFilter = requirementDefinition.getNodeFilter();
                        if (nodeFilter != null) {
                            Map<String, FilterDefinition> capabilitiesFilters = nodeFilter.getCapabilities();
                            if (MapUtils.isNotEmpty(capabilitiesFilters)) {
                                for (Map.Entry<String, FilterDefinition> capabilityFilterEntry : capabilitiesFilters.entrySet()) {
                                    FilterDefinition filterDefinition = capabilityFilterEntry.getValue();
                                    for (Map.Entry<String, List<PropertyConstraint>> constraintEntry : filterDefinition.getProperties().entrySet()) {
                                        List<PropertyConstraint> constraints = constraintEntry.getValue();
                                        checkPropertyConstraints("node_filter.capabilities", CapabilityType.class, capabilityFilterEntry.getKey(), constraintEntry.getKey(), constraints, context);
                                    }
                                }
                            }
                        // FIXME check also the value properties filter of a node filter
                        }
                    }
                }
            }
        }
    }
}
Also used : RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) FilterDefinition(org.alien4cloud.tosca.model.definitions.FilterDefinition) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) List(java.util.List) ArrayList(java.util.ArrayList) ParsingContext(alien4cloud.tosca.parser.ParsingContext) Capability(org.alien4cloud.tosca.model.templates.Capability) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) Map(java.util.Map) NodeFilter(org.alien4cloud.tosca.model.definitions.NodeFilter)

Example 18 with PropertyConstraint

use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.

the class SuggestionService method checkPropertyConstraints.

private void checkPropertyConstraints(String prefix, Class<? extends AbstractInheritableToscaType> type, String elementId, String propertyName, List<PropertyConstraint> constraints, ParsingContext context) {
    if (constraints != null && !constraints.isEmpty()) {
        for (PropertyConstraint propertyConstraint : constraints) {
            if (propertyConstraint instanceof EqualConstraint) {
                EqualConstraint equalConstraint = (EqualConstraint) propertyConstraint;
                String valueToCheck = equalConstraint.getEqual();
                if (checkProperty(prefix, propertyName, valueToCheck, type, elementId, context) == null) {
                    createSuggestionEntry(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, CapabilityType.class, Sets.newHashSet(valueToCheck), elementId, propertyName);
                }
            } else if (propertyConstraint instanceof ValidValuesConstraint) {
                ValidValuesConstraint validValuesConstraint = (ValidValuesConstraint) propertyConstraint;
                if (validValuesConstraint.getValidValues() != null && !validValuesConstraint.getValidValues().isEmpty()) {
                    AbstractSuggestionEntry foundSuggestion = null;
                    for (String valueToCheck : validValuesConstraint.getValidValues()) {
                        foundSuggestion = checkProperty(prefix, propertyName, valueToCheck, type, elementId, context);
                        if (foundSuggestion == null) {
                            // No suggestion exists don't need to check any more for other values
                            break;
                        }
                    }
                    if (foundSuggestion == null) {
                        createSuggestionEntry(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, CapabilityType.class, Sets.newHashSet(validValuesConstraint.getValidValues()), elementId, propertyName);
                    }
                }
            }
        }
    }
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) AbstractSuggestionEntry(alien4cloud.model.common.AbstractSuggestionEntry) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)

Example 19 with PropertyConstraint

use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.

the class PropertyDefinitionConverter method convert.

public PropertyDefinition convert(FormPropertyDefinition definitionAnnotation) {
    if (definitionAnnotation == null) {
        return null;
    }
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType(definitionAnnotation.type());
    // FIXME ? can be other than a scalar here ?
    propertyDefinition.setDefault(new ScalarPropertyValue(definitionAnnotation.defaultValue()));
    propertyDefinition.setDescription(definitionAnnotation.description());
    propertyDefinition.setPassword(definitionAnnotation.isPassword());
    propertyDefinition.setRequired(definitionAnnotation.isRequired());
    List<PropertyConstraint> constraints = Lists.newArrayList();
    if (!definitionAnnotation.constraints().equal().isEmpty()) {
        EqualConstraint equalConstraint = new EqualConstraint();
        equalConstraint.setEqual(definitionAnnotation.constraints().equal());
        constraints.add(equalConstraint);
    }
    if (!definitionAnnotation.constraints().greaterOrEqual().isEmpty()) {
        GreaterOrEqualConstraint greaterOrEqualConstraint = new GreaterOrEqualConstraint();
        greaterOrEqualConstraint.setGreaterOrEqual(definitionAnnotation.constraints().greaterOrEqual());
        constraints.add(greaterOrEqualConstraint);
    }
    if (!definitionAnnotation.constraints().greaterThan().isEmpty()) {
        GreaterThanConstraint greaterThanConstraint = new GreaterThanConstraint();
        greaterThanConstraint.setGreaterThan(definitionAnnotation.constraints().greaterThan());
        constraints.add(greaterThanConstraint);
    }
    if (!definitionAnnotation.constraints().inRange().isEmpty()) {
        String inRangeText = definitionAnnotation.constraints().inRange();
        Matcher matcher = IN_RANGE_REGEXP.matcher(inRangeText);
        if (matcher.matches()) {
            InRangeConstraint inRangeConstraint = new InRangeConstraint();
            inRangeConstraint.setRangeMinValue(matcher.group(1).trim());
            inRangeConstraint.setRangeMaxValue(matcher.group(2).trim());
            constraints.add(inRangeConstraint);
        } else {
            throw new FormDescriptorGenerationException("In range constraint definition must be in this format '[ $min - $max ]'");
        }
    }
    if (definitionAnnotation.constraints().length() >= 0) {
        LengthConstraint lengthConstraint = new LengthConstraint();
        lengthConstraint.setLength(definitionAnnotation.constraints().length());
        constraints.add(lengthConstraint);
    }
    if (!definitionAnnotation.constraints().lessOrEqual().isEmpty()) {
        LessOrEqualConstraint lessOrEqualConstraint = new LessOrEqualConstraint();
        lessOrEqualConstraint.setLessOrEqual(definitionAnnotation.constraints().lessOrEqual());
        constraints.add(lessOrEqualConstraint);
    }
    if (!definitionAnnotation.constraints().lessThan().isEmpty()) {
        LessThanConstraint lessThanConstraint = new LessThanConstraint();
        lessThanConstraint.setLessThan(definitionAnnotation.constraints().lessThan());
        constraints.add(lessThanConstraint);
    }
    if (definitionAnnotation.constraints().maxLength() >= 0) {
        MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint();
        maxLengthConstraint.setMaxLength(definitionAnnotation.constraints().maxLength());
        constraints.add(maxLengthConstraint);
    }
    if (definitionAnnotation.constraints().minLength() >= 0) {
        MinLengthConstraint minLengthConstraint = new MinLengthConstraint();
        minLengthConstraint.setMinLength(definitionAnnotation.constraints().minLength());
        constraints.add(minLengthConstraint);
    }
    if (!definitionAnnotation.constraints().pattern().isEmpty()) {
        PatternConstraint patternConstraint = new PatternConstraint();
        patternConstraint.setPattern(definitionAnnotation.constraints().pattern());
        constraints.add(patternConstraint);
    }
    if (definitionAnnotation.constraints().validValues().length > 0) {
        ValidValuesConstraint validValuesConstraint = new ValidValuesConstraint();
        validValuesConstraint.setValidValues(Lists.newArrayList(definitionAnnotation.constraints().validValues()));
        constraints.add(validValuesConstraint);
    }
    if (!constraints.isEmpty()) {
        propertyDefinition.setConstraints(constraints);
    }
    return propertyDefinition;
}
Also used : LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) FormDescriptorGenerationException(alien4cloud.ui.form.exception.FormDescriptorGenerationException) Matcher(java.util.regex.Matcher) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) FormPropertyDefinition(alien4cloud.ui.form.annotation.FormPropertyDefinition) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) InRangeConstraint(org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint) PatternConstraint(org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)

Example 20 with PropertyConstraint

use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.

the class ToscaPropertyDefaultValueConstraintsValidator method isValid.

@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
    PropertyValue defaultValue = value.getDefault();
    if (defaultValue == null) {
        // no default value is specified.
        return true;
    }
    // validate that the default value matches the defined constraints.
    IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
    if (toscaType == null) {
        return false;
    }
    if (!(defaultValue instanceof ScalarPropertyValue)) {
        // No constraint can be made on other thing than scalar values
        return false;
    }
    String defaultValueAsString = ((ScalarPropertyValue) defaultValue).getValue();
    Object parsedDefaultValue;
    try {
        parsedDefaultValue = toscaType.parse(defaultValueAsString);
    } catch (InvalidPropertyValueException e) {
        return false;
    }
    if (value.getConstraints() != null) {
        for (PropertyConstraint constraint : value.getConstraints()) {
            try {
                constraint.validate(parsedDefaultValue);
            } catch (ConstraintViolationException e) {
                return false;
            }
        }
    }
    return true;
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)

Aggregations

PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)30 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)18 Test (org.junit.Test)15 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)11 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)10 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)8 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)5 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)4 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)4 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)3 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)3 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)3 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)3 PatternConstraint (org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)3 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)2 IntrospectionException (java.beans.IntrospectionException)2