use of alien4cloud.topology.task.NodeFilterConstraintViolation 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;
}
Aggregations