[ONOS-7732] Automating switch workflow - workflow program counter & cli refactoring

Change-Id: I90f2085dbb253c8ea4598be31a8fd12760a1f739
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
index f4d9470..cadbc91 100644
--- 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
@@ -25,6 +25,7 @@
 
 import java.lang.reflect.Modifier;
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
@@ -43,9 +44,9 @@
     private String initWorkletType;
 
     /**
-     * List of worklet  description.
+     * Sequential program(List of worklet desc) to be executed.
      */
-    private List<WorkletDescription> workletDescList;
+    private List<WorkletDescription> program;
 
     /**
      * Set of workflow attributes.
@@ -63,7 +64,7 @@
     private ImmutableListWorkflow(Builder builder) {
         super(builder.id);
         this.initWorkletType = builder.initWorkletType;
-        workletDescList = ImmutableList.copyOf(builder.workletDescList);
+        program = ImmutableList.copyOf(builder.workletDescList);
         attributes = ImmutableSet.copyOf(builder.attributes);
     }
 
@@ -86,7 +87,7 @@
 
         ProgramCounter pc = current.clone();
 
-        for (int i = current.workletIndex(); i < workletDescList.size(); pc = increased(pc), i++) {
+        for (int i = current.workletIndex(); i < program.size(); pc = increased(pc), i++) {
 
             if (cnt++ > Worklet.MAX_WORKS) {
                 throw new WorkflowException("Maximum worklet execution exceeded");
@@ -139,22 +140,21 @@
     public ProgramCounter increased(ProgramCounter pc) throws WorkflowException {
 
         int increaedIndex = pc.workletIndex() + 1;
-        if (increaedIndex >= workletDescList.size()) {
+        if (increaedIndex >= program.size()) {
             throw new WorkflowException("Out of bound in program counter(" + pc + ")");
         }
 
-        WorkletDescription workletDesc = workletDescList.get(increaedIndex);
+        WorkletDescription workletDesc = program.get(increaedIndex);
         return ProgramCounter.valueOf(workletDesc.tag(), increaedIndex);
     }
 
     @Override
     public Worklet getWorkletInstance(ProgramCounter pc) throws WorkflowException {
 
-        return getWorkletInstance(workletDescList.get(pc.workletIndex()).tag());
+        return getWorkletInstance(program.get(pc.workletIndex()).tag());
     }
 
-    @Override
-    public Worklet getWorkletInstance(String workletType) throws WorkflowException {
+    private Worklet getWorkletInstance(String workletType) throws WorkflowException {
 
         if (Worklet.Common.INIT.tag().equals(workletType)) {
             return Worklet.Common.INIT;
@@ -198,14 +198,23 @@
     }
 
     @Override
-    public List<WorkletDescription> getWorkletDescList() {
-        return ImmutableList.copyOf(workletDescList);
+    public List<ProgramCounter> getProgram() {
+
+        int size = program.size();
+        List pcList = new ArrayList();
+        for (int i = 0; i < size; i++) {
+            pcList.add(ProgramCounter.valueOf(program.get(i).tag(), i));
+        }
+
+        return pcList;
     }
 
     @Override
     public WorkletDescription getWorkletDesc(ProgramCounter pc) {
-        Optional<WorkletDescription> workletDescription = workletDescList.stream().filter(a -> Objects.equals(a.tag(),
-                workletDescList.get(pc.workletIndex()).tag())).findAny();
+        Optional<WorkletDescription> workletDescription =
+                program.stream()
+                        .filter(a -> Objects.equals(a.tag(), program.get(pc.workletIndex()).tag()))
+                        .findAny();
         if (workletDescription.isPresent()) {
             return workletDescription.get();
         }
@@ -220,8 +229,8 @@
      * @return index of class in worklet type list
      */
     private int getClassIndex(Class aClass) {
-        for (int i = 0; i < workletDescList.size(); i++) {
-            if (Objects.equals(aClass.getName(), workletDescList.get(i))) {
+        for (int i = 0; i < program.size(); i++) {
+            if (Objects.equals(aClass.getName(), program.get(i))) {
                 return i;
             }
         }
@@ -265,7 +274,7 @@
         }
         return Objects.equals(this.id(), ((ImmutableListWorkflow) obj).id())
                 && Objects.equals(this.initWorkletType, ((ImmutableListWorkflow) obj).initWorkletType)
-                && Objects.equals(this.workletDescList, ((ImmutableListWorkflow) obj).workletDescList)
+                && Objects.equals(this.program, ((ImmutableListWorkflow) obj).program)
                 && Objects.equals(this.attributes, ((ImmutableListWorkflow) obj).attributes);
     }
 
@@ -274,7 +283,7 @@
         return MoreObjects.toStringHelper(getClass())
                 .add("id", id())
                 .add("initWorklet", initWorkletType)
-                .add("workList", workletDescList)
+                .add("workList", program)
                 .add("attributes", attributes)
                 .toString();
     }
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
index 5073105..3849afe 100644
--- 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
@@ -68,15 +68,6 @@
     Worklet getWorkletInstance(ProgramCounter pc) 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
@@ -104,11 +95,11 @@
     Set<WorkflowAttribute> attributes();
 
     /**
-     * Returns worklet desc list.
+     * Build the list of ProgramCounters, and returns.
      *
-     * @return worklet description list
+     * @return program counter list
      */
-    List<WorkletDescription> getWorkletDescList();
+    List<ProgramCounter> getProgram();
 
     /**
      * Returns worklet description.
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
index 06d36ce..d18cdae 100644
--- 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
@@ -53,14 +53,22 @@
     @Completion(WorkplaceNameCompleter.class)
     private String name = null;
 
-    @Option(name = "-f", aliases = "--filter", description = "including filter",
+    @Option(name = "-wf", aliases = "--whitefilter", description = "whitelisting filter",
             required = false, multiValued = false)
     private String inFilter = null;
 
-    @Option(name = "-e", aliases = "--excludefilter", description = "excluding filter",
+    @Option(name = "-bf", aliases = "--blackfilter", description = "blacklisting filter",
             required = false, multiValued = false)
     private String exFilter = null;
 
+    @Option(name = "-nc", aliases = "--notcompleted", description = "not completed workflow context",
+            required = false, multiValued = false)
+    private boolean notCompleted = false;
+
+    @Option(name = "-s", aliases = "--simpleprint", description = "simplified print format",
+            required = false, multiValued = false)
+    private boolean simplePrint = false;
+
     @Override
     protected void doExecute() {
 
@@ -180,35 +188,61 @@
      */
     private void printWorkplaceContexts(WorkplaceStore workplaceStore, String workplaceName) {
         for (WorkflowContext context : workplaceStore.getWorkplaceContexts(workplaceName)) {
+
+            if (notCompleted && context.current().isCompleted()) {
+                continue;
+            }
+
             String str = context.toString();
             if (Objects.nonNull(inFilter)) {
                 if (str.indexOf(inFilter) != -1) {
                     if (Objects.nonNull(exFilter)) {
                         if (str.indexOf(exFilter) == -1) {
-                            print(" - " + context);
+                            printContext(context);
                         }
                     } else {
-                        print(" - " + context);
+                        printContext(context);
                     }
                 }
             } else {
                 if (Objects.nonNull(exFilter)) {
                     if (str.indexOf(exFilter) == -1) {
-                        print(" - " + context);
+                        printContext(context);
                     }
                 } else {
-                    print(" - " + context);
+                    printContext(context);
                 }
             }
         }
     }
 
     /**
+     * Prints a workflow context.
+     * @param context a workflow context
+     */
+    private void printContext(WorkflowContext context) {
+        if (simplePrint) {
+            String str = " - "
+                     + "name=" + context.name()
+                     + ", state=" + context.state()
+                     + ", current=" + context.current().workletType();
+            print(str);
+
+        } else {
+            print(" - " + context);
+        }
+    }
+
+    /**
      * Gets workplace string.
      * @param workplace workplace
      * @return workplace string
      */
     private String getWorkplaceString(Workplace workplace) {
-        return workplace.toString();
+        if (simplePrint) {
+            return workplace.name();
+        } else {
+            return workplace.toString();
+        }
     }
 }
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
index 6050183..40436ba 100644
--- 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
@@ -24,6 +24,7 @@
 import com.google.common.base.MoreObjects;
 import org.onosproject.net.config.NetworkConfigRegistry;
 import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.workflow.api.ProgramCounter;
 import org.onosproject.workflow.api.WorkflowDefinitionException;
 import org.onosproject.workflow.api.WorkflowService;
 import org.onosproject.workflow.api.WorkflowExecutionService;
@@ -40,7 +41,6 @@
 import org.onosproject.workflow.api.Worklet;
 import org.onosproject.workflow.api.WorkflowContext;
 import org.onosproject.workflow.api.JsonDataModel;
-import org.onosproject.workflow.api.WorkletDescription;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
@@ -175,9 +175,9 @@
 
         List<String> errors = new ArrayList<>();
 
-        for (WorkletDescription workletType : workflow.getWorkletDescList()) {
+        for (ProgramCounter pc : workflow.getProgram()) {
 
-            Worklet worklet = workflow.getWorkletInstance(workletType.tag());
+            Worklet worklet = workflow.getWorkletInstance(pc);
             if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) {
                 continue;
             }
@@ -204,7 +204,7 @@
                     String path = matcher.group(1);
 
                     WorkletDataModelFieldDesc desc =
-                            new WorkletDataModelFieldDesc(workletType.tag(), path, field.getType(),
+                            new WorkletDataModelFieldDesc(pc.workletType(), path, field.getType(),
                                     jsonDataModel.optional());
 
                     WorkletDataModelFieldDesc existing = descMap.get(path);
@@ -298,9 +298,9 @@
             throw new WorkflowDataModelException(workflow.id(), worklowDescJson, errors);
         }
 
-        for (WorkletDescription workletType : workflow.getWorkletDescList()) {
+        for (ProgramCounter pc : workflow.getProgram()) {
 
-            Worklet worklet = workflow.getWorkletInstance(workletType.tag());
+            Worklet worklet = workflow.getWorkletInstance(pc);
             if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) {
                 continue;
             }