Search in sources :

Example 26 with AbstractPropertyValue

use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.

the class TopologyPropertiesValidationService method addRequiredPropertyIdToTaskProperties.

private void addRequiredPropertyIdToTaskProperties(String prefix, Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> relatedProperties, PropertiesTask task, boolean skipInputProperties) {
    for (Map.Entry<String, AbstractPropertyValue> propertyEntry : properties.entrySet()) {
        PropertyDefinition propertyDef = relatedProperties.get(propertyEntry.getKey());
        String propertyErrorKey = prefix == null ? propertyEntry.getKey() : prefix + "." + propertyEntry.getKey();
        AbstractPropertyValue value = propertyEntry.getValue();
        if (propertyDef != null && propertyDef.isRequired()) {
            if (value == null) {
                addRequiredPropertyError(task, propertyErrorKey);
            } else if (value instanceof ScalarPropertyValue) {
                String propertyValue = ((ScalarPropertyValue) value).getValue();
                if (StringUtils.isBlank(propertyValue)) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (value instanceof ComplexPropertyValue) {
                Map<String, Object> mapValue = ((ComplexPropertyValue) value).getValue();
                if (MapUtils.isEmpty(mapValue)) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (value instanceof ListPropertyValue) {
                List<Object> listValue = ((ListPropertyValue) value).getValue();
                if (listValue.isEmpty()) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (FunctionEvaluator.containGetSecretFunction(value)) {
                // this is a get_secret function, we should not validate the get_secret here
                continue;
            } else if (skipInputProperties) {
                // get_input Will be validated later on
                continue;
            } else {
                addRequiredPropertyError(task, propertyErrorKey);
            }
        }
    }
}
Also used : ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 27 with AbstractPropertyValue

use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.

the class SetMatchedNodePropertyModifier method setNodeCapabilityProperty.

private void setNodeCapabilityProperty(FlowExecutionContext context, LocationResourceTemplate locationResourceTemplate, NodeTemplate nodeTemplate, String capabilityName, DeploymentMatchingConfiguration matchingConfiguration) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    Capability locationResourceCapability = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName);
    if (locationResourceCapability == null) {
        throw new NotFoundException("The capability <" + capabilityName + "> cannot be found on node template <" + templateId + "> of type <" + locationResourceTemplate.getTemplate().getType() + ">");
    }
    PropertyDefinition propertyDefinition = ToscaContext.getOrFail(CapabilityType.class, locationResourceCapability.getType()).getProperties().get(propertyName);
    if (propertyDefinition == null) {
        throw new NotFoundException("No property with name <" + propertyName + "> can be found on capability <" + capabilityName + "> of type <" + locationResourceCapability.getType() + ">");
    }
    AbstractPropertyValue locationResourcePropertyValue = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName).getProperties().get(propertyName);
    ensureNotSet(locationResourcePropertyValue, "by the admin in the Location Resource Template", propertyName, propertyValue);
    AbstractPropertyValue originalNodePropertyValue = safe(nodeTemplate.getCapabilities().get(capabilityName).getProperties()).get(propertyName);
    ensureNotSet(originalNodePropertyValue, "in the portable topology", propertyName, propertyValue);
    // Update the configuration
    NodePropsOverride nodePropsOverride = getTemplatePropsOverride(matchingConfiguration);
    if (propertyValue == null && nodePropsOverride.getCapabilities().get(capabilityName) != null) {
        nodePropsOverride.getCapabilities().get(capabilityName).getProperties().remove(propertyName);
    } else {
        // Set check constraints
        ConstraintPropertyService.checkPropertyConstraint(propertyName, propertyValue, propertyDefinition);
        NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = nodePropsOverride.getCapabilities().computeIfAbsent(capabilityName, k -> new NodeCapabilitiesPropsOverride());
        nodeCapabilitiesPropsOverride.getProperties().put(propertyName, PropertyService.asPropertyValue(propertyValue));
    }
    context.saveConfiguration(matchingConfiguration);
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) NotFoundException(alien4cloud.exception.NotFoundException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride)

Example 28 with AbstractPropertyValue

use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.

the class InputsModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Input modifier requires an environment context."));
    ApplicationEnvironment environment = environmentContext.getEnvironment();
    DeploymentInputs deploymentInputs = context.getConfiguration(DeploymentInputs.class, InputsModifier.class.getSimpleName()).orElse(new DeploymentInputs(environment.getTopologyVersion(), environment.getId()));
    if (deploymentInputs.getInputs() == null) {
        deploymentInputs.setInputs(Maps.newHashMap());
    }
    Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
    Map<String, PropertyValue> applicationInputs = inputService.getAppContextualInputs(context.getEnvironmentContext().get().getApplication(), topology.getInputs());
    Map<String, PropertyValue> locationInputs = inputService.getLocationContextualInputs(locations, topology.getInputs());
    // If the initial topology or any modifier configuration has changed since last inputs update then we refresh inputs.
    if (deploymentInputs.getLastUpdateDate() == null || deploymentInputs.getLastUpdateDate().before(context.getLastFlowParamUpdate())) {
        // FIXME exclude the application and location provided inputs from this method as it process them...
        boolean updated = deploymentInputService.synchronizeInputs(topology.getInputs(), deploymentInputs.getInputs());
        if (updated) {
            // save the config if changed. This is for ex, if an input has been deleted from the topology
            context.saveConfiguration(deploymentInputs);
        }
    }
    PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputsModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
    Map<String, AbstractPropertyValue> inputValues = Maps.newHashMap(deploymentInputs.getInputs());
    inputValues.putAll(applicationInputs);
    inputValues.putAll(locationInputs);
    inputValues.putAll(preconfiguredInputsConfiguration.getInputs());
    // Now that we have inputs ready let's process get_input functions in the topology to actually replace values.
    if (topology.getNodeTemplates() != null) {
        FunctionEvaluatorContext evaluatorContext = new FunctionEvaluatorContext(topology, inputValues);
        for (Entry<String, NodeTemplate> entry : topology.getNodeTemplates().entrySet()) {
            NodeTemplate nodeTemplate = entry.getValue();
            processGetInput(evaluatorContext, nodeTemplate, nodeTemplate.getProperties());
            if (nodeTemplate.getRelationships() != null) {
                for (Entry<String, RelationshipTemplate> relEntry : nodeTemplate.getRelationships().entrySet()) {
                    RelationshipTemplate relationshipTemplate = relEntry.getValue();
                    processGetInput(evaluatorContext, relationshipTemplate, relationshipTemplate.getProperties());
                }
            }
            if (nodeTemplate.getCapabilities() != null) {
                for (Entry<String, Capability> capaEntry : nodeTemplate.getCapabilities().entrySet()) {
                    Capability capability = capaEntry.getValue();
                    processGetInput(evaluatorContext, nodeTemplate, capability.getProperties());
                }
            }
            if (nodeTemplate.getRequirements() != null) {
                for (Entry<String, Requirement> requirementEntry : nodeTemplate.getRequirements().entrySet()) {
                    Requirement requirement = requirementEntry.getValue();
                    processGetInput(evaluatorContext, nodeTemplate, requirement.getProperties());
                }
            }
        }
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) FunctionEvaluatorContext(org.alien4cloud.tosca.utils.FunctionEvaluatorContext) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) Requirement(org.alien4cloud.tosca.model.templates.Requirement) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Location(alien4cloud.model.orchestrators.locations.Location)

Example 29 with AbstractPropertyValue

use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.

the class TopologyModifierSupport method setNodePropertyPathValue.

private void setNodePropertyPathValue(Csar csar, Topology topology, NodeTemplate nodeTemplate, String propertyPath, AbstractPropertyValue propertyValue, boolean lastPropertyIsAList) {
    Map<String, AbstractPropertyValue> propertyValues = nodeTemplate.getProperties();
    String nodePropertyName = feedPropertyValue(propertyValues, propertyPath, propertyValue, lastPropertyIsAList);
    Object nodePropertyValue = propertyValues.get(nodePropertyName);
    UpdateNodePropertyValueOperation updateNodePropertyValueOperation = new UpdateNodePropertyValueOperation();
    updateNodePropertyValueOperation.setNodeName(nodeTemplate.getName());
    updateNodePropertyValueOperation.setPropertyName(nodePropertyName);
    // TODO: can be necessary to serialize value before setting it in case of different types
    updateNodePropertyValueOperation.setPropertyValue(nodePropertyValue);
    updateNodePropertyValueProcessor.process(csar, topology, updateNodePropertyValueOperation);
}
Also used : UpdateNodePropertyValueOperation(org.alien4cloud.tosca.editor.operations.nodetemplate.UpdateNodePropertyValueOperation) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 30 with AbstractPropertyValue

use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.

the class DeploymentInputService method synchronizeInputs.

/**
 * Ensure that the specified input values matches the eventually updated input definitions.
 *
 * @param inputDefinitions Inputs definitions as specified in the topology.
 * @param inputValues Input properties values as specified by the user.
 * @return true if there is an update on inputValues (removal or addition). false if nothing has changed
 */
public boolean synchronizeInputs(Map<String, PropertyDefinition> inputDefinitions, Map<String, AbstractPropertyValue> inputValues) {
    boolean updated = false;
    if (!MapUtils.isEmpty(inputValues)) {
        // Ensure that previous defined values are still compatible with the latest input definition (as the topology may have changed).
        Iterator<Map.Entry<String, AbstractPropertyValue>> inputPropertyEntryIterator = inputValues.entrySet().iterator();
        while (inputPropertyEntryIterator.hasNext()) {
            Map.Entry<String, AbstractPropertyValue> inputPropertyEntry = inputPropertyEntryIterator.next();
            // remove if the value is null, or the input is not register as one
            if (inputPropertyEntry.getValue() == null || !safe(inputDefinitions).containsKey(inputPropertyEntry.getKey())) {
                inputPropertyEntryIterator.remove();
            } else if (!(inputPropertyEntry.getValue() instanceof FunctionPropertyValue)) {
                try {
                    ConstraintPropertyService.checkPropertyConstraint(inputPropertyEntry.getKey(), ((PropertyValue) inputPropertyEntry.getValue()).getValue(), inputDefinitions.get(inputPropertyEntry.getKey()));
                } catch (ConstraintViolationException | ConstraintValueDoNotMatchPropertyTypeException e) {
                    // Property is not valid anymore for the input, remove the old value
                    inputPropertyEntryIterator.remove();
                    updated = true;
                }
            }
        }
    }
    // set default values for every unset property.
    for (Map.Entry<String, PropertyDefinition> inputDefinitionEntry : safe(inputDefinitions).entrySet()) {
        AbstractPropertyValue existingValue = inputValues.get(inputDefinitionEntry.getKey());
        if (existingValue == null) {
            // If user has not specified a value and there is
            PropertyValue defaultValue = inputDefinitionEntry.getValue().getDefault();
            if (defaultValue != null) {
                inputValues.put(inputDefinitionEntry.getKey(), defaultValue);
                updated = true;
            }
        }
    }
    return updated;
}
Also used : FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Aggregations

AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)57 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)23 Map (java.util.Map)18 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)17 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)17 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)14 Capability (org.alien4cloud.tosca.model.templates.Capability)14 NotFoundException (alien4cloud.exception.NotFoundException)10 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)9 Test (org.junit.Test)8 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)7 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)7 NodeType (org.alien4cloud.tosca.model.types.NodeType)7 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)6 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)5 List (java.util.List)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)4 HashMap (java.util.HashMap)3