Search in sources :

Example 6 with RelationshipTemplate

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

the class RelationshipPostProcessor method process.

public void process(NodeType nodeTemplateType, Map.Entry<String, RelationshipTemplate> instance) {
    RelationshipTemplate relationshipTemplate = instance.getValue();
    if (relationshipTemplate.getTarget() == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
        // the node template name is required
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NODE_TEMPLATE_NAME_REQUIRED, null, node.getStartMark(), null, node.getEndMark(), null));
    }
    RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    propertyValueChecker.checkProperties(relationshipType, relationshipTemplate.getProperties(), instance.getKey());
    RequirementDefinition rd = getRequirementDefinitionByName(nodeTemplateType, relationshipTemplate.getRequirementName());
    if (rd == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getRequirementName());
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
        return;
    }
    if (relationshipTemplate.getType() == null) {
        // if the relationship type has not been defined on the requirement assignment it may be defined on the requirement definition.
        relationshipTemplate.setType(rd.getRelationshipType());
    }
    referencePostProcessor.process(new ReferencePostProcessor.TypeReference(relationshipTemplate, relationshipTemplate.getType(), RelationshipType.class));
    relationshipTemplate.setRequirementType(rd.getType());
    ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
    // now find the target of the relation
    NodeTemplate targetNodeTemplate = archiveRoot.getTopology().getNodeTemplates().get(relationshipTemplate.getTarget());
    if (targetNodeTemplate == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getTarget());
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getTarget()));
        return;
    }
    // alien actually supports a capability type in the TOSCA yaml
    String capabilityStr = relationshipTemplate.getTargetedCapabilityName();
    Capability capability = null;
    if (capabilityStr == null) {
        // the capability type is not known, we assume that we are parsing a Short notation (node only)
        if (targetNodeTemplate.getCapabilities() != null) {
            // let's try to find all match for a given type
            capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, relationshipTemplate.getRequirementType());
            if (capability == null) {
                capability = targetNodeTemplate.getCapabilities().get(relationshipTemplate.getRequirementName());
                if (capability != null) {
                    relationshipTemplate.setTargetedCapabilityName(rd.getId());
                }
            }
        }
    } else {
        // Let's try to find if the target node has a capability as named in the capability string of the relationship (requirement assignment)
        if (targetNodeTemplate.getCapabilities() != null) {
            capability = targetNodeTemplate.getCapabilities().get(capabilityStr);
        }
        if (capability == null) {
            // The capabilityStr may be the name of a type
            capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, capabilityStr);
        }
    }
    if (capability == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate);
        // we should fail
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_CAPABILITY_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
        return;
    }
    RelationshipType indexedRelationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    if (indexedRelationshipType == null) {
        // Error managed by the reference post processor.
        return;
    }
    Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
    TemplateBuilder.fillProperties(properties, indexedRelationshipType.getProperties(), relationshipTemplate.getProperties(), false);
    relationshipTemplate.setProperties(properties);
    relationshipTemplate.setAttributes(indexedRelationshipType.getAttributes());
    // FIXME we should check that the artifact is defined at the type level.
    safe(instance.getValue().getArtifacts()).values().forEach(templateDeploymentArtifactPostProcessor);
    Map<String, DeploymentArtifact> mergedArtifacts = instance.getValue().getArtifacts();
    if (mergedArtifacts == null) {
        mergedArtifacts = new HashMap<>();
    }
    mergedArtifacts.putAll(safe(indexedRelationshipType.getArtifacts()));
    relationshipTemplate.setArtifacts(mergedArtifacts);
    // TODO Manage interfaces inputs to copy them to all operations.
    for (Interface anInterface : safe(instance.getValue().getInterfaces()).values()) {
        safe(anInterface.getOperations()).values().stream().map(Operation::getImplementationArtifact).filter(Objects::nonNull).forEach(implementationArtifactPostProcessor);
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) Node(org.yaml.snakeyaml.nodes.Node) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) ParsingError(alien4cloud.tosca.parser.ParsingError) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate)

Example 7 with RelationshipTemplate

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

the class TopologyNavigationUtil method getHostOfTypeInHostingHierarchy.

/**
 * Deeply explore the hosted_on hierarchy of the given node to find a node of the given type.
 */
public static NodeTemplate getHostOfTypeInHostingHierarchy(Topology topology, NodeTemplate nodeTemplate, String hostType) {
    if (nodeTemplate.getRelationships() != null) {
        for (RelationshipTemplate relationshipTemplate : nodeTemplate.getRelationships().values()) {
            RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
            if (isOfType(relationshipType, NormativeRelationshipConstants.HOSTED_ON)) {
                NodeTemplate hostNode = topology.getNodeTemplates().get(relationshipTemplate.getTarget());
                NodeType hostNodeType = ToscaContext.get(NodeType.class, hostNode.getType());
                if (isOfType(hostNodeType, hostType)) {
                    return hostNode;
                } else {
                    return getHostOfTypeInHostingHierarchy(topology, hostNode, hostType);
                }
            }
        }
    }
    return null;
}
Also used : RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType)

Example 8 with RelationshipTemplate

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

the class NodeTemplateRelationshipPostProcessor method process.

@Override
public void process(NodeTemplate instance) {
    final NodeType nodeType = ToscaContext.get(NodeType.class, instance.getType());
    if (nodeType == null) {
        // error managed by the reference post processor.
        return;
    }
    Map<String, RelationshipTemplate> updated = Maps.newLinkedHashMap();
    safe(instance.getRelationships()).entrySet().forEach(entry -> {
        relationshipPostProcessor.process(nodeType, entry);
        String relationshipTemplateName = entry.getValue().getName();
        if (StringUtils.isEmpty(relationshipTemplateName)) {
            // from 2.0.0 the relationship's name is filled by the parser
            relationshipTemplateName = buildRelationShipTemplateName(entry.getValue());
        }
        relationshipTemplateName = getUniqueKey(updated, relationshipTemplateName);
        updated.put(relationshipTemplateName, entry.getValue());
        entry.getValue().setName(relationshipTemplateName);
    });
    instance.setRelationships(updated);
}
Also used : RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType)

Example 9 with RelationshipTemplate

use of org.alien4cloud.tosca.model.templates.RelationshipTemplate 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 10 with RelationshipTemplate

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

the class DeleteNodeProcessor method removeRelationShipReferences.

/**
 * Removes all relationships connected to the removed node (either as source or target)
 *
 * @param removedNode The name of the removed node
 * @param topology The topology in which to remove the references.
 * @param typesTobeUnloaded List of types to remove from the topology (when relationships are removed)
 */
private void removeRelationShipReferences(String removedNode, Csar csar, Topology topology, List<String> typesTobeUnloaded) {
    List<String> relationshipsToRemove = Lists.newArrayList();
    for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
        if (removedNode.equals(nodeTemplate.getName())) {
            // remove all relationships
            for (Entry<String, RelationshipTemplate> relationshipTemplateEntry : safe(nodeTemplate.getRelationships()).entrySet()) {
                typesTobeUnloaded.add(relationshipTemplateEntry.getValue().getType());
                workflowBuilderService.removeRelationship(topology, csar, nodeTemplate.getName(), relationshipTemplateEntry.getKey(), relationshipTemplateEntry.getValue());
            }
        } else {
            // This is for later removal
            relationshipsToRemove.clear();
            for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) {
                if (removedNode.equals(relationshipTemplate.getTarget())) {
                    relationshipsToRemove.add(relationshipTemplate.getName());
                    typesTobeUnloaded.add(relationshipTemplate.getType());
                    workflowBuilderService.removeRelationship(topology, csar, nodeTemplate.getName(), relationshipTemplate.getName(), relationshipTemplate);
                }
            }
            // remove the relationship from the node's relationship map.
            for (String relationshipToRemove : relationshipsToRemove) {
                nodeTemplate.getRelationships().remove(relationshipToRemove);
            }
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate)

Aggregations

RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)47 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)37 Map (java.util.Map)12 Capability (org.alien4cloud.tosca.model.templates.Capability)12 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)11 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)7 NotFoundException (alien4cloud.exception.NotFoundException)6 PaaSRelationshipTemplate (alien4cloud.paas.model.PaaSRelationshipTemplate)4 LinkedHashMap (java.util.LinkedHashMap)4 Topology (org.alien4cloud.tosca.model.templates.Topology)4 AlreadyExistException (alien4cloud.exception.AlreadyExistException)3 ParsingError (alien4cloud.tosca.parser.ParsingError)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Interface (org.alien4cloud.tosca.model.definitions.Interface)3 RequirementDefinition (org.alien4cloud.tosca.model.definitions.RequirementDefinition)3 Requirement (org.alien4cloud.tosca.model.templates.Requirement)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 Workflow (org.alien4cloud.tosca.model.workflow.Workflow)3