Search in sources :

Example 21 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue 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 22 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue 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)

Example 23 with ScalarPropertyValue

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

the class ServiceStepDefinitions method iSetThePropertyToForTheService.

@And("^I set the property \"([^\"]*)\" to \"([^\"]*)\" for the service \"([^\"]*)\"$")
public void iSetThePropertyToForTheService(String propertyName, String propertyValue, String serviceName) throws Throwable {
    String serviceId = Context.getInstance().getServiceId(serviceName);
    PatchServiceResourceRequest request = new PatchServiceResourceRequest();
    NodeInstanceDTO nodeInstance = new NodeInstanceDTO();
    nodeInstance.setProperties(Maps.newHashMap());
    nodeInstance.getProperties().put(propertyName, new ScalarPropertyValue(propertyValue));
    request.setNodeInstance(nodeInstance);
    Context.getInstance().registerRestResponse(Context.getRestClientInstance().patchJSon("/rest/v1/services/" + serviceId, JsonUtil.toString(request)));
}
Also used : NodeInstanceDTO(alien4cloud.rest.service.model.NodeInstanceDTO) PatchServiceResourceRequest(alien4cloud.rest.service.model.PatchServiceResourceRequest) TestUtils.nullAsString(alien4cloud.it.utils.TestUtils.nullAsString) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) And(cucumber.api.java.en.And)

Example 24 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue 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)

Example 25 with ScalarPropertyValue

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

the class SuggestionService method checkProperties.

private void checkProperties(String nodePrefix, Map<String, AbstractPropertyValue> propertyValueMap, Class<? extends AbstractInheritableToscaType> type, String elementId, ParsingContext context) {
    if (MapUtils.isNotEmpty(propertyValueMap)) {
        for (Map.Entry<String, AbstractPropertyValue> propertyValueEntry : propertyValueMap.entrySet()) {
            String propertyName = propertyValueEntry.getKey();
            AbstractPropertyValue propertyValue = propertyValueEntry.getValue();
            if (propertyValue instanceof ScalarPropertyValue) {
                String propertyTextValue = ((ScalarPropertyValue) propertyValue).getValue();
                checkProperty(nodePrefix, propertyName, propertyTextValue, type, elementId, context);
            }
        }
    }
}
Also used : ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Aggregations

ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)37 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)15 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)14 Test (org.junit.Test)13 Map (java.util.Map)10 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)8 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)7 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)7 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)7 HashMap (java.util.HashMap)5 List (java.util.List)5 Set (java.util.Set)5 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)4 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)4 Capability (org.alien4cloud.tosca.model.templates.Capability)4