Search in sources :

Example 11 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class RelationshipPostProcessor method getCapabilityByType.

private Capability getCapabilityByType(NodeTemplate targetNodeTemplate, RelationshipTemplate relationshipTemplate, String capabilityType) {
    Capability capability = null;
    Map<String, Capability> compatibleCapabilityByType = capabilityMatcherService.getCompatibleCapabilityByType(targetNodeTemplate, capabilityType);
    Entry<String, Capability> capabilityEntry = null;
    if (compatibleCapabilityByType.size() == 1) {
        capabilityEntry = compatibleCapabilityByType.entrySet().iterator().next();
    } else if (compatibleCapabilityByType.size() > 1) {
        capabilityEntry = compatibleCapabilityByType.entrySet().iterator().next();
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate);
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.REQUIREMENT_CAPABILITY_MULTIPLE_MATCH, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
    }
    if (capabilityEntry != null) {
        capability = capabilityEntry.getValue();
        relationshipTemplate.setTargetedCapabilityName(capabilityEntry.getKey());
    }
    return capability;
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) Capability(org.alien4cloud.tosca.model.templates.Capability) Node(org.yaml.snakeyaml.nodes.Node)

Example 12 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class TemplateBuilder method fillCapabilitiesMap.

private static void fillCapabilitiesMap(Map<String, Capability> map, List<CapabilityDefinition> elements, Map<String, Capability> mapToMerge, boolean adaptToType) {
    if (elements == null) {
        return;
    }
    for (CapabilityDefinition capa : elements) {
        Capability toAddCapa = MapUtils.getObject(mapToMerge, capa.getId());
        Map<String, AbstractPropertyValue> capaProperties = null;
        CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capa.getType());
        if (capabilityType != null && capabilityType.getProperties() != null) {
            // Inject all default values from the type.
            capaProperties = PropertyUtil.getDefaultPropertyValuesFromPropertyDefinitions(capabilityType.getProperties());
            // Override them with values as defined in the actual Capability Definition of the node type.
            if (capa.getProperties() != null) {
                capaProperties.putAll(capa.getProperties());
            }
        }
        // only merge if the types are equals
        if (toAddCapa == null || (StringUtils.isNotBlank(toAddCapa.getType()) && !Objects.equals(toAddCapa.getType(), capa.getType()))) {
            toAddCapa = new Capability();
            toAddCapa.setType(capa.getType());
            toAddCapa.setProperties(capaProperties);
        } else {
            if (StringUtils.isBlank(toAddCapa.getType())) {
                toAddCapa.setType(capa.getType());
            }
            if (MapUtils.isNotEmpty(capaProperties)) {
                Map<String, AbstractPropertyValue> nodeCapaProperties = safe(toAddCapa.getProperties());
                capaProperties.putAll(nodeCapaProperties);
                toAddCapa.setProperties(capaProperties);
            }
        }
        Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
        fillProperties(properties, capabilityType != null ? capabilityType.getProperties() : null, toAddCapa.getProperties(), adaptToType);
        toAddCapa.setProperties(properties);
        map.put(capa.getId(), toAddCapa);
    }
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Capability(org.alien4cloud.tosca.model.templates.Capability) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 13 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class DanglingRequirementService method addDanglingNodes.

private void addDanglingNodes(Topology topology, TopologyContext topologyContext, NodeTemplate nodeTemplate, RequirementDefinition requirementDefinition, int count) {
    // TODO If the TOSCA context does not has the TOSCA normative types then add it automatically
    String danglingTemplateType = requirementDefinition.getNodeType() == null ? NormativeTypesConstant.ROOT_NODE_TYPE : requirementDefinition.getNodeType();
    NodeType danglingNodeType = ToscaContext.get(NodeType.class, danglingTemplateType);
    List<CapabilityDefinition> compatibleCapabilityByType = capabilityMatcherService.getCompatibleCapabilityByType(danglingNodeType, requirementDefinition.getType());
    CapabilityDefinition targetCapabilityDefinition = compatibleCapabilityByType.size() == 0 ? null : compatibleCapabilityByType.get(0);
    RelationshipType danglingRelationshipType = fetchValidRelationshipType(requirementDefinition, targetCapabilityDefinition);
    // check if the type is scalable (then count is used as a scalability parameter) or if we should add multiple instances
    CapabilityDefinition scalable = NodeTypeUtils.getCapabilityByType(danglingNodeType, NormativeCapabilityTypes.SCALABLE);
    if (scalable == null) {
        scalable = NodeTypeUtils.getCapabilityByType(danglingNodeType, AlienCapabilityTypes.CLUSTER_CONTROLLER);
    }
    List<NodeTemplate> addedNodes = Lists.newArrayList();
    if (scalable == null) {
        for (int i = 0; i < count; i++) {
            NodeTemplate addedNode = addDanglingNode(topology, topologyContext, nodeTemplate, requirementDefinition, danglingNodeType, danglingRelationshipType, targetCapabilityDefinition);
            addedNodes.add(addedNode);
        }
    } else {
        NodeTemplate danglingTemplate = addDanglingNode(topology, topologyContext, nodeTemplate, requirementDefinition, danglingNodeType, danglingRelationshipType, targetCapabilityDefinition);
        Capability scalableCapability = danglingTemplate.getCapabilities().get(scalable.getId());
        TopologyUtils.setScalingProperty(NormativeComputeConstants.SCALABLE_DEFAULT_INSTANCES, count, scalableCapability);
        TopologyUtils.setScalingProperty(NormativeComputeConstants.SCALABLE_MAX_INSTANCES, requirementDefinition.getUpperBound(), scalableCapability);
        addedNodes.add(danglingTemplate);
    }
    // Recursively add dangling nodes.
    for (NodeTemplate addedNode : addedNodes) {
        addDanglingRequirements(topology, topologyContext, addedNode, null);
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)

Example 14 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class DeleteInputProcessor method processInputOperation.

@Override
protected void processInputOperation(Csar csar, Topology topology, DeleteInputOperation operation, Map<String, PropertyDefinition> inputs) {
    if (!inputs.containsKey(operation.getInputName())) {
        throw new NotFoundException("Input " + operation.getInputName() + "not found in topology");
    }
    for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
        NodeType nodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType());
        removeInputIdInProperties(nodeTemplate.getProperties(), nodeType.getProperties(), operation.getInputName());
        if (nodeTemplate.getRelationships() != null) {
            for (RelationshipTemplate relationshipTemplate : nodeTemplate.getRelationships().values()) {
                RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
                removeInputIdInProperties(relationshipTemplate.getProperties(), relationshipType.getProperties(), operation.getInputName());
            }
        }
        if (nodeTemplate.getCapabilities() != null) {
            for (Capability capability : nodeTemplate.getCapabilities().values()) {
                CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType());
                removeInputIdInProperties(capability.getProperties(), capabilityType.getProperties(), operation.getInputName());
            }
        }
    }
    deletePreConfiguredInput(csar, topology, operation);
    inputs.remove(operation.getInputName());
    log.debug("Remove the input " + operation.getInputName() + " from the topology " + topology.getId());
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) NotFoundException(alien4cloud.exception.NotFoundException)

Example 15 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class UpdateCapabilityPropertyValueProcessor method process.

@Override
@SneakyThrows
public void process(Csar csar, Topology topology, UpdateCapabilityPropertyValueOperation operation) {
    String propertyName = operation.getPropertyName();
    Object propertyValue = operation.getPropertyValue();
    Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology);
    NodeTemplate nodeTemplate = TopologyUtils.getNodeTemplate(topology.getId(), operation.getNodeName(), nodeTemplates);
    Capability capability = nodeTemplate.getCapabilities().get(operation.getCapabilityName());
    CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType());
    if (!capabilityType.getProperties().containsKey(propertyName)) {
        throw new NotFoundException("Property <" + propertyName + "> doesn't exists for node <" + operation.getNodeName() + "> of type <" + capabilityType + ">");
    }
    log.debug("Updating property [ {} ] of the capability [ {} ] for the Node template [ {} ] from the topology [ {} ]: changing value from [{}] to [{}].", propertyName, capability.getType(), operation.getNodeName(), topology.getId(), capabilityType.getProperties().get(propertyName), propertyValue);
    try {
        propertyService.setCapabilityPropertyValue(capability, capabilityType.getProperties().get(propertyName), propertyName, propertyValue);
    } catch (ConstraintFunctionalException e) {
        throw new PropertyValueException("Error when setting node " + operation.getNodeName() + " property.", e, propertyName, propertyValue);
    }
}
Also used : ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) NotFoundException(alien4cloud.exception.NotFoundException) PropertyValueException(org.alien4cloud.tosca.editor.exception.PropertyValueException) SneakyThrows(lombok.SneakyThrows)

Aggregations

Capability (org.alien4cloud.tosca.model.templates.Capability)50 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)27 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)26 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)14 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)12 Map (java.util.Map)10 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)10 NotFoundException (alien4cloud.exception.NotFoundException)9 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 Test (org.junit.Test)9 Set (java.util.Set)8 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)5 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 HashMap (java.util.HashMap)4 List (java.util.List)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4 Topology (org.alien4cloud.tosca.model.templates.Topology)4 Location (alien4cloud.model.orchestrators.locations.Location)3 PaaSNodeTemplate (alien4cloud.paas.model.PaaSNodeTemplate)3