use of org.alien4cloud.tosca.exceptions.ConstraintViolationException in project alien4cloud by alien4cloud.
the class RestConstraintValidator method fromException.
public static RestResponse<ConstraintUtil.ConstraintInformation> fromException(ConstraintFunctionalException ex, String propertyName, Object propertyValue) {
if (ex instanceof ConstraintViolationException) {
ConstraintViolationException e = (ConstraintViolationException) ex;
log.debug("Constraint violation error for property <" + propertyName + "> with value <" + propertyValue + ">", e);
return RestResponseBuilder.<ConstraintUtil.ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR).message(e.getMessage()).build()).build();
} else if (ex instanceof ConstraintValueDoNotMatchPropertyTypeException) {
ConstraintValueDoNotMatchPropertyTypeException e = (ConstraintValueDoNotMatchPropertyTypeException) ex;
log.debug("Constraint value violation error for property <" + e.getConstraintInformation().getName() + "> with value <" + e.getConstraintInformation().getValue() + "> and type <" + e.getConstraintInformation().getType() + ">", e);
return RestResponseBuilder.<ConstraintUtil.ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR).message(e.getMessage()).build()).build();
} else {
throw new IllegalArgumentException("Unexpected ConstraintFunctionalException type", ex);
}
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException 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;
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException 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);
}
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException 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;
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException in project alien4cloud by alien4cloud.
the class InputService method onCopyConfiguration.
@EventListener
@Order(10)
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
DeploymentInputs deploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
if (deploymentInputs == null || MapUtils.isEmpty(deploymentInputs.getInputs())) {
// Nothing to copy
return;
}
Topology topology = topologyServiceCore.getOrFail(Csar.createId(target.getApplicationId(), target.getTopologyVersion()));
if (MapUtils.isNotEmpty(topology.getInputs())) {
Map<String, PropertyDefinition> inputsDefinitions = topology.getInputs();
Map<String, AbstractPropertyValue> inputsToCopy = deploymentInputs.getInputs().entrySet().stream().filter(inputEntry -> inputsDefinitions.containsKey(inputEntry.getKey())).filter(inputEntry -> {
// Copy only inputs which satisfy the new input definition
try {
if (!(inputEntry.getValue() instanceof FunctionPropertyValue)) {
ConstraintPropertyService.checkPropertyConstraint(inputEntry.getKey(), PropertyService.asPropertyValue(inputEntry.getValue()), inputsDefinitions.get(inputEntry.getKey()));
}
return true;
} catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException e) {
return false;
}
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (MapUtils.isNotEmpty(inputsToCopy)) {
DeploymentInputs targetDeploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(target.getTopologyVersion(), target.getId()));
if (targetDeploymentInputs == null) {
targetDeploymentInputs = new DeploymentInputs(target.getTopologyVersion(), target.getId());
}
// Copy inputs from original topology
targetDeploymentInputs.setInputs(inputsToCopy);
deploymentConfigurationDao.save(targetDeploymentInputs);
}
}
}
Aggregations