Search in sources :

Example 16 with WorkflowStep

use of org.alien4cloud.tosca.model.workflow.WorkflowStep in project alien4cloud by alien4cloud.

the class WorkflowUtils method cloneStep.

public static WorkflowStep cloneStep(WorkflowStep step) {
    WorkflowStep cloned;
    if (step instanceof NodeWorkflowStep) {
        NodeWorkflowStep nodeWorkflowStep = (NodeWorkflowStep) step;
        cloned = new NodeWorkflowStep();
        ((NodeWorkflowStep) cloned).setHostId(nodeWorkflowStep.getHostId());
    } else {
        RelationshipWorkflowStep relationshipWorkflowStep = (RelationshipWorkflowStep) step;
        cloned = new RelationshipWorkflowStep();
        ((RelationshipWorkflowStep) cloned).setTargetRelationship(relationshipWorkflowStep.getTargetRelationship());
        ((RelationshipWorkflowStep) cloned).setSourceHostId(relationshipWorkflowStep.getSourceHostId());
        ((RelationshipWorkflowStep) cloned).setTargetHostId(relationshipWorkflowStep.getTargetHostId());
    }
    cloned.setActivities(step.getActivities());
    cloned.setFilter(step.getFilter());
    cloned.setName(step.getName());
    cloned.setOnFailure(step.getOnFailure());
    cloned.setOnSuccess(new HashSet<>(step.getOnSuccess()));
    cloned.setOperationHost(step.getOperationHost());
    cloned.setPrecedingSteps(new HashSet<>(step.getPrecedingSteps()));
    cloned.setTarget(step.getTarget());
    return cloned;
}
Also used : RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep)

Example 17 with WorkflowStep

use of org.alien4cloud.tosca.model.workflow.WorkflowStep in project alien4cloud by alien4cloud.

the class WorkflowUtils method processInlineWorkflow.

private static void processInlineWorkflow(Map<String, Workflow> workflowMap, Workflow workflow) {
    final Set<String> newInlinedStepNames = new HashSet<>();
    // Clone the map as we iterate and in the same time modify it
    Maps.newHashMap(workflow.getSteps()).entrySet().stream().filter(entry -> entry.getValue().getActivity() instanceof InlineWorkflowActivity).forEach(inlinedStepEntry -> {
        String inlinedStepName = inlinedStepEntry.getKey();
        WorkflowStep inlinedStep = inlinedStepEntry.getValue();
        InlineWorkflowActivity inlineWorkflowActivity = (InlineWorkflowActivity) inlinedStep.getActivity();
        String inlinedName = inlineWorkflowActivity.getInline();
        Workflow inlined = workflowMap.get(inlinedName);
        if (inlined == null) {
            throw new NotFoundException("Inlined workflow " + inlinedName);
        }
        Map<String, WorkflowStep> generatedSteps = cloneSteps(inlined.getSteps());
        Map<String, WorkflowStep> generatedStepsWithNewNames = generatedSteps.entrySet().stream().collect(Collectors.toMap(entry -> {
            String newName = generateNewWfStepNameWithPrefix(inlinedStepEntry.getKey() + "_", workflow.getSteps().keySet(), newInlinedStepNames, entry.getKey());
            newInlinedStepNames.add(newName);
            if (!newName.equals(entry.getKey())) {
                entry.getValue().setName(newName);
                generatedSteps.forEach((generatedStepId, generatedStep) -> {
                    if (generatedStep.removeFollowing(entry.getKey())) {
                        generatedStep.addFollowing(newName);
                    }
                    if (generatedStep.removePreceding(entry.getKey())) {
                        generatedStep.addPreceding(newName);
                    }
                });
            }
            return newName;
        }, Map.Entry::getValue));
        // Find all root steps of the workflow and link them to the parent workflows
        final Map<String, WorkflowStep> rootInlinedSteps = generatedStepsWithNewNames.values().stream().filter(generatedStepWithNewName -> generatedStepWithNewName.getPrecedingSteps().isEmpty()).peek(rootInlinedStep -> rootInlinedStep.addAllPrecedings(inlinedStep.getPrecedingSteps())).collect(Collectors.toMap(WorkflowStep::getName, rootInlinedStep -> rootInlinedStep));
        inlinedStep.getPrecedingSteps().forEach(precedingStepName -> {
            WorkflowStep precedingStep = workflow.getSteps().get(precedingStepName);
            precedingStep.removeFollowing(inlinedStepName);
            precedingStep.addAllFollowings(rootInlinedSteps.keySet());
        });
        // Find all leaf steps of the workflow and link them to the parent workflows
        final Map<String, WorkflowStep> leafInlinedSteps = generatedStepsWithNewNames.values().stream().filter(generatedStepWithNewName -> generatedStepWithNewName.getOnSuccess().isEmpty()).peek(leafInlinedStep -> leafInlinedStep.addAllFollowings(inlinedStep.getOnSuccess())).collect(Collectors.toMap(WorkflowStep::getName, leafInlinedStep -> leafInlinedStep));
        inlinedStep.getOnSuccess().forEach(onSuccessStepName -> {
            WorkflowStep onSuccessStep = workflow.getSteps().get(onSuccessStepName);
            onSuccessStep.removePreceding(inlinedStepName);
            onSuccessStep.addAllPrecedings(leafInlinedSteps.keySet());
        });
        // Remove the inlined step and replace by other workflow's steps
        workflow.getSteps().remove(inlinedStepName);
        workflow.addAllSteps(generatedStepsWithNewNames);
    });
    // Check if the workflow contains inline activity event after processing
    boolean processedWorkflowContainsInline = workflow.getSteps().values().stream().anyMatch(step -> step.getActivity() instanceof InlineWorkflowActivity);
    if (processedWorkflowContainsInline) {
        // Recursively process inline workflow until no step is inline workflow
        processInlineWorkflow(workflowMap, workflow);
    }
}
Also used : NormativeComputeConstants(org.alien4cloud.tosca.normative.constants.NormativeComputeConstants) SetStateWorkflowActivity(org.alien4cloud.tosca.model.workflow.activities.SetStateWorkflowActivity) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) DelegateWorkflowActivity(org.alien4cloud.tosca.model.workflow.activities.DelegateWorkflowActivity) NodeType(org.alien4cloud.tosca.model.types.NodeType) StringUtils(org.apache.commons.lang3.StringUtils) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) TopologyNavigationUtil(org.alien4cloud.tosca.utils.TopologyNavigationUtil) STOP(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.STOP) HashSet(java.util.HashSet) START(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.START) INSTALL(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.INSTALL) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) InvalidNameException(alien4cloud.exception.InvalidNameException) InlineWorkflowActivity(org.alien4cloud.tosca.model.workflow.activities.InlineWorkflowActivity) Map(java.util.Map) Interface(org.alien4cloud.tosca.model.definitions.Interface) Operation(org.alien4cloud.tosca.model.definitions.Operation) Set(java.util.Set) TopologyContext(alien4cloud.paas.wf.TopologyContext) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) CallOperationWorkflowActivity(org.alien4cloud.tosca.model.workflow.activities.CallOperationWorkflowActivity) NotFoundException(alien4cloud.exception.NotFoundException) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) UNINSTALL(org.alien4cloud.tosca.normative.constants.NormativeWorkflowNameConstants.UNINSTALL) Pattern(java.util.regex.Pattern) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) ToscaTypeUtils.isOfType(org.alien4cloud.tosca.utils.ToscaTypeUtils.isOfType) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) InlineWorkflowActivity(org.alien4cloud.tosca.model.workflow.activities.InlineWorkflowActivity) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) NotFoundException(alien4cloud.exception.NotFoundException) Map(java.util.Map) HashSet(java.util.HashSet)

Example 18 with WorkflowStep

use of org.alien4cloud.tosca.model.workflow.WorkflowStep in project alien4cloud by alien4cloud.

the class AbstractWorkflowBuilder method removeEdge.

public void removeEdge(Workflow wf, String from, String to) {
    WorkflowStep fromStep = wf.getSteps().get(from);
    if (fromStep == null) {
        throw new InconsistentWorkflowException(String.format("Inconsistent workflow: a step nammed '%s' can not be found while it's referenced else where ...", from));
    }
    WorkflowStep toStep = wf.getSteps().get(to);
    if (toStep == null) {
        throw new InconsistentWorkflowException(String.format("Inconsistent workflow: a step nammed '%s' can not be found while it's referenced else where ...", to));
    }
    fromStep.removeFollowing(to);
    toStep.removePreceding(from);
}
Also used : InconsistentWorkflowException(alien4cloud.paas.wf.exception.InconsistentWorkflowException) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep)

Example 19 with WorkflowStep

use of org.alien4cloud.tosca.model.workflow.WorkflowStep in project alien4cloud by alien4cloud.

the class AbstractWorkflowBuilder method removePredecessors.

private List<WorkflowStep> removePredecessors(Workflow wf, WorkflowStep step) {
    List<WorkflowStep> result = Lists.newArrayList();
    if (step.getPrecedingSteps() == null || step.getPrecedingSteps().size() == 0) {
        return result;
    }
    Object[] precedings = step.getPrecedingSteps().toArray();
    for (Object precedingId : precedings) {
        WorkflowStep precedingStep = wf.getSteps().get(precedingId);
        unlinkSteps(precedingStep, step);
        result.add(precedingStep);
    }
    return result;
}
Also used : NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep)

Example 20 with WorkflowStep

use of org.alien4cloud.tosca.model.workflow.WorkflowStep in project alien4cloud by alien4cloud.

the class AbstractWorkflowBuilder method connectStepFrom.

void connectStepFrom(Workflow wf, String stepId, String[] stepNames) {
    WorkflowStep to = wf.getSteps().get(stepId);
    if (to == null) {
        throw new InconsistentWorkflowException(String.format("Inconsistent workflow: a step nammed '%s' can not be found while it's referenced else where ...", stepId));
    }
    for (String preceding : stepNames) {
        WorkflowStep precedingStep = wf.getSteps().get(preceding);
        if (precedingStep == null) {
            throw new InconsistentWorkflowException(String.format("Inconsistent workflow: a step nammed '%s' can not be found while it's referenced else where ...", preceding));
        }
        WorkflowUtils.linkSteps(precedingStep, to);
    }
}
Also used : InconsistentWorkflowException(alien4cloud.paas.wf.exception.InconsistentWorkflowException) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep)

Aggregations

WorkflowStep (org.alien4cloud.tosca.model.workflow.WorkflowStep)51 RelationshipWorkflowStep (org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep)24 NodeWorkflowStep (org.alien4cloud.tosca.model.workflow.NodeWorkflowStep)23 Workflow (org.alien4cloud.tosca.model.workflow.Workflow)20 Test (org.junit.Test)17 Path (alien4cloud.paas.wf.model.Path)7 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)5 InconsistentWorkflowException (alien4cloud.paas.wf.exception.InconsistentWorkflowException)4 TopologyDTO (alien4cloud.topology.TopologyDTO)4 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)3 StringUtils (org.apache.commons.lang3.StringUtils)3