Search in sources :

Example 6 with WorkflowStep

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

the class GraphPathsTest method test23.

/**
 * <pre>
 *      c
 *     /
 * -- a
 *     \
 *      b
 * </pre>
 */
@Test
public void test23() {
    Workflow wf = new Workflow();
    wf.setName(INSTALL);
    WorkflowStep a = wf.addStep(new SimpleStep("a"));
    WorkflowStep b = wf.addStep(new SimpleStep("b"));
    WorkflowStep c = wf.addStep(new SimpleStep("c"));
    WorkflowUtils.linkSteps(a, b);
    WorkflowUtils.linkSteps(a, c);
    List<Path> paths = WorkflowGraphUtils.getWorkflowGraphCycles(wf);
    log.info(paths.toString());
}
Also used : Path(alien4cloud.paas.wf.model.Path) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) Test(org.junit.Test)

Example 7 with WorkflowStep

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

the class GraphPathsTest method testOrphan.

/**
 * <pre>
 *     -- a --
 *    /       \
 *    \       /
 *     -- b --
 * </pre>
 */
@Test
public void testOrphan() {
    Workflow wf = new Workflow();
    wf.setName(INSTALL);
    WorkflowStep a = wf.addStep(new SimpleStep("a"));
    WorkflowStep b = wf.addStep(new SimpleStep("b"));
    WorkflowUtils.linkSteps(a, b);
    WorkflowUtils.linkSteps(b, a);
    List<Path> paths = WorkflowGraphUtils.getWorkflowGraphCycles(wf);
    System.out.println(paths);
    Assert.assertEquals(1, paths.size());
    log.info(paths.toString());
}
Also used : Path(alien4cloud.paas.wf.model.Path) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) Test(org.junit.Test)

Example 8 with WorkflowStep

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

the class WorkflowPostProcessor method splitMultipleActivitiesSteps.

/**
 * Called after yaml parsing.
 *
 * Add support of activities on alien-dsl-2.0.0 and higher.
 * For activity, other than the first, we create 1 step per activity.
 */
private void splitMultipleActivitiesSteps(TopologyContext topologyContext) {
    if (!ToscaParser.ALIEN_DSL_200.equals(topologyContext.getDSLVersion()) || MapUtils.isEmpty(topologyContext.getTopology().getWorkflows())) {
        return;
    }
    for (Workflow wf : topologyContext.getTopology().getWorkflows().values()) {
        if (wf.getSteps() != null) {
            Map<String, WorkflowStep> stepsToAdd = new HashMap<>();
            Map<String, LinkedList<String>> newStepsNames = new HashMap<>();
            for (WorkflowStep step : wf.getSteps().values()) {
                if (step.getActivities() == null) {
                    Node node = ParsingContextExecution.getObjectToNodeMap().get(step);
                    ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.WORKFLOW_HAS_ERRORS, null, getSafeNodeStartMark(node), "Step should have at least one activity", getSafeNodeEndMark(node), step.getName()));
                    continue;
                } else if (step.getActivities().size() < 2) {
                    continue;
                }
                // We have a step with multiple activities we'll call it old step
                // We will split this step into multiple steps, the first activity will be contained in the step with the same name
                LinkedList<String> newStepsNamesForCurrentStep = newStepsNames.computeIfAbsent(step.getName(), k -> new LinkedList<>());
                for (int i = 1; i < step.getActivities().size(); i++) {
                    // here we iterate on activities to create new step
                    WorkflowStep singleActivityStep = WorkflowUtils.cloneStep(step);
                    singleActivityStep.setActivities(Lists.newArrayList(step.getActivities().get(i)));
                    String wfStepName = WorkflowUtils.generateNewWfStepName(wf.getSteps().keySet(), stepsToAdd.keySet(), step.getName());
                    singleActivityStep.setName(wfStepName);
                    singleActivityStep.getOnSuccess().clear();
                    stepsToAdd.put(wfStepName, singleActivityStep);
                    newStepsNamesForCurrentStep.add(wfStepName);
                }
                // new steps are created, we can clean activities
                step.getActivities().subList(1, step.getActivities().size()).clear();
            }
            // Generated steps must be executed in a sequential manner
            newStepsNames.forEach((stepName, generatedStepsNames) -> {
                // first old step is chained to the first generated step
                WorkflowStep firstStep = wf.getSteps().get(stepName);
                Set<String> currentFirstStepOnSuccess = firstStep.getOnSuccess();
                firstStep.setOnSuccess(Sets.newHashSet(generatedStepsNames.getFirst()));
                WorkflowStep lastGeneratedStep = stepsToAdd.get(generatedStepsNames.getLast());
                lastGeneratedStep.setOnSuccess(currentFirstStepOnSuccess);
                for (int i = 0; i < generatedStepsNames.size() - 1; i++) {
                    // Each generated step is chained with the preceding to create a sequence
                    stepsToAdd.get(generatedStepsNames.get(i)).addFollowing(generatedStepsNames.get(i + 1));
                }
            });
            // add new steps to the workflow
            wf.addAllSteps(stepsToAdd);
        }
    }
}
Also used : WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) HashMap(java.util.HashMap) Node(org.yaml.snakeyaml.nodes.Node) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) LinkedList(java.util.LinkedList) ParsingError(alien4cloud.tosca.parser.ParsingError)

Example 9 with WorkflowStep

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

the class WorkflowPostProcessor method finalizeParsedWorkflows.

/**
 * Called after yaml parsing.
 */
private void finalizeParsedWorkflows(TopologyContext topologyContext, Node node) {
    if (MapUtils.isEmpty(topologyContext.getTopology().getWorkflows())) {
        return;
    }
    normalizeWorkflowNames(topologyContext.getTopology().getWorkflows());
    for (Workflow wf : topologyContext.getTopology().getWorkflows().values()) {
        wf.setStandard(WorkflowUtils.isStandardWorkflow(wf));
        if (wf.getSteps() != null) {
            for (WorkflowStep step : wf.getSteps().values()) {
                if (step.getOnSuccess() != null) {
                    Iterator<String> followingIds = step.getOnSuccess().iterator();
                    while (followingIds.hasNext()) {
                        String followingId = followingIds.next();
                        WorkflowStep followingStep = wf.getSteps().get(followingId);
                        if (followingStep == null) {
                            followingIds.remove();
                            ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.UNKNWON_WORKFLOW_STEP, null, getSafeNodeStartMark(node), null, getSafeNodeEndMark(node), followingId));
                        } else {
                            followingStep.addPreceding(step.getName());
                        }
                    }
                }
            }
        }
        try {
            WorkflowUtils.fillHostId(wf, topologyContext);
        } catch (NotFoundException e) {
            log.trace("Not found exception during fill host id occurs when a relationship specified in workflow does not exist. This exception is ignored as the workflow validation trigger errors for such situations.", e);
        }
        int errorCount = workflowBuilderService.validateWorkflow(topologyContext, wf);
        if (errorCount > 0) {
            processWorkflowErrors(wf, wf.getErrors(), node);
        }
    }
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) NotFoundException(alien4cloud.exception.NotFoundException)

Example 10 with WorkflowStep

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

the class ToscaParserAlien200Test method parseTopologyTemplateWithRelationshipWorkflow.

@Test
public void parseTopologyTemplateWithRelationshipWorkflow() throws ParsingException {
    Mockito.reset(csarRepositorySearchService);
    Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-ALIEN14")).thenReturn(Mockito.mock(Csar.class));
    NodeType mockCompute = new NodeType();
    mockCompute.setElementId(NormativeComputeConstants.COMPUTE_TYPE);
    mockCompute.setArchiveName("tosca-normative-types");
    mockCompute.setArchiveVersion("1.0.0-ALIEN14");
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(NormativeComputeConstants.COMPUTE_TYPE), Mockito.any(Set.class))).thenReturn(mockCompute);
    RelationshipType mockHostedOn = Mockito.mock(RelationshipType.class);
    Mockito.when(mockHostedOn.getElementId()).thenReturn(NormativeRelationshipConstants.HOSTED_ON);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq(NormativeRelationshipConstants.HOSTED_ON), Mockito.any(Set.class))).thenReturn(mockHostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-topology-template-relationship-workflow.yml"));
    Assert.assertFalse(parsingResult.getResult().getTopology().getWorkflows().isEmpty());
    Assert.assertTrue(parsingResult.getResult().getTopology().getWorkflows().get("install") != null);
    Workflow wf = parsingResult.getResult().getTopology().getWorkflows().get("install");
    WorkflowStep relStep = wf.getSteps().get("OracleJDK_hostedOnComputeHost_pre_configure_source");
    Assert.assertNotNull(relStep);
    Assert.assertTrue(relStep instanceof RelationshipWorkflowStep);
    RelationshipWorkflowStep relationshipWorkflowStep = (RelationshipWorkflowStep) relStep;
    Assert.assertNotNull(relationshipWorkflowStep.getTargetRelationship());
    Assert.assertNotNull(relationshipWorkflowStep.getSourceHostId());
    Assert.assertNotNull(relationshipWorkflowStep.getTargetHostId());
    WorkflowStep nStep = wf.getSteps().get("OracleJDK_start");
    Assert.assertNotNull(nStep);
    Assert.assertTrue(nStep instanceof NodeWorkflowStep);
    NodeWorkflowStep nodeWorkflowStep = (NodeWorkflowStep) nStep;
    Assert.assertNotNull(nodeWorkflowStep.getHostId());
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) Test(org.junit.Test)

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