[ONOS-7732] Automating switch workflow: api, app, and sample workflows
Change-Id: Iee87d4fe6cf61c1f8904d1d77df5f913a712b64a
diff --git a/apps/workflow/BUCK b/apps/workflow/BUCK
new file mode 100644
index 0000000..d7e5d1b
--- /dev/null
+++ b/apps/workflow/BUCK
@@ -0,0 +1,12 @@
+BUNDLES = [
+ '//apps/workflow/api:onos-apps-workflow-api',
+ '//apps/workflow/app:onos-apps-workflow-app',
+]
+
+onos_app (
+ category = 'Utility',
+ description = "Workflow application",
+ included_bundles = BUNDLES,
+ title = 'Workflow',
+ url = 'http://onosproject.org',
+)
diff --git a/apps/workflow/BUILD b/apps/workflow/BUILD
new file mode 100644
index 0000000..118458e
--- /dev/null
+++ b/apps/workflow/BUILD
@@ -0,0 +1,12 @@
+BUNDLES = [
+ "//apps/workflow/api:onos-apps-workflow-api",
+ "//apps/workflow/app:onos-apps-workflow-app",
+]
+
+onos_app(
+ category = "Utility",
+ description = "Workflow application",
+ included_bundles = BUNDLES,
+ title = "Workflow",
+ url = "http://onosproject.org",
+)
diff --git a/apps/workflow/api/BUCK b/apps/workflow/api/BUCK
new file mode 100644
index 0000000..f9461af
--- /dev/null
+++ b/apps/workflow/api/BUCK
@@ -0,0 +1,11 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+ '//lib:jackson-core',
+ '//lib:jackson-annotations',
+ '//lib:jackson-databind',
+ '//core/store/serializers:onos-core-serializers',
+]
+
+osgi_jar_with_tests (
+ deps = COMPILE_DEPS,
+)
diff --git a/apps/workflow/api/BUILD b/apps/workflow/api/BUILD
new file mode 100644
index 0000000..721e4b9
--- /dev/null
+++ b/apps/workflow/api/BUILD
@@ -0,0 +1,7 @@
+COMPILE_DEPS = CORE_DEPS + KRYO + JACKSON + [
+ "//core/store/serializers:onos-core-serializers",
+]
+
+osgi_jar_with_tests(
+ deps = COMPILE_DEPS,
+)
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/AbstractWorkflow.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/AbstractWorkflow.java
new file mode 100644
index 0000000..8a5f2b8
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/AbstractWorkflow.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import java.net.URI;
+
+/**
+ * Abstract class for workflow.
+ */
+public abstract class AbstractWorkflow implements Workflow {
+
+ /**
+ * ID of workflow.
+ */
+ private URI id;
+
+ /**
+ * Constructor for AbstractWorkflow.
+ * @param id ID of workflow
+ */
+ protected AbstractWorkflow(URI id) {
+ this.id = id;
+ }
+
+ @Override
+ public URI id() {
+ return id;
+ }
+
+ @Override
+ public WorkflowContext buildContext(Workplace workplace, DataModelTree data) throws WorkflowException {
+ return new DefaultWorkflowContext(id, workplace.name(), data);
+ }
+
+ @Override
+ public WorkflowContext buildSystemContext(Workplace workplace, DataModelTree data) throws WorkflowException {
+ return new SystemWorkflowContext(id, workplace.name(), data);
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/AbstractWorklet.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/AbstractWorklet.java
new file mode 100644
index 0000000..b9fb133
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/AbstractWorklet.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+
+import org.onosproject.event.Event;
+
+/**
+ * Abstract class for worklet.
+ */
+public abstract class AbstractWorklet implements Worklet {
+
+ @Override
+ public String tag() {
+ return this.getClass().getName();
+ }
+
+ @Override
+ public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isCompleted should not be called");
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isNext should not be called");
+ }
+
+ @Override
+ public void timeout(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("Timeout happened");
+ }
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/BranchWorklet.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/BranchWorklet.java
new file mode 100644
index 0000000..4f91bba
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/BranchWorklet.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+/**
+ * An interface representing branch worklet. Branch worklet is used for branching workflow execution.
+ */
+public interface BranchWorklet extends Worklet {
+
+ @Override
+ default void process(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("This workletType.process should not be called");
+ }
+
+ @Override
+ default boolean isNext(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("This workletType.isNext should not be called");
+ }
+
+ /**
+ * Returns next worklet class for branching.
+ * @param context workflow context
+ * @return next worklet class
+ * @throws WorkflowException workflow exception
+ */
+ Class<? extends Worklet> next(WorkflowContext context) throws WorkflowException;
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ContextEventMapStore.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ContextEventMapStore.java
new file mode 100644
index 0000000..c26b3be
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ContextEventMapStore.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.store.service.DocumentPath;
+import org.onosproject.store.service.Versioned;
+
+import java.util.Map;
+
+/**
+ * WorkflowContext Event Map Store.
+ */
+public interface ContextEventMapStore {
+
+ /**
+ * Registers workflow context event mapping.
+ * @param eventType the class name of event
+ * @param eventHint event hint string value of the event
+ * @param contextName workflow context name
+ * @param workletType the class name of worklet
+ * @throws WorkflowException workflow exception
+ */
+ void registerEventMap(String eventType, String eventHint,
+ String contextName, String workletType) throws WorkflowException;
+
+ /**
+ * Unregisters workflow context event mapping.
+ * @param eventType the class name of event
+ * @param eventHint event hint string value of the event
+ * @param contextName workflow context name
+ * @throws WorkflowException workflow exception
+ */
+ void unregisterEventMap(String eventType, String eventHint,
+ String contextName) throws WorkflowException;
+
+ /**
+ * Returns workflow context event mapping.
+ * @param eventType the class name of event
+ * @param eventHint event hint string value of the event
+ * @return workflow context event mapping
+ * @throws WorkflowException workflow exception
+ */
+ Map<String, String> getEventMap(String eventType, String eventHint) throws WorkflowException;
+
+ /**
+ * Returns child nodes on document tree path.
+ * @param path document tree path
+ * @return children under document tree path
+ * @throws WorkflowException workflow exception
+ */
+ Map<String, Versioned<String>> getChildren(String path) throws WorkflowException;
+
+ /**
+ * Returns document path.
+ * @param path document path string
+ * @return document tree
+ * @throws WorkflowException workflow exception
+ */
+ DocumentPath getDocumentPath(String path) throws WorkflowException;
+
+ /**
+ * Transforms document tree to json tree.
+ * @return json tree
+ * @throws WorkflowException workflow exception
+ */
+ ObjectNode asJsonTree() throws WorkflowException;
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DataModelPointer.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DataModelPointer.java
new file mode 100644
index 0000000..271ce4f
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DataModelPointer.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+/**
+ * Common data model tree path.
+ */
+public interface DataModelPointer {
+
+ /**
+ * Workplace array pointer.
+ */
+ String WORKPLACES_PTR = "/workplaces";
+
+ /**
+ * Workplace name pointer.
+ */
+ String WORKPLACE_NAME_PTR = "/name";
+
+ /**
+ * Workplace data pointer.
+ */
+ String WORKPLACE_DATA_PTR = "/data";
+
+ /**
+ * Workplace workflow pointer.
+ */
+ String WORKPLACE_WORKFLOWS_PTR = "/workflows";
+
+ /**
+ * Workflow op pointer.
+ */
+ String WORKFLOW_OP_PTR = "/op";
+
+ /**
+ * Workflow id pointer.
+ */
+ String WORKFLOW_ID_PTR = "/id";
+
+ /**
+ * Workflow data pointer.
+ */
+ String WORKFLOW_DATA_PTR = "/data";
+
+ /**
+ * Gets path string.
+ * @return path string
+ */
+ String getPath();
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DataModelTree.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DataModelTree.java
new file mode 100644
index 0000000..3236164
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DataModelTree.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+/**
+ * Interface for data model tree.
+ */
+public interface DataModelTree {
+
+ /**
+ * Data model tree node type (map or array).
+ */
+ enum Nodetype {
+
+ /**
+ * Map type data model tree node.
+ */
+ MAP,
+
+ /**
+ * Array type data model tree node.
+ */
+ ARRAY
+ }
+
+ /**
+ * Returns subtree on the path.
+ * @param path data model tree path
+ * @return subtree on the path
+ */
+ DataModelTree subtree(String path);
+
+ /**
+ * Attaches subtree on the path.
+ * @param path data model tree path where subtree will be attached
+ * @param tree subtree to be attached
+ * @throws WorkflowException workflow exception
+ */
+ void attach(String path, DataModelTree tree) throws WorkflowException;
+
+ /**
+ * Allocates leaf node on the path.
+ * @param path data model tree path where new leaf node will be allocated
+ * @param leaftype leaf node type
+ * @return data model tree
+ * @throws WorkflowException workflow exception
+ */
+ DataModelTree alloc(String path, Nodetype leaftype) throws WorkflowException;
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultRpcDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultRpcDescription.java
new file mode 100644
index 0000000..4a4715e
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultRpcDescription.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.MissingNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.base.MoreObjects;
+
+/**
+ * Class of workflow RPC description.
+ */
+public final class DefaultRpcDescription implements RpcDescription {
+
+ /**
+ * Workflow RPC operation.
+ */
+ private final String op;
+
+ /**
+ * Parameters.
+ */
+ private final JsonNode params;
+
+ /**
+ * Invocation ID.
+ */
+ private final String id;
+
+ /**
+ * Constructor of workplace description.
+ * @param builder workplace builder
+ */
+ private DefaultRpcDescription(Builder builder) {
+ this.op = builder.op;
+ this.params = builder.params;
+ this.id = builder.id;
+ }
+
+ @Override
+ public String op() {
+ return this.op;
+ }
+
+ @Override
+ public JsonNode params() {
+ return this.params;
+ }
+
+ @Override
+ public String id() {
+ return this.id;
+ }
+
+ /**
+ * Creating workflow RPC description from json tree.
+ * @param root root node for workflow RPC description
+ * @return workflow RPC description
+ * @throws WorkflowException workflow exception
+ */
+ public static DefaultRpcDescription valueOf(JsonNode root) throws WorkflowException {
+
+ JsonNode node = root.at(RPC_OP_PTR);
+ if (!(node instanceof TextNode)) {
+ throw new WorkflowException("invalid RPC operation for " + root);
+ }
+ String rpcOp = node.asText();
+
+ node = root.at(RPC_PARAMS_PTR);
+ if (node instanceof MissingNode) {
+ throw new WorkflowException("invalid RPC parameters for " + root);
+ }
+ JsonNode rpcParams = node;
+
+ node = root.at(RPC_ID_PTR);
+ if (!(node instanceof TextNode)) {
+ throw new WorkflowException("invalid RPC invocation ID for " + root);
+ }
+ String rpcId = node.asText();
+
+
+ return builder()
+ .setOp(rpcOp)
+ .setParams(rpcParams)
+ .setId(rpcId)
+ .build();
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("op", op())
+ .add("params", params())
+ .add("id", id())
+ .toString();
+ }
+
+ /**
+ * Gets builder instance.
+ * @return builder instance
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder for workplace RPC description.
+ */
+ public static class Builder {
+
+ /**
+ * Workflow RPC operation.
+ */
+ private String op;
+
+ /**
+ * Parameters.
+ */
+ private JsonNode params;
+
+ /**
+ * Invocation ID.
+ */
+ private String id;
+
+ /**
+ * Sets workflow RPC operation.
+ * @param op workflow RPC operation
+ * @return builder
+ */
+ public Builder setOp(String op) {
+ this.op = op;
+ return this;
+ }
+
+ /**
+ * Sets workflow RPC parameters.
+ * @param params workflow RPC parameters
+ * @return builder
+ */
+ public Builder setParams(JsonNode params) {
+ this.params = params;
+ return this;
+ }
+
+ /**
+ * Sets workflow RPC invocation ID.
+ * @param id workflow invocation ID
+ * @return builder
+ */
+ public Builder setId(String id) {
+ this.id = id;
+ return this;
+ }
+
+ /**
+ * Builds workplace RPC description from builder.
+ * @return instance of workflow RPC description
+ */
+ public DefaultRpcDescription build() {
+ return new DefaultRpcDescription(this);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkflowContext.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkflowContext.java
new file mode 100644
index 0000000..e16376f
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkflowContext.java
@@ -0,0 +1,242 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+import org.onosproject.event.Event;
+
+import java.net.URI;
+
+/**
+ * Default implementation of WorkflowContext.
+ */
+public class DefaultWorkflowContext extends WorkflowContext {
+
+ /**
+ * ID of workflow.
+ */
+ private URI workflowId;
+
+ /**
+ * Workplace name of the workflow.
+ */
+ private String workplaceName;
+
+ /**
+ * State of workflow.
+ */
+ private WorkflowState state;
+
+ /**
+ * Current worklet of the workflow.
+ */
+ private String current;
+
+ /**
+ * Cause of workflow exception.
+ */
+ private String cause;
+
+ /**
+ * Completion event type.
+ */
+ private transient Class<? extends Event> completionEventType;
+
+ /**
+ * Completion event hint.
+ */
+ private transient String completionEventHint;
+
+ /**
+ * Completion event generator method reference.
+ */
+ private transient WorkExecutor completionEventGenerator;
+
+ /**
+ * Completion event timeout milliseconds.
+ */
+ private transient long completionEventTimeoutMs;
+
+ /**
+ * Service reference for workflow service.
+ */
+ private transient WorkflowExecutionService workflowExecutionService;
+
+ /**
+ * Service reference for workflow store.
+ */
+ private transient WorkflowStore workflowStore;
+
+ /**
+ * Service reference for workplace store.
+ */
+ private transient WorkplaceStore workplaceStore;
+
+ /**
+ * Constructor of DefaultWorkflowContext.
+ * @param workflowId ID of workflow
+ * @param workplaceName name of workplace
+ * @param data data model tree
+ */
+ public DefaultWorkflowContext(URI workflowId, String workplaceName, DataModelTree data) {
+ super(data);
+ this.workflowId = workflowId;
+ this.workplaceName = workplaceName;
+ this.state = WorkflowState.IDLE;
+ this.current = Worklet.Common.INIT.name();
+ }
+
+ /**
+ * DefaultWorkflowContext name builder.
+ * @param workflowid workflow id
+ * @param workplacename workplace name
+ * @return DefaultWorkflowContext name
+ */
+ public static String nameBuilder(URI workflowid, String workplacename) {
+ return workflowid.toString() + "@" + workplacename;
+ }
+
+ @Override
+ public String name() {
+ return nameBuilder(workflowId, workplaceName);
+ }
+
+ @Override
+ public String distributor() {
+ return workplaceName();
+ }
+
+ @Override
+ public URI workflowId() {
+ return this.workflowId;
+ }
+
+ @Override
+ public String workplaceName() {
+ return workplaceName;
+ }
+
+ @Override
+ public WorkflowState state() {
+ return state;
+ }
+
+ @Override
+ public void setState(WorkflowState state) {
+ this.state = state;
+ }
+
+ @Override
+ public String current() {
+ return this.current;
+ }
+
+ @Override
+ public void setCurrent(Worklet worklet) {
+ this.current = worklet.tag();
+ }
+
+ @Override
+ public String cause() {
+ return this.cause;
+ }
+
+ @Override
+ public void setCause(String cause) {
+ this.cause = cause;
+ }
+
+ @Override
+ public void completed() {
+ setTriggerNext(true);
+ }
+
+ @Override
+ public void waitCompletion(Class<? extends Event> eventType, String eventHint,
+ WorkExecutor eventGenerator, long timeoutMs) {
+ this.completionEventType = eventType;
+ this.completionEventHint = eventHint;
+ this.completionEventGenerator = eventGenerator;
+ this.completionEventTimeoutMs = timeoutMs;
+ }
+
+ @Override
+ public void waitFor(long timeoutMs) {
+ this.completionEventTimeoutMs = timeoutMs;
+ }
+
+ @Override
+ public Class<? extends Event> completionEventType() {
+ return completionEventType;
+ }
+
+ @Override
+ public String completionEventHint() {
+ return completionEventHint;
+ }
+
+ @Override
+ public WorkExecutor completionEventGenerator() {
+ return completionEventGenerator;
+ }
+
+ @Override
+ public long completionEventTimeout() {
+ return completionEventTimeoutMs;
+ }
+
+ @Override
+ public void setWorkflowExecutionService(WorkflowExecutionService workflowExecutionService) {
+ this.workflowExecutionService = workflowExecutionService;
+ }
+
+ @Override
+ public WorkflowExecutionService workflowService() {
+ return workflowExecutionService;
+ }
+
+ @Override
+ public void setWorkflowStore(WorkflowStore workflowStore) {
+ this.workflowStore = workflowStore;
+ }
+
+ @Override
+ public WorkflowStore workflowStore() {
+ return workflowStore;
+ }
+
+ @Override
+ public void setWorkplaceStore(WorkplaceStore workplaceStore) {
+ this.workplaceStore = workplaceStore;
+ }
+
+ @Override
+ public WorkplaceStore workplaceStore() {
+ return workplaceStore;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("name", name())
+ .add("triggernext", triggerNext())
+ .add("data", data())
+ .add("current", current)
+ .add("state", state())
+ .add("cause", cause())
+ .toString();
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkflowDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkflowDescription.java
new file mode 100644
index 0000000..c9ded60
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkflowDescription.java
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.MissingNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.base.MoreObjects;
+
+import java.net.URI;
+
+
+/**
+ * Class for default workflow description.
+ */
+public final class DefaultWorkflowDescription implements WorkflowDescription {
+
+ /**
+ * Workplace Name.
+ */
+ private String workplaceName;
+
+ /**
+ * Workflow ID.
+ */
+ private URI id;
+
+ /**
+ * Workflow data model.
+ */
+ private JsonNode data;
+
+ /**
+ * Constructor of workflow description.
+ * @param builder workflow description builder
+ */
+ private DefaultWorkflowDescription(Builder builder) {
+ this.workplaceName = builder.workplaceName;
+ this.id = builder.id;
+ this.data = builder.data;
+ }
+
+ @Override
+ public String workplaceName() {
+ return this.workplaceName;
+ }
+
+ @Override
+ public URI id() {
+ return this.id;
+ }
+
+ @Override
+ public String workflowContextName() {
+ return DefaultWorkflowContext.nameBuilder(id(), workplaceName());
+ }
+
+ @Override
+ public JsonNode data() {
+ return this.data;
+ }
+
+ /**
+ * Creating workflow description from json tree.
+ * @param root root node for workflow description
+ * @return workflow description
+ * @throws WorkflowException workflow exception
+ */
+ public static DefaultWorkflowDescription valueOf(JsonNode root) throws WorkflowException {
+
+ JsonNode node = root.at(ptr(WF_WORKPLACE));
+ if (!(node instanceof TextNode)) {
+ throw new WorkflowException("invalid workflow workplace for " + root);
+ }
+ String wfWorkplaceName = node.asText();
+
+ node = root.at(ptr(WF_ID));
+ if (!(node instanceof TextNode)) {
+ throw new WorkflowException("invalid workflow id for " + root);
+ }
+ URI wfId = URI.create(node.asText());
+
+ node = root.at(ptr(WF_DATA));
+ if (node instanceof MissingNode) {
+ throw new WorkflowException("invalid workflow data for " + root);
+ }
+ JsonNode wfData = node;
+
+ return builder()
+ .workplaceName(wfWorkplaceName)
+ .id(wfId)
+ .data(wfData)
+ .build();
+ }
+
+ private static String ptr(String field) {
+ return "/" + field;
+ }
+
+ @Override
+ public JsonNode toJson() {
+ ObjectNode root = JsonNodeFactory.instance.objectNode();
+ root.put(WF_WORKPLACE, workplaceName());
+ root.put(WF_ID, id().toString());
+ root.put(WF_DATA, data());
+ return root;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("workplace", workplaceName())
+ .add("id", id())
+ .add("data", data())
+ .toString();
+ }
+
+ /**
+ * Gets builder instance.
+ * @return builder instance
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder for workflow description.
+ */
+ public static class Builder {
+
+ /**
+ * Workplace name.
+ */
+ private String workplaceName;
+
+ /**
+ * Workflow ID.
+ */
+ private URI id;
+
+ /**
+ * Workflow data model.
+ */
+ private JsonNode data;
+
+ /**
+ * Sets workplace name.
+ * @param workplaceName workplace name
+ * @return builder
+ */
+ public Builder workplaceName(String workplaceName) {
+ this.workplaceName = workplaceName;
+ return this;
+ }
+
+ /**
+ * Sets workflow id.
+ * @param id workflow ID
+ * @return builder
+ */
+ public Builder id(URI id) {
+ this.id = id;
+ return this;
+ }
+
+ /**
+ * Sets workflow id.
+ * @param id workflow ID string
+ * @return builder
+ */
+ public Builder id(String id) {
+ this.id = URI.create(id);
+ return this;
+ }
+
+ /**
+ * Sets workflow data model.
+ * @param data workflow data model
+ * @return builder
+ */
+ public Builder data(JsonNode data) {
+ this.data = data;
+ return this;
+ }
+
+ /**
+ * Builds workflow description from builder.
+ * @return instance of workflow description
+ */
+ public DefaultWorkflowDescription build() {
+ return new DefaultWorkflowDescription(this);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkplace.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkplace.java
new file mode 100644
index 0000000..1c60eaf
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkplace.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.osgi.DefaultServiceDirectory;
+import org.onlab.osgi.ServiceNotFoundException;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.stream.Collectors;
+
+/**
+ * Default implementation of workplace.
+ */
+public class DefaultWorkplace extends Workplace {
+
+ /**
+ * Name of workplace.
+ */
+ private String name;
+
+ /**
+ * Constructor of DefaultWorkplace.
+ * @param name name of workplace
+ * @param data data model tree
+ */
+ public DefaultWorkplace(String name, DataModelTree data) {
+ super(data);
+ this.name = name;
+ }
+
+ @Override
+ public String name() {
+ return this.name;
+ }
+
+ @Override
+ public String distributor() {
+ return name();
+ }
+
+ @Override
+ public Collection<WorkflowContext> getContexts() throws WorkflowException {
+ WorkplaceStore workplaceStore;
+ try {
+ workplaceStore = DefaultServiceDirectory.getService(WorkplaceStore.class);
+ } catch (ServiceNotFoundException e) {
+ throw new WorkflowException(e);
+ }
+
+ return workplaceStore.getWorkplaceContexts(name());
+ }
+
+ /**
+ * Returns collection of context names.
+ * @return collection of context names
+ */
+ private Collection<String> getContextNames() {
+ Collection<WorkflowContext> ctx;
+ try {
+ ctx = getContexts();
+ } catch (WorkflowException e) {
+ ctx = Collections.emptyList();
+ }
+
+ return ctx.stream().map(x -> x.name()).collect(Collectors.toList());
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("name", name())
+ .add("triggernext", triggerNext())
+ .add("context", data())
+ .add("contexts", getContextNames())
+ .toString();
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkplaceDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkplaceDescription.java
new file mode 100644
index 0000000..95da550
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkplaceDescription.java
@@ -0,0 +1,169 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.MissingNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.base.MoreObjects;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Class for default workplace description.
+ */
+public final class DefaultWorkplaceDescription implements WorkplaceDescription {
+
+ /**
+ * Name of workplace.
+ */
+ private final String name;
+
+ /**
+ * Data model of workplace(Optional).
+ */
+ private final Optional<JsonNode> optData;
+
+ /**
+ * Constructor of workplace description.
+ * @param builder workplace builder
+ */
+ private DefaultWorkplaceDescription(Builder builder) {
+ this.name = builder.name;
+ this.optData = builder.optData;
+ }
+
+ @Override
+ public String name() {
+ return this.name;
+ }
+
+ @Override
+ public Optional<JsonNode> data() {
+ return this.optData;
+ }
+
+ /**
+ * Creating workplace description from json tree.
+ * @param root root node for workplace description
+ * @return workplace description
+ * @throws WorkflowException workflow exception
+ */
+ public static DefaultWorkplaceDescription valueOf(JsonNode root) throws WorkflowException {
+
+ JsonNode node = root.at(ptr(WP_NAME));
+ if (!(node instanceof TextNode)) {
+ throw new WorkflowException("invalid workplace name for " + root);
+ }
+
+ Builder builder = builder()
+ .name(node.asText());
+
+ node = root.at(ptr(WP_DATA));
+ if (node != null && !(node instanceof MissingNode)) {
+ if (!(node instanceof ObjectNode) && !(node instanceof ArrayNode)) {
+ throw new WorkflowException("invalid workplace data for " + root);
+ }
+ builder.data(node);
+ }
+
+ return builder.build();
+ }
+
+ private static String ptr(String field) {
+ return "/" + field;
+ }
+
+ @Override
+ public JsonNode toJson() {
+ ObjectNode root = JsonNodeFactory.instance.objectNode();
+ root.put(WP_NAME, name());
+ if (data().isPresent()) {
+ root.put(WP_DATA, data().get());
+ }
+ return root;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("name", name())
+ .add("optData", data())
+ .toString();
+ }
+
+ /**
+ * Gets builder instance.
+ * @return builder instance
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder for workplace description.
+ */
+ public static class Builder {
+
+ /**
+ * Workplace name.
+ */
+ private String name;
+
+ /**
+ * Workplace optData model.
+ */
+ private Optional<JsonNode> optData = Optional.empty();
+
+ /**
+ * List of workflow.
+ */
+ private List<DefaultWorkflowDescription> workflowDescs = new ArrayList<DefaultWorkflowDescription>();
+
+ /**
+ * Sets workplace name.
+ * @param name workplace name
+ * @return builder
+ */
+ public Builder name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ /**
+ * Sets optData model.
+ * @param data workplace optData model
+ * @return builder
+ */
+ public Builder data(JsonNode data) {
+ this.optData = Optional.of(data);
+ return this;
+ }
+
+ /**
+ * Builds workplace description from builder.
+ * @return instance of workflow description
+ */
+ public DefaultWorkplaceDescription build() {
+ return new DefaultWorkplaceDescription(this);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventHintSupplier.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventHintSupplier.java
new file mode 100644
index 0000000..076bfc0
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventHintSupplier.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import org.onosproject.event.Event;
+
+/**
+ * Functional interface for delivering event hint supplier.
+ */
+@FunctionalInterface
+public interface EventHintSupplier {
+ String apply(Event event) throws Throwable;
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventTask.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventTask.java
new file mode 100644
index 0000000..3b2b30a
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventTask.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+import org.onosproject.event.Event;
+
+import java.util.Objects;
+
+/**
+ * Class for event task.
+ */
+public final class EventTask extends HandlerTask {
+
+ /**
+ * Event triggering event task.
+ */
+ private final Event event;
+
+ /**
+ * Event hint value for finding target event.
+ */
+ private final String eventHint;
+
+ /**
+ * Constructor of event task.
+ * @param builder builder of event task
+ */
+ private EventTask(Builder builder) {
+ super(builder);
+ this.event = builder.event;
+ this.eventHint = builder.eventHint;
+ }
+
+ /**
+ * Gets event of event task.
+ * @return event triggering event task
+ */
+ public Event event() {
+ return event;
+ }
+
+ /**
+ * Gets event type (class name of event) of event task.
+ * @return event type
+ */
+ public String eventType() {
+ return event.getClass().getName();
+ }
+
+ /**
+ * Gets event hint of event task.
+ * @return event hint string
+ */
+ public String eventHint() {
+ return eventHint;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.toString());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (!(obj instanceof EventTask)) {
+ return false;
+ }
+ return Objects.equals(this.event(), ((EventTask) obj).event())
+ && Objects.equals(this.eventType(), ((EventTask) obj).eventType())
+ && Objects.equals(this.eventHint(), ((EventTask) obj).eventHint());
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("context", context())
+ .add("workletType", workletType())
+ .add("event", event())
+ .add("eventHint", eventHint())
+ .toString();
+ }
+
+ /**
+ * Gets a instance of builder.
+ * @return instance of builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder of EventTask.
+ */
+ public static class Builder extends HandlerTask.Builder {
+
+ /**
+ * Event triggering event task.
+ */
+ private Event event;
+
+ /**
+ * Event hint value for finding target event.
+ */
+ private String eventHint;
+
+ /**
+ * Sets event.
+ * @param event event triggering event task
+ * @return Builder of EventTask
+ */
+ public Builder event(Event event) {
+ this.event = event;
+ return this;
+ }
+
+ /**
+ * Sets event hint.
+ * @param eventHint event hint value for finding target event
+ * @return Builder of EventTask
+ */
+ public Builder eventHint(String eventHint) {
+ this.eventHint = eventHint;
+ return this;
+ }
+
+ @Override
+ public Builder context(WorkflowContext context) {
+ super.context(context);
+ return this;
+ }
+
+ @Override
+ public Builder workletType(String workletType) {
+ super.workletType(workletType);
+ return this;
+ }
+
+ /**
+ * Builds EventTask.
+ * @return instance of EventTask
+ */
+ public EventTask build() {
+ return new EventTask(this);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventTimeoutTask.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventTimeoutTask.java
new file mode 100644
index 0000000..92516d4
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/EventTimeoutTask.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * Class for event timeout task.
+ */
+public final class EventTimeoutTask extends HandlerTask {
+
+ /**
+ * Event type (Class name of event).
+ */
+ private final String eventType;
+
+ /**
+ * Event hint value for finding target event.
+ */
+ private final String eventHint;
+
+ /**
+ * Constructor of EventTimeoutTask.
+ * @param builder builder of EventTimeoutTask
+ */
+ private EventTimeoutTask(Builder builder) {
+ super(builder);
+ this.eventType = builder.eventType;
+ this.eventHint = builder.eventHint;
+ }
+
+ /**
+ * Gets event type (Class name of event).
+ * @return event type
+ */
+ public String eventType() {
+ return eventType;
+ }
+
+ /**
+ * Gets event hint value for finding target event.
+ * @return event hint string
+ */
+ public String eventHint() {
+ return eventHint;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.toString());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (!(obj instanceof EventTask)) {
+ return false;
+ }
+ return Objects.equals(this.eventType(), ((EventTask) obj).eventType())
+ && Objects.equals(this.eventHint(), ((EventTask) obj).eventHint());
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("context", context())
+ .add("workletType", workletType())
+ .add("eventType", eventType())
+ .add("eventHint", eventHint())
+ .toString();
+ }
+
+ /**
+ * Gets a instance of builder.
+ * @return instance of builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder of EventTimeoutTask.
+ */
+ public static class Builder extends HandlerTask.Builder {
+ /**
+ * Event type (Class name of event).
+ */
+ private String eventType;
+
+ /**
+ * Event hint value for finding target event.
+ */
+ private String eventHint;
+
+ /**
+ * Sets Event type (Class name of event).
+ * @param eventType event type
+ * @return builder of EventTimeoutTask
+ */
+ public Builder eventType(String eventType) {
+ this.eventType = eventType;
+ return this;
+ }
+
+ /**
+ * Sets event hint string for finding target event.
+ * @param eventHint event hint string
+ * @return builder of EventTimeoutTask
+ */
+ public Builder eventHint(String eventHint) {
+ this.eventHint = eventHint;
+ return this;
+ }
+
+ @Override
+ public Builder context(WorkflowContext context) {
+ super.context(context);
+ return this;
+ }
+
+ @Override
+ public Builder workletType(String workletType) {
+ super.workletType(workletType);
+ return this;
+ }
+
+ /**
+ * Builds EventTimeoutTask.
+ * @return instance of EventTimeoutTask
+ */
+ public EventTimeoutTask build() {
+ return new EventTimeoutTask(this);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/HandlerTask.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/HandlerTask.java
new file mode 100644
index 0000000..106466b
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/HandlerTask.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Abstract class for handler task.
+ */
+public abstract class HandlerTask {
+
+ /**
+ * Workflow context of handler task.
+ */
+ private final WorkflowContext context;
+
+ /**
+ * Worklet type of handler task.
+ */
+ private final String workletType;
+
+ /**
+ * Constructor for handler task.
+ * @param builder handler task builder
+ */
+ protected HandlerTask(Builder builder) {
+ this.context = builder.context;
+ this.workletType = builder.workletType;
+ }
+
+ /**
+ * Returns workflow context of this handler task.
+ * @return workflow context
+ */
+ public WorkflowContext context() {
+ return context;
+ }
+
+ /**
+ * Returns worklet type name of this handler task.
+ * @return worklet type
+ */
+ public String workletType() {
+ return workletType;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("context", context())
+ .add("workletType", workletType())
+ .toString();
+ }
+
+ /**
+ * Builder of HandlerTask.
+ */
+ public static class Builder {
+ protected WorkflowContext context;
+ protected String workletType;
+
+ /**
+ * Sets workflow context of handler task.
+ * @param context workflow context
+ * @return builder of handler task
+ */
+ public Builder context(WorkflowContext context) {
+ this.context = context;
+ return this;
+ }
+
+ /**
+ * Sets worklet type of handler task.
+ * @param workletType worklet type
+ * @return builder of handler task
+ */
+ public Builder workletType(String workletType) {
+ this.workletType = workletType;
+ return this;
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/HandlerTaskBatchDelegate.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/HandlerTaskBatchDelegate.java
new file mode 100644
index 0000000..c5859f2
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/HandlerTaskBatchDelegate.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import java.util.Collection;
+
+/**
+ * Facade for receiving notifications from the handler task batch service.
+ */
+public interface HandlerTaskBatchDelegate {
+
+ /**
+ * Submits the specified batch of handler task operations for processing.
+ *
+ * @param operations batch of operations
+ */
+ void execute(Collection<Collection<HandlerTask>> operations);
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ImmutableListWorkflow.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ImmutableListWorkflow.java
new file mode 100644
index 0000000..b5ea456
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ImmutableListWorkflow.java
@@ -0,0 +1,288 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.onlab.osgi.DefaultServiceDirectory;
+import org.onlab.osgi.ServiceNotFoundException;
+
+import java.lang.reflect.Modifier;
+import java.net.URI;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Class for immutable list workflow.
+ */
+public final class ImmutableListWorkflow extends AbstractWorkflow {
+
+ /**
+ * Init worklet type(class name of init worklet type).
+ */
+ private String initWorkletType;
+
+ /**
+ * List of worklet.
+ */
+ private List<String> workletTypeList;
+
+ /**
+ * Set of workflow attributes.
+ */
+ private Set<WorkflowAttribute> attributes;
+
+ /**
+ * Constructor of ImmutableListWorkflow.
+ * @param builder builder of ImmutableListWorkflow
+ */
+ private ImmutableListWorkflow(Builder builder) {
+ super(builder.id);
+ this.initWorkletType = builder.initWorkletType;
+ workletTypeList = ImmutableList.copyOf(builder.workletTypeList);
+ attributes = ImmutableSet.copyOf(builder.attributes);
+ }
+
+ @Override
+ public Worklet init(WorkflowContext context) throws WorkflowException {
+ if (Objects.isNull(initWorkletType)) {
+ return null;
+ }
+
+ return getWorkletInstance(initWorkletType);
+ }
+
+
+ @Override
+ public Worklet next(WorkflowContext context) throws WorkflowException {
+
+ int cnt = 0;
+ for (int i = 0; i < workletTypeList.size(); i++) {
+
+ if (cnt++ > Worklet.MAX_WORKS) {
+ throw new WorkflowException("Maximum worklet execution exceeded");
+ }
+
+ String workletType = workletTypeList.get(i);
+
+ if (Worklet.Common.COMPLETED.tag().equals(workletType)) {
+ return Worklet.Common.COMPLETED;
+ }
+
+ if (Worklet.Common.INIT.tag().equals(workletType)) {
+ continue;
+ }
+
+ Worklet worklet = getWorkletInstance(workletType);
+ Class workClass = worklet.getClass();
+
+ if (BranchWorklet.class.isAssignableFrom(workClass)) {
+ Class nextClass = ((BranchWorklet) worklet).next(context);
+ if (nextClass == null) {
+ throw new WorkflowException("Invalid next Worklet for " + workClass);
+ }
+
+ // TODO : it does not support duplicated use of WorkType. It needs to consider label concept
+ int nextIdx = getClassIndex(nextClass);
+ if (nextIdx == -1) {
+ throw new WorkflowException("Failed to find next " + nextClass + " for " + workClass);
+ }
+
+ i = nextIdx;
+ continue;
+
+ } else {
+ if (worklet.isNext(context)) {
+ return worklet;
+ }
+ }
+ }
+ return Worklet.Common.COMPLETED;
+ }
+
+ @Override
+ public Worklet getWorkletInstance(String workletType) throws WorkflowException {
+
+ WorkflowStore store;
+ try {
+ store = DefaultServiceDirectory.getService(WorkflowStore.class);
+ } catch (ServiceNotFoundException e) {
+ throw new WorkflowException(e);
+ }
+
+ Class workClass;
+ try {
+ workClass = store.getClass(workletType);
+ } catch (ClassNotFoundException e) {
+ throw new WorkflowException(e);
+ }
+
+ if (!isAllowed(workClass)) {
+ throw new WorkflowException("Not allowed class(" + workClass.getSimpleName() + ")");
+ }
+
+ Worklet worklet;
+ try {
+ worklet = (Worklet) workClass.newInstance();
+ } catch (Exception e) {
+ throw new WorkflowException(e);
+ }
+
+ return worklet;
+ }
+
+ @Override
+ public Set<WorkflowAttribute> attributes() {
+ return ImmutableSet.copyOf(attributes);
+ }
+
+ /**
+ * Gets index of class in worklet type list.
+ * @param aClass class to get index
+ * @return index of class in worklet type list
+ */
+ private int getClassIndex(Class aClass) {
+ for (int i = 0; i < workletTypeList.size(); i++) {
+ if (Objects.equals(aClass.getName(), workletTypeList.get(i))) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Checks whether class is allowed class or not.
+ * @param clazz class to check
+ * @return Check result
+ */
+ private boolean isAllowed(Class clazz) {
+ // non static inner class is not allowed
+ if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) {
+ return false;
+ }
+ // enum is not allowed
+ if (clazz.isEnum()) {
+ return false;
+ }
+ // class should be subclass of Work
+ if (!Worklet.class.isAssignableFrom(clazz)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.toString());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (!(obj instanceof EventTask)) {
+ return false;
+ }
+ return Objects.equals(this.id(), ((ImmutableListWorkflow) obj).id())
+ && Objects.equals(this.initWorkletType, ((ImmutableListWorkflow) obj).initWorkletType)
+ && Objects.equals(this.workletTypeList, ((ImmutableListWorkflow) obj).workletTypeList)
+ && Objects.equals(this.attributes, ((ImmutableListWorkflow) obj).attributes);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("id", id())
+ .add("initWorklet", initWorkletType)
+ .add("workList", workletTypeList)
+ .add("attributes", attributes)
+ .toString();
+ }
+
+ /**
+ * Gets a instance of builder.
+ * @return instance of builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder of ImmutableListWorkflow.
+ */
+ public static class Builder {
+
+ private URI id;
+ private String initWorkletType;
+ private final List<String> workletTypeList = Lists.newArrayList();
+ private final Set<WorkflowAttribute> attributes = Sets.newHashSet();
+
+ /**
+ * Sets id of immutable list workflow.
+ * @param uri id of immutable list workflow
+ * @return builder
+ */
+ public Builder id(URI uri) {
+ this.id = uri;
+ workletTypeList.add(Worklet.Common.INIT.tag());
+ return this;
+ }
+
+ /**
+ * Sets init worklet class name of immutable list workflow.
+ * @param workletClassName class name of worklet
+ * @return builder
+ */
+ public Builder init(String workletClassName) {
+ this.initWorkletType = workletClassName;
+ return this;
+ }
+
+ /**
+ * Chains worklet class name of immutable list workflow.
+ * @param workletClassName class name of worklet
+ * @return builder
+ */
+ public Builder chain(String workletClassName) {
+ workletTypeList.add(workletClassName);
+ return this;
+ }
+
+ /**
+ * Adds workflow attribute.
+ * @param attribute workflow attribute to be added
+ * @return builder
+ */
+ public Builder attribute(WorkflowAttribute attribute) {
+ attributes.add(attribute);
+ return this;
+ }
+
+ /**
+ * Builds ImmutableListWorkflow.
+ * @return instance of ImmutableListWorkflow
+ */
+ public ImmutableListWorkflow build() {
+ workletTypeList.add(Worklet.Common.COMPLETED.tag());
+ return new ImmutableListWorkflow(this);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/JsonDataModelTree.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/JsonDataModelTree.java
new file mode 100644
index 0000000..3a74bd2
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/JsonDataModelTree.java
@@ -0,0 +1,319 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+
+import com.fasterxml.jackson.core.JsonPointer;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.JsonNodeType;
+import com.fasterxml.jackson.databind.node.MissingNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * Class for json data model tree.
+ */
+public final class JsonDataModelTree implements DataModelTree {
+
+ /**
+ * Root node of json data model tree.
+ */
+ private JsonNode root;
+
+ /**
+ * Constructor of JsonDataModelTree.
+ */
+ public JsonDataModelTree() {
+ this.root = JsonNodeFactory.instance.objectNode();
+ }
+
+ /**
+ * Constructor of JsonDataModelTree.
+ * @param root root node of json data model tree
+ */
+ public JsonDataModelTree(JsonNode root) {
+ this.root = root;
+ }
+
+ @Override
+ public DataModelTree subtree(String path) {
+ JsonNode node = root.at(path);
+ if (Objects.isNull(node) || node.isMissingNode()) {
+ return null;
+ }
+ return new JsonDataModelTree(node);
+ }
+
+ @Override
+ public void attach(String path, DataModelTree tree) throws WorkflowException {
+ if (root == null || root instanceof MissingNode) {
+ throw new WorkflowException("Invalid root node");
+ }
+
+ JsonPointer ptr = JsonPointer.compile(path);
+ JsonNode node = root.at(ptr);
+ if (!(node instanceof MissingNode)) {
+ throw new WorkflowException("Path(" + path + ") has already subtree(" + node + ")");
+ }
+
+ if (!(tree instanceof JsonDataModelTree)) {
+ throw new WorkflowException("Invalid subTree(" + tree + ")");
+ }
+ JsonNode attachingNode = ((JsonDataModelTree) tree).root();
+
+ alloc(ptr.head(), Nodetype.MAP);
+ JsonNode parentNode = root.at(ptr.head());
+
+ if (!parentNode.isObject()) {
+ throw new WorkflowException("Invalid parentNode type(" + parentNode.getNodeType() + ")");
+ }
+
+ String key = ptr.last().getMatchingProperty();
+ ((ObjectNode) parentNode).put(key, attachingNode);
+ }
+
+ @Override
+ public JsonDataModelTree alloc(String path, Nodetype leaftype) throws WorkflowException {
+ if (root == null || root instanceof MissingNode) {
+ throw new WorkflowException("Invalid root node");
+ }
+
+ JsonPointer ptr = JsonPointer.compile(path);
+ return alloc(ptr, leaftype);
+ }
+
+ /**
+ * Allocates json data model tree on json pointer path with specific leaf type.
+ * @param ptr json pointer to allocate
+ * @param leaftype type of leaf node
+ * @return json data model tree
+ * @throws WorkflowException workflow exception
+ */
+ private JsonDataModelTree alloc(JsonPointer ptr, Nodetype leaftype) throws WorkflowException {
+ if (root == null || root instanceof MissingNode) {
+ throw new WorkflowException("Invalid root node");
+ }
+
+ switch (leaftype) {
+ case MAP:
+ alloc(root, ptr, JsonNodeType.OBJECT);
+ break;
+ case ARRAY:
+ alloc(root, ptr, JsonNodeType.ARRAY);
+ break;
+ default:
+ throw new WorkflowException("Not supported leaftype(" + leaftype + ")");
+ }
+ return this;
+ }
+
+ /**
+ * Gets root json node.
+ * @return root json node
+ * @throws WorkflowException workflow exception
+ */
+ public JsonNode root() throws WorkflowException {
+ return nodeAt("");
+ }
+
+ /**
+ * Gets root json node as ObjectNode (MAP type).
+ * @return root json node as ObjectNode
+ * @throws WorkflowException workflow exception
+ */
+ public ObjectNode rootObject() throws WorkflowException {
+ return objectAt("");
+ }
+
+ /**
+ * Gets root json node as ArrayNode (Array type).
+ * @return root json node as ArrayNode
+ * @throws WorkflowException workflow exception
+ */
+ public ArrayNode rootArray() throws WorkflowException {
+ return arrayAt("");
+ }
+
+ /**
+ * Gets json node on specific path.
+ * @param path path of json node
+ * @return json node on specific path
+ * @throws WorkflowException workflow exception
+ */
+ public JsonNode nodeAt(String path) throws WorkflowException {
+ JsonPointer ptr = JsonPointer.compile(path);
+ return nodeAt(ptr);
+ }
+
+ /**
+ * Gets json node on specific json pointer.
+ * @param ptr json pointer
+ * @return json node on specific json pointer.
+ * @throws WorkflowException workflow exception
+ */
+ public JsonNode nodeAt(JsonPointer ptr) throws WorkflowException {
+ if (root == null || root instanceof MissingNode) {
+ throw new WorkflowException("Invalid root node");
+ }
+ JsonNode node = root.at(ptr);
+ return node;
+ }
+
+ /**
+ * Gets json node on specific path as ObjectNode.
+ * @param path path of json node
+ * @return ObjectNode type json node on specific path
+ * @throws WorkflowException workflow exception
+ */
+ public ObjectNode objectAt(String path) throws WorkflowException {
+ JsonPointer ptr = JsonPointer.compile(path);
+ return objectAt(ptr);
+ }
+
+ /**
+ * Gets json node on specific json pointer as ObjectNode.
+ * @param ptr json pointer
+ * @return ObjectNode type json node on specific json pointer.
+ * @throws WorkflowException workflow exception
+ */
+ public ObjectNode objectAt(JsonPointer ptr) throws WorkflowException {
+ if (root == null || root instanceof MissingNode) {
+ throw new WorkflowException("Invalid root node");
+ }
+ JsonNode node = root.at(ptr);
+ if (!(node instanceof ObjectNode)) {
+ throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
+ }
+ return (ObjectNode) node;
+ }
+
+ /**
+ * Gets json node on specific path as ArrayNode.
+ * @param path path of json node
+ * @return ArrayNode type json node on specific path
+ * @throws WorkflowException workflow exception
+ */
+ public ArrayNode arrayAt(String path) throws WorkflowException {
+ JsonPointer ptr = JsonPointer.compile(path);
+ return arrayAt(ptr);
+ }
+
+ /**
+ * Gets json node on specific json pointer as ArrayNode.
+ * @param ptr json pointer
+ * @return ArrayNode type json node on specific json pointer.
+ * @throws WorkflowException workflow exception
+ */
+ public ArrayNode arrayAt(JsonPointer ptr) throws WorkflowException {
+ if (root == null || root instanceof MissingNode) {
+ throw new WorkflowException("Invalid root node");
+ }
+ JsonNode node = root.at(ptr);
+ if (!(node instanceof ArrayNode)) {
+ throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
+ }
+ return (ArrayNode) node;
+ }
+
+ /**
+ * Allocates json data model tree on json pointer path with specific leaf type.
+ * @param node current json node in the json tree path
+ * @param ptr json pointer
+ * @param leaftype leaf type to be allocated
+ * @return allocated json node
+ * @throws WorkflowException workflow exception
+ */
+ private JsonNode alloc(JsonNode node, JsonPointer ptr, JsonNodeType leaftype) throws WorkflowException {
+
+ if (ptr.matches()) {
+ if (node instanceof MissingNode) {
+ node = createEmpty(leaftype);
+ } else {
+ //TODO: checking existing node type is matched with leaftype
+ if (Objects.equals(node.getNodeType(), leaftype)) {
+ throw new WorkflowException("Requesting leaftype(" + leaftype + ") is not matched with "
+ + "existing nodetype(" + node.getNodeType() + ") for " + ptr);
+ }
+ }
+ return node;
+ }
+
+ if (ptr.getMatchingIndex() != -1) {
+ if (node instanceof MissingNode) {
+ node = createEmpty(JsonNodeType.ARRAY);
+ }
+ JsonNode child = alloc(node.get(ptr.getMatchingIndex()), ptr.tail(), leaftype);
+ if (!node.has(ptr.getMatchingIndex())) {
+ ((ArrayNode) node).insert(ptr.getMatchingIndex(), child);
+ }
+ } else if (ptr.getMatchingProperty() != null) {
+ if (node instanceof MissingNode) {
+ node = createEmpty(JsonNodeType.OBJECT);
+ }
+ JsonNode child = alloc(node.get(ptr.getMatchingProperty()), ptr.tail(), leaftype);
+ if (!node.has(ptr.getMatchingProperty())) {
+ ((ObjectNode) node).put(ptr.getMatchingProperty(), child);
+ }
+ }
+ return node;
+ }
+
+ /**
+ * Creating empty json node.
+ * @param type json node type to create
+ * @return created json node
+ * @throws WorkflowException workflow exception
+ */
+ private JsonNode createEmpty(JsonNodeType type) throws WorkflowException {
+ if (type == JsonNodeType.OBJECT) {
+ return JsonNodeFactory.instance.objectNode();
+ } else if (type == JsonNodeType.ARRAY) {
+ return JsonNodeFactory.instance.arrayNode();
+ } else if (type == JsonNodeType.STRING) {
+ return JsonNodeFactory.instance.textNode("");
+ } else {
+ throw new WorkflowException("Not supported JsonNodetype(" + type + ")");
+ }
+ }
+
+ /**
+ * Gets the pretty json formatted string of this json data model tree.
+ * @return pretty json formatted string of this json data model tree
+ */
+ public String formattedRootString() {
+ String str = "";
+ try {
+ str = (new ObjectMapper()).writerWithDefaultPrettyPrinter().writeValueAsString(root);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ }
+ return str;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("json", formattedRootString())
+ .toString();
+ }
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/RpcDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/RpcDescription.java
new file mode 100644
index 0000000..5b067b4
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/RpcDescription.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Interface for workflow RPC description.
+ */
+public interface RpcDescription {
+
+ /**
+ * Workflow RPC operation pointer.
+ */
+ String RPC_OP_PTR = "/op";
+
+ /**
+ * Workflow RPC parameters pointer.
+ */
+ String RPC_PARAMS_PTR = "/params";
+
+ /**
+ * Workflow RPC invocation ID pointer.
+ */
+ String RPC_ID_PTR = "/id";
+
+ /**
+ * Returns workflow RPC operation.
+ * @return workflow RPC operation
+ */
+ String op();
+
+ /**
+ * Returns workflow RPC parameters.
+ * @return workflow RPC parameters
+ */
+ JsonNode params();
+
+ /**
+ * Returns workflow RPC invocation ID.
+ * @return workflow RPC invocation ID
+ */
+ String id();
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/SystemWorkflowContext.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/SystemWorkflowContext.java
new file mode 100644
index 0000000..5642c97
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/SystemWorkflowContext.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+
+import java.net.URI;
+
+/**
+ * WorkflowContext for system workflow.
+ */
+public class SystemWorkflowContext extends DefaultWorkflowContext {
+
+ /**
+ * Timestamp when this system workflow context was created.
+ */
+ private final long timestamp;
+
+ /**
+ * Distributor string for designating which onos node executes this workflow context with work partition.
+ */
+ private String distributor;
+
+ /**
+ * The constructor of SystemWorkflowContext.
+ * @param workflowId id of workflow
+ * @param workplaceName workplace name
+ * @param data data model tree
+ */
+ public SystemWorkflowContext(URI workflowId, String workplaceName, DataModelTree data) {
+ super(workflowId, workplaceName, data);
+ timestamp = System.currentTimeMillis();
+ //initial distributor(It can be changed)
+ distributor = name();
+ }
+
+ @Override
+ public String distributor() {
+ return distributor;
+ }
+
+ /**
+ * Sets distributor string of this workflow context.
+ * @param distributor distributor string
+ */
+ public void setDistributor(String distributor) {
+ this.distributor = distributor;
+ }
+
+ @Override
+ public String name() {
+ return workflowId().toString()
+ + ":" + timestamp
+ + "@" + workplaceName();
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("name", name())
+ .add("triggernext", triggerNext())
+ .add("data", data())
+ .add("current", current())
+ .add("state", state())
+ .add("cause", cause())
+ .toString();
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/TimeoutTask.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/TimeoutTask.java
new file mode 100644
index 0000000..5b98bd1
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/TimeoutTask.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.base.MoreObjects;
+
+
+public final class TimeoutTask extends HandlerTask {
+
+ private TimeoutTask(Builder builder) {
+ super(builder);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("context", context())
+ .add("workletType", workletType())
+ .toString();
+ }
+
+ /**
+ * Gets a instance of builder.
+ * @return instance of builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder of TimeoutTask.
+ */
+ public static class Builder extends HandlerTask.Builder {
+ @Override
+ public Builder context(WorkflowContext context) {
+ super.context(context);
+ return this;
+ }
+
+ @Override
+ public Builder workletType(String workletType) {
+ super.workletType(workletType);
+ return this;
+ }
+
+ /**
+ * Builds TimeoutTask.
+ * @return instance of TimeoutTask
+ */
+ public TimeoutTask build() {
+ return new TimeoutTask(this);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/TimerChain.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/TimerChain.java
new file mode 100644
index 0000000..33c6aa3
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/TimerChain.java
@@ -0,0 +1,223 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import java.util.Date;
+import java.util.PriorityQueue;
+import java.util.Timer;
+import java.util.TimerTask;
+
+/**
+ * Class for time chain timer.
+ */
+public class TimerChain {
+
+ private PriorityQueue<TimerChainTask> taskQueue = new PriorityQueue<>();
+
+ private Timer impendingTimer;
+ private TimerChainTask impendingTask;
+
+ /**
+ * Schedules timer event.
+ * @param afterMs millisecond which time event happens.
+ * @param runnable runnable to be executed after 'afterMs'
+ */
+ public void schedule(long afterMs, Runnable runnable) {
+ schedule(new Date((new Date()).getTime() + afterMs), runnable);
+ }
+
+ /**
+ * Schedules timer event.
+ * @param date date which timer event happens.
+ * @param runnable runnable to be executed on 'date'
+ */
+ public void schedule(Date date, Runnable runnable) {
+ schedule(new TimerChainTask(this, date, runnable));
+ }
+
+ /**
+ * Schedule timer chain task.
+ * @param task task to be scheduled.
+ */
+ private void schedule(TimerChainTask task) {
+ synchronized (this) {
+ if (taskQueue.size() == 0) {
+ scheduleImpending(task);
+ return;
+ }
+
+ if (task.date().getTime() < head().date().getTime()) {
+ impendingTimer.cancel();
+ impendingTask.cancel();
+ TimerChainTask prevImpendingTask = pop().copy();
+ taskQueue.offer(prevImpendingTask);
+
+ scheduleImpending(task);
+ } else {
+ taskQueue.offer(task);
+ }
+ }
+ }
+
+ /**
+ * Schedule impending timer task.
+ * @param task impending timer chain task
+ * @return timer chain task
+ */
+ private TimerChainTask scheduleImpending(TimerChainTask task) {
+ taskQueue.offer(task);
+ Timer timer = new Timer();
+ this.setImpendingTask(task);
+ this.setImpendingTimer(timer);
+ timer.schedule(task, task.date());
+ return task;
+ }
+
+ /**
+ * Gets impending timer.
+ * @return impending timer
+ */
+ public Timer implendingTimer() {
+ return impendingTimer;
+ }
+
+ /**
+ * Sets impending timer.
+ * @param impendingTimer impending timer
+ */
+ public void setImpendingTimer(Timer impendingTimer) {
+ this.impendingTimer = impendingTimer;
+ }
+
+ /**
+ * Gets impending timer task.
+ * @return impending timer task
+ */
+ public TimerTask impendingTask() {
+ return impendingTask;
+ }
+
+ /**
+ * Sets impending timer task.
+ * @param impendingTask impending timer task
+ */
+ public void setImpendingTask(TimerChainTask impendingTask) {
+ this.impendingTask = impendingTask;
+ }
+
+ /**
+ * Gets head of timer chain task queue.
+ * @return timer chain task
+ */
+ public TimerChainTask head() {
+ if (taskQueue.size() > 0) {
+ return taskQueue.peek();
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Pops head of timer chain task queue.
+ * @return timer chain task
+ */
+ public TimerChainTask pop() {
+ if (taskQueue.size() > 0) {
+ return taskQueue.poll();
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Class for timer chain task.
+ */
+ public static class TimerChainTask extends TimerTask implements Comparable<TimerChainTask> {
+
+ private final TimerChain timerchain;
+ private final Date date;
+ private final Runnable runnable;
+
+ /**
+ * Constructor of timer chain task.
+ * @param timerchain timer chain
+ * @param date date to be scheduled
+ * @param runnable runnable to be executed by timer
+ */
+ public TimerChainTask(TimerChain timerchain, Date date, Runnable runnable) {
+ this.timerchain = timerchain;
+ this.date = date;
+ this.runnable = runnable;
+ }
+
+ /**
+ * Gets date.
+ * @return date of timer chain task
+ */
+ public Date date() {
+ return this.date;
+ }
+
+ /**
+ * Gets runnable.
+ * @return runnable of timer chain task
+ */
+ public Runnable runnable() {
+ return this.runnable;
+ }
+
+ @Override
+ public void run() {
+
+ TimerChainTask nextTask;
+ synchronized (timerchain) {
+ if (timerchain.impendingTask() != this) {
+ System.out.println("Invalid impendingTask");
+ runnable().run();
+ return;
+ }
+
+ timerchain.implendingTimer().cancel();
+ timerchain.pop();
+
+ nextTask = timerchain.head();
+
+ if (nextTask != null) {
+ Timer nextTimer = new Timer();
+ this.timerchain.setImpendingTask(nextTask);
+ this.timerchain.setImpendingTimer(nextTimer);
+ nextTimer.schedule(nextTask, nextTask.date());
+ }
+ }
+
+ runnable().run();
+
+ }
+
+ @Override
+ public int compareTo(TimerChainTask target) {
+ return date().compareTo(target.date());
+ }
+
+ /**
+ * Copies timer chain task.
+ * @return timer chain task
+ */
+ public TimerChainTask copy() {
+ return new TimerChainTask(timerchain, date, runnable);
+ }
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkExecutor.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkExecutor.java
new file mode 100644
index 0000000..e834e81
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkExecutor.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+/**
+ * Functional interface for delivering method reference of worklet completion event generator.
+ */
+@FunctionalInterface
+public interface WorkExecutor {
+
+ /**
+ * Applies this method reference.
+ * @throws WorkflowException workflow exception
+ */
+ void apply() throws WorkflowException;
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Workflow.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Workflow.java
new file mode 100644
index 0000000..95e6e08
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Workflow.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import java.net.URI;
+import java.util.Set;
+
+/**
+ * An interface representing workflow.
+ */
+public interface Workflow {
+
+ /**
+ * Id of workflow.
+ * @return id
+ */
+ URI id();
+
+ /**
+ * Returns init worklet.
+ * @param context workflow context
+ * @return init worklet
+ * @throws WorkflowException workflow exception
+ */
+ Worklet init(WorkflowContext context) throws WorkflowException;
+
+ /**
+ * Returns next worklet.
+ * @param context workflow context
+ * @return next worklet
+ * @throws WorkflowException workflow exception
+ */
+ Worklet next(WorkflowContext context) throws WorkflowException;
+
+ /**
+ * Returns instance of worklet.
+ * @param workletType class name of worklet
+ * @return instance of worklet
+ * @throws WorkflowException workflow exception
+ */
+ Worklet getWorkletInstance(String workletType) throws WorkflowException;
+
+ /**
+ * Builds workflow context.
+ * @param workplace workplace of system workflow
+ * @param data data model of system workflow context
+ * @return workflow context
+ * @throws WorkflowException workflow exception
+ */
+ WorkflowContext buildContext(Workplace workplace, DataModelTree data) throws WorkflowException;
+
+ /**
+ * Builds system workflow context.
+ * @param workplace workplace of system workflow
+ * @param data data model of system workflow context
+ * @return system workflow context
+ * @throws WorkflowException workflow exception
+ */
+ WorkflowContext buildSystemContext(Workplace workplace, DataModelTree data) throws WorkflowException;
+
+ /**
+ * Returns workflow attributes.
+ * @return attributes
+ */
+ Set<WorkflowAttribute> attributes();
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowAttribute.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowAttribute.java
new file mode 100644
index 0000000..18ed701
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowAttribute.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+/**
+ * Attributes of workflow.
+ */
+public enum WorkflowAttribute {
+
+ /**
+ * Removes workflow context after completion of workflow.
+ */
+ REMOVE_AFTER_COMPLETE,
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowBatchDelegate.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowBatchDelegate.java
new file mode 100644
index 0000000..d54a92a
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowBatchDelegate.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.google.common.annotations.Beta;
+
+import java.util.Collection;
+
+/**
+ * Facade for receiving notifications from the workflow batch service.
+ */
+@Beta
+public interface WorkflowBatchDelegate {
+
+ /**
+ * Submits the specified batch of workflow operations for processing.
+ *
+ * @param operations batch of operations
+ */
+ void execute(Collection<WorkflowData> operations);
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowContext.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowContext.java
new file mode 100644
index 0000000..12d4673
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowContext.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import org.onosproject.event.Event;
+
+import java.net.URI;
+
+/**
+ * An abstract class representing WorkflowContext.
+ */
+public abstract class WorkflowContext extends WorkflowData {
+
+ /**
+ * Constructor of workflow context.
+ * @param data data model tree
+ */
+ public WorkflowContext(DataModelTree data) {
+ super(data);
+ }
+
+ /**
+ * Returns workflow id of this workflow context.
+ * @return workflow id
+ */
+ public abstract URI workflowId();
+
+ /**
+ * Returns workplace name.
+ * @return workplace name
+ */
+ public abstract String workplaceName();
+
+ /**
+ * Returns the current state of workflow context.
+ * @return current state of workflow context
+ */
+ public abstract WorkflowState state();
+
+ /**
+ * Sets the current state of workflow context.
+ * @param state current state of workflow context
+ */
+ public abstract void setState(WorkflowState state);
+
+ /**
+ * Sets the current worklet of workflow context.
+ * @param worklet current worklet
+ */
+ public abstract void setCurrent(Worklet worklet);
+
+ /**
+ * Returns the class name of current worklet.
+ * @return the class name of current worklet
+ */
+ public abstract String current();
+
+ /**
+ * Returns the cause string of exception state.
+ * @return cause string
+ */
+ public abstract String cause();
+
+ /**
+ * Sets the cause string of exception state.
+ * @param cause cause string
+ */
+ public abstract void setCause(String cause);
+
+ /**
+ * Indicates the worklet process become completed.
+ * By calling this, workflow triggers the next worklet selection
+ */
+ public abstract void completed();
+
+ /**
+ * Waits an event which have 'eventHint' after executing executor.
+ * If the event happens, Worklet.isCompleted will be called.
+ * If the event does not happen for timeoutMs, Worklet.timeout will be called.
+ * @param eventType the class of event to wait
+ * @param eventHint the event of the event to wait
+ * @param eventGenerator a method reference to be executed after executing executor
+ * @param timeoutMs timeout millisecond
+ */
+ public abstract void waitCompletion(Class<? extends Event> eventType, String eventHint,
+ WorkExecutor eventGenerator, long timeoutMs);
+
+ /**
+ * Waits timeout milliseconds. After timeoutMs Worklet.timeout will be called.
+ * @param timeoutMs timeout millisecond
+ */
+ public abstract void waitFor(long timeoutMs);
+
+ /**
+ * Returns the class of a completion event to wait.
+ * @return the class of a completion event
+ */
+ public abstract Class<? extends Event> completionEventType();
+
+ /**
+ * Returns the event hint string to wait.
+ * @return the event hint string
+ */
+ public abstract String completionEventHint();
+
+ /**
+ * Returns method reference for generating completion event.
+ * @return a method reference
+ */
+ public abstract WorkExecutor completionEventGenerator();
+
+ /**
+ * Returns completion event timeout.
+ * @return completion event timeout
+ */
+ public abstract long completionEventTimeout();
+
+ /**
+ * Sets workflow service.
+ * @param workflowExecutionService workflow service
+ */
+ public abstract void setWorkflowExecutionService(WorkflowExecutionService workflowExecutionService);
+
+ /**
+ * Gets workflow service.
+ * @return workflow service
+ */
+ public abstract WorkflowExecutionService workflowService();
+
+ /**
+ * Sets workflow store.
+ * @param workflowStore workflow store.
+ */
+ public abstract void setWorkflowStore(WorkflowStore workflowStore);
+
+ /**
+ * Gets worklow store.
+ * @return workflow store
+ */
+ public abstract WorkflowStore workflowStore();
+
+ /**
+ * Sets workplace store.
+ * @param workplaceStore work place store.
+ */
+ public abstract void setWorkplaceStore(WorkplaceStore workplaceStore);
+
+ /**
+ * Gets workplace store.
+ * @return workplace store
+ */
+ public abstract WorkplaceStore workplaceStore();
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowData.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowData.java
new file mode 100644
index 0000000..6a8590c
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowData.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+
+/**
+ * An abstract class representing workflow data.
+ */
+public abstract class WorkflowData {
+
+ private DataModelTree data;
+ private boolean triggerNext = true; // default is true
+
+ /**
+ * Constructor of workflow data.
+ * @param data data model tree
+ */
+ public WorkflowData(DataModelTree data) {
+ this.data = data;
+ }
+
+ /**
+ * Returns name.
+ * @return name
+ */
+ public abstract String name();
+
+ /**
+ * Returns work-partition distributor.
+ * @return work-partition distributor
+ */
+ public abstract String distributor();
+
+ /**
+ * Returns context model tree.
+ * @return context model tree
+ */
+ public DataModelTree data() {
+ return data;
+ }
+
+ /**
+ * Returns whether to trigger next worklet selection.
+ * @return whether to trigger next worklet selection
+ */
+ public boolean triggerNext() {
+ return triggerNext;
+ }
+
+ /**
+ * Sets whether to handle update event.
+ * @param triggerNext whether to handle update event
+ */
+ public void setTriggerNext(boolean triggerNext) {
+ this.triggerNext = triggerNext;
+ }
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDataEvent.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDataEvent.java
new file mode 100644
index 0000000..4833251
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDataEvent.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Workflow data event class.
+ */
+public class WorkflowDataEvent extends AbstractEvent<WorkflowDataEvent.Type, WorkflowData> {
+
+ /**
+ * Workflow data event types.
+ */
+ public enum Type {
+
+ /**
+ * Insertion of workflow data.
+ */
+ INSERT,
+
+ /**
+ * Updation of workflow data.
+ */
+ UPDATE,
+
+ /**
+ * Removal of workflow data.
+ */
+ REMOVE
+ }
+
+ /**
+ * Constructor for workflow data event.
+ * @param type workflow data event type
+ * @param data workflow data
+ */
+ public WorkflowDataEvent(Type type, WorkflowData data) {
+ super(type, data);
+ }
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDataListener.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDataListener.java
new file mode 100644
index 0000000..b777e74
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDataListener.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Interface for workflow data listener.
+ */
+public interface WorkflowDataListener extends EventListener<WorkflowDataEvent> {
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDescription.java
new file mode 100644
index 0000000..ca483f0
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowDescription.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.net.URI;
+
+
+/**
+ * interface for workflow description.
+ */
+public interface WorkflowDescription {
+
+ /**
+ * Workflow workplace field name.
+ */
+ String WF_WORKPLACE = "workplace";
+
+ /**
+ * Workflow ID field name.
+ */
+ String WF_ID = "id";
+
+ /**
+ * Workflow data field name.
+ */
+ String WF_DATA = "data";
+
+ /**
+ * Gets workplace name.
+ * @return workplace name
+ */
+ String workplaceName();
+
+ /**
+ * Gets workflow ID.
+ * @return workflow ID
+ */
+ URI id();
+
+ /**
+ * Gets workflow context name.
+ * @return workflow context name
+ */
+ String workflowContextName();
+
+ /**
+ * Gets workflow data model.
+ * @return workflow data model
+ */
+ JsonNode data();
+
+ /**
+ * Gets json of workflow description.
+ * @return json of workflow description
+ */
+ JsonNode toJson();
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowException.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowException.java
new file mode 100644
index 0000000..09506bd
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowException.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+/**
+ * Workflow exception class.
+ */
+public class WorkflowException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Constructor for workflow exception.
+ * @param msg exception message
+ */
+ public WorkflowException(String msg) {
+ super(msg);
+ }
+
+ /**
+ * Constructor for workflow exception.
+ * @param e throwable to deliver
+ */
+ public WorkflowException(Throwable e) {
+ super(e);
+ }
+
+ /**
+ * Constructor for workflow exception.
+ * @param msg exception message
+ * @param e throwable to deliver
+ */
+ public WorkflowException(String msg, Throwable e) {
+ super(msg, e);
+ }
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowExecutionService.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowExecutionService.java
new file mode 100644
index 0000000..ad9f731
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowExecutionService.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import org.onosproject.event.Event;
+import org.onosproject.event.ListenerService;
+
+/**
+ * Interface for workflow execution service.
+ */
+public interface WorkflowExecutionService extends ListenerService<WorkflowDataEvent, WorkflowDataListener> {
+
+ /**
+ * Executes init worklet.
+ * @param context workflow context
+ */
+ void execInitWorklet(WorkflowContext context);
+
+ /**
+ * Triggers workflow event map.
+ * @param event triggering event
+ * @param generator event hint generation method reference
+ */
+ void eventMapTrigger(Event event, EventHintSupplier generator);
+
+ /**
+ * Registers workflow event map.
+ * @param eventType event type (class name of event)
+ * @param eventHint event hint value
+ * @param contextName workflow context name to be called by this event map
+ * @param workletType worklet type to be called by this event map
+ * @throws WorkflowException workflow exception
+ */
+ void registerEventMap(Class<? extends Event> eventType, String eventHint,
+ String contextName, String workletType) throws WorkflowException;
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowService.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowService.java
new file mode 100644
index 0000000..b11e835
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowService.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Interface for workflow service.
+ */
+public interface WorkflowService {
+
+ /**
+ * Creates workplace.
+ * @param wpDesc workplace description
+ * @throws WorkflowException workflow exception
+ */
+ void createWorkplace(WorkplaceDescription wpDesc) throws WorkflowException;
+
+ /**
+ * Removes workplace.
+ * @param wpDesc workplace description
+ * @throws WorkflowException workflow exception
+ */
+ void removeWorkplace(WorkplaceDescription wpDesc) throws WorkflowException;
+
+ /**
+ * Clears all workplaces.
+ * @throws WorkflowException workflow exception
+ */
+ void clearWorkplace() throws WorkflowException;
+
+ /**
+ * Invokes workflow.
+ * @param wfDesc workflow description
+ * @throws WorkflowException workflow exception
+ */
+ void invokeWorkflow(WorkflowDescription wfDesc) throws WorkflowException;
+
+ /**
+ * Invokes workflow.
+ * @param worklowDescJson workflow description json
+ * @throws WorkflowException workflow exception
+ */
+ void invokeWorkflow(JsonNode worklowDescJson) throws WorkflowException;
+
+ /**
+ * Terminates workflow.
+ * @param wfDesc workflow description
+ * @throws WorkflowException workflow exception
+ */
+ void terminateWorkflow(WorkflowDescription wfDesc) throws WorkflowException;
+}
\ No newline at end of file
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowState.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowState.java
new file mode 100644
index 0000000..0f92624
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowState.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+/**
+ * State of workflow.
+ */
+public enum WorkflowState {
+
+ /**
+ * Workflow is idle state.
+ */
+ IDLE,
+
+ /**
+ * Workflow is running state.
+ */
+ RUNNING,
+
+ /**
+ * Workflow is exception state.
+ */
+ EXCEPTION
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowStore.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowStore.java
new file mode 100644
index 0000000..ac0de4d
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowStore.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import java.net.URI;
+import java.util.Collection;
+
+/**
+ * Store for managing workflow.
+ */
+public interface WorkflowStore {
+
+ /**
+ * Registers workflow.
+ * @param workflow registering workflow
+ */
+ void register(Workflow workflow);
+
+ /**
+ * Unregisters workflow.
+ * @param id id of workflow
+ */
+ void unregister(URI id);
+
+ /**
+ * Gets workflow.
+ * @param id id of workflow
+ * @return workflow
+ */
+ Workflow get(URI id);
+
+ /**
+ * Gets all workflow.
+ * @return collection of workflow
+ */
+ Collection<Workflow> getAll();
+
+ /**
+ * Registers local class loader.
+ * @param loader class loader
+ */
+ void registerLocal(ClassLoader loader);
+
+ /**
+ * Unregisters local class loader.
+ * @param loader class loader
+ */
+ void unregisterLocal(ClassLoader loader);
+
+ /**
+ * Gets class from registered class loaders.
+ * @param name name of class
+ * @return class
+ * @throws ClassNotFoundException class not found exception
+ */
+ Class getClass(String name) throws ClassNotFoundException;
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Worklet.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Worklet.java
new file mode 100644
index 0000000..386fa38
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Worklet.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+
+import org.onosproject.event.Event;
+
+/**
+ * An interface representing worklet. A workflow is composed of worklets.
+ */
+public interface Worklet {
+
+ int MAX_WORKS = 1000;
+
+ /**
+ * Returns tag name of worklet. class name is usually used.
+ * @return tag name
+ */
+ String tag();
+
+ /**
+ * Processes tasks of the worklet under the workflow context.
+ * @param context workflow context
+ * @throws WorkflowException workflow exception
+ */
+ void process(WorkflowContext context) throws WorkflowException;
+
+ /**
+ * Checks whether is this worklet next worklet to be done under the workflow context.
+ * @param context workflow context
+ * @return true means this worklet is the next worklet to be processed
+ * @throws WorkflowException workflow exception
+ */
+ boolean isNext(WorkflowContext context) throws WorkflowException;
+
+ /**
+ * Checks whether is this worklet completed or not. 'isCompleted' checking is triggered by an event task.
+ * @param context workflow context
+ * @param event an event triggering this 'isCompleted' checking
+ * @return completed or not
+ * @throws WorkflowException workflow exception
+ */
+ boolean isCompleted(WorkflowContext context, Event event) throws WorkflowException;
+
+ /**
+ * Completion event timeout handler.
+ * @param context workflow context
+ * @throws WorkflowException workflow exception
+ */
+ void timeout(WorkflowContext context) throws WorkflowException;
+
+ /**
+ * Common worklet enum.
+ */
+ enum Common implements Worklet {
+
+ /**
+ * Init worklet.
+ */
+ INIT {
+ @Override
+ public String tag() {
+ return INIT.name();
+ }
+
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").process should not be called");
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isNext should not be called");
+ }
+
+ @Override
+ public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isCompleted should not be called");
+ }
+
+ @Override
+ public void timeout(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").timeout should not be called");
+ }
+ },
+
+ /**
+ * Completed worklet.
+ */
+ COMPLETED {
+ @Override
+ public String tag() {
+ return COMPLETED.name();
+ }
+
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").process should not be called");
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isNext should not be called");
+ }
+
+ @Override
+ public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isCompleted should not be called");
+ }
+
+ @Override
+ public void timeout(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").timeout should not be called");
+ }
+ },
+
+ /**
+ * Interrupted worklet.
+ */
+ INTERRUPTED {
+ @Override
+ public String tag() {
+ return INTERRUPTED.name();
+ }
+
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").process should not be called");
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isNext should not be called");
+ }
+
+ @Override
+ public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").isCompleted should not be called");
+ }
+
+ @Override
+ public void timeout(WorkflowContext context) throws WorkflowException {
+ throw new WorkflowException("(" + tag() + ").timeout should not be called");
+ }
+ }
+ }
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Workplace.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Workplace.java
new file mode 100644
index 0000000..a7439d6
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/Workplace.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import java.util.Collection;
+
+/**
+ * Abstract class for workplace. A common workflow data container for multiple work-flows.
+ */
+public abstract class Workplace extends WorkflowData {
+
+ public static final String SYSTEM_WORKPLACE = "system-workplace";
+
+ /**
+ * Constructor of workplace.
+ * @param data data model tree
+ */
+ public Workplace(DataModelTree data) {
+ super(data);
+ }
+
+ /**
+ * Gets workflow contexts of workplace.
+ * @return collection of workflow context
+ * @throws WorkflowException workflow exception
+ */
+ abstract Collection<WorkflowContext> getContexts() throws WorkflowException;
+
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceDescription.java
new file mode 100644
index 0000000..16515eb
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceDescription.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.util.Optional;
+
+/**
+ * Interface for workplace description.
+ */
+public interface WorkplaceDescription {
+
+ /**
+ * Workplace name field name.
+ */
+ String WP_NAME = "name";
+
+ /**
+ * Workplace data field name.
+ */
+ String WP_DATA = "data";
+
+ /**
+ * Gets workplace name.
+ * @return workplace name
+ */
+ String name();
+
+ /**
+ * Gets optional workplace data model.
+ * @return workplace optData model
+ */
+ Optional<JsonNode> data();
+
+ /**
+ * Gets json of workflow description.
+ * @return json of workflow description
+ */
+ JsonNode toJson();
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceStore.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceStore.java
new file mode 100644
index 0000000..b73cd4f
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceStore.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import org.onosproject.store.Store;
+import org.onosproject.store.service.StorageException;
+
+import java.util.Collection;
+
+/**
+ * Interface for workplace store.
+ */
+public interface WorkplaceStore extends Store<WorkflowDataEvent, WorkplaceStoreDelegate> {
+
+ /**
+ * Registers workplace on workplace store.
+ * @param name workplace name to register
+ * @param workplace workplace
+ * @throws StorageException storage exception
+ */
+ void registerWorkplace(String name, Workplace workplace) throws StorageException;
+
+ /**
+ * Removes workplace from workplace store.
+ * @param name workplace name to remove
+ * @throws StorageException storage exception
+ */
+ void removeWorkplace(String name) throws StorageException;
+
+ /**
+ * Gets workplace on workplace store.
+ * @param name workplace name to get
+ * @return workplace
+ * @throws StorageException storage exception
+ */
+ Workplace getWorkplace(String name) throws StorageException;
+
+ /**
+ * Commits workplace on workplace store.
+ * @param name workplace name to commit
+ * @param workplace workplace to commit
+ * @param handleEvent whether or not to handle workplace(workflow data) event
+ * @throws StorageException storage exception
+ */
+ void commitWorkplace(String name, Workplace workplace, boolean handleEvent) throws StorageException;
+
+ /**
+ * Gets all workplaces from workplace store.
+ * @return collection of workplace
+ * @throws StorageException storage exception
+ */
+ Collection<Workplace> getWorkplaces() throws StorageException;
+
+ /**
+ * Registers workflow context on workplace store.
+ * @param name workflow context name to register
+ * @param context workflow context to register
+ * @throws StorageException storage exception
+ */
+ void registerContext(String name, WorkflowContext context) throws StorageException;
+
+ /**
+ * Removes workflow context from workplace store.
+ * @param name workflow context name
+ * @throws StorageException storage exception
+ */
+ void removeContext(String name) throws StorageException;
+
+ /**
+ * Gets workflow context with name.
+ * @param name workflow context name to get
+ * @return workflow context
+ * @throws StorageException storage exception
+ */
+ WorkflowContext getContext(String name) throws StorageException;
+
+ /**
+ * Commits workflow context on workplace store.
+ * @param name workflow context name to commit
+ * @param context workflow context to commit
+ * @param handleEvent whether or not to handle workflow context(workflow data) event
+ * @throws StorageException storage exception
+ */
+ void commitContext(String name, WorkflowContext context, boolean handleEvent) throws StorageException;
+
+ /**
+ * Gets all workflow context from workplace store.
+ * @return collection of workflow context
+ * @throws StorageException storage exception
+ */
+ Collection<WorkflowContext> getContexts() throws StorageException;
+
+ /**
+ * Gets workflow contexts belonging to a workplace.
+ * @param workplaceName workplace name
+ * @return collection of workflow contexts belonging to a workplace
+ */
+ Collection<WorkflowContext> getWorkplaceContexts(String workplaceName);
+
+ /**
+ * Removes all workflow contexts beloinging to a workplace.
+ * @param workplaceName workplace name
+ */
+ void removeWorkplaceContexts(String workplaceName);
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceStoreDelegate.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceStoreDelegate.java
new file mode 100644
index 0000000..9e7ba14
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkplaceStoreDelegate.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.api;
+
+import org.onosproject.store.StoreDelegate;
+
+/**
+ * Workplace store delegate.
+ */
+public interface WorkplaceStoreDelegate extends StoreDelegate<WorkflowDataEvent> {
+}
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/package-info.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/package-info.java
new file mode 100644
index 0000000..7e68f8f
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Package for workflow APIs.
+ */
+package org.onosproject.workflow.api;
\ No newline at end of file
diff --git a/apps/workflow/app/BUCK b/apps/workflow/app/BUCK
new file mode 100644
index 0000000..aa0e62a
--- /dev/null
+++ b/apps/workflow/app/BUCK
@@ -0,0 +1,16 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+ '//lib:KRYO',
+ '//lib:jsch',
+ '//lib:org.apache.karaf.shell.console',
+ '//lib:jackson-core',
+ '//lib:jackson-annotations',
+ '//lib:jackson-databind',
+ '//cli:onos-cli',
+ '//core/store/serializers:onos-core-serializers',
+ '//apps/workflow/api:onos-apps-workflow-api',
+]
+
+osgi_jar_with_tests(
+ deps = COMPILE_DEPS,
+)
diff --git a/apps/workflow/app/BUILD b/apps/workflow/app/BUILD
new file mode 100644
index 0000000..97ef1b4
--- /dev/null
+++ b/apps/workflow/app/BUILD
@@ -0,0 +1,8 @@
+COMPILE_DEPS = CORE_DEPS + KRYO + JACKSON + CLI + [
+ "//core/store/serializers:onos-core-serializers",
+ "//apps/workflow/api:onos-apps-workflow-api",
+]
+
+osgi_jar_with_tests(
+ deps = COMPILE_DEPS,
+)
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowCommand.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowCommand.java
new file mode 100644
index 0000000..94b099b
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowCommand.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.cli;
+
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.workflow.api.DefaultWorkflowDescription;
+import org.onosproject.workflow.api.WorkflowService;
+import org.onosproject.workflow.api.WorkflowException;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+@Command(scope = "onos", name = "workflow", description = "workflow cli")
+public class WorkFlowCommand extends AbstractShellCommand {
+
+ @Argument (index = 0, name = "cmd", description = "command(invoke)", required = true)
+ private String cmd = null;
+
+ @Argument (index = 1, name = "id", description = "workflow id(URI)", required = true)
+ private String id = null;
+
+ @Argument (index = 2, name = "name", description = "workplace name", required = true)
+ private String name = null;
+
+ @Override
+ protected void execute() {
+ if (Objects.isNull(cmd)) {
+ error("invalid cmd parameter");
+ return;
+ }
+
+ switch (cmd) {
+ case "invoke":
+ if (Objects.isNull(id)) {
+ error("invalid workflow id parameter");
+ return;
+ }
+
+ if (Objects.isNull(name)) {
+ error("invalid workplace name parameter");
+ return;
+ }
+
+ invoke(id, name);
+ break;
+ default:
+ print("Unsupported cmd: " + cmd);
+ }
+ }
+
+ /**
+ * Invokes workflow.
+ * @param workflowId workflow id
+ * @param workplaceName workplace name
+ */
+ private void invoke(String workflowId, String workplaceName) {
+
+ WorkflowService service = get(WorkflowService.class);
+ DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder()
+ .workplaceName(workplaceName)
+ .id(workflowId)
+ .data(JsonNodeFactory.instance.objectNode())
+ .build();
+ try {
+ service.invokeWorkflow(wfDesc);
+ } catch (WorkflowException e) {
+ error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
+ }
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowEventMapCommand.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowEventMapCommand.java
new file mode 100644
index 0000000..0a8e492
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowEventMapCommand.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.cli;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.workflow.api.ContextEventMapStore;
+import org.onosproject.workflow.api.WorkflowException;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+@Command(scope = "onos", name = "workflow-eventmap", description = "workflow event map cli")
+public class WorkFlowEventMapCommand extends AbstractShellCommand {
+
+ @Argument (index = 0, name = "cmd", description = "command(print)", required = true)
+ private String cmd = null;
+
+ @Override
+ protected void execute() {
+ if (Objects.isNull(cmd)) {
+ error("invalid cmd parameter");
+ return;
+ }
+
+ ContextEventMapStore store = get(ContextEventMapStore.class);
+ try {
+ switch (cmd) {
+ case "print":
+ JsonNode tree = store.asJsonTree();
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tree));
+ } catch (JsonProcessingException e) {
+ error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
+ }
+ break;
+
+ default:
+ print("Unsupported cmd: " + cmd);
+ }
+ } catch (WorkflowException e) {
+ error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
+ }
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowStoreCommand.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowStoreCommand.java
new file mode 100644
index 0000000..820d141
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowStoreCommand.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.workflow.api.Workflow;
+import org.onosproject.workflow.api.WorkflowStore;
+
+import java.util.Objects;
+import java.net.URI;
+
+@Command(scope = "onos", name = "workflowstore", description = "workflow store cli")
+public class WorkFlowStoreCommand extends AbstractShellCommand {
+
+ @Argument (index = 0, name = "cmd", description = "command(rm)", required = false)
+ private String cmd = null;
+
+ @Argument (index = 1, name = "id", description = "workflow id(URI)", required = false)
+ private String id = null;
+
+ @Override
+ protected void execute() {
+
+ if (Objects.isNull(cmd)) {
+ printAllWorkflow();
+ return;
+ }
+
+ if (Objects.isNull(id)) {
+ print("invalid id");
+ return;
+ }
+
+ switch (cmd) {
+ case "rm":
+ rmWorkflow(id);
+ break;
+ default:
+ print("Unsupported cmd: " + cmd);
+ }
+ }
+
+ private void rmWorkflow(String id) {
+ WorkflowStore workflowStore = get(WorkflowStore.class);
+ workflowStore.unregister(URI.create(id));
+ }
+
+ private void printAllWorkflow() {
+ WorkflowStore workflowStore = get(WorkflowStore.class);
+ for (Workflow workflow : workflowStore.getAll()) {
+ print(getWorkflowString(workflow));
+ }
+ }
+
+ private String getWorkflowString(Workflow workflow) {
+ return workflow.toString();
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowTestCommand.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowTestCommand.java
new file mode 100644
index 0000000..090dad0
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkFlowTestCommand.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.cli;
+
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.workflow.api.DefaultWorkflowDescription;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.WorkflowService;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+@Command(scope = "onos", name = "workflow-test", description = "workflow test cli")
+public class WorkFlowTestCommand extends AbstractShellCommand {
+
+ @Argument (index = 0, name = "cmd", description = "command(invoke)", required = true)
+ private String cmd = null;
+
+ @Argument (index = 1, name = "number", description = "number of test", required = true)
+ private String number = null;
+
+ @Override
+ protected void execute() {
+ if (Objects.isNull(cmd)) {
+ error("invalid cmd parameter");
+ return;
+ }
+
+ switch (cmd) {
+ case "invoke":
+ if (Objects.isNull(number)) {
+ error("invalid number of test");
+ return;
+ }
+
+ int num;
+ try {
+ num = Integer.parseInt(number);
+ } catch (Exception e) {
+ error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
+ return;
+ }
+
+ test(num);
+ break;
+ default:
+ print("Unsupported cmd: " + cmd);
+ }
+ }
+
+ /**
+ * Invokes workflow.
+ * @param workflowId workflow id
+ * @param workplaceName workplace name
+ */
+ private void invoke(String workflowId, String workplaceName) {
+
+ WorkflowService service = get(WorkflowService.class);
+ DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder()
+ .workplaceName(workplaceName)
+ .id(workflowId)
+ .data(JsonNodeFactory.instance.objectNode())
+ .build();
+ try {
+ service.invokeWorkflow(wfDesc);
+ } catch (WorkflowException e) {
+ error(e.getMessage() + "trace: " + Arrays.asList(e.getStackTrace()));
+ }
+ }
+
+ /**
+ * Workflow invoke test.
+ * @param num the number of workflow to test
+ */
+ private void test(int num) {
+ for (int i = 0; i <= num; i++) {
+ String wpName = "test-" + i;
+ invoke("sample.workflow-0", wpName);
+ }
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCommand.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCommand.java
new file mode 100644
index 0000000..ded5882
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCommand.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.workflow.api.WorkflowContext;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.WorkflowService;
+import org.onosproject.workflow.api.Workplace;
+import org.onosproject.workflow.api.DefaultWorkplaceDescription;
+import org.onosproject.workflow.api.WorkplaceStore;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+@Command(scope = "onos", name = "workplace",
+ description = "workplace cli")
+public class WorkplaceStoreCommand extends AbstractShellCommand {
+
+ @Argument (index = 0, name = "cmd", description = "command(add/rm/clear/print)", required = false)
+ private String cmd = null;
+
+ @Argument (index = 1, name = "name", description = "workspace name", required = false)
+ private String name = null;
+
+ @Option(name = "-f", aliases = "--filter", description = "including filter",
+ required = false, multiValued = false)
+ private String inFilter = null;
+
+ @Option(name = "-e", aliases = "--excludefilter", description = "excluding filter",
+ required = false, multiValued = false)
+ private String exFilter = null;
+
+ @Override
+ protected void execute() {
+
+ if (Objects.isNull(cmd)) {
+ printAllWorkplace();
+ return;
+ }
+
+ switch (cmd) {
+ case "add":
+ if (Objects.isNull(name)) {
+ error("invalid name");
+ return;
+ }
+ addEmptyWorkplace(name);
+ return;
+
+ case "rm":
+ if (Objects.isNull(name)) {
+ print("invalid name");
+ return;
+ }
+ rmWorkplace(name);
+ break;
+
+ case "clear":
+ clearWorkplace();
+ break;
+
+ case "print":
+ if (Objects.isNull(name)) {
+ print("invalid name");
+ return;
+ }
+ printWorkplace(name);
+ break;
+
+ default:
+ print("Unsupported cmd: " + cmd);
+ }
+ }
+
+ /**
+ * Adds empty workplace.
+ * @param name workplace name
+ */
+ private void addEmptyWorkplace(String name) {
+ WorkflowService service = get(WorkflowService.class);
+ DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder()
+ .name(name)
+ .build();
+ try {
+ service.createWorkplace(wpDesc);
+ } catch (WorkflowException e) {
+ error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
+ }
+ }
+
+ /**
+ * Clears all workplaces.
+ */
+ private void clearWorkplace() {
+ WorkflowService service = get(WorkflowService.class);
+ try {
+ service.clearWorkplace();
+ } catch (WorkflowException e) {
+ error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
+ }
+ }
+
+ /**
+ * Removes workplace.
+ * @param name workplace name to remove
+ */
+ private void rmWorkplace(String name) {
+ WorkflowService service = get(WorkflowService.class);
+ DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder()
+ .name(name)
+ .build();
+ try {
+ service.removeWorkplace(wpDesc);
+ } catch (WorkflowException e) {
+ error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
+ }
+ }
+
+ /**
+ * Prints workplace.
+ * @param name workplace name
+ */
+ private void printWorkplace(String name) {
+ WorkplaceStore workplaceStore = get(WorkplaceStore.class);
+ Workplace workplace = workplaceStore.getWorkplace(name);
+ print(getWorkplaceString(workplace));
+ }
+
+ /**
+ * Prints all workplaces.
+ */
+ private void printAllWorkplace() {
+ WorkplaceStore workplaceStore = get(WorkplaceStore.class);
+ for (Workplace workplace : workplaceStore.getWorkplaces()) {
+ print(getWorkplaceString(workplace));
+ printWorkplaceContexts(workplaceStore, workplace.name());
+ }
+ }
+
+ /**
+ * Prints contexts of workplace.
+ * @param workplaceStore workplace store service
+ * @param workplaceName workplace name
+ */
+ private void printWorkplaceContexts(WorkplaceStore workplaceStore, String workplaceName) {
+ for (WorkflowContext context : workplaceStore.getWorkplaceContexts(workplaceName)) {
+ String str = context.toString();
+ if (Objects.nonNull(inFilter)) {
+ if (str.indexOf(inFilter) != -1) {
+ if (Objects.nonNull(exFilter)) {
+ if (str.indexOf(exFilter) == -1) {
+ print(" - " + context);
+ }
+ } else {
+ print(" - " + context);
+ }
+ }
+ } else {
+ if (Objects.nonNull(exFilter)) {
+ if (str.indexOf(exFilter) == -1) {
+ print(" - " + context);
+ }
+ } else {
+ print(" - " + context);
+ }
+ }
+ }
+ }
+
+ /**
+ * Gets workplace string.
+ * @param workplace workplace
+ * @return workplace string
+ */
+ private String getWorkplaceString(Workplace workplace) {
+ return workplace.toString();
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/package-info.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/package-info.java
new file mode 100644
index 0000000..0383d04
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Workflow cli package.
+ */
+package org.onosproject.workflow.cli;
\ No newline at end of file
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/DistributedContextEventMapTreeStore.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/DistributedContextEventMapTreeStore.java
new file mode 100644
index 0000000..b7c7366
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/DistributedContextEventMapTreeStore.java
@@ -0,0 +1,198 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.AsyncDocumentTree;
+import org.onosproject.store.service.DocumentPath;
+import org.onosproject.store.service.IllegalDocumentModificationException;
+import org.onosproject.store.service.NoSuchDocumentPathException;
+import org.onosproject.store.service.Ordering;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+import org.onosproject.workflow.api.ContextEventMapStore;
+import org.onosproject.workflow.api.WorkflowException;
+import org.slf4j.Logger;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(immediate = true)
+@Service
+public class DistributedContextEventMapTreeStore implements ContextEventMapStore {
+
+ protected static final Logger log = getLogger(DistributedContextEventMapTreeStore.class);
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ private CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ private StorageService storageService;
+
+ private ApplicationId appId;
+
+ private AsyncDocumentTree<String> eventMapTree;
+
+ @Activate
+ public void activate() {
+
+ appId = coreService.registerApplication("org.onosproject.contexteventmapstore");
+ log.info("appId=" + appId);
+
+ KryoNamespace eventMapNamespace = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .build();
+
+ eventMapTree = storageService.<String>documentTreeBuilder()
+ .withSerializer(Serializer.using(eventMapNamespace))
+ .withName("context-event-map-store")
+ .withOrdering(Ordering.INSERTION)
+ .buildDocumentTree();
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ eventMapTree.destroy();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void registerEventMap(String eventType, String eventHint,
+ String contextName, String workletType) throws WorkflowException {
+ DocumentPath dpath = DocumentPath.from(Lists.newArrayList("root", eventType, eventHint, contextName));
+ String currentWorkletType = completeVersioned(eventMapTree.get(dpath));
+ if (currentWorkletType == null) {
+ complete(eventMapTree.createRecursive(dpath, workletType));
+ } else {
+ complete(eventMapTree.replace(dpath, workletType, currentWorkletType));
+ }
+ }
+
+ @Override
+ public void unregisterEventMap(String eventType, String eventHint, String contextName) throws WorkflowException {
+ DocumentPath contextPath = DocumentPath.from(Lists.newArrayList("root", eventType, eventHint, contextName));
+ complete(eventMapTree.removeNode(contextPath));
+ }
+
+ @Override
+ public Map<String, String> getEventMap(String eventType, String eventHint) throws WorkflowException {
+ DocumentPath path = DocumentPath.from(Lists.newArrayList("root", eventType, eventHint));
+ Map<String, Versioned<String>> contexts = complete(eventMapTree.getChildren(path));
+ Map<String, String> eventMap = Maps.newHashMap();
+ if (Objects.isNull(contexts)) {
+ return eventMap;
+ }
+
+ for (Map.Entry<String, Versioned<String>> entry : contexts.entrySet()) {
+ eventMap.put(entry.getKey(), entry.getValue().value());
+ }
+ return eventMap;
+ }
+
+ @Override
+ public Map<String, Versioned<String>> getChildren(String path) throws WorkflowException {
+ DocumentPath dpath = DocumentPath.from(path);
+ Map<String, Versioned<String>> entries = complete(eventMapTree.getChildren(dpath));
+ return entries;
+ }
+
+ @Override
+ public DocumentPath getDocumentPath(String path) throws WorkflowException {
+ DocumentPath dpath = DocumentPath.from(path);
+ return dpath;
+ }
+
+ @Override
+ public ObjectNode asJsonTree() throws WorkflowException {
+
+ DocumentPath rootPath = DocumentPath.from(Lists.newArrayList("root"));
+ Map<String, Versioned<String>> eventmap = complete(eventMapTree.getChildren(rootPath));
+
+ ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
+
+ for (Map.Entry<String, Versioned<String>> eventTypeEntry : eventmap.entrySet()) {
+
+ String eventType = eventTypeEntry.getKey();
+
+ ObjectNode eventTypeNode = JsonNodeFactory.instance.objectNode();
+ rootNode.put(eventType, eventTypeNode);
+
+ DocumentPath eventTypePath = DocumentPath.from(Lists.newArrayList("root", eventType));
+ Map<String, Versioned<String>> hintmap = complete(eventMapTree.getChildren(eventTypePath));
+
+ for (Map.Entry<String, Versioned<String>> hintEntry : hintmap.entrySet()) {
+
+ String hint = hintEntry.getKey();
+
+ ObjectNode hintNode = JsonNodeFactory.instance.objectNode();
+ eventTypeNode.put(hint, hintNode);
+
+ DocumentPath hintPath = DocumentPath.from(Lists.newArrayList("root", eventType, hint));
+ Map<String, Versioned<String>> contextmap = complete(eventMapTree.getChildren(hintPath));
+
+ for (Map.Entry<String, Versioned<String>> ctxtEntry : contextmap.entrySet()) {
+ hintNode.put(ctxtEntry.getKey(), ctxtEntry.getValue().value());
+ }
+ }
+ }
+
+ return rootNode;
+ }
+
+ private <T> T complete(CompletableFuture<T> future) throws WorkflowException {
+ try {
+ return future.get();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new WorkflowException(e.getCause().getMessage());
+ } catch (ExecutionException e) {
+ if (e.getCause() instanceof IllegalDocumentModificationException) {
+ throw new WorkflowException("Node or parent does not exist or is root or is not a Leaf Node",
+ e.getCause());
+ } else if (e.getCause() instanceof NoSuchDocumentPathException) {
+ return null;
+ } else {
+ throw new WorkflowException("Datastore operation failed", e.getCause());
+ }
+ }
+ }
+
+ private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) throws WorkflowException {
+ return Optional.ofNullable(complete(future))
+ .map(Versioned::value)
+ .orElse(null);
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/DistributedWorkplaceStore.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/DistributedWorkplaceStore.java
new file mode 100644
index 0000000..ca8a454
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/DistributedWorkplaceStore.java
@@ -0,0 +1,394 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.BaseJsonNode;
+import com.fasterxml.jackson.databind.node.BigIntegerNode;
+import com.fasterxml.jackson.databind.node.BinaryNode;
+import com.fasterxml.jackson.databind.node.BooleanNode;
+import com.fasterxml.jackson.databind.node.ContainerNode;
+import com.fasterxml.jackson.databind.node.DecimalNode;
+import com.fasterxml.jackson.databind.node.DoubleNode;
+import com.fasterxml.jackson.databind.node.FloatNode;
+import com.fasterxml.jackson.databind.node.IntNode;
+import com.fasterxml.jackson.databind.node.JsonNodeCreator;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.JsonNodeType;
+import com.fasterxml.jackson.databind.node.LongNode;
+import com.fasterxml.jackson.databind.node.MissingNode;
+import com.fasterxml.jackson.databind.node.NullNode;
+import com.fasterxml.jackson.databind.node.NumericNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.POJONode;
+import com.fasterxml.jackson.databind.node.ShortNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.fasterxml.jackson.databind.node.ValueNode;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.workflow.api.DataModelTree;
+import org.onosproject.workflow.api.DefaultWorkplace;
+import org.onosproject.workflow.api.DefaultWorkflowContext;
+import org.onosproject.workflow.api.JsonDataModelTree;
+import org.onosproject.workflow.api.SystemWorkflowContext;
+import org.onosproject.workflow.api.WorkflowContext;
+import org.onosproject.workflow.api.WorkflowData;
+import org.onosproject.workflow.api.WorkflowState;
+import org.onosproject.workflow.api.Workplace;
+import org.onosproject.workflow.api.WorkflowDataEvent;
+import org.onosproject.workflow.api.WorkplaceStore;
+import org.onosproject.workflow.api.WorkplaceStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageException;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(immediate = true)
+@Service
+public class DistributedWorkplaceStore
+ extends AbstractStore<WorkflowDataEvent, WorkplaceStoreDelegate> implements WorkplaceStore {
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected StorageService storageService;
+
+ private ApplicationId appId;
+ private final Logger log = getLogger(getClass());
+
+ private final WorkplaceMapListener workplaceMapEventListener = new WorkplaceMapListener();
+ private ConsistentMap<String, WorkflowData> workplaceMap;
+ private Map<String, Workplace> localWorkplaceMap = Maps.newConcurrentMap();
+
+ private final WorkflowContextMapListener contextMapEventListener = new WorkflowContextMapListener();
+ private ConsistentMap<String, WorkflowData> contextMap;
+ private Map<String, WorkflowContext> localContextMap = Maps.newConcurrentMap();
+
+ private Map<String, Map<String, WorkflowContext>> localWorkplaceMemberMap = Maps.newConcurrentMap();
+
+ @Activate
+ public void activate() {
+
+ appId = coreService.registerApplication("org.onosproject.workplacestore");
+ log.info("appId=" + appId);
+
+ KryoNamespace workplaceNamespace = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(WorkflowData.class)
+ .register(Workplace.class)
+ .register(DefaultWorkplace.class)
+ .register(WorkflowContext.class)
+ .register(DefaultWorkflowContext.class)
+ .register(SystemWorkflowContext.class)
+ .register(WorkflowState.class)
+ .register(DataModelTree.class)
+ .register(JsonDataModelTree.class)
+ .register(List.class)
+ .register(ArrayList.class)
+ .register(JsonNode.class)
+ .register(ObjectNode.class)
+ .register(TextNode.class)
+ .register(LinkedHashMap.class)
+ .register(ArrayNode.class)
+ .register(BaseJsonNode.class)
+ .register(BigIntegerNode.class)
+ .register(BinaryNode.class)
+ .register(BooleanNode.class)
+ .register(ContainerNode.class)
+ .register(DecimalNode.class)
+ .register(DoubleNode.class)
+ .register(FloatNode.class)
+ .register(IntNode.class)
+ .register(JsonNodeType.class)
+ .register(LongNode.class)
+ .register(MissingNode.class)
+ .register(NullNode.class)
+ .register(NumericNode.class)
+ .register(POJONode.class)
+ .register(ShortNode.class)
+ .register(ValueNode.class)
+ .register(JsonNodeCreator.class)
+ .register(JsonNodeFactory.class)
+ .build();
+
+ localWorkplaceMap.clear();
+ workplaceMap = storageService.<String, WorkflowData>consistentMapBuilder()
+ .withSerializer(Serializer.using(workplaceNamespace))
+ .withName("workplace-map")
+ .withApplicationId(appId)
+ .build();
+ workplaceMap.addListener(workplaceMapEventListener);
+
+ localContextMap.clear();
+ contextMap = storageService.<String, WorkflowData>consistentMapBuilder()
+ .withSerializer(Serializer.using(workplaceNamespace))
+ .withName("workflow-context-map")
+ .withApplicationId(appId)
+ .build();
+ contextMap.addListener(contextMapEventListener);
+
+ workplaceMapEventListener.syncLocal();
+ contextMapEventListener.syncLocal();
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ workplaceMap.destroy();
+ localWorkplaceMap.clear();
+ contextMap.destroy();
+ localContextMap.clear();
+
+ log.info("Stopped");
+ }
+
+ @Override
+ public void registerWorkplace(String name, Workplace workplace) throws StorageException {
+ workplaceMap.put(name, workplace);
+ }
+
+ @Override
+ public void removeWorkplace(String name) throws StorageException {
+ removeWorkplaceContexts(name);
+ workplaceMap.remove(name);
+ }
+
+ @Override
+ public Workplace getWorkplace(String name) throws StorageException {
+ return localWorkplaceMap.get(name);
+ }
+
+ @Override
+ public Collection<Workplace> getWorkplaces() throws StorageException {
+ return ImmutableList.copyOf(localWorkplaceMap.values());
+ }
+
+ @Override
+ public void commitWorkplace(String name, Workplace workplace, boolean handleEvent) throws StorageException {
+ workplace.setTriggerNext(handleEvent);
+ if (workplaceMap.containsKey(name)) {
+ workplaceMap.replace(name, workplace);
+ } else {
+ registerWorkplace(name, workplace);
+ }
+ }
+
+ @Override
+ public void registerContext(String name, WorkflowContext context) throws StorageException {
+ contextMap.put(name, context);
+ }
+
+ @Override
+ public void removeContext(String name) throws StorageException {
+ contextMap.remove(name);
+ }
+
+ @Override
+ public WorkflowContext getContext(String name) throws StorageException {
+ return localContextMap.get(name);
+ }
+
+ @Override
+ public void commitContext(String name, WorkflowContext context, boolean handleEvent) throws StorageException {
+ context.setTriggerNext(handleEvent);
+ if (contextMap.containsKey(name)) {
+ contextMap.replace(name, context);
+ } else {
+ registerContext(name, context);
+ }
+ }
+
+ @Override
+ public Collection<WorkflowContext> getContexts() throws StorageException {
+ return ImmutableList.copyOf(localContextMap.values());
+ }
+
+ @Override
+ public Collection<WorkflowContext> getWorkplaceContexts(String workplaceName) {
+ Map<String, WorkflowContext> ctxMap = localWorkplaceMemberMap.get(workplaceName);
+ if (ctxMap == null) {
+ return Collections.emptyList();
+ }
+
+ return ImmutableList.copyOf(ctxMap.values());
+ }
+
+ @Override
+ public void removeWorkplaceContexts(String workplaceName) {
+ for (WorkflowContext ctx : getWorkplaceContexts(workplaceName)) {
+ removeContext(ctx.name());
+ }
+ }
+
+ private class WorkplaceMapListener implements MapEventListener<String, WorkflowData> {
+
+ @Override
+ public void event(MapEvent<String, WorkflowData> event) {
+
+ Workplace newWorkplace = (Workplace) Versioned.valueOrNull(event.newValue());
+ Workplace oldWorkplace = (Workplace) Versioned.valueOrNull(event.oldValue());
+
+ log.info("WorkplaceMap event: {}", event);
+ switch (event.type()) {
+ case INSERT:
+ insert(newWorkplace);
+ notifyDelegate(new WorkflowDataEvent(WorkflowDataEvent.Type.INSERT, newWorkplace));
+ break;
+ case UPDATE:
+ update(newWorkplace);
+ notifyDelegate(new WorkflowDataEvent(WorkflowDataEvent.Type.UPDATE, newWorkplace));
+ break;
+ case REMOVE:
+ remove(oldWorkplace);
+ notifyDelegate(new WorkflowDataEvent(WorkflowDataEvent.Type.REMOVE, oldWorkplace));
+ break;
+ default:
+ }
+ }
+
+ private void insert(Workplace workplace) {
+ localWorkplaceMap.put(workplace.name(), workplace);
+ }
+
+ private void update(Workplace workplace) {
+ localWorkplaceMap.replace(workplace.name(), workplace);
+ }
+
+ private void remove(Workplace workplace) {
+ localWorkplaceMap.remove(workplace.name());
+ }
+
+ public void syncLocal() {
+ workplaceMap.values().stream().forEach(
+ x -> insert((Workplace) (x.value()))
+ );
+ }
+ }
+
+ private class WorkflowContextMapListener implements MapEventListener<String, WorkflowData> {
+
+ @Override
+ public void event(MapEvent<String, WorkflowData> event) {
+
+ WorkflowContext newContext = (WorkflowContext) Versioned.valueOrNull(event.newValue());
+ WorkflowContext oldContext = (WorkflowContext) Versioned.valueOrNull(event.oldValue());
+
+ log.info("WorkflowContext event: {}", event);
+ switch (event.type()) {
+ case INSERT:
+ insert(newContext);
+ notifyDelegate(new WorkflowDataEvent(WorkflowDataEvent.Type.INSERT, newContext));
+ break;
+ case UPDATE:
+ update(newContext);
+ notifyDelegate(new WorkflowDataEvent(WorkflowDataEvent.Type.UPDATE, newContext));
+ break;
+ case REMOVE:
+ remove(oldContext);
+ notifyDelegate(new WorkflowDataEvent(WorkflowDataEvent.Type.REMOVE, oldContext));
+ break;
+ default:
+ }
+ }
+
+ /**
+ * Inserts workflow context on local hash map.
+ * @param context workflow context
+ */
+ private void insert(WorkflowContext context) {
+ String workplaceName = context.workplaceName();
+ Map<String, WorkflowContext> ctxMap = localWorkplaceMemberMap.get(workplaceName);
+ if (ctxMap == null) {
+ ctxMap = new HashMap<>();
+ localWorkplaceMemberMap.put(workplaceName, ctxMap);
+ }
+ ctxMap.put(context.name(), context);
+
+ localContextMap.put(context.name(), context);
+ }
+
+ /**
+ * Updates workflow context on local hash map.
+ * @param context workflow context
+ */
+ private void update(WorkflowContext context) {
+ String workplaceName = context.workplaceName();
+ Map<String, WorkflowContext> ctxMap = localWorkplaceMemberMap.get(workplaceName);
+ if (ctxMap == null) {
+ ctxMap = new HashMap<>();
+ localWorkplaceMemberMap.put(workplaceName, ctxMap);
+ }
+ ctxMap.put(context.name(), context);
+
+ localContextMap.put(context.name(), context);
+ }
+
+ /**
+ * Removes workflow context from local hash map.
+ * @param context workflow context
+ */
+ private void remove(WorkflowContext context) {
+ localContextMap.remove(context.name());
+
+ String workplaceName = context.workplaceName();
+ Map<String, WorkflowContext> ctxMap = localWorkplaceMemberMap.get(workplaceName);
+ if (ctxMap == null) {
+ log.error("remove-context: Failed to find workplace({}) in localWorkplaceMemberMap", workplaceName);
+ return;
+ }
+ ctxMap.remove(context.name());
+ if (ctxMap.size() == 0) {
+ localWorkplaceMemberMap.remove(workplaceName, ctxMap);
+ }
+ }
+
+ /**
+ * Synchronizes local hash map.
+ */
+ public void syncLocal() {
+ contextMap.values().stream().forEach(
+ x -> insert((WorkflowContext) (x.value()))
+ );
+ }
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/ECWorkFlowStore.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/ECWorkFlowStore.java
new file mode 100644
index 0000000..cd7df77
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/ECWorkFlowStore.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.workflow.api.AbstractWorkflow;
+import org.onosproject.workflow.api.ImmutableListWorkflow;
+import org.onosproject.workflow.api.Workflow;
+import org.onosproject.workflow.api.WorkflowAttribute;
+import org.onosproject.workflow.api.WorkflowStore;
+import org.onosproject.net.group.GroupEvent;
+import org.onosproject.net.group.GroupStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.EventuallyConsistentMap;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.WallClockTimestamp;
+import org.slf4j.Logger;
+
+import java.net.URI;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.List;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(immediate = true)
+@Service
+public class ECWorkFlowStore
+ extends AbstractStore<GroupEvent, GroupStoreDelegate> implements WorkflowStore {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected StorageService storageService;
+
+ private ApplicationId appId;
+ private EventuallyConsistentMap<URI, Workflow> workflowStore;
+ private Set<ClassLoader> classloaders = Sets.newConcurrentHashSet();
+
+ @Activate
+ public void activate() {
+
+ appId = coreService.registerApplication("org.onosproject.nfconfig");
+ log.info("appId=" + appId);
+
+ KryoNamespace.Builder workflowSerializer = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(URI.class)
+ .register(Workflow.class)
+ .register(AbstractWorkflow.class)
+ .register(ImmutableListWorkflow.class)
+ .register(List.class)
+ .register(ImmutableList.class)
+ .register(Class.class)
+ .register(WorkflowAttribute.class)
+ .register(Set.class)
+ .register(ImmutableSet.class)
+ .register(HashSet.class);
+
+ workflowStore = storageService.<URI, Workflow>eventuallyConsistentMapBuilder()
+ .withName("workflow-workplaceStore")
+ .withSerializer(workflowSerializer)
+ .withAntiEntropyPeriod(5, TimeUnit.SECONDS)
+ .withTimestampProvider((k, v) -> new WallClockTimestamp())
+ .withTombstonesDisabled()
+ .build();
+
+ classloaders.add(this.getClass().getClassLoader());
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ workflowStore.destroy();
+
+ log.info("Stopped");
+ }
+
+ @Override
+ public void register(Workflow workflow) {
+ workflowStore.put(workflow.id(), workflow);
+ }
+
+ @Override
+ public void unregister(URI id) {
+ workflowStore.remove(id);
+ }
+
+ @Override
+ public Workflow get(URI id) {
+ return workflowStore.get(id);
+ }
+
+ @Override
+ public Collection<Workflow> getAll() {
+ return workflowStore.values();
+ }
+
+ @Override
+ public void registerLocal(ClassLoader loader) {
+ classloaders.add(loader);
+ }
+
+ @Override
+ public void unregisterLocal(ClassLoader loader) {
+ classloaders.remove(loader);
+ }
+
+ @Override
+ public Class getClass(String name) throws ClassNotFoundException {
+ for (ClassLoader loader : classloaders) {
+ Class cl = null;
+ try {
+ cl = loader.loadClass(name);
+ } catch (ClassNotFoundException e) {
+ // do nothing
+ }
+ if (cl != null) {
+ return cl;
+ }
+ }
+ throw new ClassNotFoundException(name);
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/HandlerTaskAccumulator.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/HandlerTaskAccumulator.java
new file mode 100644
index 0000000..4651cd4
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/HandlerTaskAccumulator.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.onlab.util.AbstractAccumulator;
+import org.onosproject.workflow.api.HandlerTask;
+import org.onosproject.workflow.api.HandlerTaskBatchDelegate;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Timer;
+
+/**
+ * An accumulator for building batches of event task operations. Only one batch should
+ * be in process per instance at a time.
+ */
+public class HandlerTaskAccumulator extends AbstractAccumulator<HandlerTask> {
+
+ private static final int DEFAULT_MAX_EVENTS = 5000;
+ private static final int DEFAULT_MAX_IDLE_MS = 10;
+ private static final int DEFAULT_MAX_BATCH_MS = 50;
+
+ private static final Timer TIMER = new Timer("onos-workflow-handlertask-batching");
+
+ private final HandlerTaskBatchDelegate delegate;
+
+ private volatile boolean ready;
+
+ /**
+ * Creates an event-task operation accumulator.
+ *
+ * @param delegate the event-task batch delegate
+ */
+ protected HandlerTaskAccumulator(HandlerTaskBatchDelegate delegate) {
+ super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
+ this.delegate = delegate;
+ // Assume that the delegate is ready for workletType at the start
+ ready = true; //TODO validate the assumption that delegate is ready
+ }
+
+ @Override
+ public void processItems(List<HandlerTask> items) {
+ ready = false;
+ delegate.execute(epoch(items));
+ }
+
+ /**
+ * Gets epoch.
+ * @param ops handler tasks
+ * @return collection of collection of handler task.
+ */
+ private Collection<Collection<HandlerTask>> epoch(List<HandlerTask> ops) {
+
+ ListMultimap<String, HandlerTask> tasks = ArrayListMultimap.create();
+
+ // align event-tasks with context
+ for (HandlerTask op : ops) {
+ tasks.put(op.context().name(), op);
+ }
+
+ return tasks.asMap().values();
+ }
+
+ @Override
+ public boolean isReady() {
+ return ready;
+ }
+
+ /**
+ * Making accumulator to be ready.
+ */
+ public void ready() {
+ ready = true;
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkFlowEngine.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkFlowEngine.java
new file mode 100644
index 0000000..26e0bc9
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkFlowEngine.java
@@ -0,0 +1,740 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.google.common.collect.Lists;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.workflow.api.DefaultWorkplace;
+import org.onosproject.workflow.api.EventHintSupplier;
+import org.onosproject.workflow.api.EventTask;
+import org.onosproject.workflow.api.JsonDataModelTree;
+import org.onosproject.workflow.api.SystemWorkflowContext;
+import org.onosproject.workflow.api.EventTimeoutTask;
+import org.onosproject.workflow.api.TimeoutTask;
+import org.onosproject.workflow.api.TimerChain;
+import org.onosproject.workflow.api.Worklet;
+import org.onosproject.workflow.api.Workflow;
+import org.onosproject.workflow.api.WorkflowContext;
+import org.onosproject.workflow.api.WorkflowData;
+import org.onosproject.workflow.api.ContextEventMapStore;
+import org.onosproject.workflow.api.WorkflowState;
+import org.onosproject.workflow.api.WorkflowStore;
+import org.onosproject.workflow.api.WorkflowBatchDelegate;
+import org.onosproject.workflow.api.WorkflowDataEvent;
+import org.onosproject.workflow.api.WorkflowDataListener;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.HandlerTask;
+import org.onosproject.workflow.api.HandlerTaskBatchDelegate;
+import org.onosproject.workflow.api.Workplace;
+import org.onosproject.workflow.api.WorkplaceStore;
+import org.onosproject.workflow.api.WorkplaceStoreDelegate;
+import org.onosproject.workflow.api.WorkflowExecutionService;
+import org.onosproject.event.AbstractListenerManager;
+import org.onosproject.event.Event;
+import org.onosproject.net.intent.WorkPartitionService;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.stream.Collectors;
+
+import static java.util.concurrent.Executors.newFixedThreadPool;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.workflow.api.WorkflowAttribute.REMOVE_AFTER_COMPLETE;
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(immediate = true)
+@Service
+public class WorkFlowEngine extends AbstractListenerManager<WorkflowDataEvent, WorkflowDataListener>
+ implements WorkflowExecutionService {
+
+ protected static final Logger log = getLogger(WorkFlowEngine.class);
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ClusterService clusterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LeadershipService leadershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkPartitionService partitionService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkplaceStore workplaceStore;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkflowStore workflowStore;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ContextEventMapStore eventMapStore;
+
+ private final WorkplaceStoreDelegate workplaceStoreDelegate = this::post;
+
+ private final WorkflowBatchDelegate workflowBatchDelegate = new InternalWorkflowBatchDelegate();
+ private final WorkflowAccumulator workflowAccumulator = new WorkflowAccumulator(workflowBatchDelegate);
+
+ private final HandlerTaskBatchDelegate eventtaskBatchDelegate = new InternalHandlerTaskBatchDelegate();
+ private final HandlerTaskAccumulator eventtaskAccumulator = new HandlerTaskAccumulator(eventtaskBatchDelegate);
+
+ private ExecutorService workflowBatchExecutor;
+ private ExecutorService workflowExecutor;
+
+ private ExecutorService handlerTaskBatchExecutor;
+ private ExecutorService handlerTaskExecutor;
+
+ private static final int DEFAULT_WORKFLOW_THREADS = 12;
+ private static final int DEFAULT_EVENTTASK_THREADS = 12;
+ private static final int MAX_REGISTER_EVENTMAP_WAITS = 10;
+
+ private ScheduledExecutorService eventMapTriggerExecutor;
+
+ private TimerChain timerChain = new TimerChain();
+
+ public static final String APPID = "org.onosproject.workflow";
+ private ApplicationId appId;
+ private NodeId localNodeId;
+
+ @Activate
+ public void activate() {
+ appId = coreService.registerApplication(APPID);
+ workplaceStore.setDelegate(workplaceStoreDelegate);
+ localNodeId = clusterService.getLocalNode().id();
+ leadershipService.runForLeadership(appId.name());
+
+ workflowBatchExecutor = newSingleThreadExecutor(
+ groupedThreads("onos/workflow", "workflow-batch", log));
+ workflowExecutor = newFixedThreadPool(DEFAULT_WORKFLOW_THREADS,
+ groupedThreads("onos/workflow-exec", "worker-%d", log));
+ handlerTaskBatchExecutor = newSingleThreadExecutor(
+ groupedThreads("onos/workflow", "handlertask-batch", log));
+ handlerTaskExecutor = newFixedThreadPool(DEFAULT_EVENTTASK_THREADS,
+ groupedThreads("onos/handlertask-exec", "worker-%d", log));
+ eventMapTriggerExecutor = newSingleThreadScheduledExecutor(
+ groupedThreads("onos/workflow-engine", "eventmap-trigger-executor"));
+
+ (new WorkplaceWorkflow(this, workflowStore)).registerWorkflows();
+ JsonDataModelTree data = new JsonDataModelTree(JsonNodeFactory.instance.objectNode());
+ workplaceStore.registerWorkplace(Workplace.SYSTEM_WORKPLACE,
+ new DefaultWorkplace(Workplace.SYSTEM_WORKPLACE, data));
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ leadershipService.withdraw(appId.name());
+ workplaceStore.unsetDelegate(workplaceStoreDelegate);
+ workflowBatchExecutor.shutdown();
+ workflowExecutor.shutdown();
+ handlerTaskBatchExecutor.shutdown();
+ handlerTaskExecutor.shutdown();
+ eventMapTriggerExecutor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void execInitWorklet(WorkflowContext context) {
+
+ Workflow workflow = workflowStore.get(context.workflowId());
+ if (workflow == null) {
+ log.error("Invalid workflow {}", context.workflowId());
+ return;
+ }
+
+ initWorkletExecution(context);
+ try {
+ Worklet initWorklet = workflow.init(context);
+ if (initWorklet != null) {
+ initWorklet.process(context);
+ }
+
+ } catch (WorkflowException e) {
+ log.error("Exception: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ 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);
+ }
+
+ @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: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ return;
+ }
+ if (eventHint == null) {
+ // do nothing
+ log.error("Invalid eventHint, event: {}", event);
+ return;
+ }
+
+ try {
+ eventMap = eventMapStore.getEventMap(event.getClass().getName(), eventHint);
+ } catch (WorkflowException e) {
+ log.error("Exception: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ 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 workletType = entry.getValue();
+ WorkflowContext context = workplaceStore.getContext(contextName);
+ if (Objects.isNull(context)) {
+ log.info("Invalid context: {}, event: {}", contextName, event);
+ continue;
+ }
+ EventTask eventtask = EventTask.builder()
+ .event(event)
+ .eventHint(eventHint)
+ .context(context)
+ .workletType(workletType)
+ .build();
+
+ log.info("eventtaskAccumulator.add: task: {}", eventtask);
+ eventtaskAccumulator.add(eventtask);
+ }
+ }
+
+ @Override
+ public void registerEventMap(Class<? extends Event> eventType, String eventHint,
+ String contextName, String workletType) throws WorkflowException {
+ eventMapStore.registerEventMap(eventType.getName(), eventHint, contextName, workletType);
+ for (int i = 0; i < MAX_REGISTER_EVENTMAP_WAITS; i++) {
+ Map<String, String> eventMap = eventMapStore.getEventMap(eventType.getName(), eventHint);
+ if (eventMap != null && eventMap.containsKey(contextName)) {
+ return;
+ }
+ try {
+ log.info("sleep {}", i);
+ Thread.sleep(10L * (i + 1));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ @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.info("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;
+ }
+ }
+ )
+ );
+ }
+
+ /**
+ * Checks whether this workflow data job is relevant to this ONOS node.
+ * @param job workflow data
+ * @return checking result
+ */
+ private boolean isRelevant(WorkflowData job) {
+ // distributes event processing with work-partition
+ return partitionService.isMine(job.distributor(), this::stringHash);
+ }
+
+ /**
+ * Gets hash of the string.
+ * @param str string to get a hash
+ * @return hash value
+ */
+ public Long stringHash(String str) {
+ return UUID.nameUUIDFromBytes(str.getBytes()).getMostSignificantBits();
+ }
+
+ /**
+ * Class for handler task batch delegation.
+ */
+ private class InternalHandlerTaskBatchDelegate implements HandlerTaskBatchDelegate {
+ @Override
+ public void execute(Collection<Collection<HandlerTask>> operations) {
+ log.debug("Execute {} operation(s).", operations.size());
+
+ CompletableFuture.runAsync(() -> {
+ List<CompletableFuture<Collection<HandlerTask>>> futures = operations.stream()
+ .map(
+ x -> CompletableFuture.completedFuture(x)
+ .thenApplyAsync(WorkFlowEngine.this::processHandlerTask, handlerTaskExecutor)
+ .exceptionally(e -> null)
+ )
+ .collect(Collectors.toList());
+
+ // waiting the completion of futures
+ futures.parallelStream().forEach(x -> x.join());
+
+ }, handlerTaskBatchExecutor).exceptionally(e -> {
+ log.error("Error submitting batches:", e);
+ return null;
+ }).thenRun(eventtaskAccumulator::ready);
+ }
+ }
+
+ /**
+ * Initializes worklet execution.
+ * @param context workflow context
+ */
+ private void initWorkletExecution(WorkflowContext context) {
+ context.setState(WorkflowState.RUNNING);
+ context.setCause("");
+ context.setWorkflowExecutionService(this);
+ context.setWorkflowStore(workflowStore);
+ context.setWorkplaceStore(workplaceStore);
+ context.waitCompletion(null, null, null, 0L);
+ context.setTriggerNext(false);
+ }
+
+ /**
+ * Processes handler tasks.
+ * @param tasks handler tasks
+ * @return handler tasks processed
+ */
+ private Collection<HandlerTask> processHandlerTask(Collection<HandlerTask> tasks) {
+
+ for (HandlerTask task : tasks) {
+ if (task instanceof EventTimeoutTask) {
+ execEventTimeoutTask((EventTimeoutTask) task);
+ } else if (task instanceof TimeoutTask) {
+ execTimeoutTask((TimeoutTask) task);
+ } else if (task instanceof EventTask) {
+ execEventTask((EventTask) task);
+ } else {
+ log.error("Unsupported handler task {}", task);
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Executes event task.
+ * @param task event task
+ * @return event task
+ */
+ private EventTask execEventTask(EventTask task) {
+
+ Map<String, String> eventMap = null;
+ try {
+ eventMap = eventMapStore.getEventMap(task.event().getClass().getName(), task.eventHint());
+ } catch (WorkflowException e) {
+ log.error("Exception: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ return task;
+ }
+
+ if (Objects.isNull(eventMap) || eventMap.isEmpty()) {
+ return task;
+ }
+
+ if (Objects.isNull(eventMap.get(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 {
+ Worklet worklet = workflow.getWorkletInstance(task.workletType());
+ if (!Objects.equals(latestContext.current(), worklet.tag())) {
+ log.error("Current worklet({}) is not mismatched with task work({}). Ignored.",
+ latestContext.current(), worklet.tag());
+ return task;
+ }
+
+ if (worklet == Worklet.Common.COMPLETED || worklet == Worklet.Common.INIT) {
+ log.error("Current worklet is {}, Ignored", worklet);
+ return task;
+ }
+
+ initWorkletExecution(latestContext);
+
+ log.info("processHandlerTask.isCompleted-task:{}, latest:{}", task, latestContext);
+ if (worklet.isCompleted(latestContext, task.event())) {
+ eventMapStore.unregisterEventMap(
+ task.eventType(), task.eventHint(), latestContext.name());
+
+ workplaceStore.commitContext(latestContext.name(), latestContext, true);
+ return null;
+ } else {
+ workplaceStore.commitContext(latestContext.name(), latestContext, false);
+ }
+
+ } catch (WorkflowException e) {
+ log.error("Exception: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ latestContext.setCause(e.getMessage());
+ latestContext.setState(WorkflowState.EXCEPTION);
+ workplaceStore.commitContext(latestContext.name(), latestContext, false);
+ }
+
+ return task;
+ }
+
+ /**
+ * Executes event timeout task.
+ * @param task event timeout task
+ * @return handler task
+ */
+ private HandlerTask execEventTimeoutTask(EventTimeoutTask task) {
+
+ Map<String, String> eventMap = null;
+ try {
+ eventMap = eventMapStore.getEventMap(task.eventType(), task.eventHint());
+ } catch (WorkflowException e) {
+ log.error("execEventTimeoutTask: Exception: {}, trace: {}",
+ e, Lists.newArrayList(e.getStackTrace()));
+ return task;
+ }
+
+ if (Objects.isNull(eventMap) || eventMap.isEmpty()) {
+ return task;
+ }
+
+ if (Objects.isNull(eventMap.get(task.context().name()))) {
+ return task;
+ }
+
+ log.debug("execEventTimeoutTask- task: {}, hash: {}", task, stringHash(task.context().distributor()));
+
+ WorkflowContext context = 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 {
+ Worklet worklet = workflow.getWorkletInstance(task.workletType());
+ if (!Objects.equals(latestContext.current(), worklet.tag())) {
+ log.error("execEventTimeoutTask: Current worklet({}) is not mismatched with task work({}). Ignored.",
+ latestContext.current(), worklet.tag());
+ return task;
+ }
+
+ if (worklet == Worklet.Common.COMPLETED || worklet == Worklet.Common.INIT) {
+ log.error("execEventTimeoutTask: Current worklet is {}, Ignored", worklet);
+ return task;
+ }
+
+ initWorkletExecution(latestContext);
+
+ log.info("execEventTimeoutTask.timeout-task:{}, latest:{}", task, latestContext);
+ eventMapStore.unregisterEventMap(
+ task.eventType(), task.eventHint(), latestContext.name());
+
+ worklet.timeout(latestContext);
+ workplaceStore.commitContext(latestContext.name(), latestContext, latestContext.triggerNext());
+
+ } catch (WorkflowException e) {
+ log.error("Exception: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ latestContext.setCause(e.getMessage());
+ latestContext.setState(WorkflowState.EXCEPTION);
+ workplaceStore.commitContext(latestContext.name(), latestContext, false);
+ }
+
+ return task;
+ }
+
+ /**
+ * 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 {
+ Worklet worklet = workflow.getWorkletInstance(task.workletType());
+ if (!Objects.equals(latestContext.current(), worklet.tag())) {
+ log.error("execTimeoutTask: Current worklet({}) is not mismatched with task work({}). Ignored.",
+ latestContext.current(), worklet.tag());
+ return task;
+ }
+
+ if (worklet == Worklet.Common.COMPLETED || worklet == Worklet.Common.INIT) {
+ log.error("execTimeoutTask: Current worklet is {}, Ignored", worklet);
+ return task;
+ }
+
+ initWorkletExecution(latestContext);
+
+ worklet.timeout(latestContext);
+ workplaceStore.commitContext(latestContext.name(), latestContext, latestContext.triggerNext());
+
+ } catch (WorkflowException e) {
+ log.error("Exception: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ latestContext.setCause(e.getMessage());
+ latestContext.setState(WorkflowState.EXCEPTION);
+ workplaceStore.commitContext(latestContext.name(), latestContext, false);
+ }
+
+ return task;
+ }
+
+ /**
+ * Class for delegation of workflow batch execution.
+ */
+ private class InternalWorkflowBatchDelegate implements WorkflowBatchDelegate {
+ @Override
+ public void execute(Collection<WorkflowData> operations) {
+ log.debug("Execute {} operation(s).", operations.size());
+
+ CompletableFuture.runAsync(() -> {
+ List<CompletableFuture<WorkflowData>> futures = operations.stream()
+ .map(
+ x -> CompletableFuture.completedFuture(x)
+ .thenApplyAsync(WorkFlowEngine.this::execWorkflow, workflowExecutor)
+ .exceptionally(e -> null)
+ )
+ .collect(Collectors.toList());
+
+ // waiting the completion of futures
+ futures.parallelStream().forEach(x -> x.join());
+
+ }, workflowBatchExecutor).exceptionally(e -> {
+ log.error("Error submitting batches:", e);
+ return null;
+ }).thenRun(workflowAccumulator::ready);
+ }
+ }
+
+ /**
+ * Executes workflow.
+ * @param dataModelContainer workflow data model container(workflow or workplace)
+ * @return
+ */
+ private WorkflowData execWorkflow(WorkflowData dataModelContainer) {
+ if (dataModelContainer instanceof WorkflowContext) {
+ return execWorkflowContext((WorkflowContext) dataModelContainer);
+ } else if (dataModelContainer instanceof Workplace) {
+ return execWorkplace((Workplace) dataModelContainer);
+ } else {
+ log.error("Invalid context {}", dataModelContainer);
+ return null;
+ }
+ }
+
+ /**
+ * 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 Worklet worklet = workflow.next(latestContext);
+
+ if (worklet == Worklet.Common.INIT) {
+ log.error("workflow.next gave INIT. It cannot be executed (context: {})", context.name());
+ return latestContext;
+ }
+
+ latestContext.setCurrent(worklet);
+ 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("execWorkflowContext.process:{}, {}", worklet.tag(), latestContext);
+ worklet.process(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.completionEventHint(),
+ latestContext.name(), worklet.tag());
+
+ latestContext.completionEventGenerator().apply();
+
+ if (latestContext.completionEventTimeout() != 0L) {
+ final EventTimeoutTask eventTimeoutTask = EventTimeoutTask.builder()
+ .context(latestContext)
+ .workletType(worklet.tag())
+ .eventType(latestContext.completionEventType().getName())
+ .eventHint(latestContext.completionEventHint())
+ .build();
+ timerChain.schedule(latestContext.completionEventTimeout(),
+ () -> {
+ eventtaskAccumulator.add(eventTimeoutTask);
+ });
+ }
+ } else {
+ if (latestContext.completionEventTimeout() != 0L) {
+ final TimeoutTask timeoutTask = TimeoutTask.builder()
+ .context(latestContext)
+ .workletType(worklet.tag())
+ .build();
+
+ timerChain.schedule(latestContext.completionEventTimeout(),
+ () -> {
+ eventtaskAccumulator.add(timeoutTask);
+ });
+ }
+ }
+
+ workplaceStore.commitContext(latestContext.name(), latestContext, latestContext.triggerNext());
+
+ } catch (WorkflowException e) {
+ log.error("Exception: {}, trace: {}", e, Lists.newArrayList(e.getStackTrace()));
+ latestContext.setCause(e.getMessage());
+ latestContext.setState(WorkflowState.EXCEPTION);
+ workplaceStore.commitContext(latestContext.name(), latestContext, false);
+ }
+
+ return latestContext;
+ }
+
+ /**
+ * Execute workplace.
+ * @param workplace workplace
+ * @return workplace
+ */
+ private Workplace execWorkplace(Workplace workplace) {
+
+ return null;
+ }
+
+}
\ No newline at end of file
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowAccumulator.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowAccumulator.java
new file mode 100644
index 0000000..d13b66f
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowAccumulator.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.google.common.collect.Maps;
+import org.onlab.util.AbstractAccumulator;
+import org.onosproject.workflow.api.WorkflowData;
+import org.onosproject.workflow.api.WorkflowBatchDelegate;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Timer;
+
+/**
+ * An accumulator for building batches of workflow operations. Only one batch should
+ * be in process per instance at a time.
+ */
+public class WorkflowAccumulator extends AbstractAccumulator<WorkflowData> {
+
+ private static final int DEFAULT_MAX_EVENTS = 1000;
+ private static final int DEFAULT_MAX_IDLE_MS = 10;
+ private static final int DEFAULT_MAX_BATCH_MS = 50;
+
+ private static final Timer TIMER = new Timer("onos-workflow-op-batching");
+
+ private final WorkflowBatchDelegate delegate;
+
+ private volatile boolean ready;
+
+ /**
+ * Creates an intent operation accumulator.
+ *
+ * @param delegate the intent batch delegate
+ */
+ protected WorkflowAccumulator(WorkflowBatchDelegate delegate) {
+ super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
+ this.delegate = delegate;
+ // Assume that the delegate is ready for worklettype at the start
+ ready = true; //TODO validate the assumption that delegate is ready
+ }
+
+ @Override
+ public void processItems(List<WorkflowData> items) {
+ ready = false;
+ delegate.execute(reduce(items));
+ }
+
+ private Collection<WorkflowData> reduce(List<WorkflowData> ops) {
+ Map<String, WorkflowData> map = Maps.newHashMap();
+ for (WorkflowData op : ops) {
+ map.put(op.name(), op);
+ }
+ //TODO check the version... or maybe workplaceStore will handle this.
+ return map.values();
+ }
+
+ @Override
+ public boolean isReady() {
+ return ready;
+ }
+
+ /**
+ * Making accumulator to be ready.
+ */
+ public void ready() {
+ ready = true;
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowManager.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowManager.java
new file mode 100644
index 0000000..804716e
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowManager.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.workflow.api.DefaultWorkplace;
+import org.onosproject.workflow.api.JsonDataModelTree;
+import org.onosproject.workflow.api.Workflow;
+import org.onosproject.workflow.api.WorkflowContext;
+import org.onosproject.workflow.api.WorkflowDescription;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.WorkflowService;
+import org.onosproject.workflow.api.WorkflowExecutionService;
+import org.onosproject.workflow.api.WorkflowStore;
+import org.onosproject.workflow.api.Workplace;
+import org.onosproject.workflow.api.WorkplaceDescription;
+import org.onosproject.workflow.api.WorkplaceStore;
+import org.slf4j.Logger;
+
+import java.net.URI;
+import java.util.Objects;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(immediate = true)
+@Service
+public class WorkflowManager implements WorkflowService {
+
+ protected static final Logger log = getLogger(WorkflowManager.class);
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ private WorkflowExecutionService workflowExecutionService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkplaceStore workplaceStore;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkflowStore workflowStore;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ private NetworkConfigService networkConfigService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ private NetworkConfigRegistry networkConfigRegistry;
+
+ private WorkflowNetConfigListener netcfgListener;
+
+ @Activate
+ public void activate() {
+ netcfgListener = new WorkflowNetConfigListener(this);
+ networkConfigRegistry.registerConfigFactory(netcfgListener.getConfigFactory());
+ networkConfigService.addListener(netcfgListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ networkConfigService.removeListener(netcfgListener);
+ networkConfigRegistry.unregisterConfigFactory(netcfgListener.getConfigFactory());
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createWorkplace(WorkplaceDescription wpDesc) throws WorkflowException {
+ log.info("createWorkplace: {}", wpDesc);
+
+ JsonNode root;
+ if (wpDesc.data().isPresent()) {
+ root = wpDesc.data().get();
+ } else {
+ root = JsonNodeFactory.instance.objectNode();
+ }
+ DefaultWorkplace workplace =
+ new DefaultWorkplace(wpDesc.name(), new JsonDataModelTree(root));
+ workplaceStore.registerWorkplace(wpDesc.name(), workplace);
+ }
+
+ @Override
+ public void removeWorkplace(WorkplaceDescription wpDesc) throws WorkflowException {
+ log.info("removeWorkplace: {}", wpDesc);
+ //TODO: Removing workflows belong to this workplace
+ workplaceStore.removeWorkplace(wpDesc.name());
+ }
+
+ @Override
+ public void clearWorkplace() throws WorkflowException {
+ log.info("clearWorkplace");
+ workplaceStore.getWorkplaces().stream()
+ .filter(wp -> !Objects.equals(wp.name(), Workplace.SYSTEM_WORKPLACE))
+ .forEach(wp -> workplaceStore.removeWorkplace(wp.name()));
+ }
+
+ @Override
+ public void invokeWorkflow(WorkflowDescription wfDesc) throws WorkflowException {
+ invokeWorkflow(wfDesc.toJson());
+ }
+
+ @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(WorkplaceWorkflow.WF_CREATE_WORKFLOW));
+ if (Objects.isNull(workflow)) {
+ throw new WorkflowException("Invalid workflow " + WorkplaceWorkflow.WF_CREATE_WORKFLOW);
+ }
+
+ WorkflowContext context = workflow.buildSystemContext(workplace, new JsonDataModelTree(worklowDescJson));
+ workflowExecutionService.execInitWorklet(context);
+ }
+
+ @Override
+ public void terminateWorkflow(WorkflowDescription wfDesc) throws WorkflowException {
+ log.info("terminateWorkflow: {}", wfDesc);
+ if (Objects.nonNull(workplaceStore.getContext(wfDesc.workflowContextName()))) {
+ workplaceStore.removeContext(wfDesc.workflowContextName());
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfig.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfig.java
new file mode 100644
index 0000000..2bca555
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfig.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.Config;
+import org.onosproject.workflow.api.DefaultRpcDescription;
+import org.onosproject.workflow.api.RpcDescription;
+import org.onosproject.workflow.api.WorkflowException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+
+public class WorkflowNetConfig extends Config<ApplicationId> {
+
+ private static final Logger log = LoggerFactory.getLogger(WorkflowNetConfig.class);
+
+ /**
+ * Workflow RPC pointer.
+ */
+ private static final String RPC_PTR = "/rpc";
+
+ public Collection<RpcDescription> getRpcDescriptions() throws WorkflowException {
+
+ JsonNode node = object.at(RPC_PTR);
+ if (!(node instanceof ArrayNode)) {
+ throw new WorkflowException("invalid rpc for " + object);
+ }
+ ArrayNode rpcArrayNode = (ArrayNode) node;
+
+ List<RpcDescription> rpcDescriptions = new ArrayList<>();
+ for (JsonNode rpcNode : rpcArrayNode) {
+ rpcDescriptions.add(DefaultRpcDescription.valueOf(rpcNode));
+ }
+
+ return rpcDescriptions;
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfigListener.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfigListener.java
new file mode 100644
index 0000000..59a57e9
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfigListener.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.basics.SubjectFactories;
+import org.onosproject.workflow.api.DefaultWorkflowDescription;
+import org.onosproject.workflow.api.RpcDescription;
+import org.onosproject.workflow.api.WorkflowService;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.DefaultWorkplaceDescription;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ScheduledExecutorService;
+
+import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+
+public class WorkflowNetConfigListener implements NetworkConfigListener {
+
+ private static final Logger log = LoggerFactory.getLogger(WorkflowNetConfigListener.class);
+
+ public static final String CONFIG_KEY = "workflow";
+ public static final String EXECUTOR_GROUPNAME = "onos/workflow-netcfg";
+ public static final String EXECUTOR_PATTERN = "netcfg-event-handler";
+
+ private final ConfigFactory<ApplicationId, WorkflowNetConfig> configFactory =
+ new ConfigFactory<ApplicationId, WorkflowNetConfig>(
+ SubjectFactories.APP_SUBJECT_FACTORY, WorkflowNetConfig.class, CONFIG_KEY) {
+ @Override
+ public WorkflowNetConfig createConfig() {
+ return new WorkflowNetConfig();
+ }
+ };
+
+ private final WorkflowService workflowService;
+
+ private final ScheduledExecutorService executor =
+ newSingleThreadScheduledExecutor(groupedThreads(EXECUTOR_GROUPNAME, EXECUTOR_PATTERN));
+
+ public WorkflowNetConfigListener(WorkflowService workflowService) {
+ this.workflowService = workflowService;
+ }
+
+ public ConfigFactory<ApplicationId, WorkflowNetConfig> getConfigFactory() {
+ return configFactory;
+ }
+
+ @Override
+ public boolean isRelevant(NetworkConfigEvent event) {
+ return true;
+ }
+
+ @Override
+ public void event(NetworkConfigEvent event) {
+ log.info("Configuration event: {}", event);
+ switch (event.type()) {
+ case CONFIG_ADDED:
+ case CONFIG_UPDATED:
+ if (!event.config().isPresent()) {
+ log.error("No configuration found");
+ return;
+ }
+ WorkflowNetConfig config = (WorkflowNetConfig) event.config().get();
+
+ //Single thread executor(locking is not required)
+ executor.execute(new Handler(workflowService, config));
+ break;
+ default:
+ break;
+ }
+ }
+
+ public static class Handler implements Runnable {
+
+ private WorkflowService workflowService;
+ private WorkflowNetConfig config;
+
+ public Handler(WorkflowService workflowService, WorkflowNetConfig config) {
+ this.workflowService = workflowService;
+ this.config = config;
+ }
+
+ @Override
+ public void run() {
+
+ try {
+ Collection<RpcDescription> rpcs = config.getRpcDescriptions();
+ log.info("" + rpcs);
+ for (RpcDescription rpc : rpcs) {
+ if (!rpcMap.containsKey(rpc.op())) {
+ log.error("Invalid RPC: {}", rpc);
+ continue;
+ }
+
+ rpcMap.get(rpc.op()).apply(this.workflowService, rpc);
+ }
+ } catch (WorkflowException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ @FunctionalInterface
+ public interface RpcCall {
+ void apply(WorkflowService workflowService, RpcDescription rpcDesc) throws WorkflowException;
+ }
+
+ private static Map<String, RpcCall> rpcMap = new HashMap<>();
+ static {
+ rpcMap.put("workplace.create",
+ (service, desc) -> service.createWorkplace(DefaultWorkplaceDescription.valueOf(desc.params())));
+ rpcMap.put("workplace.remove",
+ (service, desc) -> service.removeWorkplace(DefaultWorkplaceDescription.valueOf(desc.params())));
+ rpcMap.put("workflow.invoke",
+ (service, desc) -> service.invokeWorkflow(desc.params()));
+ rpcMap.put("workflow.terminate",
+ (service, desc) -> service.terminateWorkflow(DefaultWorkflowDescription.valueOf(desc.params())));
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkplaceWorkflow.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkplaceWorkflow.java
new file mode 100644
index 0000000..af62aaa
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkplaceWorkflow.java
@@ -0,0 +1,213 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.BooleanNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.event.Event;
+import org.onosproject.workflow.api.AbstractWorklet;
+import org.onosproject.workflow.api.DefaultWorkflowContext;
+import org.onosproject.workflow.api.DefaultWorkplace;
+import org.onosproject.workflow.api.ImmutableListWorkflow;
+import org.onosproject.workflow.api.JsonDataModelTree;
+import org.onosproject.workflow.api.SystemWorkflowContext;
+import org.onosproject.workflow.api.Workflow;
+import org.onosproject.workflow.api.WorkflowAttribute;
+import org.onosproject.workflow.api.WorkflowContext;
+import org.onosproject.workflow.api.WorkflowData;
+import org.onosproject.workflow.api.WorkflowDataEvent;
+import org.onosproject.workflow.api.DefaultWorkflowDescription;
+import org.onosproject.workflow.api.WorkflowDescription;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.WorkflowExecutionService;
+import org.onosproject.workflow.api.WorkflowStore;
+import org.onosproject.workflow.api.Workplace;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URI;
+import java.util.Objects;
+
+import static org.onosproject.workflow.api.WorkflowDataEvent.Type.INSERT;
+
+public class WorkplaceWorkflow {
+
+ private static final Logger log = LoggerFactory.getLogger(WorkplaceWorkflow.class);
+
+ private WorkflowExecutionService workflowExecutionService;
+ private WorkflowStore workflowStore;
+
+ public WorkplaceWorkflow(WorkflowExecutionService workflowExecutionService,
+ WorkflowStore workflowStore) {
+ this.workflowExecutionService = workflowExecutionService;
+ this.workflowStore = workflowStore;
+ }
+
+ public static final String WF_CREATE_WORKFLOW = "workplace.create-workflow";
+
+ public void registerWorkflows() {
+
+ Workflow workflow = ImmutableListWorkflow.builder()
+ .id(URI.create(WF_CREATE_WORKFLOW))
+ .attribute(WorkflowAttribute.REMOVE_AFTER_COMPLETE)
+ .init(ChangeDistributor.class.getName())
+ .chain(CreateWorkplace.class.getName())
+ .chain(CreateWorkflowContext.class.getName())
+ .build();
+ workflowStore.register(workflow);
+ }
+
+ public abstract static class AbsWorkflowWorklet extends AbstractWorklet {
+
+ protected WorkflowDescription getWorkflowDesc(WorkflowContext context) throws WorkflowException {
+
+ JsonNode root = ((JsonDataModelTree) context.data()).root();
+ return DefaultWorkflowDescription.valueOf(root);
+ }
+ }
+
+ public static class ChangeDistributor extends AbsWorkflowWorklet {
+
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+
+ String workplaceName = getWorkflowDesc(context).workplaceName();
+ // Sets workflow job distribution hash value to make this workflow to be executed on the
+ // same cluster node to execute workplace tasks.
+ ((SystemWorkflowContext) context).setDistributor(workplaceName);
+
+ context.completed();
+ }
+ }
+
+ public static class CreateWorkplace extends AbsWorkflowWorklet {
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+
+ WorkflowDescription wfDesc = getWorkflowDesc(context);
+
+ Workplace workplace = context.workplaceStore().getWorkplace(wfDesc.workplaceName());
+ return Objects.isNull(workplace);
+ }
+
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+
+ WorkflowDescription wfDesc = getWorkflowDesc(context);
+
+ // creates workplace with empty data model
+ DefaultWorkplace workplace = new DefaultWorkplace(wfDesc.workplaceName(), new JsonDataModelTree());
+ log.info("registerWorkplace {}", workplace);
+ context.waitCompletion(WorkflowDataEvent.class, wfDesc.workplaceName(),
+ () -> context.workplaceStore().registerWorkplace(wfDesc.workplaceName(), workplace),
+ 60000L
+ );
+ }
+
+ @Override
+ public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException {
+
+ if (!(event instanceof WorkflowDataEvent)) {
+ return false;
+ }
+
+ WorkflowDataEvent wfEvent = (WorkflowDataEvent) event;
+ WorkflowData wfData = wfEvent.subject();
+
+ WorkflowDescription wfDesc = getWorkflowDesc(context);
+
+ if (wfData instanceof Workplace
+ && Objects.equals(wfData.name(), wfDesc.workplaceName())
+ && wfEvent.type() == INSERT) {
+ log.info("isCompleted(true): event:{}, context:{}, workplace:{}",
+ event, context, wfDesc.workplaceName());
+ return true;
+ } else {
+ log.info("isCompleted(false) event:{}, context:{}, workplace:{}",
+ event, context, wfDesc.workplaceName());
+ return false;
+ }
+ }
+ }
+
+ public static class CreateWorkflowContext extends AbsWorkflowWorklet {
+
+ private static final String SUBMITTED = "submitted";
+
+ private boolean isSubmitted(WorkflowContext context) throws WorkflowException {
+ JsonNode node = ((JsonDataModelTree) context.data()).nodeAt("/" + SUBMITTED);
+ if (!(node instanceof BooleanNode)) {
+ return false;
+ }
+ return node.asBoolean();
+ }
+
+ private void submitTrue(WorkflowContext context) throws WorkflowException {
+ JsonNode root = ((JsonDataModelTree) context.data()).root();
+ if (!(root instanceof ObjectNode)) {
+ throw new WorkflowException("Invalid root node for " + context);
+ }
+ ((ObjectNode) root).put(SUBMITTED, true);
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+
+ WorkflowDescription wfDesc = getWorkflowDesc(context);
+
+ String contextName = DefaultWorkflowContext.nameBuilder(wfDesc.id(), wfDesc.workplaceName());
+ if (Objects.isNull(context.workplaceStore().getContext(contextName))) {
+ return (!isSubmitted(context));
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+
+ WorkflowDescription wfDesc = getWorkflowDesc(context);
+
+ Workplace workplace = context.workplaceStore().getWorkplace(wfDesc.workplaceName());
+ if (Objects.isNull(workplace)) {
+
+ log.error("Failed to find workplace with " + wfDesc.workplaceName());
+ throw new WorkflowException("Failed to find workplace with " + wfDesc.workplaceName());
+ }
+
+ Workflow workflow = context.workflowStore().get(wfDesc.id());
+ if (Objects.isNull(workflow)) {
+ throw new WorkflowException("Failed to find workflow with " + wfDesc.id());
+ }
+
+ //String contextName = DefaultWorkflowContext.nameBuilder(wfDesc.id(), wfDesc.workplaceName());
+ String contextName = wfDesc.workflowContextName();
+ if (Objects.nonNull(context.workplaceStore().getContext(contextName))) {
+ throw new WorkflowException(contextName + " exists already");
+ }
+
+ JsonDataModelTree subTree = new JsonDataModelTree(wfDesc.data());
+ WorkflowContext buildingContext = workflow.buildContext(workplace, subTree);
+ log.info("registerContext {}", buildingContext.name());
+ context.workplaceStore().registerContext(buildingContext.name(), buildingContext);
+ submitTrue(context);
+
+ context.completed();
+ }
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/example/SampleWorkflow.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/example/SampleWorkflow.java
new file mode 100644
index 0000000..a1ca9cc
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/example/SampleWorkflow.java
@@ -0,0 +1,287 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.impl.example;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.JsonNodeType;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.workflow.api.AbstractWorklet;
+import org.onosproject.workflow.api.DataModelTree;
+import org.onosproject.workflow.api.ImmutableListWorkflow;
+import org.onosproject.workflow.api.JsonDataModelTree;
+import org.onosproject.workflow.api.Workflow;
+import org.onosproject.workflow.api.WorkflowContext;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.WorkflowExecutionService;
+import org.onosproject.workflow.api.WorkflowStore;
+import org.onosproject.workflow.api.WorkplaceStore;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URI;
+
+/**
+ * Class for sample workflow.
+ */
+@Component(immediate = true)
+public class SampleWorkflow {
+
+ private static final Logger log = LoggerFactory.getLogger(SampleWorkflow.class);
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkflowStore workflowStore;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkplaceStore workplaceStore;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected WorkflowExecutionService workflowExecutionService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DeviceService deviceService;
+
+
+ @Activate
+ public void activate() {
+ log.info("Activated");
+
+ registerWorkflows();
+
+ }
+
+ @Deactivate
+ public void deactivate() {
+ log.info("Deactivated");
+ }
+
+ /**
+ * Registers example workflows.
+ */
+ private void registerWorkflows() {
+ // registering class-loader
+ workflowStore.registerLocal(this.getClass().getClassLoader());
+
+ // registering new workflow definition
+ URI uri = URI.create("sample.workflow-0");
+ Workflow workflow = ImmutableListWorkflow.builder()
+ .id(uri)
+ .chain(SampleWorklet1.class.getName())
+ .chain(SampleWorklet2.class.getName())
+ .chain(SampleWorklet3.class.getName())
+ .chain(SampleWorklet4.class.getName())
+ .chain(SampleWorklet5.class.getName())
+ .build();
+ workflowStore.register(workflow);
+
+ // registering new workflow definition
+ uri = URI.create("sample.workflow-1");
+ workflow = ImmutableListWorkflow.builder()
+ .id(uri)
+ .chain(SampleWorklet3.class.getName())
+ .chain(SampleWorklet2.class.getName())
+ .chain(SampleWorklet1.class.getName())
+ .chain(SampleWorklet4.class.getName())
+ .chain(SampleWorklet5.class.getName())
+ .build();
+ workflowStore.register(workflow);
+
+ // registering new workflow definition
+ uri = URI.create("sample.workflow-2");
+ workflow = ImmutableListWorkflow.builder()
+ .id(uri)
+ .chain(SampleWorklet1.class.getName())
+ .chain(SampleWorklet3.class.getName())
+ .chain(SampleWorklet2.class.getName())
+ .chain(SampleWorklet4.class.getName())
+ .chain(SampleWorklet5.class.getName())
+ .build();
+ workflowStore.register(workflow);
+ }
+
+ /**
+ * Abstract class for sample worklet.
+ */
+ public abstract static class AbsSampleWorklet extends AbstractWorklet {
+
+ protected static final String SAMPLE_DATAMODEL_PTR = "/sample/job";
+
+ /**
+ * Constructor for sample worklet.
+ */
+ protected AbsSampleWorklet() {
+
+ }
+
+ /**
+ * Allocates or gets data model.
+ * @param context workflow context
+ * @return json object node
+ * @throws WorkflowException workflow exception
+ */
+ protected ObjectNode allocOrGetModel(WorkflowContext context) throws WorkflowException {
+
+ JsonDataModelTree tree = (JsonDataModelTree) context.data();
+ JsonNode params = tree.root();
+
+ if (params.at(SAMPLE_DATAMODEL_PTR).getNodeType() == JsonNodeType.MISSING) {
+ tree.alloc(SAMPLE_DATAMODEL_PTR, DataModelTree.Nodetype.MAP);
+ }
+ return (ObjectNode) params.at(SAMPLE_DATAMODEL_PTR);
+ }
+
+ /**
+ * Gets data model.
+ * @param context workflow context
+ * @return json object node
+ * @throws WorkflowException workflow exception
+ */
+ protected ObjectNode getDataModel(WorkflowContext context) throws WorkflowException {
+ DataModelTree tree = context.data();
+ return ((JsonDataModelTree) tree.subtree(SAMPLE_DATAMODEL_PTR)).rootObject();
+ }
+
+ /**
+ * Sleeps for 'ms' milli seconds.
+ * @param ms milli seconds to sleep
+ */
+ protected void sleep(long ms) {
+ try {
+ Thread.sleep(ms);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Class for sample worklet-1.
+ */
+ public static class SampleWorklet1 extends AbsSampleWorklet {
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = getDataModel(context);
+ node.put("work1", "done");
+ log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(30);
+
+ context.completed(); //Complete the job of worklet in the process
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = allocOrGetModel(context);
+ log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(30);
+ return !node.has("work1");
+
+ }
+ }
+
+ /**
+ * Class for sample worklet-2 (using timeout).
+ */
+ public static class SampleWorklet2 extends AbsSampleWorklet {
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = getDataModel(context);
+ node.put("work2", "done");
+ log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(50);
+
+ context.waitFor(50L); //Timeout will happen after 50 milli seconds.
+ }
+
+ @Override
+ public void timeout(WorkflowContext context) throws WorkflowException {
+ context.completed(); //Complete the job of worklet by timeout
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = allocOrGetModel(context);
+ log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(50);
+ return !node.has("work2");
+ }
+ }
+
+ public static class SampleWorklet3 extends AbsSampleWorklet {
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = getDataModel(context);
+ node.put("work3", "done");
+ log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(10);
+
+ context.completed();
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = allocOrGetModel(context);
+ log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(10);
+ return !node.has("work3");
+ }
+ }
+
+ public static class SampleWorklet4 extends AbsSampleWorklet {
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = getDataModel(context);
+ node.put("work4", "done");
+ log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(10);
+
+ context.completed();
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = allocOrGetModel(context);
+ log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(10);
+ return !node.has("work4");
+ }
+ }
+
+ public static class SampleWorklet5 extends AbsSampleWorklet {
+ @Override
+ public void process(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = getDataModel(context);
+ node.put("work5", "done");
+ log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(10);
+
+ context.completed();
+ }
+
+ @Override
+ public boolean isNext(WorkflowContext context) throws WorkflowException {
+ ObjectNode node = allocOrGetModel(context);
+ log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName());
+ sleep(10);
+ return !node.has("work5");
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/example/package-info.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/example/package-info.java
new file mode 100644
index 0000000..2fbc9549a
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/example/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Workflow example package.
+ */
+package org.onosproject.workflow.impl.example;
\ No newline at end of file
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/package-info.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/package-info.java
new file mode 100644
index 0000000..f82c452
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Workflow implementation package.
+ */
+package org.onosproject.workflow.impl;
\ No newline at end of file
diff --git a/apps/workflow/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/workflow/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..cee835a
--- /dev/null
+++ b/apps/workflow/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,20 @@
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+ <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+ <command>
+ <action class="org.onosproject.workflow.cli.WorkFlowStoreCommand"></action>
+ </command>
+ <command>
+ <action class="org.onosproject.workflow.cli.WorkplaceStoreCommand"></action>
+ </command>
+ <command>
+ <action class="org.onosproject.workflow.cli.WorkFlowCommand"></action>
+ </command>
+ <command>
+ <action class="org.onosproject.workflow.cli.WorkFlowEventMapCommand"></action>
+ </command>
+ <command>
+ <action class="org.onosproject.workflow.cli.WorkFlowTestCommand"></action>
+ </command>
+ </command-bundle>
+</blueprint>
diff --git a/modules.bzl b/modules.bzl
index 2a12806..37229ee 100644
--- a/modules.bzl
+++ b/modules.bzl
@@ -250,6 +250,7 @@
"//apps/imr:onos-apps-imr-oar",
"//apps/nodemetrics:onos-apps-nodemetrics-oar",
# "//web/gui2:onos-web-gui2-oar",
+ "//apps/workflow:onos-apps-workflow-oar",
]
PROTOCOL_APPS = [
diff --git a/modules.defs b/modules.defs
index 1584e60..158b07c 100644
--- a/modules.defs
+++ b/modules.defs
@@ -257,6 +257,7 @@
'//apps/layout:onos-apps-layout-oar',
'//apps/imr:onos-apps-imr-oar',
'//apps/inbandtelemetry/app:onos-apps-inbandtelemetry-app-oar',
+ '//apps/workflow:onos-apps-workflow-oar',
# nodemetrics application
'//apps/nodemetrics:onos-apps-nodemetrics-oar',
#'//web/gui2:onos-web-gui2-oar',