Search in sources :

Example 1 with ConstraintInformation

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();
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) Location(alien4cloud.model.orchestrators.locations.Location) Audit(alien4cloud.audit.annotation.Audit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ConstraintInformation

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);
            }
        }
    }
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) IntrospectionException(java.beans.IntrospectionException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)

Example 3 with ConstraintInformation

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);
        }
    }
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) IntrospectionException(java.beans.IntrospectionException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)

Example 4 with ConstraintInformation

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);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)

Example 5 with ConstraintInformation

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);
}
Also used : ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) Then(cucumber.api.java.en.Then)

Aggregations

ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)8 ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)3 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)3 IntrospectionException (java.beans.IntrospectionException)2 ConstraintTechnicalException (org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)2 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)2 Audit (alien4cloud.audit.annotation.Audit)1 Location (alien4cloud.model.orchestrators.locations.Location)1 Then (cucumber.api.java.en.Then)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Map (java.util.Map)1 ConstraintRequiredParameterException (org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException)1 InvalidPropertyValueException (org.alien4cloud.tosca.exceptions.InvalidPropertyValueException)1 IValue (org.alien4cloud.tosca.model.definitions.IValue)1 Operation (org.alien4cloud.tosca.model.definitions.Operation)1 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)1 PrimitiveDataType (org.alien4cloud.tosca.model.types.PrimitiveDataType)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1