Search in sources :

Example 1 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.

the class ToscaSerializerTest method getConstraintList.

private List<PropertyConstraint> getConstraintList() {
    List<PropertyConstraint> result = new ArrayList<PropertyConstraint>();
    ValidValuesConstraint c1 = new ValidValuesConstraint();
    c1.setValidValues(Lists.newArrayList("one", "two", "tree"));
    result.add(c1);
    GreaterOrEqualConstraint c2 = new GreaterOrEqualConstraint();
    c2.setGreaterOrEqual("2");
    result.add(c2);
    GreaterThanConstraint c3 = new GreaterThanConstraint();
    c3.setGreaterThan("3");
    result.add(c3);
    LessOrEqualConstraint c4 = new LessOrEqualConstraint();
    c4.setLessOrEqual("4");
    result.add(c4);
    LessThanConstraint c5 = new LessThanConstraint();
    c5.setLessThan("5");
    result.add(c5);
    LengthConstraint c6 = new LengthConstraint();
    c6.setLength(6);
    result.add(c6);
    MaxLengthConstraint c7 = new MaxLengthConstraint();
    c7.setMaxLength(7);
    result.add(c7);
    MinLengthConstraint c8 = new MinLengthConstraint();
    c8.setMinLength(8);
    result.add(c8);
    PatternConstraint c9 = new PatternConstraint();
    c9.setPattern("9+");
    result.add(c9);
    EqualConstraint c10 = new EqualConstraint();
    c10.setEqual("10");
    result.add(c10);
    InRangeConstraint c11 = new InRangeConstraint();
    c11.setInRange(Lists.newArrayList("11", "12"));
    result.add(c11);
    return result;
}
Also used : MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) ArrayList(java.util.ArrayList) 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) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) PatternConstraint(org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint) InRangeConstraint(org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint) 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 2 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.

the class AbstractSetMatchedPropertyModifier method ensureNotSet.

/**
 * Check that the property is not already defined in a source
 *
 * @param sourcePropertyValue null or an already defined Property Value.
 * @param messageSource The named source to add in the exception message in case of failure.
 */
// This cannot be thrown on getConstraintInformation from an equals constraint.
@SneakyThrows(IntrospectionException.class)
protected void ensureNotSet(AbstractPropertyValue sourcePropertyValue, String messageSource, String propertyName, Object propertyValue) throws ConstraintViolationException {
    if (sourcePropertyValue != null) {
        EqualConstraint constraint = new EqualConstraint();
        if (sourcePropertyValue instanceof ScalarPropertyValue) {
            constraint.setEqual(((ScalarPropertyValue) sourcePropertyValue).getValue());
        }
        ConstraintUtil.ConstraintInformation information = ConstraintUtil.getConstraintInformation(constraint);
        // If admin has defined a value users should not be able to override it.
        throw new ConstraintViolationException("Overriding value specified " + messageSource + " is not authorized.", null, information);
    }
}
Also used : ConstraintUtil(alien4cloud.tosca.properties.constraints.ConstraintUtil) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) SneakyThrows(lombok.SneakyThrows)

Example 3 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint 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 4 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint 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 5 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.

the class AbstractTemplateMatcher method isValidTemplatePropertiesMatch.

/**
 * Add filters ent/ICSARRepositorySearchService.java from the matching configuration to the node filter that will be applied for matching only if a value is
 * specified on the configuration template.
 *
 * @param templatePropertyValues The properties values from the template to match.
 * @param candidatePropertyValues The values defined on the Location Template.
 * @param propertyDefinitions The properties definitions associated with the node.
 * @param configuredFilters The filtering map (based on constraints) from matching configuration, other properties fall backs to an equal constraint/filter.
 */
protected boolean isValidTemplatePropertiesMatch(Map<String, AbstractPropertyValue> templatePropertyValues, Map<String, AbstractPropertyValue> candidatePropertyValues, Map<String, PropertyDefinition> propertyDefinitions, Map<String, List<IMatchPropertyConstraint>> configuredFilters) {
    // We perform matching on every property that is defined on the candidate (admin node) and that has a value defined in the topology.
    for (Map.Entry<String, AbstractPropertyValue> candidateValueEntry : safe(candidatePropertyValues).entrySet()) {
        List<IMatchPropertyConstraint> filter = safe(configuredFilters).get(candidateValueEntry.getKey());
        AbstractPropertyValue templatePropertyValue = templatePropertyValues.get(candidateValueEntry.getKey());
        // For now we support matching only on scalar properties.
        if (candidateValueEntry.getValue() != null && candidateValueEntry.getValue() instanceof ScalarPropertyValue && templatePropertyValue != null && templatePropertyValue instanceof ScalarPropertyValue) {
            try {
                IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinitions.get(candidateValueEntry.getKey()).getType());
                if (filter == null) {
                    // If no filter is defined then process matching using an equal constraint.
                    filter = Lists.newArrayList(new EqualConstraint());
                }
                // set the constraint value and add it to the node filter
                for (IMatchPropertyConstraint constraint : filter) {
                    constraint.setConstraintValue(toscaType, ((ScalarPropertyValue) candidateValueEntry.getValue()).getValue());
                    try {
                        constraint.validate(toscaType, ((ScalarPropertyValue) templatePropertyValue).getValue());
                    } catch (ConstraintViolationException e) {
                        return false;
                    }
                }
            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
                log.debug("The value of property for a constraint is not valid.", e);
            }
        }
    }
    return true;
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) IMatchPropertyConstraint(org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)

Aggregations

EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)10 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)5 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)5 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)5 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)4 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)4 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)4 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)3 InRangeConstraint (org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint)3 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)3 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)3 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)3 PatternConstraint (org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)2 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)2 IMatchPropertyConstraint (org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint)2 Capability (org.alien4cloud.tosca.model.templates.Capability)2 Test (org.junit.Test)2 AbstractSuggestionEntry (alien4cloud.model.common.AbstractSuggestionEntry)1