Search in sources :

Example 11 with PropertyValue

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

the class ToscaTypeConverterTest method convert_version_to_property_value.

@Test
public void convert_version_to_property_value() throws Exception {
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType(ToscaTypes.VERSION);
    PropertyValue propertyValue = converter.toPropertyValue("3.4-SNAPSHOT", propertyDefinition);
    Object version = ToscaTypes.fromYamlTypeName(propertyDefinition.getType()).parse(propertyValue.getValue().toString());
    assertThat(version).isInstanceOf(Version.class);
    assertThat(version).isEqualTo(new Version("3.4-SNAPSHOT"));
}
Also used : Version(alien4cloud.utils.version.Version) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 12 with PropertyValue

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

the class ToscaTypeConverterTest method convert_complex_data_type_to_property_value.

@Test
public void convert_complex_data_type_to_property_value() throws Exception {
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType("alien.nodes.test.ComplexDataType");
    PropertyValue propertyValue = converter.toPropertyValue(ImmutableMap.of("nested", "nested value", "nested_array", Arrays.asList("item1", "item2", "item3"), "nested_map", ImmutableMap.of("key1", "value1", "key2", "value2")), propertyDefinition);
    assertThat(propertyValue).isInstanceOf(ComplexPropertyValue.class);
    ComplexPropertyValue complexPropertyValue = (ComplexPropertyValue) propertyValue;
    assertThat(complexPropertyValue.getValue().get("nested_map")).isEqualTo(ImmutableMap.of("key1", "value1", "key2", "value2"));
    assertThat(complexPropertyValue.getValue().get("nested_array")).isEqualTo(Arrays.asList("item1", "item2", "item3"));
    assertThat(complexPropertyValue.getValue().get("nested")).isEqualTo("nested value");
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 13 with PropertyValue

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

the class FunctionEvaluator method getPropertyValue.

private static AbstractPropertyValue getPropertyValue(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertyDefinitions, String propertyAccessPath) {
    if (properties == null || !properties.containsKey(propertyAccessPath)) {
        String propertyName = PropertyUtil.getPropertyNameFromComplexPath(propertyAccessPath);
        if (propertyName == null) {
            // Non complex
            return PropertyUtil.getDefaultFromPropertyDefinitions(propertyAccessPath, propertyDefinitions);
        } else {
            // Complex
            PropertyDefinition propertyDefinition = propertyDefinitions.get(propertyName);
            AbstractPropertyValue rawValue;
            if (propertyDefinition == null) {
                return null;
            } else if (ToscaTypes.isSimple(propertyDefinition.getType())) {
                // It's a complex path (with '.') but the type in definition is finally simple
                return null;
            } else if (properties != null && (rawValue = properties.get(propertyName)) != null) {
                if (!(rawValue instanceof PropertyValue)) {
                    throw new NotSupportedException("Only support static value in a get_property");
                }
                Object value = MapUtil.get(((PropertyValue) rawValue).getValue(), propertyAccessPath.substring(propertyName.length() + 1));
                return new ScalarPropertyValue(PropertyUtil.serializePropertyValue(value));
            } else {
                return null;
            }
        }
    } else {
        return properties.get(propertyAccessPath);
    }
}
Also used : ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) NotSupportedException(alien4cloud.paas.exception.NotSupportedException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 14 with PropertyValue

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

the class PropertyValueChecker method checkProperty.

public void checkProperty(String propertyName, Node propertyValueNode, AbstractPropertyValue propertyValue, PropertyDefinition propertyDefinition, Map<String, PropertyDefinition> inputs, String templateName) {
    if (propertyValue instanceof FunctionPropertyValue) {
        FunctionPropertyValue function = (FunctionPropertyValue) propertyValue;
        String parameters = function.getParameters().get(0);
        // check get_input only
        if (function.getFunction().equals("get_input")) {
            if (inputs == null || !inputs.keySet().contains(parameters)) {
                ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.MISSING_TOPOLOGY_INPUT, templateName, propertyValueNode.getStartMark(), parameters, propertyValueNode.getEndMark(), propertyName));
            }
        }
    } else if (propertyValue instanceof PropertyValue<?>) {
        checkProperty(propertyName, propertyValueNode, (PropertyValue<?>) propertyValue, propertyDefinition, templateName);
    }
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) 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)

Example 15 with PropertyValue

use of org.alien4cloud.tosca.model.definitions.PropertyValue 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)

Aggregations

PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)28 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)20 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)17 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)16 Test (org.junit.Test)15 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)13 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)11 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)10 Map (java.util.Map)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)9 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)5 DataType (org.alien4cloud.tosca.model.types.DataType)5 Application (alien4cloud.model.application.Application)3 Location (alien4cloud.model.orchestrators.locations.Location)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Maps (com.google.common.collect.Maps)3 Arrays (java.util.Arrays)3 PreconfiguredInputsConfiguration (org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration)3 ToscaTypes (org.alien4cloud.tosca.normative.types.ToscaTypes)3 PropertiesYamlParser (org.alien4cloud.tosca.utils.PropertiesYamlParser)3