Search in sources :

Example 1 with Violations

use of alien4cloud.topology.task.NodeFilterToSatisfy.Violations 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 2 with Violations

use of alien4cloud.topology.task.NodeFilterToSatisfy.Violations in project alien4cloud by alien4cloud.

the class NodeFilterValidationService method validateNodeFilterCapabilities.

private void validateNodeFilterCapabilities(NodeFilter nodeFilter, NodeTemplate target, NodeType targetType, Map<String, CapabilityType> capabilityTypes, NodeFilterToSatisfy nodeFilterToSatisfy, boolean skipInputs) {
    if (nodeFilter.getCapabilities() == null || nodeFilter.getCapabilities().isEmpty()) {
        return;
    }
    Map<String, FilterDefinition> capabilities = nodeFilter.getCapabilities();
    for (Map.Entry<String, FilterDefinition> filterDefinitionEntry : capabilities.entrySet()) {
        String capabilityName = filterDefinitionEntry.getKey();
        CapabilityDefinition definition = getCapabilityDefinition(targetType, capabilityName);
        if (definition == null) {
            nodeFilterToSatisfy.getMissingCapabilities().add(capabilityName);
            continue;
        }
        CapabilityType capabilityType = capabilityTypes.get(definition.getType());
        List<Violations> violations = validatePropertyFilters(filterDefinitionEntry.getValue().getProperties(), target.getCapabilities().get(definition.getId()).getProperties(), capabilityType.getProperties(), skipInputs);
        violations.forEach(violation -> violation.capabilityName = capabilityName);
        if (nodeFilterToSatisfy.getViolations() == null) {
            nodeFilterToSatisfy.setViolations(violations);
        } else {
            nodeFilterToSatisfy.getViolations().addAll(violations);
        }
    }
}
Also used : FilterDefinition(org.alien4cloud.tosca.model.definitions.FilterDefinition) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Violations(alien4cloud.topology.task.NodeFilterToSatisfy.Violations) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) Map(java.util.Map)

Aggregations

Violations (alien4cloud.topology.task.NodeFilterToSatisfy.Violations)2 Map (java.util.Map)2 NodeFilterConstraintViolation (alien4cloud.topology.task.NodeFilterConstraintViolation)1 List (java.util.List)1 ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)1 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)1 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)1 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)1 FilterDefinition (org.alien4cloud.tosca.model.definitions.FilterDefinition)1 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)1 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)1 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)1 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)1