Search in sources :

Example 6 with ConstraintInformation

use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.

the class ConstraintPropertyService method rangeTypeError.

private static void rangeTypeError(final String propertyName, final List<Object> rangeValue) throws ConstraintValueDoNotMatchPropertyTypeException {
    log.debug("The property value for property {} is not of type {}: {}", propertyName, ToscaTypes.RANGE, rangeValue);
    ConstraintInformation consInformation = new ConstraintInformation(propertyName, null, rangeValue.toString(), ToscaTypes.RANGE);
    throw new ConstraintValueDoNotMatchPropertyTypeException("Range type must define numeric min and max values of the range.", null, consInformation);
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)

Example 7 with ConstraintInformation

use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.

the class ConstraintPropertyService method checkComplexPropertyDerivedFromPrimitiveTypeConstraints.

/**
 * Check constraints defined on a property which has a type derived from a primitive.
 */
private static void checkComplexPropertyDerivedFromPrimitiveTypeConstraints(final String propertyName, final String stringValue, final PropertyDefinition propertyDefinition, final DataType dataType) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    ConstraintInformation consInformation = null;
    boolean hasDefinitionConstraints = propertyDefinition.getConstraints() != null && !propertyDefinition.getConstraints().isEmpty();
    boolean hasTypeConstraints = false;
    if (dataType instanceof PrimitiveDataType && ((PrimitiveDataType) dataType).getConstraints() != null && !((PrimitiveDataType) dataType).getConstraints().isEmpty()) {
        hasTypeConstraints = true;
    }
    String derivedFromPrimitiveType = dataType.getDerivedFrom().get(0);
    // Check the type of the property even if there is no constraints.
    checkBasicType(propertyName, derivedFromPrimitiveType, stringValue);
    if (hasDefinitionConstraints || hasTypeConstraints) {
        // check the constraints if there is any defined
        if (hasDefinitionConstraints) {
            checkConstraints(propertyName, stringValue, derivedFromPrimitiveType, propertyDefinition.getConstraints());
        }
        if (hasTypeConstraints) {
            checkConstraints(propertyName, stringValue, derivedFromPrimitiveType, ((PrimitiveDataType) dataType).getConstraints());
        }
    }
}
Also used : PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)

Example 8 with ConstraintInformation

use of alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation in project alien4cloud by alien4cloud.

the class RuntimeController method validateParameters.

private void validateParameters(Interface interfass, OperationExecRequest operationRequest, Set<CSARDependency> dependencies) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException, ConstraintRequiredParameterException {
    try {
        if (dependencies != null) {
            ToscaContext.init(dependencies);
        }
        ArrayList<String> missingParams = Lists.newArrayList();
        Operation operation = interfass.getOperations().get(operationRequest.getOperationName());
        if (operation.getInputParameters() != null) {
            for (Entry<String, IValue> inputParameter : operation.getInputParameters().entrySet()) {
                if (inputParameter.getValue().isDefinition()) {
                    Object requestInputParameter = operationRequest.getParameters() == null ? null : operationRequest.getParameters().get(inputParameter.getKey());
                    PropertyDefinition currentOperationParameter = (PropertyDefinition) inputParameter.getValue();
                    if (requestInputParameter != null) {
                        if (!(requestInputParameter instanceof Map) || !FunctionEvaluator.containGetSecretFunction(PropertyService.asFunctionPropertyValue(requestInputParameter))) {
                            // recover the good property definition for the current parameter
                            ConstraintPropertyService.checkPropertyConstraint(inputParameter.getKey(), requestInputParameter, currentOperationParameter);
                        }
                    } else if (currentOperationParameter.isRequired()) {
                        // input param not in the request, id required this is a missing parameter...
                        missingParams.add(inputParameter.getKey());
                    } else {
                        // set the value to null
                        operation.getInputParameters().put(inputParameter.getKey(), null);
                    }
                }
            }
        }
        // check required input issue
        if (!missingParams.isEmpty()) {
            log.error("Missing required parameter", missingParams);
            ConstraintInformation constraintInformation = new ConstraintInformation(null, null, missingParams.toString(), "required");
            throw new ConstraintRequiredParameterException("Missing required parameters", null, constraintInformation);
        }
    } finally {
        if (ToscaContext.get() != null) {
            ToscaContext.destroy();
        }
    }
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) ApiOperation(io.swagger.annotations.ApiOperation) Operation(org.alien4cloud.tosca.model.definitions.Operation) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Map(java.util.Map) ConstraintRequiredParameterException(org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException)

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