Search in sources :

Example 26 with ScalarPropertyValue

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

the class PaaSUtils method injectInputIntoOperations.

private static void injectInputIntoOperations(String inputName, List<String> path, AbstractPropertyValue value, Map<String, Operation> operations) {
    if (MapUtils.isEmpty(operations)) {
        return;
    }
    if (value != null) {
        value = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, path);
    } else {
        value = new ScalarPropertyValue();
    }
    // the input name should be uppercase
    AbstractPropertyValue finalValue = value;
    operations.forEach((operationName, operation) -> {
        if (operation.getInputParameters() == null) {
            operation.setInputParameters(Maps.newHashMap());
        }
        // DO NOT OVERRIDE
        operation.getInputParameters().putIfAbsent(inputName, finalValue);
    });
}
Also used : FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 27 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue 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 28 with ScalarPropertyValue

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

the class FunctionEvaluatorTest method setPropertiesValues.

private void setPropertiesValues(Map<String, AbstractPropertyValue> properties, String prefix) {
    ScalarPropertyValue scalarPropValue = new ScalarPropertyValue(prefix + "scalar value");
    Map<String, Object> complex = Maps.newHashMap();
    complex.put("scalar", prefix + "complex scalar value");
    complex.put("map", Maps.newHashMap());
    ((Map) complex.get("map")).put("element_1", prefix + "element 1 value");
    ((Map) complex.get("map")).put("element_2", prefix + "element 2 value");
    complex.put("list", Lists.newArrayList(prefix + "list value 1", prefix + "list value 2"));
    ComplexPropertyValue complexPropValue = new ComplexPropertyValue(complex);
    FunctionPropertyValue getInputPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_INPUT, Lists.newArrayList("scalar_input"));
    FunctionPropertyValue getSecretPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_SECRET, Lists.newArrayList("my/path"));
    FunctionPropertyValue getScalarPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "scalar_prop"));
    FunctionPropertyValue getComplexPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "complex_prop.scalar"));
    FunctionPropertyValue getComplexPropListValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "complex_prop.list[1]"));
    FunctionPropertyValue getComplexPropMapValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "complex_prop.map.element_1"));
    ConcatPropertyValue concatPropValue = new ConcatPropertyValue();
    concatPropValue.setFunction_concat("concat");
    concatPropValue.setParameters(Lists.newArrayList(new ScalarPropertyValue("input is: "), getInputPropValue, new ScalarPropertyValue(" property is: "), getScalarPropValue));
    ConcatPropertyValue concatGetSecretPropValue = new ConcatPropertyValue();
    concatGetSecretPropValue.setFunction_concat("concat");
    concatGetSecretPropValue.setParameters(Lists.newArrayList(new ScalarPropertyValue("input is: "), getSecretPropValue));
    FunctionPropertyValue getConcatPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "concat_prop"));
    ConcatPropertyValue concatGetConcatPropValue = new ConcatPropertyValue();
    concatGetConcatPropValue.setFunction_concat("concat");
    concatGetConcatPropValue.setParameters(Lists.newArrayList(new ScalarPropertyValue("get concat is: "), getConcatPropValue));
    properties.put("scalar_prop", scalarPropValue);
    properties.put("complex_prop", complexPropValue);
    properties.put("get_input_prop", getInputPropValue);
    properties.put("get_secret_prop", getSecretPropValue);
    properties.put("get_scalar_prop", getScalarPropValue);
    properties.put("get_complex_prop", getComplexPropValue);
    properties.put("get_complex_prop_list", getComplexPropListValue);
    properties.put("get_complex_prop_map", getComplexPropMapValue);
    properties.put("concat_prop", concatPropValue);
    properties.put("concat_prop_and_get_secret", concatGetSecretPropValue);
    properties.put("get_concat_prop", getConcatPropValue);
    properties.put("concat_get_concat_prop", concatGetConcatPropValue);
}
Also used : FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 29 with ScalarPropertyValue

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

the class FunctionEvaluatorTest method getEvaluationContext.

/**
 * Generate the topology below (no need type validations) and inputs to be used for next tests.
 *
 * @return A function evaluator context for the test topology.
 */
private FunctionEvaluatorContext getEvaluationContext() {
    NodeTemplate myNode = new NodeTemplate();
    myNode.setProperties(Maps.newHashMap());
    setPropertiesValues(myNode.getProperties(), "");
    Capability capability = new Capability();
    myNode.setCapabilities(Maps.newHashMap());
    myNode.getCapabilities().put("my_capability", capability);
    capability.setProperties(Maps.newHashMap());
    setPropertiesValues(capability.getProperties(), "capa ");
    Topology topology = new Topology();
    topology.setNodeTemplates(Maps.newHashMap());
    topology.getNodeTemplates().put("my_node", myNode);
    Map<String, AbstractPropertyValue> inputs = Maps.newHashMap();
    inputs.put("scalar_input", new ScalarPropertyValue("scalar input value"));
    return new FunctionEvaluatorContext(topology, inputs);
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) Topology(org.alien4cloud.tosca.model.templates.Topology) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 30 with ScalarPropertyValue

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

ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)37 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)15 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)14 Test (org.junit.Test)13 Map (java.util.Map)10 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)8 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)7 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)7 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)7 HashMap (java.util.HashMap)5 List (java.util.List)5 Set (java.util.Set)5 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)4 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)4 Capability (org.alien4cloud.tosca.model.templates.Capability)4