Search in sources :

Example 1 with ConcatPropertyValue

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

the class FunctionEvaluator method parseAttribute.

/**
 * Parse an attribute value that can be : {@link ConcatPropertyValue} / {@link AttributeDefinition}
 *
 * @param attributeId
 * @param attributeValue
 * @param topology
 * @param runtimeInformations
 * @param currentInstance
 * @param basePaaSTemplate
 * @param builtPaaSTemplates
 * @return
 */
public static String parseAttribute(String attributeId, IValue attributeValue, Topology topology, Map<String, Map<String, InstanceInformation>> runtimeInformations, String currentInstance, IPaaSTemplate<? extends AbstractToscaType> basePaaSTemplate, Map<String, PaaSNodeTemplate> builtPaaSTemplates) {
    if (attributeValue == null) {
        return null;
    }
    // handle AttributeDefinition type
    if (attributeValue instanceof AttributeDefinition) {
        String runtimeAttributeValue = extractRuntimeInformationAttribute(runtimeInformations, currentInstance, Lists.newArrayList(basePaaSTemplate), attributeId);
        if (runtimeAttributeValue != null) {
            if (!runtimeAttributeValue.contains("=Error!]") && !runtimeAttributeValue.equals("")) {
                return runtimeAttributeValue;
            }
        }
        return ((AttributeDefinition) attributeValue).getDefault();
    }
    // handle concat function
    if (attributeValue instanceof ConcatPropertyValue) {
        StringBuilder evaluatedAttribute = new StringBuilder();
        ConcatPropertyValue concatPropertyValue = (ConcatPropertyValue) attributeValue;
        for (IValue concatParam : concatPropertyValue.getParameters()) {
            // scalar type
            if (concatParam instanceof ScalarPropertyValue) {
                // scalar case
                evaluatedAttribute.append(((ScalarPropertyValue) concatParam).getValue());
            } else if (concatParam instanceof PropertyDefinition) {
                // Definition case
                // TODO : ?? what should i do here ?? currently returns default value in the definition
                evaluatedAttribute.append(((PropertyDefinition) concatParam).getDefault());
            } else if (concatParam instanceof FunctionPropertyValue) {
                // Function case
                FunctionPropertyValue functionPropertyValue = (FunctionPropertyValue) concatParam;
                List<? extends IPaaSTemplate> paasTemplates = getPaaSTemplatesFromKeyword(basePaaSTemplate, functionPropertyValue.getTemplateName(), builtPaaSTemplates);
                switch(functionPropertyValue.getFunction()) {
                    case ToscaFunctionConstants.GET_ATTRIBUTE:
                        evaluatedAttribute.append(extractRuntimeInformationAttribute(runtimeInformations, currentInstance, paasTemplates, functionPropertyValue.getElementNameToFetch()));
                        break;
                    case ToscaFunctionConstants.GET_PROPERTY:
                        evaluatedAttribute.append(extractRuntimeInformationProperty(topology, functionPropertyValue.getElementNameToFetch(), paasTemplates));
                        break;
                    case ToscaFunctionConstants.GET_OPERATION_OUTPUT:
                        String defaultValue = "<" + functionPropertyValue.getElementNameToFetch() + ">";
                        evaluatedAttribute.append(extractRuntimeInformationOperationOutput(runtimeInformations, currentInstance, paasTemplates, functionPropertyValue, defaultValue));
                        break;
                    default:
                        log.warn("Function [{}] is not yet handled in concat operation.", functionPropertyValue.getFunction());
                        break;
                }
            }
        }
        return evaluatedAttribute.toString();
    }
    // handle functions. For now, only support Get_OPERATION_OUTPUT on attributes scope
    if (attributeValue instanceof FunctionPropertyValue) {
        FunctionPropertyValue function = (FunctionPropertyValue) attributeValue;
        switch(function.getFunction()) {
            case ToscaFunctionConstants.GET_OPERATION_OUTPUT:
                List<? extends IPaaSTemplate> paasTemplates = getPaaSTemplatesFromKeyword(basePaaSTemplate, function.getTemplateName(), builtPaaSTemplates);
                return extractRuntimeInformationOperationOutput(runtimeInformations, currentInstance, paasTemplates, function, null);
            default:
                return null;
        }
    }
    return null;
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) AttributeDefinition(org.alien4cloud.tosca.model.definitions.AttributeDefinition) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 2 with ConcatPropertyValue

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

the class TemplateBuilder method fillProperties.

public static void fillProperties(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertiesDefinitions, Map<String, AbstractPropertyValue> originalProperties, boolean adaptToType) {
    if (propertiesDefinitions == null || properties == null) {
        return;
    }
    for (Map.Entry<String, PropertyDefinition> entry : propertiesDefinitions.entrySet()) {
        AbstractPropertyValue originalValue = MapUtils.getObject(originalProperties, entry.getKey());
        if (originalValue == null) {
            AbstractPropertyValue pv = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(entry.getValue());
            properties.put(entry.getKey(), pv);
        } else if (originalValue instanceof FunctionPropertyValue || originalValue instanceof ConcatPropertyValue) {
            properties.put(entry.getKey(), originalValue);
        } else {
            // we check the property type before accepting it
            try {
                ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), originalValue, entry.getValue());
                properties.put(entry.getKey(), originalValue);
            } catch (ConstraintFunctionalException e) {
                log.debug("Not able to merge property <" + entry.getKey() + "> value due to a type check exception", e);
                if (adaptToType) {
                    AbstractPropertyValue pv = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(entry.getValue());
                    properties.put(entry.getKey(), pv);
                }
            }
        }
    }
    if (!adaptToType) {
        // maybe we could put validations here actually.
        for (Map.Entry<String, AbstractPropertyValue> originalProperty : safe(originalProperties).entrySet()) {
            if (!properties.containsKey(originalProperty.getKey())) {
                properties.put(originalProperty.getKey(), originalProperty.getValue());
            }
        }
    }
}
Also used : ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Map(java.util.Map) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 3 with ConcatPropertyValue

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

the class TopologyTreeBuilderService method processIValueForOperationOutput.

private <V extends AbstractInstantiableToscaType> void processIValueForOperationOutput(String name, IValue iValue, final IPaaSTemplate<V> paaSTemplate, final Map<String, PaaSNodeTemplate> paaSNodeTemplates, final boolean fromAttributes) {
    if (iValue instanceof FunctionPropertyValue) {
        FunctionPropertyValue function = (FunctionPropertyValue) iValue;
        if (ToscaFunctionConstants.GET_OPERATION_OUTPUT.equals(function.getFunction())) {
            String formatedAttributeName = null;
            List<? extends IPaaSTemplate> paaSTemplates = FunctionEvaluator.getPaaSTemplatesFromKeyword(paaSTemplate, function.getTemplateName(), paaSNodeTemplates);
            if (fromAttributes) {
                // nodeId:attributeName
                formatedAttributeName = AlienUtils.prefixWith(AlienUtils.COLON_SEPARATOR, name, paaSTemplate.getId());
            }
            registerOperationOutput(paaSTemplates, function.getInterfaceName(), function.getOperationName(), function.getElementNameToFetch(), formatedAttributeName);
        }
    } else if (iValue instanceof ConcatPropertyValue) {
        ConcatPropertyValue concatFunction = (ConcatPropertyValue) iValue;
        for (IValue param : concatFunction.getParameters()) {
            processIValueForOperationOutput(name, param, paaSTemplate, paaSNodeTemplates, false);
        }
    }
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 4 with ConcatPropertyValue

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

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

the class FunctionEvaluatorTest method concatOfAnGetSecretShouldFailed.

@Test
public void concatOfAnGetSecretShouldFailed() {
    FunctionEvaluatorContext context = getEvaluationContext();
    NodeTemplate template = context.getTopology().getNodeTemplates().get("my_node");
    AbstractPropertyValue resolved = FunctionEvaluator.tryResolveValue(context, template, template.getProperties(), template.getProperties().get("concat_prop_and_get_secret"));
    Assert.assertNotNull(resolved);
    Assert.assertEquals(ConcatPropertyValue.class, resolved.getClass());
    ConcatPropertyValue resolvedConcat = (ConcatPropertyValue) resolved;
    Assert.assertEquals(ScalarPropertyValue.class, resolvedConcat.getParameters().get(0).getClass());
    Assert.assertEquals(FunctionPropertyValue.class, resolvedConcat.getParameters().get(1).getClass());
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue) Test(org.junit.Test)

Aggregations

ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)6 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)5 Map (java.util.Map)2 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)2 IValue (org.alien4cloud.tosca.model.definitions.IValue)2 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)2 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)2 ConstraintFunctionalException (org.alien4cloud.tosca.exceptions.ConstraintFunctionalException)1 AttributeDefinition (org.alien4cloud.tosca.model.definitions.AttributeDefinition)1 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)1 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)1 Test (org.junit.Test)1