Search in sources :

Example 1 with EventTimeoutTask

use of org.onosproject.workflow.api.EventTimeoutTask in project onos by opennetworkinglab.

the class WorkFlowEngine method execWorkflowContext.

/**
 * Executes workflow context.
 *
 * @param context workflow context
 * @return workflow context
 */
private WorkflowContext execWorkflowContext(WorkflowContext context) {
    Workflow workflow = workflowStore.get(context.workflowId());
    if (workflow == null) {
        log.error("Invalid workflow {}", context.workflowId());
        return null;
    }
    final WorkflowContext latestContext = workplaceStore.getContext(context.name());
    if (latestContext == null) {
        log.error("Invalid workflow context {}", context.name());
        return null;
    }
    initWorkletExecution(latestContext);
    try {
        final ProgramCounter pc = workflow.next(latestContext);
        final Worklet worklet = workflow.getWorkletInstance(pc);
        if (worklet == Worklet.Common.INIT) {
            log.error("workflow.next gave INIT. It cannot be executed (context: {})", context.name());
            return latestContext;
        }
        latestContext.setCurrent(pc);
        if (worklet == Worklet.Common.COMPLETED) {
            if (workflow.attributes().contains(REMOVE_AFTER_COMPLETE)) {
                workplaceStore.removeContext(latestContext.name());
                return null;
            } else {
                latestContext.setState(WorkflowState.IDLE);
                workplaceStore.commitContext(latestContext.name(), latestContext, false);
                return latestContext;
            }
        }
        log.info("{} worklet.process:{}", latestContext.name(), worklet.tag());
        log.trace("{} context: {}", latestContext.name(), latestContext);
        dataModelInjector.inject(worklet, latestContext);
        WorkletDescription workletDesc = workflow.getWorkletDesc(pc);
        if (Objects.nonNull(workletDesc)) {
            if (!(workletDesc.tag().equals("INIT") || workletDesc.tag().equals("COMPLETED"))) {
                staticDataModelInjector.inject(worklet, workletDesc);
            }
        }
        worklet.process(latestContext);
        dataModelInjector.inhale(worklet, latestContext);
        log.info("{} worklet.process(done): {}", latestContext.name(), worklet.tag());
        log.trace("{} context: {}", latestContext.name(), latestContext);
        if (latestContext.completionEventType() != null) {
            if (latestContext.completionEventGenerator() == null) {
                String msg = String.format("Invalid exepecting event(%s), generator(%s)", latestContext.completionEventType(), latestContext.completionEventGenerator());
                throw new WorkflowException(msg);
            }
            registerEventMap(latestContext.completionEventType(), latestContext.completionEventHints(), latestContext.name(), pc.toString());
            latestContext.completionEventGenerator().apply();
            if (latestContext.completionEventTimeout() != 0L) {
                final EventTimeoutTask eventTimeoutTask = EventTimeoutTask.builder().context(latestContext).programCounter(pc).eventType(latestContext.completionEventType().getName()).eventHintSet(latestContext.completionEventHints()).build();
                timerChain.schedule(latestContext.completionEventTimeout(), () -> {
                    eventtaskAccumulator.add(eventTimeoutTask);
                });
            }
        } else {
            if (latestContext.completionEventTimeout() != 0L) {
                final TimeoutTask timeoutTask = TimeoutTask.builder().context(latestContext).programCounter(pc).build();
                timerChain.schedule(latestContext.completionEventTimeout(), () -> {
                    eventtaskAccumulator.add(timeoutTask);
                });
            } else {
                // completed case
                // increase program counter
                latestContext.setCurrent(workflow.increased(pc));
            }
        }
        workplaceStore.commitContext(latestContext.name(), latestContext, latestContext.triggerNext());
    } catch (WorkflowException e) {
        log.error("Exception: ", e);
        latestContext.setCause(e.getMessage());
        latestContext.setState(WorkflowState.EXCEPTION);
        workplaceStore.commitContext(latestContext.name(), latestContext, false);
    } catch (StorageException e) {
        log.error("Exception: ", e);
    // StorageException does not commit context.
    } catch (Exception e) {
        log.error("Exception: ", e);
        latestContext.setCause(e.getMessage());
        latestContext.setState(WorkflowState.EXCEPTION);
        workplaceStore.commitContext(latestContext.name(), latestContext, false);
    }
    return latestContext;
}
Also used : WorkletDescription(org.onosproject.workflow.api.WorkletDescription) EventTimeoutTask(org.onosproject.workflow.api.EventTimeoutTask) WorkflowContext(org.onosproject.workflow.api.WorkflowContext) SystemWorkflowContext(org.onosproject.workflow.api.SystemWorkflowContext) WorkflowException(org.onosproject.workflow.api.WorkflowException) Workflow(org.onosproject.workflow.api.Workflow) Worklet(org.onosproject.workflow.api.Worklet) ProgramCounter(org.onosproject.workflow.api.ProgramCounter) StorageException(org.onosproject.store.service.StorageException) StorageException(org.onosproject.store.service.StorageException) WorkflowException(org.onosproject.workflow.api.WorkflowException) EventTimeoutTask(org.onosproject.workflow.api.EventTimeoutTask) TimeoutTask(org.onosproject.workflow.api.TimeoutTask)

Aggregations

StorageException (org.onosproject.store.service.StorageException)1 EventTimeoutTask (org.onosproject.workflow.api.EventTimeoutTask)1 ProgramCounter (org.onosproject.workflow.api.ProgramCounter)1 SystemWorkflowContext (org.onosproject.workflow.api.SystemWorkflowContext)1 TimeoutTask (org.onosproject.workflow.api.TimeoutTask)1 Workflow (org.onosproject.workflow.api.Workflow)1 WorkflowContext (org.onosproject.workflow.api.WorkflowContext)1 WorkflowException (org.onosproject.workflow.api.WorkflowException)1 Worklet (org.onosproject.workflow.api.Worklet)1 WorkletDescription (org.onosproject.workflow.api.WorkletDescription)1