Search in sources :

Example 21 with PropertyValue

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

the class InputValidationModifier method process.

/**
 * Validate all required input is provided with a non null value.
 *
 * @param topology The topology to process.
 * @param context The object that stores warnings and errors (tasks) associated with the execution flow. Note that the flow will end-up if an error
 */
@Override
public void process(Topology topology, FlowExecutionContext context) {
    Optional<DeploymentInputs> inputsOptional = context.getConfiguration(DeploymentInputs.class, InputValidationModifier.class.getSimpleName());
    PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputValidationModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
    // Define a task regarding properties
    PropertiesTask task = new PropertiesTask();
    task.setCode(TaskCode.INPUT_PROPERTY);
    task.setProperties(Maps.newHashMap());
    task.getProperties().put(TaskLevel.REQUIRED, Lists.newArrayList());
    Map<String, AbstractPropertyValue> inputValues = safe(inputsOptional.orElse(new DeploymentInputs()).getInputs());
    Map<String, PropertyValue> predefinedInputValues = safe(preconfiguredInputsConfiguration.getInputs());
    // override deployer inputValues with predefinedInputValues
    inputValues = Maps.newHashMap(inputValues);
    inputValues.putAll(predefinedInputValues);
    for (Entry<String, PropertyDefinition> propDef : safe(topology.getInputs()).entrySet()) {
        if (propDef.getValue().isRequired() && inputValues.get(propDef.getKey()) == null) {
            task.getProperties().get(TaskLevel.REQUIRED).add(propDef.getKey());
        }
    }
    if (CollectionUtils.isNotEmpty(task.getProperties().get(TaskLevel.REQUIRED))) {
        context.log().error(task);
    }
    // Check input artifacts
    deploymentInputArtifactValidationService.validate(topology, inputsOptional.orElse(new DeploymentInputs())).forEach(inputArtifactTask -> context.log().error(inputArtifactTask));
}
Also used : PropertiesTask(alien4cloud.topology.task.PropertiesTask) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 22 with PropertyValue

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

the class FunctionEvaluator method getFromPath.

private static AbstractPropertyValue getFromPath(FunctionEvaluatorContext evaluatorContext, AbstractInstantiableTemplate targetTemplate, Map<String, AbstractPropertyValue> properties, String propertyPath) {
    if (propertyPath.contains(".")) {
        String propertyName = propertyPath.split("\\.")[0];
        AbstractPropertyValue propertyValue = properties.get(propertyName);
        if (!(propertyValue instanceof PropertyValue)) {
            // if the value is not a property value resolve it first
            propertyValue = tryResolveValue(evaluatorContext, targetTemplate, properties, propertyValue);
            if (propertyValue == null) {
                return null;
            }
        }
        // now it is a property value
        Object value = MapUtil.get(((PropertyValue) propertyValue).getValue(), propertyPath.substring(propertyName.length() + 1));
        if (value == null) {
            return null;
        } else if (value instanceof String) {
            return new ScalarPropertyValue((String) value);
        } else if (value instanceof List) {
            return new ListPropertyValue((List<Object>) value);
        } else if (value instanceof Map) {
            return new ComplexPropertyValue((Map<String, Object>) value);
        }
        throw new IllegalArgumentException("The value of a property must be a scalar, a list or a map.");
    }
    return safe(properties).get(propertyPath);
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) 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) 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) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Map(java.util.Map)

Example 23 with PropertyValue

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

the class FunctionEvaluatorTest method nodeGetComplexProp.

@Test
public void nodeGetComplexProp() {
    FunctionEvaluatorContext context = getEvaluationContext();
    NodeTemplate template = context.getTopology().getNodeTemplates().get("my_node");
    PropertyValue resolved = resolveValue(context, template, template.getProperties(), template.getProperties().get("get_complex_prop"));
    Assert.assertNotNull(resolved);
    Assert.assertEquals(ScalarPropertyValue.class, resolved.getClass());
    Assert.assertEquals("complex scalar value", resolved.getValue());
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) 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) Test(org.junit.Test)

Example 24 with PropertyValue

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

the class FunctionEvaluatorTest method nodeGetScalarProp.

@Test
public void nodeGetScalarProp() {
    FunctionEvaluatorContext context = getEvaluationContext();
    NodeTemplate template = context.getTopology().getNodeTemplates().get("my_node");
    PropertyValue resolved = resolveValue(context, template, template.getProperties(), template.getProperties().get("get_scalar_prop"));
    Assert.assertNotNull(resolved);
    Assert.assertEquals(ScalarPropertyValue.class, resolved.getClass());
    Assert.assertEquals("scalar value", resolved.getValue());
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) 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) Test(org.junit.Test)

Example 25 with PropertyValue

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

the class InputsMappingFileVariableResolverTest method check_inputs_mapping_can_be_parsed_when_variable.

@Ignore("Update when ToscaTypeConverter behavior is clearly defined")
@Test
public // Bad test
void check_inputs_mapping_can_be_parsed_when_variable() throws Exception {
    inputsMappingFileVariableResolverConfigured.customConverter(new ToscaTypeConverter((concreteType, id) -> {
        if (id.equals("datatype.complex_input_entry.sub1")) {
            DataType dataType = new DataType();
            dataType.setDeriveFromSimpleType(false);
            dataType.setProperties(// 
            ImmutableMap.of(// 
            "complex", // 
            buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING)));
            return dataType;
        }
        if (id.equals("datatype.complex_input_entry")) {
            DataType dataType = new DataType();
            dataType.setDeriveFromSimpleType(false);
            dataType.setProperties(// 
            ImmutableMap.of(// 
            "sub1", // 
            buildPropDef("datatype.complex_input_entry.sub1"), // 
            "sub2", // 
            buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING), // 
            "field01", // 
            buildPropDef(ToscaTypes.STRING)));
            return dataType;
        }
        return null;
    }));
    Map<String, PropertyValue> inputsMappingFileResolved = resolve("src/test/resources/alien/variables/inputs_mapping_with_variables.yml");
    assertThat(inputsMappingFileResolved.get("int_input")).isEqualTo(new ScalarPropertyValue("1"));
    assertThat(inputsMappingFileResolved.get("float_input")).isEqualTo(new ScalarPropertyValue("3.14"));
    assertThat(inputsMappingFileResolved.get("string_input")).isEqualTo(new ScalarPropertyValue("text_3.14"));
    // 
    assertThat(inputsMappingFileResolved.get("complex_input")).isEqualTo(new ComplexPropertyValue(// 
    ImmutableMap.of("sub1", new ComplexPropertyValue(// 
    ImmutableMap.of("complex", // 
    new ComplexPropertyValue(ImmutableMap.of("subfield", new ScalarPropertyValue("text"))))), "sub2", new ComplexPropertyValue(// 
    ImmutableMap.of("subfield21", // 
    new ScalarPropertyValue("1"))), "field01", // 
    new ScalarPropertyValue("text"))));
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Arrays(java.util.Arrays) InputsMappingFileVariableResolver.configure(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver.configure) DataType(org.alien4cloud.tosca.model.types.DataType) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ImmutableMap(com.google.common.collect.ImmutableMap) InputsMappingFileVariableResolverConfigured(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver.InputsMappingFileVariableResolverConfigured) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) FileSystemResource(org.springframework.core.io.FileSystemResource) Test(org.junit.Test) Maps(com.google.common.collect.Maps) PropertiesYamlParser(org.alien4cloud.tosca.utils.PropertiesYamlParser) Ignore(org.junit.Ignore) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) Map(java.util.Map) Application(alien4cloud.model.application.Application) PropertyDefinitionUtils.buildPropDef(org.alien4cloud.tosca.variable.PropertyDefinitionUtils.buildPropDef) ToscaTypes(org.alien4cloud.tosca.normative.types.ToscaTypes) Assert(org.junit.Assert) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) DataType(org.alien4cloud.tosca.model.types.DataType) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Ignore(org.junit.Ignore) Test(org.junit.Test)

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