Search in sources :

Example 21 with RelationshipTemplate

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

the class DefaultWorkflowBuilder method addRelationship.

@Override
public void addRelationship(Workflow workflow, String nodeId, NodeTemplate nodeTemplate, String relationshipName, RelationshipTemplate relationshipTemplate, TopologyContext topologyContext) {
    boolean sourceIsNative = WorkflowUtils.isNativeOrSubstitutionNode(nodeId, topologyContext);
    boolean targetIsNative = WorkflowUtils.isNativeOrSubstitutionNode(relationshipTemplate.getTarget(), topologyContext);
    if (!sourceIsNative || !targetIsNative) {
        // source or target is native or abstract
        // for native types we don't care about relation ships in workflows
        RelationshipDeclarativeWorkflow relationshipDeclarativeWorkflow = defaultDeclarativeWorkflows.getRelationshipWorkflows().get(workflow.getName());
        // only trigger this method if it's a default workflow
        if (relationshipDeclarativeWorkflow != null) {
            Map<String, WorkflowStep> relationshipOperationSteps = safe(relationshipDeclarativeWorkflow.getOperations()).entrySet().stream().filter(operationEntry -> !targetIsNative || operationEntry.getValue().getOperationHost() == RelationshipOperationHost.SOURCE).filter(operationEntry -> !sourceIsNative || operationEntry.getValue().getOperationHost() == RelationshipOperationHost.TARGET).collect(Collectors.toMap(Map.Entry::getKey, operationEntry -> WorkflowUtils.addRelationshipOperationStep(workflow, nodeId, relationshipTemplate.getName(), ToscaRelationshipLifecycleConstants.CONFIGURE_SHORT, operationEntry.getKey(), operationEntry.getValue().getOperationHost().toString())));
            Steps sourceSteps = new Steps(workflow, nodeId);
            Steps targetSteps = new Steps(workflow, relationshipTemplate.getTarget());
            safe(relationshipDeclarativeWorkflow.getOperations()).forEach((relationshipOperationName, relationshipOperationDependencies) -> {
                WorkflowStep currentStep = relationshipOperationSteps.get(relationshipOperationName);
                if (currentStep != null) {
                    // It might be filtered if source or target is native
                    declareStepDependencies(relationshipOperationDependencies.getSource(), currentStep, sourceSteps);
                    declareStepDependencies(relationshipOperationDependencies.getTarget(), currentStep, targetSteps);
                    declareStepDependencies(relationshipOperationDependencies, currentStep, new Steps(relationshipOperationSteps, Collections.emptyMap(), null));
                }
            });
            RelationshipWeavingDeclarativeWorkflow relationshipWeavingDeclarativeWorkflow = getRelationshipWeavingDeclarativeWorkflow(relationshipTemplate.getType(), topologyContext, workflow.getName());
            declareWeaving(relationshipWeavingDeclarativeWorkflow.getSource(), sourceSteps, targetSteps);
            declareWeaving(relationshipWeavingDeclarativeWorkflow.getTarget(), targetSteps, sourceSteps);
        }
    } else {
        // both source and target are native then the relationship does not have any operation implemented
        // we will just try to declare weaving between source node operations and target node operations
        Steps sourceSteps = new Steps(workflow, nodeId);
        Steps targetSteps = new Steps(workflow, relationshipTemplate.getTarget());
        RelationshipWeavingDeclarativeWorkflow relationshipWeavingDeclarativeWorkflow = getRelationshipWeavingDeclarativeWorkflow(relationshipTemplate.getType(), topologyContext, workflow.getName());
        declareWeaving(relationshipWeavingDeclarativeWorkflow.getSource(), sourceSteps, targetSteps);
        declareWeaving(relationshipWeavingDeclarativeWorkflow.getTarget(), targetSteps, sourceSteps);
    }
}
Also used : ToscaNodeLifecycleConstants(alien4cloud.paas.plan.ToscaNodeLifecycleConstants) RelationshipWeavingDeclarativeWorkflow(org.alien4cloud.tosca.model.workflow.declarative.RelationshipWeavingDeclarativeWorkflow) OperationDeclarativeWorkflow(org.alien4cloud.tosca.model.workflow.declarative.OperationDeclarativeWorkflow) ToscaRelationshipLifecycleConstants(alien4cloud.paas.plan.ToscaRelationshipLifecycleConstants) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) ArrayList(java.util.ArrayList) DefaultDeclarativeWorkflows(org.alien4cloud.tosca.model.workflow.declarative.DefaultDeclarativeWorkflows) List(java.util.List) RelationshipDeclarativeWorkflow(org.alien4cloud.tosca.model.workflow.declarative.RelationshipDeclarativeWorkflow) NodeDeclarativeWorkflow(org.alien4cloud.tosca.model.workflow.declarative.NodeDeclarativeWorkflow) Map(java.util.Map) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipWeaving(org.alien4cloud.tosca.model.workflow.declarative.RelationshipWeaving) RelationshipOperationHost(org.alien4cloud.tosca.model.workflow.declarative.RelationshipOperationHost) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) Steps(alien4cloud.paas.wf.util.Steps) Collections(java.util.Collections) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) WorkflowUtils(alien4cloud.paas.wf.util.WorkflowUtils) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) Steps(alien4cloud.paas.wf.util.Steps) RelationshipWeavingDeclarativeWorkflow(org.alien4cloud.tosca.model.workflow.declarative.RelationshipWeavingDeclarativeWorkflow) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) RelationshipDeclarativeWorkflow(org.alien4cloud.tosca.model.workflow.declarative.RelationshipDeclarativeWorkflow) Map(java.util.Map)

Example 22 with RelationshipTemplate

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

the class WorkflowsBuilderService method removeRelationship.

public void removeRelationship(Topology topology, Csar csar, String sourceNodeId, String relationshipName, RelationshipTemplate relationshipTemplate) {
    TopologyContext topologyContext = buildTopologyContext(topology, csar);
    topologyContext.getTopology().getWorkflows().putAll(topologyContext.getTopology().getUnprocessedWorkflows());
    NodeTemplate sourceNode = topology.getNodeTemplates().get(sourceNodeId);
    String targetNodeId = relationshipTemplate.getTarget();
    NodeTemplate targetNode = topologyContext.getTopology().getNodeTemplates().get(targetNodeId);
    for (Workflow wf : topology.getWorkflows().values()) {
        AbstractWorkflowBuilder builder = getWorkflowBuilder(topologyContext.getDSLVersion(), wf);
        // Remove relationships from source to target
        // Remove relationships from target to source
        Map<String, RelationshipTemplate> sourceRelationships = sourceNode.getRelationships().entrySet().stream().filter(relationshipEntry -> relationshipEntry.getValue().getTarget().equals(targetNodeId)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        Map<String, RelationshipTemplate> targetRelationships = AlienUtils.safe(targetNode.getRelationships()).entrySet().stream().filter(relationshipEntry -> relationshipEntry.getValue().getTarget().equals(sourceNodeId)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        builder.removeRelationships(wf, sourceNodeId, sourceRelationships, targetNodeId, targetRelationships);
        sourceRelationships.entrySet().stream().filter(entry -> !entry.getKey().equals(relationshipName)).forEach(entry -> builder.addRelationship(wf, sourceNode.getName(), sourceNode, entry.getKey(), entry.getValue(), topologyContext));
        targetRelationships.forEach((id, relationship) -> builder.addRelationship(wf, targetNodeId, targetNode, id, relationship, topologyContext));
        // Remove unique relationship that we really want to remove
        WorkflowUtils.fillHostId(wf, topologyContext);
    }
    postProcessTopologyWorkflows(topologyContext);
    debugWorkflow(topologyContext.getTopology());
}
Also used : ToscaParser(alien4cloud.tosca.parser.ToscaParser) Lists(org.elasticsearch.common.collect.Lists) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) HashMap(java.util.HashMap) STOP(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.STOP) Maps(org.elasticsearch.common.collect.Maps) DefaultDeclarativeWorkflows(org.alien4cloud.tosca.model.workflow.declarative.DefaultDeclarativeWorkflows) START(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.START) INSTALL(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.INSTALL) Map(java.util.Map) YamlParserUtil(alien4cloud.utils.YamlParserUtil) Csar(org.alien4cloud.tosca.model.Csar) WorkflowValidator(alien4cloud.paas.wf.validation.WorkflowValidator) AlienUtils(alien4cloud.utils.AlienUtils) Resource(javax.annotation.Resource) TaskCode(alien4cloud.topology.task.TaskCode) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ICSARRepositorySearchService(alien4cloud.component.ICSARRepositorySearchService) NotFoundException(alien4cloud.exception.NotFoundException) List(java.util.List) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) AbstractWorkflowActivity(org.alien4cloud.tosca.model.workflow.activities.AbstractWorkflowActivity) AbstractToscaType(org.alien4cloud.tosca.model.types.AbstractToscaType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) PostConstruct(javax.annotation.PostConstruct) BadWorkflowOperationException(alien4cloud.paas.wf.exception.BadWorkflowOperationException) Topology(org.alien4cloud.tosca.model.templates.Topology) UNINSTALL(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.UNINSTALL) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) WorkflowUtils(alien4cloud.paas.wf.util.WorkflowUtils) WorkflowTask(alien4cloud.topology.task.WorkflowTask) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with RelationshipTemplate

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

the class TopologyCompositionService method renameNodeTemplate.

private void renameNodeTemplate(Topology topology, String oldName, String newName) {
    // quite improbable but ...
    if (topology.getNodeTemplates().containsKey(newName)) {
        throw new AlreadyExistException(String.format("A node with name '%s' already exists in this topology", newName));
    }
    NodeTemplate nodeTemplate = topology.getNodeTemplates().remove(oldName);
    // manage relationships that target this node
    for (NodeTemplate otherNodes : topology.getNodeTemplates().values()) {
        if (otherNodes.getRelationships() == null || otherNodes.getRelationships().isEmpty()) {
            continue;
        }
        for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) {
            if (relationshipTemplate.getTarget().equals(oldName)) {
                relationshipTemplate.setTarget(newName);
            }
        }
    }
    // all output stuffs
    MapUtil.replaceKey(topology.getOutputProperties(), oldName, newName);
    MapUtil.replaceKey(topology.getOutputCapabilityProperties(), oldName, newName);
    MapUtil.replaceKey(topology.getOutputAttributes(), oldName, newName);
    // group members must be updated
    if (topology.getGroups() != null) {
        for (NodeGroup nodeGroup : topology.getGroups().values()) {
            Set<String> members = nodeGroup.getMembers();
            if (members != null && members.remove(oldName)) {
                members.add(newName);
            }
        }
    }
    // substitutions
    if (topology.getSubstitutionMapping() != null) {
        renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getCapabilities(), oldName, newName);
        renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getRequirements(), oldName, newName);
    }
    // finally the node itself
    topology.getNodeTemplates().put(newName, nodeTemplate);
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) AlreadyExistException(alien4cloud.exception.AlreadyExistException) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup)

Example 24 with RelationshipTemplate

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

the class TopologyCompositionService method processComposition.

/**
 * Process the composition:
 * <ul>
 * <li>remove the 'proxy' node from the parent.
 * <li>merge the child topology nodes into the parent nodes.
 * <li>
 * </ul>
 *
 * @param compositionCouple
 */
private void processComposition(CompositionCouple compositionCouple) {
    // first of all, remove the proxy node from the parent
    NodeTemplate proxyNodeTemplate = compositionCouple.parent.getNodeTemplates().remove(compositionCouple.nodeName);
    // properties of the proxy are used to feed the property values of child node that use get_input
    for (NodeTemplate childNodeTemplate : compositionCouple.child.getNodeTemplates().values()) {
        for (Entry<String, AbstractPropertyValue> propertyEntry : childNodeTemplate.getProperties().entrySet()) {
            AbstractPropertyValue pValue = propertyEntry.getValue();
            if (isGetInput(pValue)) {
                String inputName = ((FunctionPropertyValue) pValue).getTemplateName();
                propertyEntry.setValue(proxyNodeTemplate.getProperties().get(inputName));
            }
        }
        for (Entry<String, Capability> capabilityEntry : childNodeTemplate.getCapabilities().entrySet()) {
            if (capabilityEntry.getValue().getProperties() != null) {
                for (Entry<String, AbstractPropertyValue> propertyEntry : capabilityEntry.getValue().getProperties().entrySet()) {
                    AbstractPropertyValue pValue = propertyEntry.getValue();
                    if (isGetInput(pValue)) {
                        String inputName = ((FunctionPropertyValue) pValue).getTemplateName();
                        propertyEntry.setValue(proxyNodeTemplate.getProperties().get(inputName));
                    }
                }
            }
        }
    }
    // all relations from the proxy must now start from the corresponding node
    if (proxyNodeTemplate.getRelationships() != null) {
        for (Entry<String, RelationshipTemplate> e : proxyNodeTemplate.getRelationships().entrySet()) {
            String relationShipKey = e.getKey();
            RelationshipTemplate proxyRelationShip = e.getValue();
            String requirementName = proxyRelationShip.getRequirementName();
            SubstitutionTarget substitutionTarget = compositionCouple.child.getSubstitutionMapping().getRequirements().get(requirementName);
            NodeTemplate nodeTemplate = compositionCouple.child.getNodeTemplates().get(substitutionTarget.getNodeTemplateName());
            if (nodeTemplate.getRelationships() == null) {
                Map<String, RelationshipTemplate> relationships = Maps.newHashMap();
                nodeTemplate.setRelationships(relationships);
            }
            nodeTemplate.getRelationships().put(relationShipKey, proxyRelationShip);
            proxyRelationShip.setRequirementName(substitutionTarget.getTargetId());
        }
    }
    // all relations that target the proxy must be redirected to the corresponding child node
    for (NodeTemplate otherNodes : compositionCouple.parent.getNodeTemplates().values()) {
        if (otherNodes.getRelationships() != null) {
            for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) {
                if (relationshipTemplate.getTarget().equals(compositionCouple.nodeName)) {
                    SubstitutionTarget st = compositionCouple.child.getSubstitutionMapping().getCapabilities().get(relationshipTemplate.getTargetedCapabilityName());
                    relationshipTemplate.setTarget(st.getNodeTemplateName());
                    relationshipTemplate.setTargetedCapabilityName(st.getTargetId());
                }
            }
        }
    }
    if (compositionCouple.parent.getOutputAttributes() != null) {
        Set<String> outputAttributes = compositionCouple.parent.getOutputAttributes().remove(compositionCouple.nodeName);
        if (outputAttributes != null) {
            for (String proxyAttributeName : outputAttributes) {
                sustituteGetAttribute(compositionCouple.child, compositionCouple.parent, proxyAttributeName);
            }
        }
    }
    // the parent itself expose stuffs, we eventually need to replace substitution targets
    if (compositionCouple.parent.getSubstitutionMapping() != null) {
        if (compositionCouple.parent.getSubstitutionMapping().getCapabilities() != null) {
            for (Entry<String, SubstitutionTarget> substitutionCapabilityEntry : compositionCouple.parent.getSubstitutionMapping().getCapabilities().entrySet()) {
                if (substitutionCapabilityEntry.getValue().getNodeTemplateName().equals(compositionCouple.nodeName)) {
                    String targetCapability = substitutionCapabilityEntry.getValue().getTargetId();
                    // just substitute the substitution target
                    substitutionCapabilityEntry.setValue(compositionCouple.child.getSubstitutionMapping().getCapabilities().get(targetCapability));
                }
            }
        }
        if (compositionCouple.parent.getSubstitutionMapping().getRequirements() != null) {
            for (Entry<String, SubstitutionTarget> e : compositionCouple.parent.getSubstitutionMapping().getRequirements().entrySet()) {
                if (e.getValue().getNodeTemplateName().equals(compositionCouple.nodeName)) {
                    String targetCapability = e.getValue().getTargetId();
                    // just substitute the substitution target
                    e.setValue(compositionCouple.child.getSubstitutionMapping().getRequirements().get(targetCapability));
                }
            }
        }
    }
    // merge each child nodes into the parent
    compositionCouple.parent.getNodeTemplates().putAll(compositionCouple.child.getNodeTemplates());
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 25 with RelationshipTemplate

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

the class TopologyStepDefinitions method I_should_have_a_relationship_with_type_from_to_in_ALIEN.

@Then("^I should have a relationship \"([^\"]*)\" with type \"([^\"]*)\" from \"([^\"]*)\" to \"([^\"]*)\" in ALIEN$")
public void I_should_have_a_relationship_with_type_from_to_in_ALIEN(String relName, String relType, String source, String target) throws Throwable {
    // I should have a relationship with type
    String topologyJson = Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId());
    RestResponse<TopologyDTO> topologyResponse = JsonUtil.read(topologyJson, TopologyDTO.class, Context.getJsonMapper());
    NodeTemplate sourceNode = topologyResponse.getData().getTopology().getNodeTemplates().get(source);
    relName = relName == null || relName.isEmpty() ? getRelationShipName(relType, target) : relName;
    RelationshipTemplate rel = sourceNode.getRelationships().get(relName);
    assertNotNull(rel);
    assertEquals(relType, rel.getType());
    assertEquals(target, rel.getTarget());
    assertNotNull(rel.getRequirementName());
    assertNotNull(rel.getRequirementType());
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) TopologyDTO(alien4cloud.topology.TopologyDTO) Then(cucumber.api.java.en.Then)

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