Search in sources :

Example 1 with Worklet

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

the class WorkFlowEngine method execEventTimeoutTask.

/**
 * Executes event timeout task.
 *
 * @param task event timeout task
 * @return handler task
 */
private HandlerTask execEventTimeoutTask(EventTimeoutTask task) {
    if (!eventMapStore.isEventMapPresent(task.context().name())) {
        log.trace("EventMap doesnt exist for taskcontext:{}", task.context().name());
        return task;
    }
    log.debug("execEventTimeoutTask- task: {}, hash: {}", task, stringHash(task.context().distributor()));
    WorkflowContext context = (WorkflowContext) (task.context());
    Workflow workflow = workflowStore.get(context.workflowId());
    if (workflow == null) {
        log.error("execEventTimeoutTask: Invalid workflow {}", context.workflowId());
        return task;
    }
    WorkflowContext latestContext = workplaceStore.getContext(context.name());
    if (latestContext == null) {
        log.error("execEventTimeoutTask: Invalid workflow context {}", context.name());
        return task;
    }
    try {
        if (!Objects.equals(latestContext.current(), task.programCounter())) {
            log.error("execEventTimeoutTask: Current worklet({}) is not mismatched with task work({}). Ignored.", latestContext.current(), task.programCounter());
            return task;
        }
        Worklet worklet = workflow.getWorkletInstance(task.programCounter());
        if (worklet == Worklet.Common.COMPLETED || worklet == Worklet.Common.INIT) {
            log.error("execEventTimeoutTask: Current worklet is {}, Ignored", worklet);
            return task;
        }
        initWorkletExecution(latestContext);
        eventMapStore.unregisterEventMap(task.eventType(), latestContext.name());
        log.info("{} worklet.timeout(for event):{}", latestContext.name(), worklet.tag());
        log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext);
        dataModelInjector.inject(worklet, latestContext);
        WorkletDescription workletDesc = workflow.getWorkletDesc(task.programCounter());
        if (Objects.nonNull(workletDesc)) {
            if (!(workletDesc.tag().equals("INIT") || workletDesc.tag().equals("COMPLETED"))) {
                staticDataModelInjector.inject(worklet, workletDesc);
            }
        }
        worklet.timeout(latestContext);
        dataModelInjector.inhale(worklet, latestContext);
        log.info("{} worklet.timeout(for event)(done):{}", latestContext.name(), worklet.tag());
        log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext);
        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 task;
}
Also used : WorkletDescription(org.onosproject.workflow.api.WorkletDescription) 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) StorageException(org.onosproject.store.service.StorageException) StorageException(org.onosproject.store.service.StorageException) WorkflowException(org.onosproject.workflow.api.WorkflowException)

Example 2 with Worklet

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

the class WorkFlowEngine method post.

@Override
protected void post(WorkflowDataEvent event) {
    if (event.subject() == null || !isRelevant(event.subject())) {
        log.debug("ignore event {}", event);
        return;
    }
    // trigger next worklet selection
    WorkflowData dataModelContainer = event.subject();
    switch(event.type()) {
        case INSERT:
        case UPDATE:
            if (dataModelContainer.triggerNext()) {
                log.debug("workflowAccumulator.add: {}", dataModelContainer);
                workflowAccumulator.add(dataModelContainer);
            } else {
                log.debug("pass-workflowAccumulator.add: {}", dataModelContainer);
            }
            break;
        case REMOVE:
            break;
        default:
    }
    // trigger EventTask for WorkflowDataEvent
    eventMapTriggerExecutor.submit(() -> eventMapTrigger(event, // event hint supplier
    (ev) -> {
        if (ev == null || ev.subject() == null) {
            return null;
        }
        if (ev.subject() instanceof WorkflowData) {
            return ((WorkflowData) ev.subject()).name();
        } else {
            return null;
        }
    }));
}
Also used : WorkletDescription(org.onosproject.workflow.api.WorkletDescription) CoreService(org.onosproject.core.CoreService) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) WorkflowStore(org.onosproject.workflow.api.WorkflowStore) Workflow(org.onosproject.workflow.api.Workflow) Workplace(org.onosproject.workflow.api.Workplace) EventHintSupplier(org.onosproject.workflow.api.EventHintSupplier) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) Map(java.util.Map) ApplicationId(org.onosproject.core.ApplicationId) ProgramCounter(org.onosproject.workflow.api.ProgramCounter) WorkPartitionService(org.onosproject.net.intent.WorkPartitionService) Event(org.onosproject.event.Event) NodeId(org.onosproject.cluster.NodeId) REMOVE_AFTER_COMPLETE(org.onosproject.workflow.api.WorkflowAttribute.REMOVE_AFTER_COMPLETE) DefaultWorkplace(org.onosproject.workflow.api.DefaultWorkplace) AbstractListenerManager(org.onosproject.event.AbstractListenerManager) Deactivate(org.osgi.service.component.annotations.Deactivate) Collection(java.util.Collection) Set(java.util.Set) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) WorkflowData(org.onosproject.workflow.api.WorkflowData) HandlerTask(org.onosproject.workflow.api.HandlerTask) List(java.util.List) EventTimeoutTask(org.onosproject.workflow.api.EventTimeoutTask) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) ClusterService(org.onosproject.cluster.ClusterService) JsonDataModelTree(org.onosproject.workflow.api.JsonDataModelTree) JsonDataModelInjector(org.onosproject.workflow.api.JsonDataModelInjector) WorkflowState(org.onosproject.workflow.api.WorkflowState) HandlerTaskBatchDelegate(org.onosproject.workflow.api.HandlerTaskBatchDelegate) StorageException(org.onosproject.store.service.StorageException) WorkflowException(org.onosproject.workflow.api.WorkflowException) EventTask(org.onosproject.workflow.api.EventTask) WorkplaceStoreDelegate(org.onosproject.workflow.api.WorkplaceStoreDelegate) CompletableFuture(java.util.concurrent.CompletableFuture) WorkflowExecutionService(org.onosproject.workflow.api.WorkflowExecutionService) Worklet(org.onosproject.workflow.api.Worklet) Component(org.osgi.service.component.annotations.Component) WorkflowDataListener(org.onosproject.workflow.api.WorkflowDataListener) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Activate(org.osgi.service.component.annotations.Activate) TimeoutTask(org.onosproject.workflow.api.TimeoutTask) ContextEventMapStore(org.onosproject.workflow.api.ContextEventMapStore) ExecutorService(java.util.concurrent.ExecutorService) StaticDataModelInjector(org.onosproject.workflow.api.StaticDataModelInjector) Logger(org.slf4j.Logger) WorkflowContext(org.onosproject.workflow.api.WorkflowContext) WorkflowBatchDelegate(org.onosproject.workflow.api.WorkflowBatchDelegate) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) WorkflowDataEvent(org.onosproject.workflow.api.WorkflowDataEvent) SystemWorkflowContext(org.onosproject.workflow.api.SystemWorkflowContext) WorkplaceStore(org.onosproject.workflow.api.WorkplaceStore) TimerChain(org.onosproject.workflow.api.TimerChain) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) CheckCondition.check(org.onosproject.workflow.api.CheckCondition.check) Reference(org.osgi.service.component.annotations.Reference) LeadershipService(org.onosproject.cluster.LeadershipService) WorkflowData(org.onosproject.workflow.api.WorkflowData)

Example 3 with Worklet

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

the class WorkFlowEngine method execInitWorklet.

@Override
public void execInitWorklet(WorkflowContext context) {
    Workflow workflow = workflowStore.get(context.workflowId());
    if (workflow == null) {
        log.error("Invalid workflow id:{}", context.workflowId());
        return;
    }
    initWorkletExecution(context);
    try {
        Worklet initWorklet = workflow.init(context);
        if (initWorklet != null) {
            log.info("{} worklet.process:{}", context.name(), initWorklet.tag());
            log.trace("{} context: {}", context.name(), context);
            dataModelInjector.inject(initWorklet, context);
            initWorklet.process(context);
            dataModelInjector.inhale(initWorklet, context);
            log.info("{} worklet.process(done): {}", context.name(), initWorklet.tag());
            log.trace("{} context: {}", context.name(), context);
        }
        check(workplaceStore.getContext(context.name()) == null, "Duplicated workflow context(" + context.name() + ") assignment.");
    } catch (WorkflowException e) {
        log.error("Exception: ", e);
        context.setCause(e.getMessage());
        context.setState(WorkflowState.EXCEPTION);
        workplaceStore.commitContext(context.name(), context, false);
        return;
    }
    // trigger the execution of next worklet.
    workplaceStore.registerContext(context.name(), context);
}
Also used : WorkflowException(org.onosproject.workflow.api.WorkflowException) Workflow(org.onosproject.workflow.api.Workflow) Worklet(org.onosproject.workflow.api.Worklet)

Example 4 with Worklet

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

the class WorkFlowEngine method execTimeoutTask.

/**
 * Executes timeout task.
 *
 * @param task time out task
 * @return handler task
 */
private HandlerTask execTimeoutTask(TimeoutTask task) {
    log.debug("execTimeoutTask- task: {}, hash: {}", task, stringHash(task.context().distributor()));
    WorkflowContext context = (WorkflowContext) (task.context());
    Workflow workflow = workflowStore.get(context.workflowId());
    if (workflow == null) {
        log.error("execTimeoutTask: Invalid workflow {}", context.workflowId());
        return task;
    }
    WorkflowContext latestContext = workplaceStore.getContext(context.name());
    if (latestContext == null) {
        log.error("execTimeoutTask: Invalid workflow context {}", context.name());
        return task;
    }
    try {
        if (!Objects.equals(latestContext.current(), task.programCounter())) {
            log.error("execTimeoutTask: Current worklet({}) is not mismatched with task work({}). Ignored.", latestContext.current(), task.programCounter());
            return task;
        }
        Worklet worklet = workflow.getWorkletInstance(task.programCounter());
        if (worklet == Worklet.Common.COMPLETED || worklet == Worklet.Common.INIT) {
            log.error("execTimeoutTask: Current worklet is {}, Ignored", worklet);
            return task;
        }
        initWorkletExecution(latestContext);
        log.info("{} worklet.timeout:{}", latestContext.name(), worklet.tag());
        log.trace("{} context: {}", latestContext.name(), latestContext);
        dataModelInjector.inject(worklet, latestContext);
        WorkletDescription workletDesc = workflow.getWorkletDesc(task.programCounter());
        if (Objects.nonNull(workletDesc)) {
            if (!(workletDesc.tag().equals("INIT") || workletDesc.tag().equals("COMPLETED"))) {
                staticDataModelInjector.inject(worklet, workletDesc);
            }
        }
        worklet.timeout(latestContext);
        dataModelInjector.inhale(worklet, latestContext);
        log.info("{} worklet.timeout(done):{}", latestContext.name(), worklet.tag());
        log.trace("{} context: {}", latestContext.name(), latestContext);
        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 task;
}
Also used : WorkletDescription(org.onosproject.workflow.api.WorkletDescription) 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) StorageException(org.onosproject.store.service.StorageException) StorageException(org.onosproject.store.service.StorageException) WorkflowException(org.onosproject.workflow.api.WorkflowException)

Example 5 with Worklet

use of org.onosproject.workflow.api.Worklet 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

WorkflowException (org.onosproject.workflow.api.WorkflowException)8 Worklet (org.onosproject.workflow.api.Worklet)8 Workflow (org.onosproject.workflow.api.Workflow)6 StorageException (org.onosproject.store.service.StorageException)5 ProgramCounter (org.onosproject.workflow.api.ProgramCounter)5 SystemWorkflowContext (org.onosproject.workflow.api.SystemWorkflowContext)5 WorkflowContext (org.onosproject.workflow.api.WorkflowContext)5 WorkletDescription (org.onosproject.workflow.api.WorkletDescription)4 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 EventTimeoutTask (org.onosproject.workflow.api.EventTimeoutTask)2 TimeoutTask (org.onosproject.workflow.api.TimeoutTask)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 MissingNode (com.fasterxml.jackson.databind.node.MissingNode)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1