Search in sources :

Example 1 with IMatchPropertyConstraint

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

use of org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint in project yorc-a4c-plugin by ystia.

the class AbstractLocationConfigurer method printProperties.

private void printProperties(Map<String, List<IMatchPropertyConstraint>> props) {
    for (String kprop : props.keySet()) {
        List<IMatchPropertyConstraint> lc = props.get(kprop);
        for (IMatchPropertyConstraint mpc : lc) {
            if (mpc instanceof LessOrEqualConstraint) {
                LessOrEqualConstraint cons = (LessOrEqualConstraint) mpc;
                log.debug("  " + kprop + " <= " + cons.getLessOrEqual());
            } else if (mpc instanceof EqualConstraint) {
                EqualConstraint cons = (EqualConstraint) mpc;
                log.debug("  " + kprop + " == " + cons.getEqual());
            } else if (mpc instanceof GreaterOrEqualConstraint) {
                GreaterOrEqualConstraint cons = (GreaterOrEqualConstraint) mpc;
                log.debug("  " + kprop + " >= " + cons.getGreaterOrEqual());
            } else if (mpc instanceof GreaterThanConstraint) {
                GreaterThanConstraint cons = (GreaterThanConstraint) mpc;
                log.debug("  " + kprop + " > " + cons.getGreaterThan());
            } else if (mpc instanceof LessThanConstraint) {
                LessThanConstraint cons = (LessThanConstraint) mpc;
                log.debug("  " + kprop + " < " + cons.getLessThan());
            } else {
                log.debug("  " + kprop + " " + mpc.toString());
            }
        }
    }
}
Also used : LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) IMatchPropertyConstraint(org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)

Aggregations

EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)2 IMatchPropertyConstraint (org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint)2 Map (java.util.Map)1 ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)1 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)1 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)1 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)1 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)1 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)1 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)1 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)1