use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.
the class LocationMetaPropertiesController method upsertMetaProperty.
/**
* Update or create a property for an orchestrator
*
* @param orchestratorId id of the orchestrator the location belongs to.
* @param locationId id of the location to update
* @param propertyRequest property request
* @return information on the constraint
*/
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public RestResponse<ConstraintInformation> upsertMetaProperty(@ApiParam(value = "Id of the orchestrator for which the location is defined.") @PathVariable String orchestratorId, @ApiParam(value = "Id of the location to get", required = true) @PathVariable String locationId, @ApiParam(value = "Id of the location to get", required = true) @RequestBody PropertyRequest propertyRequest) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
AuthorizationUtil.hasOneRoleIn(Role.ADMIN);
Location location = locationService.getOrFail(locationId);
try {
metaPropertiesService.upsertMetaProperty(location, propertyRequest.getDefinitionId(), propertyRequest.getValue());
} catch (ConstraintViolationException e) {
log.error("Constraint violation error for property <" + propertyRequest.getDefinitionId() + "> with value <" + propertyRequest.getValue() + ">", e);
return RestResponseBuilder.<ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR).message(e.getMessage()).build()).build();
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
log.error("Constraint value violation error for property <" + e.getConstraintInformation().getName() + "> with value <" + e.getConstraintInformation().getValue() + "> and type <" + e.getConstraintInformation().getType() + ">", e);
return RestResponseBuilder.<ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR).message(e.getMessage()).build()).build();
}
return RestResponseBuilder.<ConstraintInformation>builder().data(null).error(null).build();
}
use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkSimplePropertyConstraint.
/**
* Check constraints defined on a property for a specified value
*
* @param propertyName Property name (mainly used to create a comprehensive error message)
* @param stringValue Tested property value
* @param propertyDefinition Full property definition with type, constraints, default value,...
* @throws ConstraintViolationException
* @throws ConstraintValueDoNotMatchPropertyTypeException
*/
private static void checkSimplePropertyConstraint(final String propertyName, final String stringValue, final PropertyDefinition propertyDefinition) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
ConstraintInformation consInformation = null;
// check any property definition without constraints (type/value)
checkBasicType(propertyName, propertyDefinition.getType(), stringValue);
if (propertyDefinition.getConstraints() != null && !propertyDefinition.getConstraints().isEmpty()) {
IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinition.getType());
for (PropertyConstraint constraint : propertyDefinition.getConstraints()) {
try {
consInformation = ConstraintUtil.getConstraintInformation(constraint);
consInformation.setPath(propertyName + ".constraints[" + consInformation.getName() + "]");
constraint.initialize(toscaType);
constraint.validate(toscaType, stringValue);
} catch (ConstraintViolationException e) {
throw new ConstraintViolationException(e.getMessage(), e, consInformation);
} catch (IntrospectionException e) {
// ConstraintValueDoNotMatchPropertyTypeException is not supposed to be raised here (only in constraint definition validation)
log.info("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
throw new ConstraintTechnicalException("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
}
}
}
}
use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkConstraints.
private static void checkConstraints(final String propertyName, final String stringValue, final String typeName, List<PropertyConstraint> constraints) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
ConstraintInformation consInformation = null;
for (PropertyConstraint constraint : constraints) {
IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(typeName);
try {
consInformation = ConstraintUtil.getConstraintInformation(constraint);
consInformation.setPath(propertyName + ".constraints[" + consInformation.getName() + "]");
constraint.initialize(toscaType);
constraint.validate(toscaType, stringValue);
} catch (ConstraintViolationException e) {
throw new ConstraintViolationException(e.getMessage(), e, consInformation);
} catch (IntrospectionException e) {
// ConstraintValueDoNotMatchPropertyTypeException is not supposed to be raised here (only in constraint definition validation)
log.info("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
throw new ConstraintTechnicalException("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
}
}
}
use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkBasicType.
/**
* Check that a given value is matching the native type defined on the property definition.
*
* @param propertyName The name of the property under validation
* @param primitiveType The primitive type to check the value against.
* @param propertyValue The value to check.
* @throws ConstraintValueDoNotMatchPropertyTypeException in case the value does not match the primitive type.
*/
private static void checkBasicType(final String propertyName, final String primitiveType, final String propertyValue) throws ConstraintValueDoNotMatchPropertyTypeException {
// "string" (basic case, no exception), "float", "integer", "version"
try {
IPropertyType<?> propertyType = ToscaTypes.fromYamlTypeName(primitiveType);
propertyType.parse(propertyValue);
} catch (InvalidPropertyValueException e) {
log.debug("The property value for property {} is not of type {}: {}", propertyName, primitiveType, propertyValue, e);
ConstraintInformation consInformation = new ConstraintInformation(propertyName, null, propertyValue, primitiveType);
throw new ConstraintValueDoNotMatchPropertyTypeException(e.getMessage(), e, consInformation);
}
}
use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.
the class TopologyStepDefinitions method I_should_receive_a_RestResponse_with_constraint_data_name_and_reference.
@Then("^I should receive a RestResponse with constraint data name \"([^\"]*)\" and reference \"([^\"]*)\"$")
public void I_should_receive_a_RestResponse_with_constraint_data_name_and_reference(String name, String reference) throws Throwable {
RestResponse<?> restResponse = JsonUtil.read(Context.getInstance().getRestResponse(), Context.getJsonMapper());
Assert.assertNotNull(restResponse.getData());
ConstraintInformation constraint = JsonUtil.readObject(JsonUtil.toString(restResponse.getData()), ConstraintInformation.class);
assertEquals(constraint.getName().toString(), name);
assertEquals(constraint.getReference().toString(), reference);
}
Aggregations