Search in sources :

Example 6 with ConstraintValueDoNotMatchPropertyTypeException

use of org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException in project alien4cloud by alien4cloud.

the class PropertyDefinitionPostProcessor method validate.

private void validate(PropertyDefinition propertyDefinition, PropertyConstraint constraint) {
    IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinition.getType());
    if (toscaType == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(propertyDefinition.getType());
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.INVALID_CONSTRAINT, "Constraint parsing issue", node.getStartMark(), "Limitation - Constraint cannot be used for type " + propertyDefinition.getType(), node.getEndMark(), "constraint"));
    } else {
        try {
            constraint.initialize(toscaType);
        } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
            Node node = ParsingContextExecution.getObjectToNodeMap().get(constraint);
            ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.VALIDATION_ERROR, "ToscaPropertyConstraint", node.getStartMark(), e.getMessage(), node.getEndMark(), "constraint"));
        }
    }
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) Node(org.yaml.snakeyaml.nodes.Node)

Example 7 with ConstraintValueDoNotMatchPropertyTypeException

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

Example 8 with ConstraintValueDoNotMatchPropertyTypeException

use of org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException 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;
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) NodeFilterConstraintViolation(alien4cloud.topology.task.NodeFilterConstraintViolation) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) Violations(alien4cloud.topology.task.NodeFilterToSatisfy.Violations) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 9 with ConstraintValueDoNotMatchPropertyTypeException

use of org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException 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);
        }
    }
}
Also used : ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) TagUtil(alien4cloud.utils.TagUtil) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) TopologyServiceCore(alien4cloud.topology.TopologyServiceCore) MetaPropConfiguration(alien4cloud.model.common.MetaPropConfiguration) MetaPropertiesService(alien4cloud.common.MetaPropertiesService) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Service(org.springframework.stereotype.Service) Application(alien4cloud.model.application.Application) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Map(java.util.Map) ConstraintPropertyService(alien4cloud.utils.services.ConstraintPropertyService) OnDeploymentConfigCopyEvent(org.alien4cloud.alm.deployment.configuration.events.OnDeploymentConfigCopyEvent) MapUtils(org.apache.commons.collections4.MapUtils) Order(org.springframework.core.annotation.Order) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Csar(org.alien4cloud.tosca.model.Csar) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) EventListener(org.springframework.context.event.EventListener) PropertyService(alien4cloud.utils.services.PropertyService) AbstractDeploymentConfig(org.alien4cloud.alm.deployment.configuration.model.AbstractDeploymentConfig) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NotFoundException(alien4cloud.exception.NotFoundException) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) Topology(org.alien4cloud.tosca.model.templates.Topology) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Map(java.util.Map) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 10 with ConstraintValueDoNotMatchPropertyTypeException

use of org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException in project alien4cloud by alien4cloud.

the class InputService method setInputValues.

public void setInputValues(ApplicationEnvironment environment, Topology topology, Map<String, Object> inputProperties) {
    if (safe(inputProperties).isEmpty()) {
        return;
    }
    // Get the configuration object
    DeploymentInputs configuration = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(environment.getTopologyVersion(), environment.getId()));
    if (configuration == null) {
        configuration = new DeploymentInputs(environment.getTopologyVersion(), environment.getId());
    }
    for (Map.Entry<String, Object> inputPropertyValue : inputProperties.entrySet()) {
        if (topology.getInputs() == null || topology.getInputs().get(inputPropertyValue.getKey()) == null) {
            throw new NotFoundException("Input", inputPropertyValue.getKey(), "Input <" + inputPropertyValue.getKey() + "> cannot be found on topology for application <" + environment.getApplicationId() + "> environement <" + environment.getId() + ">");
        }
        try {
            propertyService.setPropertyValue(configuration.getInputs(), topology.getInputs().get(inputPropertyValue.getKey()), inputPropertyValue.getKey(), inputPropertyValue.getValue());
        } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException e) {
            throw new ConstraintTechnicalException("Dispatching constraint violation.", e);
        }
    }
    // Save configuration
    deploymentConfigurationDao.save(configuration);
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) NotFoundException(alien4cloud.exception.NotFoundException) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Map(java.util.Map) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)

Aggregations

ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)17 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)11 Map (java.util.Map)7 ConstraintTechnicalException (org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)4 Audit (alien4cloud.audit.annotation.Audit)3 NotFoundException (alien4cloud.exception.NotFoundException)3 Application (alien4cloud.model.application.Application)3 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)3 Location (alien4cloud.model.orchestrators.locations.Location)3 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)3 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)3 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)3 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 ConstraintUtil (alien4cloud.tosca.properties.constraints.ConstraintUtil)2 DeploymentInputs (org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs)2 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)2 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)2 Topology (org.alien4cloud.tosca.model.templates.Topology)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2