Implement CLI support for workflow logs

Added CLI commands to display workflow logs for each context

Change-Id: Ic0a0f323cebc8a21325dd075b20bbdfd104aeae1
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCommand.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCommand.java
new file mode 100644
index 0000000..f67a833
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCommand.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2022-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.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.workflow.api.WorkflowLogStore;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Displays workflow logs of each context.
+ */
+@Service
+@Command(scope = "onos", name = "workflow-logs", description = "Workflow Logs CLI")
+public class WorkflowLogStoreCommand extends AbstractShellCommand {
+
+    protected static final String SHOW = "show";
+
+     // Executes this command.
+    @Argument(name = "command", description = "command (" + SHOW + ")", required = true)
+    @Completion(WorkflowLogStoreCompleter.class)
+    private String command;
+
+    @Argument(index = 1, name = "arg1")
+    @Completion(WorkflowLogStoreCtxtNameCompleter.class)
+    private String arg1;
+
+    @Override
+    protected void doExecute() {
+        switch (command) {
+            case SHOW:
+                showLogs();
+                break;
+            default:
+                error("Wrong command - " + command);
+                break;
+        }
+    }
+
+    private void showLogs() {
+        WorkflowLogStore workflowLogStore = get(WorkflowLogStore.class);
+        Map<String, List<String>> logMap = workflowLogStore.asJavaMap();
+
+        if (arg1 == null) {
+            logMap.forEach(this::printLogs);
+            return;
+        }
+
+        if (!logMap.containsKey(arg1)) {
+            error("There are no logs for key:" + arg1);
+            return;
+        }
+        printLogs(arg1, logMap.get(arg1));
+    }
+
+    private void printLogs(String contextName, List<String> logs) {
+        String msg = "Context Name: " + contextName + "\n"
+                + "Log:\n" + String.join("\n", logs) + "\n";
+        print(msg);
+    }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCompleter.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCompleter.java
new file mode 100644
index 0000000..cdcdf2b
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCompleter.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2022-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.google.common.collect.ImmutableList;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractChoicesCompleter;
+
+import java.util.List;
+
+import static org.onosproject.workflow.cli.WorkflowLogStoreCommand.SHOW;
+
+/**
+ * Workflow Log Store command completer.
+ */
+@Service
+public class WorkflowLogStoreCompleter extends AbstractChoicesCompleter {
+    @Override
+    protected List<String> choices() {
+        return ImmutableList.of(SHOW);
+    }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCtxtNameCompleter.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCtxtNameCompleter.java
new file mode 100644
index 0000000..e9c66a8
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkflowLogStoreCtxtNameCompleter.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2022-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.google.common.collect.Lists;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.workflow.api.WorkflowLogStore;
+
+import java.util.List;
+
+import static org.onlab.osgi.DefaultServiceDirectory.getService;
+
+/**
+ * Workflow Log Store - context name completer.
+ */
+@Service
+public class WorkflowLogStoreCtxtNameCompleter extends AbstractChoicesCompleter {
+    @Override
+    protected List<String> choices() {
+        WorkflowLogStore workflowLogStore = getService(WorkflowLogStore.class);
+        return Lists.newArrayList(workflowLogStore.asJavaMap().keySet());
+    }
+}