[ONOS-7732] Automating switch workflow: api, app, and sample workflows

Change-Id: Iee87d4fe6cf61c1f8904d1d77df5f913a712b64a
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