Search in sources :

Example 1 with AttributeDefinition

use of org.alien4cloud.tosca.model.definitions.AttributeDefinition in project yorc-a4c-plugin by ystia.

the class ToscaComponentUtils method formatOperationInputs.

public static String formatOperationInputs(int indentLevel, Map<String, ? extends IValue> properties) throws IOException {
    StringBuilder buffer = new StringBuilder();
    for (Map.Entry<String, ? extends IValue> propertyEntry : properties.entrySet()) {
        if (propertyEntry.getValue() != null) {
            if (propertyEntry.getValue() instanceof PropertyValue && ((PropertyValue) propertyEntry.getValue()).getValue() == null) {
                continue;
            }
            buffer.append("\n").append(ToscaPropertySerializerUtils.indent(indentLevel)).append(propertyEntry.getKey()).append(": ");
            if (!propertyEntry.getValue().isDefinition()) {
                buffer.append(ToscaPropertySerializerUtils.formatPropertyValue(indentLevel, (AbstractPropertyValue) propertyEntry.getValue()));
            } else {
                Map<String, Object> velocityContext = ToscaComponentExporter.getVelocityContext();
                velocityContext.put("indent", indentLevel + 1);
                String template;
                if (propertyEntry.getValue() instanceof PropertyDefinition) {
                    velocityContext.put("property", propertyEntry.getValue());
                    template = "org/ystia/yorc/alien4cloud/plugin/tosca/property_def.vm";
                } else if (propertyEntry.getValue() instanceof AttributeDefinition) {
                    velocityContext.put("attribute", propertyEntry.getValue());
                    template = "org/ystia/yorc/alien4cloud/plugin/tosca/attribute_def.vm";
                } else {
                    throw new RuntimeException("Unsupported type: " + propertyEntry.getValue().getClass());
                }
                StringWriter writer = new StringWriter();
                VelocityUtil.generate(template, writer, velocityContext);
                buffer.append("\n").append(ToscaPropertySerializerUtils.indent(indentLevel + 1)).append(writer.toString());
            }
        }
    }
    return buffer.toString();
}
Also used : StringWriter(java.io.StringWriter) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) AttributeDefinition(org.alien4cloud.tosca.model.definitions.AttributeDefinition) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 2 with AttributeDefinition

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

the class IndexedModelTest method mergeInheritableIndexMaps.

@Test
public void mergeInheritableIndexMaps() {
    NodeType son = new NodeType();
    son.setElementId("son");
    son.setArchiveVersion("1");
    PropertyDefinition propDef = new PropertyDefinition();
    AttributeDefinition attrDef = new AttributeDefinition();
    propDef.setType("string");
    propDef.setDefault(new ScalarPropertyValue("default_parent"));
    attrDef.setType("string");
    parent.setProperties(MapUtil.newHashMap(new String[] { "prop1" }, new PropertyDefinition[] { propDef }));
    parent.setAttributes(Maps.<String, IValue>newHashMap());
    parent.getAttributes().put("attr1", attrDef);
    propDef = new PropertyDefinition();
    propDef.setDefault(new ScalarPropertyValue("default_parent2"));
    propDef.setType("string");
    attrDef = new AttributeDefinition();
    attrDef.setType("version");
    parent.getProperties().put("prop2", propDef);
    parent.setAttributes(Maps.<String, IValue>newHashMap());
    parent.getAttributes().put("attr2", attrDef);
    propDef = new PropertyDefinition();
    propDef.setDefault(new ScalarPropertyValue("default_son"));
    propDef.setType("string");
    attrDef = new AttributeDefinition();
    attrDef.setType("integer");
    son.setProperties(MapUtil.newHashMap(new String[] { "prop1" }, new PropertyDefinition[] { propDef }));
    // son.setAttributes(MapUtil.newHashMap(new String[] { "attr1" }, new AttributeDefinition[] { attrDef }));
    son.setAttributes(Maps.<String, IValue>newHashMap());
    son.getAttributes().put("attr1", attrDef);
    propDef = new PropertyDefinition();
    propDef.setDefault(new ScalarPropertyValue("default_son2"));
    propDef.setType("integer");
    attrDef = new AttributeDefinition();
    attrDef.setType("boolean");
    son.getProperties().put("prop3", propDef);
    son.getAttributes().put("attr3", attrDef);
    IndexedModelUtils.mergeInheritableIndex(parent, son);
    // son should have 3 : 1 from himself, 1 from his parent, and one that he overrides from the parent
    assertEquals(3, son.getProperties().size());
    assertEquals(3, son.getAttributes().size());
    // son should'nt have parent's one when both defined the same
    PropertyDefinition propDefSon = son.getProperties().get("prop1");
    assertNotNull(propDefSon);
    assertEquals(new ScalarPropertyValue("default_son"), propDefSon.getDefault());
    AttributeDefinition attrDefSon = (AttributeDefinition) son.getAttributes().get("attr1");
    assertEquals("integer", attrDefSon.getType());
    // son has all parent's
    for (String key : parent.getProperties().keySet()) {
        assertTrue(son.getProperties().containsKey(key));
    }
    for (String key : parent.getAttributes().keySet()) {
        assertTrue(son.getAttributes().containsKey(key));
    }
}
Also used : NodeType(org.alien4cloud.tosca.model.types.NodeType) AttributeDefinition(org.alien4cloud.tosca.model.definitions.AttributeDefinition) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 3 with AttributeDefinition

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

the class TopologySubstitutionService method addAttributeFromPropertyDefinition.

private void addAttributeFromPropertyDefinition(PropertyDefinition pd, String propertyName, NodeType substituteNodeType) {
    // FIXME we have an issue here : if several nodes have the same attribute name, or if an attribute and a property have the same name,
    Map<String, IValue> attributes = substituteNodeType.getAttributes();
    if (pd != null && !attributes.containsKey(propertyName)) {
        if (ToscaTypes.isSimple(pd.getType())) {
            AttributeDefinition attributeDefinition = new AttributeDefinition();
            attributeDefinition.setType(pd.getType());
            attributeDefinition.setDescription(pd.getDescription());
            // FIXME known issue we don't support complex attributes right now.
            if (pd.getDefault() != null && pd.getDefault() instanceof ScalarPropertyValue) {
                attributeDefinition.setDefault(((ScalarPropertyValue) pd.getDefault()).getValue());
            }
            attributes.put(propertyName, attributeDefinition);
        }
    // FIXME else: known issue we don't support complex attributes right now.
    }
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) AttributeDefinition(org.alien4cloud.tosca.model.definitions.AttributeDefinition) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)

Example 4 with AttributeDefinition

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

Aggregations

AttributeDefinition (org.alien4cloud.tosca.model.definitions.AttributeDefinition)4 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)3 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)3 IValue (org.alien4cloud.tosca.model.definitions.IValue)2 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)1 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)1 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)1 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)1 NodeType (org.alien4cloud.tosca.model.types.NodeType)1 Test (org.junit.Test)1