Search in sources :

Example 6 with WorkflowContext

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

the class WorkFlowCommand method eval.

/**
 * Evaluates workflow context.
 * @param workflowContextName workflow context name
 */
private void eval(String workflowContextName) {
    WorkplaceStore storService = get(WorkplaceStore.class);
    WorkflowExecutionService execService = get(WorkflowExecutionService.class);
    WorkflowContext context = storService.getContext(workflowContextName);
    if (context == null) {
        error("failed to find workflow context {}", workflowContextName);
        return;
    }
    execService.eval(workflowContextName);
}
Also used : WorkplaceStore(org.onosproject.workflow.api.WorkplaceStore) WorkflowExecutionService(org.onosproject.workflow.api.WorkflowExecutionService) WorkflowContext(org.onosproject.workflow.api.WorkflowContext)

Example 7 with WorkflowContext

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

the class WorkFlowEngine method execEventTask.

/**
 * Executes event task.
 *
 * @param task event task
 * @return event task
 */
private EventTask execEventTask(EventTask task) {
    if (!eventMapStore.isEventMapPresent(task.context().name())) {
        log.trace("EventMap doesnt exist for taskcontext:{}", task.context().name());
        return task;
    }
    log.debug("execEventTask- task: {}, hash: {}", task, stringHash(task.context().distributor()));
    WorkflowContext context = (WorkflowContext) (task.context());
    Workflow workflow = workflowStore.get(context.workflowId());
    if (workflow == null) {
        log.error("Invalid workflow {}", context.workflowId());
        return task;
    }
    WorkflowContext latestContext = workplaceStore.getContext(context.name());
    if (latestContext == null) {
        log.error("Invalid workflow context {}", context.name());
        return task;
    }
    try {
        if (!Objects.equals(latestContext.current(), task.programCounter())) {
            log.error("Current worklet({}) is not mismatched with task work({}). Ignored.", latestContext.current(), task.programCounter());
            return task;
        }
        Worklet worklet = workflow.getWorkletInstance(task.programCounter());
        if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) {
            log.error("Current worklet is {}, Ignored", worklet);
            return task;
        }
        initWorkletExecution(latestContext);
        log.info("{} worklet.isCompleted:{}", latestContext.name(), worklet.tag());
        log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext);
        dataModelInjector.inject(worklet, latestContext);
        boolean completed = worklet.isCompleted(latestContext, task.event());
        dataModelInjector.inhale(worklet, latestContext);
        if (completed) {
            log.info("{} worklet.isCompleted(true):{}", latestContext.name(), worklet.tag());
            log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext);
            eventMapStore.unregisterEventMap(task.eventType(), latestContext.name());
            // completed case
            // increase program counter
            ProgramCounter pc = latestContext.current();
            latestContext.setCurrent(workflow.increased(pc));
            workplaceStore.commitContext(latestContext.name(), latestContext, true);
            return null;
        } else {
            log.info("{} worklet.isCompleted(false):{}", latestContext.name(), worklet.tag());
            log.trace("{} task:{}, context: {}", latestContext.name(), task, latestContext);
            workplaceStore.commitContext(latestContext.name(), latestContext, false);
        }
    } 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 : 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)

Example 8 with WorkflowContext

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

the class WorkFlowEngine method eventMapTrigger.

@Override
public void eventMapTrigger(Event event, EventHintSupplier supplier) {
    if (event.subject() instanceof SystemWorkflowContext) {
        return;
    }
    Map<String, String> eventMap;
    String eventHint;
    try {
        eventHint = supplier.apply(event);
    } catch (Throwable e) {
        log.error("Exception: ", e);
        return;
    }
    if (eventHint == null) {
        // do nothing
        log.error("Invalid eventHint, event: {}", event);
        return;
    }
    try {
        eventMap = eventMapStore.getEventMapByHint(event.getClass().getName(), eventHint);
    } catch (WorkflowException e) {
        log.error("Exception: ", e);
        return;
    }
    if (Objects.isNull(eventMap) || eventMap.isEmpty()) {
        // do nothing;
        log.debug("Invalid eventMap, event: {}", event);
        return;
    }
    for (Map.Entry<String, String> entry : eventMap.entrySet()) {
        String contextName = entry.getKey();
        String strProgramCounter = entry.getValue();
        ProgramCounter pc;
        try {
            pc = ProgramCounter.valueOf(strProgramCounter);
        } catch (IllegalArgumentException e) {
            log.error("Exception: ", e);
            return;
        }
        WorkflowContext context = workplaceStore.getContext(contextName);
        if (Objects.isNull(context)) {
            log.info("Invalid context: {}, event: {}", contextName, event);
            continue;
        }
        EventTask eventtask = null;
        try {
            eventtask = EventTask.builder().event(event).eventHint(eventHint).context(context).programCounter(pc).build();
        } catch (WorkflowException e) {
            log.error("Exception: ", e);
        }
        log.debug("eventtaskAccumulator.add: task: {}", eventtask);
        if (!Objects.isNull(eventtask)) {
            eventtaskAccumulator.add(eventtask);
        }
    }
}
Also used : SystemWorkflowContext(org.onosproject.workflow.api.SystemWorkflowContext) WorkflowException(org.onosproject.workflow.api.WorkflowException) WorkflowContext(org.onosproject.workflow.api.WorkflowContext) SystemWorkflowContext(org.onosproject.workflow.api.SystemWorkflowContext) ProgramCounter(org.onosproject.workflow.api.ProgramCounter) Map(java.util.Map) EventTask(org.onosproject.workflow.api.EventTask)

Example 9 with WorkflowContext

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

the class WorkflowManager method invokeWorkflow.

@Override
public void invokeWorkflow(JsonNode worklowDescJson) throws WorkflowException {
    log.info("invokeWorkflow: {}", worklowDescJson);
    Workplace workplace = workplaceStore.getWorkplace(Workplace.SYSTEM_WORKPLACE);
    if (Objects.isNull(workplace)) {
        throw new WorkflowException("Invalid system workplace");
    }
    Workflow workflow = workflowStore.get(URI.create(worklowDescJson.get("id").asText()));
    if (Objects.isNull(workflow)) {
        throw new WorkflowException("Invalid Workflow");
    }
    checkWorkflowDataModelSchema(workflow, worklowDescJson);
    Workflow wfCreationWf = workflowStore.get(URI.create(WorkplaceWorkflow.WF_CREATE_WORKFLOW));
    if (Objects.isNull(wfCreationWf)) {
        throw new WorkflowException("Invalid workflow " + WorkplaceWorkflow.WF_CREATE_WORKFLOW);
    }
    WorkflowContext context = wfCreationWf.buildSystemContext(workplace, new JsonDataModelTree(worklowDescJson));
    workflowExecutionService.execInitWorklet(context);
}
Also used : WorkflowException(org.onosproject.workflow.api.WorkflowException) WorkflowContext(org.onosproject.workflow.api.WorkflowContext) JsonDataModelTree(org.onosproject.workflow.api.JsonDataModelTree) Workflow(org.onosproject.workflow.api.Workflow) Workplace(org.onosproject.workflow.api.Workplace) DefaultWorkplace(org.onosproject.workflow.api.DefaultWorkplace)

Aggregations

WorkflowContext (org.onosproject.workflow.api.WorkflowContext)9 WorkflowException (org.onosproject.workflow.api.WorkflowException)7 SystemWorkflowContext (org.onosproject.workflow.api.SystemWorkflowContext)6 Workflow (org.onosproject.workflow.api.Workflow)6 StorageException (org.onosproject.store.service.StorageException)4 Worklet (org.onosproject.workflow.api.Worklet)4 ProgramCounter (org.onosproject.workflow.api.ProgramCounter)3 WorkletDescription (org.onosproject.workflow.api.WorkletDescription)3 JsonDataModelTree (org.onosproject.workflow.api.JsonDataModelTree)2 WorkplaceStore (org.onosproject.workflow.api.WorkplaceStore)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Map (java.util.Map)1 DefaultWorkplace (org.onosproject.workflow.api.DefaultWorkplace)1 EventTask (org.onosproject.workflow.api.EventTask)1 EventTimeoutTask (org.onosproject.workflow.api.EventTimeoutTask)1 TimeoutTask (org.onosproject.workflow.api.TimeoutTask)1 WorkflowExecutionService (org.onosproject.workflow.api.WorkflowExecutionService)1 WorkflowStore (org.onosproject.workflow.api.WorkflowStore)1 Workplace (org.onosproject.workflow.api.Workplace)1