Search in sources :

Example 1 with AbstractPropertyValue

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

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

the class FunctionEvaluator method doGetProperty.

private static AbstractPropertyValue doGetProperty(FunctionEvaluatorContext evaluatorContext, AbstractInstantiableTemplate targetTemplate, FunctionPropertyValue function) {
    if (targetTemplate == null) {
        return null;
    }
    // If a requirement or capability name is defined then it is applied to the node template.
    if (function.getCapabilityOrRequirementName() != null) {
        if (targetTemplate instanceof RelationshipTemplate) {
            throw new IllegalArgumentException("Get property that specifies a capability or relationship target must be placed on a node template and not a relationship template.");
        }
        AbstractPropertyValue propertyValue = null;
        Capability targetCapability = safe(((NodeTemplate) targetTemplate).getCapabilities()).get(function.getCapabilityOrRequirementName());
        if (targetCapability != null) {
            propertyValue = getFromPath(evaluatorContext, targetTemplate, targetCapability.getProperties(), function.getElementNameToFetch());
        }
        if (propertyValue == null) {
            Requirement requirement = safe(((NodeTemplate) targetTemplate).getRequirements()).get(function.getCapabilityOrRequirementName());
            if (requirement != null) {
                propertyValue = getFromPath(evaluatorContext, targetTemplate, requirement.getProperties(), function.getElementNameToFetch());
            }
        }
        if (propertyValue == null) {
            // try to find the value from the host node.
            propertyValue = doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function);
        }
        return tryResolveValue(evaluatorContext, targetTemplate, targetTemplate.getProperties(), propertyValue);
    }
    // Try to fetch from the node.
    AbstractPropertyValue propertyValue = getFromPath(evaluatorContext, targetTemplate, targetTemplate.getProperties(), function.getElementNameToFetch());
    if (propertyValue == null && targetTemplate instanceof NodeTemplate) {
        propertyValue = doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function);
    }
    // if the property refers to a function (get_input/get_property then try to resolve it).
    return tryResolveValue(evaluatorContext, targetTemplate, targetTemplate.getProperties(), propertyValue);
}
Also used : Requirement(org.alien4cloud.tosca.model.templates.Requirement) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 3 with AbstractPropertyValue

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

the class RebuildRelationshipProcessor method processRelationshipOperation.

@Override
protected void processRelationshipOperation(Csar csar, Topology topology, RebuildRelationshipOperation operation, NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate) {
    // rebuild a relationship template based on the current relationship type
    log.debug("Rebuilding the relationship [ {} ] in the node template [ {} ] of topology [ {} ] .", operation.getRelationshipName(), operation.getNodeName(), topology.getId());
    RelationshipType relType = ToscaContext.getOrFail(RelationshipType.class, relationshipTemplate.getType());
    Map<String, AbstractPropertyValue> properties = Maps.newHashMap();
    TemplateBuilder.fillProperties(properties, relType.getProperties(), relationshipTemplate.getProperties());
    relationshipTemplate.setProperties(properties);
    relationshipTemplate.setAttributes(relType.getAttributes());
}
Also used : RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 4 with AbstractPropertyValue

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

the class UnsetRelationshipPropertyAsInputProcessor method processRelationshipOperation.

@Override
protected void processRelationshipOperation(Csar csar, Topology topology, UnsetRelationshipPropertyAsInputOperation operation, NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate) {
    RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    PropertyDefinition relationshipPropertyDefinition = getOrFail(relationshipType.getProperties(), operation.getPropertyName(), "Property {} do not exist for relationship {} of node {}", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName());
    AbstractPropertyValue defaultPropertyValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(relationshipPropertyDefinition);
    relationshipTemplate.getProperties().put(operation.getPropertyName(), defaultPropertyValue);
    log.debug("Remove association from property [ {} ] of relationship template [ {} ] of node [ {} ] to an input of the topology [ {} ].", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName(), topology.getId());
}
Also used : RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 5 with AbstractPropertyValue

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

the class UnsetNodeCapabilityPropertyAsSecretProcessor method processNodeOperation.

@Override
protected void processNodeOperation(Csar csar, Topology topology, UnsetNodeCapabilityPropertyAsSecretOperation operation, NodeTemplate nodeTemplate) {
    Capability capabilityTemplate = getOrFail(nodeTemplate.getCapabilities(), operation.getCapabilityName(), "Capability {} do not exist for node {}", operation.getCapabilityName(), operation.getNodeName());
    // check if the node property value is a get_secret
    AbstractPropertyValue currentValue = capabilityTemplate.getProperties().get(operation.getPropertyName());
    if (currentValue != null && !isGetSecret(currentValue)) {
        throw new NotFoundException("Property {} of node {} is not an secret.", operation.getPropertyName(), operation.getNodeName());
    }
    CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capabilityTemplate.getType());
    PropertyDefinition capabilityPropertyDefinition = getOrFail(capabilityType.getProperties(), operation.getPropertyName(), "Property {} do not exist for capability {} of node {}", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName());
    AbstractPropertyValue defaultPropertyValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(capabilityPropertyDefinition);
    capabilityTemplate.getProperties().put(operation.getPropertyName(), defaultPropertyValue);
    log.debug("Remove secret property [ {} ] of capability template [ {} ] of node [ {} ] of the topology [ {} ].", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName(), topology.getId());
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Capability(org.alien4cloud.tosca.model.templates.Capability) NotFoundException(alien4cloud.exception.NotFoundException) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Aggregations

AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)57 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)23 Map (java.util.Map)18 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)17 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)17 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)14 Capability (org.alien4cloud.tosca.model.templates.Capability)14 NotFoundException (alien4cloud.exception.NotFoundException)10 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)9 Test (org.junit.Test)8 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)7 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)7 NodeType (org.alien4cloud.tosca.model.types.NodeType)7 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)6 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)5 List (java.util.List)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)4 HashMap (java.util.HashMap)3