Search in sources :

Example 11 with ConstraintValueDoNotMatchPropertyTypeException

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

the class OrchestratorPropertiesService method setOrchestratorProperties.

public void setOrchestratorProperties(ApplicationEnvironment environment, Map<String, String> providerDeploymentProperties) {
    if (MapUtils.isNotEmpty(providerDeploymentProperties)) {
        OrchestratorDeploymentProperties properties = deploymentConfigurationDao.findById(OrchestratorDeploymentProperties.class, AbstractDeploymentConfig.generateId(environment.getTopologyVersion(), environment.getId()));
        try {
            orchestratorPropertiesValidationService.checkConstraints(properties.getOrchestratorId(), providerDeploymentProperties);
        } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException e) {
            throw new ConstraintTechnicalException("Error on deployer user orchestrator properties validation", e);
        }
        if (properties.getProviderDeploymentProperties() == null) {
            properties.setProviderDeploymentProperties(providerDeploymentProperties);
        } else {
            properties.getProviderDeploymentProperties().putAll(providerDeploymentProperties);
        }
        deploymentConfigurationDao.save(properties);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException) OrchestratorDeploymentProperties(org.alien4cloud.alm.deployment.configuration.model.OrchestratorDeploymentProperties)

Example 12 with ConstraintValueDoNotMatchPropertyTypeException

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

the class AbstractTemplateMatcher method isValidTemplatePropertiesMatch.

/**
 * Add filters ent/ICSARRepositorySearchService.java from the matching configuration to the node filter that will be applied for matching only if a value is
 * specified on the configuration template.
 *
 * @param templatePropertyValues The properties values from the template to match.
 * @param candidatePropertyValues The values defined on the Location Template.
 * @param propertyDefinitions The properties definitions associated with the node.
 * @param configuredFilters The filtering map (based on constraints) from matching configuration, other properties fall backs to an equal constraint/filter.
 */
protected boolean isValidTemplatePropertiesMatch(Map<String, AbstractPropertyValue> templatePropertyValues, Map<String, AbstractPropertyValue> candidatePropertyValues, Map<String, PropertyDefinition> propertyDefinitions, Map<String, List<IMatchPropertyConstraint>> configuredFilters) {
    // We perform matching on every property that is defined on the candidate (admin node) and that has a value defined in the topology.
    for (Map.Entry<String, AbstractPropertyValue> candidateValueEntry : safe(candidatePropertyValues).entrySet()) {
        List<IMatchPropertyConstraint> filter = safe(configuredFilters).get(candidateValueEntry.getKey());
        AbstractPropertyValue templatePropertyValue = templatePropertyValues.get(candidateValueEntry.getKey());
        // For now we support matching only on scalar properties.
        if (candidateValueEntry.getValue() != null && candidateValueEntry.getValue() instanceof ScalarPropertyValue && templatePropertyValue != null && templatePropertyValue instanceof ScalarPropertyValue) {
            try {
                IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinitions.get(candidateValueEntry.getKey()).getType());
                if (filter == null) {
                    // If no filter is defined then process matching using an equal constraint.
                    filter = Lists.newArrayList(new EqualConstraint());
                }
                // set the constraint value and add it to the node filter
                for (IMatchPropertyConstraint constraint : filter) {
                    constraint.setConstraintValue(toscaType, ((ScalarPropertyValue) candidateValueEntry.getValue()).getValue());
                    try {
                        constraint.validate(toscaType, ((ScalarPropertyValue) templatePropertyValue).getValue());
                    } catch (ConstraintViolationException e) {
                        return false;
                    }
                }
            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
                log.debug("The value of property for a constraint is not valid.", e);
            }
        }
    }
    return true;
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) IMatchPropertyConstraint(org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)

Example 13 with ConstraintValueDoNotMatchPropertyTypeException

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

the class PreconfiguredInputsModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Preconfigured input modifier requires an environment context."));
    ApplicationEnvironment environment = environmentContext.getEnvironment();
    Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
    AlienContextVariables alienContextVariables = new AlienContextVariables();
    alienContextVariables.setApplicationEnvironment(environment);
    alienContextVariables.setLocation(locations.values().stream().findFirst().get());
    alienContextVariables.setApplication(environmentContext.getApplication());
    // TODO: avoid reloading every time - find a way to know the last update on files (git hash ?)
    Properties appVarProps = quickFileStorageService.loadApplicationVariables(environmentContext.getApplication().getId());
    Properties envTypeVarProps = quickFileStorageService.loadEnvironmentTypeVariables(topology.getId(), environment.getEnvironmentType());
    Properties envVarProps = quickFileStorageService.loadEnvironmentVariables(topology.getId(), environment.getId());
    Map<String, Object> inputsMappingsMap = quickFileStorageService.loadInputsMappingFile(topology.getId());
    InputsMappingFileVariableResolver.InputsResolvingResult inputsResolvingResult = InputsMappingFileVariableResolver.configure(appVarProps, envTypeVarProps, envVarProps, alienContextVariables).resolve(inputsMappingsMap, topology.getInputs());
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getMissingVariables())) {
        context.log().error(new MissingVariablesTask(inputsResolvingResult.getMissingVariables()));
    }
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getUnresolved())) {
        context.log().error(new UnresolvablePredefinedInputsTask(inputsResolvingResult.getUnresolved()));
    }
    // checking constraints
    Map<String, ConstraintUtil.ConstraintInformation> violations = Maps.newHashMap();
    Map<String, ConstraintUtil.ConstraintInformation> typesViolations = Maps.newHashMap();
    for (Map.Entry<String, PropertyValue> entry : safe(inputsResolvingResult.getResolved()).entrySet()) {
        try {
            ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), entry.getValue(), topology.getInputs().get(entry.getKey()));
        } catch (ConstraintViolationException e) {
            violations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
            typesViolations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        }
    }
    if (MapUtils.isNotEmpty(violations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(violations, TaskCode.PREDEFINED_INPUTS_CONSTRAINT_VIOLATION));
    }
    if (MapUtils.isNotEmpty(typesViolations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(typesViolations, TaskCode.PREDEFINED_INPUTS_TYPE_VIOLATION));
    }
    PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = new PreconfiguredInputsConfiguration(environment.getTopologyVersion(), environment.getId());
    preconfiguredInputsConfiguration.setInputs(inputsResolvingResult.getResolved());
    // add unresolved so that they are not considered as deployer input
    inputsResolvingResult.getUnresolved().forEach(unresolved -> preconfiguredInputsConfiguration.getInputs().put(unresolved, null));
    // TODO: improve me
    preconfiguredInputsConfiguration.setLastUpdateDate(new Date());
    preconfiguredInputsConfiguration.setCreationDate(new Date());
    context.saveConfiguration(preconfiguredInputsConfiguration);
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PredefinedInputsConstraintViolationTask(alien4cloud.topology.task.PredefinedInputsConstraintViolationTask) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) Properties(java.util.Properties) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Date(java.util.Date) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) AlienContextVariables(org.alien4cloud.tosca.variable.AlienContextVariables) InputsMappingFileVariableResolver(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver) UnresolvablePredefinedInputsTask(alien4cloud.topology.task.UnresolvablePredefinedInputsTask) MissingVariablesTask(alien4cloud.topology.task.MissingVariablesTask) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Example 14 with ConstraintValueDoNotMatchPropertyTypeException

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

the class AbstractSetMatchedPropertyModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, NodeMatchingConfigAutoSelectModifier.class.getSimpleName());
    if (!configurationOptional.isPresent()) {
        // we should not end-up here as location matching should be processed first
        context.log().error(new LocationPolicyTask());
        return;
    }
    DeploymentMatchingConfiguration matchingConfiguration = configurationOptional.get();
    Map<String, String> lastUserSubstitutions = getUserMatches(matchingConfiguration);
    U template = getTemplates(topology).get(templateId);
    if (template == null) {
        throw new NotFoundException("Topology [" + topology.getId() + "] does not contains any " + getSubject() + " with id [" + templateId + "]");
    }
    String substitutionId = lastUserSubstitutions.get(templateId);
    if (substitutionId == null) {
        throw new NotFoundException("The " + getSubject() + " [" + templateId + "] from topology [" + topology.getId() + "] is not matched.");
    }
    Map<String, V> allAvailableResourceTemplates = getAvailableResourceTemplates(context);
    V resourceTemplate = allAvailableResourceTemplates.get(substitutionId);
    try {
        setProperty(context, resourceTemplate, template, matchingConfiguration);
    } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException e) {
        throw new ConstraintTechnicalException("Dispatching constraint violation.", e);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) NotFoundException(alien4cloud.exception.NotFoundException) LocationPolicyTask(alien4cloud.topology.task.LocationPolicyTask) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException) NodeMatchingConfigAutoSelectModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.NodeMatchingConfigAutoSelectModifier) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)

Example 15 with ConstraintValueDoNotMatchPropertyTypeException

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

the class ToscaPropertyConstraintValidator method isValid.

@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
    if (value.getConstraints() == null) {
        return true;
    }
    IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
    if (toscaType == null) {
        return false;
    }
    boolean isValid = true;
    for (int i = 0; i < value.getConstraints().size(); i++) {
        PropertyConstraint constraint = value.getConstraints().get(i);
        try {
            constraint.initialize(toscaType);
        } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
            log.info("Constraint definition error", e);
            context.buildConstraintViolationWithTemplate("CONSTRAINTS.VALIDATION.TYPE").addPropertyNode("constraints").addBeanNode().inIterable().atIndex(i).addConstraintViolation();
            isValid = false;
        }
    }
    return isValid;
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint)

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