Workflow description and programming counter enhancement

workflow description lable would help to jump the worklets in workflow lcm

Change-Id: If28902e02679fe312d3839539b709f9b235dd15a
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkletDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkletDescription.java
index 82bb3f8..ee69273 100644
--- a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkletDescription.java
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/DefaultWorkletDescription.java
@@ -22,37 +22,52 @@
 import com.google.common.base.MoreObjects;
 import org.slf4j.Logger;
 
+import java.util.Optional;
+
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
- * Class for default worklet description.
+ * Class for default work-let description.
  */
 public final class DefaultWorkletDescription implements WorkletDescription {
 
     protected static final Logger log = getLogger(DefaultWorkletDescription.class);
 
     /**
-     * worklet Name.
+     * work-let Name.
      */
     private String tag;
 
     /**
-     * worklet data model.
+     * Label of work-let description(Optional).
      */
-    private JsonDataModelTree data;
+    private Optional<Label> optLabel;
 
     /**
-     * Constructor of worklet description.
-     *
-     * @param builder worklet description builder
+     * work-let staticData model.
      */
-    public DefaultWorkletDescription(DefaultWorkletDescription.Builder builder) {
+    private JsonDataModelTree staticData;
+
+    /**
+     * Constructor of work-let description.
+     *
+     * @param builder work-let description builder
+     */
+    private DefaultWorkletDescription(DefaultWorkletDescription.Builder builder) {
         this.tag = builder.tag;
-        this.data = builder.data;
+        this.optLabel = builder.optLabel;
+        this.staticData = builder.staticData;
     }
 
+    /**
+     * Constructor of work-let description.
+     *
+     * @param tag work-let class name
+     */
     public DefaultWorkletDescription(String tag) {
         this.tag = tag;
+        this.optLabel = Optional.empty();
+        this.staticData = new JsonDataModelTree();
     }
 
     @Override
@@ -61,15 +76,21 @@
     }
 
     @Override
+    public Optional<Label> label() {
+        return this.optLabel;
+    }
+
+    @Override
     public JsonDataModelTree data() {
-        return this.data;
+        return this.staticData;
     }
 
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(getClass())
                 .add("tag", tag())
-                .add("data", data())
+                .add("staticData", data())
+                .add("optLabel", label())
                 .toString();
     }
 
@@ -83,24 +104,40 @@
     }
 
     /**
-     * Builder for worklet description.
+     * Builder for work-let description.
      */
     public static class Builder {
 
         /**
-         * worklet name.
+         * work-let name.
          */
         private String tag;
 
         /**
-         * static data model tree.
+         * Label of work-let description(Optional).
          */
-        JsonDataModelTree data = new JsonDataModelTree();
+        private Optional<Label> optLabel = Optional.empty();
 
         /**
-         * Sets worklet name.
+         * static staticData model tree.
+         */
+        JsonDataModelTree staticData = new JsonDataModelTree();
+
+        /**
+         * Sets optLabel of work-let description.
          *
-         * @param tag worklet name
+         * @param label optLabel of work-let description
+         * @return builder
+         */
+        public DefaultWorkletDescription.Builder label(Label label) {
+            this.optLabel = Optional.of(label);
+            return this;
+        }
+
+        /**
+         * Sets work-let name.
+         *
+         * @param tag work-let name
          * @return builder
          */
         public DefaultWorkletDescription.Builder name(String tag) {
@@ -108,63 +145,107 @@
             return this;
         }
 
-
+        /**
+         * Sets static data model with path and string data type value.
+         *
+         * @param path static data model path
+         * @param value string model value
+         * @return builder
+         * @throws WorkflowException workflow exception
+         */
         public DefaultWorkletDescription.Builder staticDataModel(String path, String value) throws WorkflowException {
 
-            data.setAt(path, value);
+            staticData.setAt(path, value);
 
             return this;
         }
 
+        /**
+         * Sets static data model with path and integer data type value.
+         *
+         * @param path static data model path
+         * @param value integer model value
+         * @return builder
+         * @throws WorkflowException workflow exception
+         */
         public DefaultWorkletDescription.Builder staticDataModel(String path, Integer value) throws WorkflowException {
 
-            data.setAt(path, value);
+            staticData.setAt(path, value);
 
             return this;
         }
 
+        /**
+         * Sets static data model with path and boolean data type value.
+         *
+         * @param path static data model path
+         * @param value boolean model value
+         * @return builder
+         * @throws WorkflowException workflow exception
+         */
         public DefaultWorkletDescription.Builder staticDataModel(String path, Boolean value) throws WorkflowException {
 
-            data.setAt(path, value);
+            staticData.setAt(path, value);
 
             return this;
         }
 
+        /**
+         * Sets static data model with path and json data type value.
+         *
+         * @param path static data model path
+         * @param value json model value
+         * @return builder
+         * @throws WorkflowException workflow exception
+         */
         public DefaultWorkletDescription.Builder staticDataModel(String path, JsonNode value) throws WorkflowException {
 
-            data.setAt(path, value);
+            staticData.setAt(path, value);
 
             return this;
         }
 
+        /**
+         * Sets static data model with path and json array data type value.
+         *
+         * @param path static data model path
+         * @param value json-array model value
+         * @return builder
+         * @throws WorkflowException workflow exception
+         */
         public DefaultWorkletDescription.Builder staticDataModel(String path, ArrayNode value)
                 throws WorkflowException {
 
-            data.setAt(path, value);
+            staticData.setAt(path, value);
 
             return this;
         }
 
+        /**
+         * Sets static data model with path and json-object data type value.
+         *
+         * @param path static data model path
+         * @param value json-object model value
+         * @return builder
+         * @throws WorkflowException workflow exception
+         */
         public DefaultWorkletDescription.Builder staticDataModel(String path, ObjectNode value)
                 throws WorkflowException {
 
-            data.setAt(path, value);
+            staticData.setAt(path, value);
 
             return this;
         }
 
-
         /**
-         * Builds worklet description from builder.
+         * Builds work-let description from builder.
          *
-         * @return instance of worklet description
+         * @return instance of work-let description
          * @throws WorkflowException workflow exception
          */
         public DefaultWorkletDescription build() {
-
             return new DefaultWorkletDescription(this);
         }
-
-
     }
 }
+
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 1965c43..6f77028 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
@@ -56,11 +56,6 @@
     private static StaticDataModelInjector staticDataModelInjector = new StaticDataModelInjector();
 
     /**
-     * Workflow Logger injector.
-     */
-    private WorkflowLoggerInjector workflowLoggerInjector = new WorkflowLoggerInjector();
-
-    /**
      * Constructor of ImmutableListWorkflow.
      *
      * @param builder builder of ImmutableListWorkflow
@@ -89,7 +84,7 @@
         ProgramCounter current = context.current();
         check(current != null, "Invalid program counter");
 
-        ProgramCounter pc = current.clone();
+        ProgramCounter pc = current.pcClone();
 
         for (int i = current.workletIndex(); i < program.size(); pc = increased(pc), i++) {
 
@@ -123,7 +118,6 @@
                 continue;
 
             } else {
-                workflowLoggerInjector.inject(worklet, context);
                 // isNext is read only. It does not perform 'inhale'.
                 dataModelInjector.inject(worklet, context);
                 WorkletDescription workletDesc = getWorkletDesc(pc);
@@ -150,7 +144,7 @@
         }
 
         WorkletDescription workletDesc = program.get(increaedIndex);
-        return ProgramCounter.valueOf(workletDesc.tag(), increaedIndex);
+        return ProgramCounter.valueOf(workletDesc.tag(), increaedIndex, "");
     }
 
     @Override
@@ -208,7 +202,7 @@
         int size = program.size();
         List pcList = new ArrayList();
         for (int i = 0; i < size; i++) {
-            pcList.add(ProgramCounter.valueOf(program.get(i).tag(), i));
+            pcList.add(ProgramCounter.valueOf(program.get(i).tag(), i, ""));
         }
 
         return pcList;
@@ -368,3 +362,4 @@
         }
     }
 }
+
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
index 8a0ec84..7e402fd 100644
--- 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
@@ -462,7 +462,7 @@
             }
             list = reader.readValue(node);
         } catch (Exception e) {
-            log.info("exception while parsing list {}", e.getStackTrace());
+            log.error("exception while parsing list ", e);
         }
         return list;
     }
@@ -487,7 +487,7 @@
             });
             map = reader.readValue(node);
         } catch (Exception e) {
-            log.info("exception while parsing map {}", e.getStackTrace());
+            log.error("exception while parsing map ", e);
         }
         return map;
     }
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ProgramCounter.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ProgramCounter.java
index a59ddf5..9fb5338 100644
--- a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ProgramCounter.java
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/ProgramCounter.java
@@ -20,67 +20,108 @@
 import java.util.regex.Pattern;
 
 /**
- * An interface representing workflow program counter.
+ * A class representing workflow program counter.
  */
 public final class ProgramCounter {
 
-    public static final ProgramCounter INIT_PC = ProgramCounter.valueOf(Worklet.Common.INIT.name(), 0);
+    public static final ProgramCounter INIT_PC
+            = ProgramCounter.valueOf("", 0, Worklet.Common.INIT.name());
+
+    public static final ProgramCounter TERMINATE_PC
+            = ProgramCounter.valueOf("", 0, Worklet.Common.COMPLETED.name());
 
     /**
-     * index of the worklet.
+     * Section name in workflow.
+     */
+    private final String sectionName;
+
+    /**
+     * index of the work-let in the sectionName.
      */
     private final int workletIndex;
 
     /**
-     * Type of worklet.
+     * Number of work-let processed.
+     */
+    private int indexCount;
+
+    /**
+     * Type of work-let.
      */
     private final String workletType;
 
+
     /**
-     * Index of worklet.
-     * @return index of worklet
+     * Section name in workflow.
+     * @return sectionName name
+     */
+    public String sectionName() {
+        return this.sectionName;
+    }
+
+    /**
+     * Index of work-let in the workflow section.
+     * @return index of work-let
      */
     public int workletIndex() {
         return this.workletIndex;
     }
 
     /**
-     * Type of worklet.
-     * @return type of worklet
+     * Number of work-let processed.
+     * @return index count of work-let
+     */
+    public int indexCount() {
+        return this.indexCount;
+    }
+
+    /**
+     * Type of work-let.
+     * @return type of work-let
      */
     public String workletType() {
         return this.workletType;
     }
 
     /**
-     * Constructor of workflow Program Counter.
-     * @param workletType type of worklet
-     * @param workletIndex index of worklet
+     * Set total number of work-let processed.
+     * @param indexCount total number of work-let processed
      */
-    private ProgramCounter(String workletType, int workletIndex) {
+    public void setIndexCount(int indexCount) {
+        this.indexCount = indexCount;
+    }
+
+    /**
+     * Constructor of workflow Program Counter.
+     * @param workletType type of work-let
+     * @param workletIndex index of work-let
+     */
+    private ProgramCounter(String sectionName, int workletIndex, int indexCount, String workletType) {
         this.workletType = workletType;
+        this.sectionName = sectionName;
         this.workletIndex = workletIndex;
+        this.indexCount = indexCount;
     }
 
     /**
      * Clones this workflow Program Counter.
      * @return clone of this workflow Program Counter
      */
-    public ProgramCounter clone() {
-        return ProgramCounter.valueOf(this.workletType(), this.workletIndex());
+    public ProgramCounter pcClone() {
+        return ProgramCounter.valueOf(this.sectionName(), this.workletIndex(), this.workletType());
     }
 
     /**
-     * Returns whether this program counter is INIT worklet program counter.
-     * @return whether this program counter is INIT worklet program counter
+     * Returns whether this program counter is INIT work-let program counter.
+     * @return whether this program counter is INIT work-let program counter
      */
     public boolean isInit() {
         return Worklet.Common.INIT.tag().equals(this.workletType);
     }
 
     /**
-     * Returns whether this program counter is COMPLETED worklet program counter.
-     * @return whether this program counter is COMPLETED worklet program counter
+     * Returns whether this program counter is COMPLETED work-let program counter.
+     * @return whether this program counter is COMPLETED work-let program counter
      */
     public boolean isCompleted() {
         return Worklet.Common.COMPLETED.tag().equals(this.workletType);
@@ -100,38 +141,45 @@
             return false;
         }
         return Objects.equals(this.workletType(), ((ProgramCounter) obj).workletType())
-                && Objects.equals(this.workletIndex(), ((ProgramCounter) obj).workletIndex());
+                && Objects.equals(this.sectionName(), ((ProgramCounter) obj).sectionName())
+                && Objects.equals(this.workletIndex(), ((ProgramCounter) obj).workletIndex())
+         && Objects.equals(this.workletIndex(), ((ProgramCounter) obj).workletIndex());
     }
 
     @Override
     public String toString() {
-        return String.format("(%d)%s", workletIndex, workletType);
+        return String.format("(%s:%d:%d)%s", sectionName, workletIndex, indexCount, workletType);
     }
 
     /**
      * Builder of workflow Program Counter.
-     * @param workletType type of worklet
-     * @param workletIndex index of worklet
+     * @param workletType type of work-let
+     * @param sectionName name of section
+     * @param workletIndex index of work-let
      * @return program counter
      */
-    public static ProgramCounter valueOf(String workletType, int workletIndex) {
-        return new ProgramCounter(workletType, workletIndex);
+    public static ProgramCounter valueOf(String sectionName, int workletIndex, String workletType) {
+        return valueOf(sectionName, workletIndex, workletIndex, workletType);
+    }
+
+    public static ProgramCounter valueOf(String sectionName, int workletIndex, int moveCount, String workletType) {
+        return new ProgramCounter(sectionName, workletIndex, moveCount, workletType);
     }
 
     /**
      * Builder of workflow Program Counter.
-     * @param strProgramCounter string format for program counter
+     * @param strProgramCounter string format for program counter '([sectionName]:[index])[class name]'
      * @return program counter
      */
     public static ProgramCounter valueOf(String strProgramCounter) {
 
-        Matcher m = Pattern.compile("\\((\\d+)\\)(.+)").matcher(strProgramCounter);
+        Matcher m = Pattern.compile("\\((.+)\\:(\\d+)\\:(\\d+)\\)(.+)").matcher(strProgramCounter);
 
         if (!m.matches()) {
             throw new IllegalArgumentException("Malformed program counter string");
         }
 
-        return new ProgramCounter(m.group(2), Integer.parseInt(m.group(1)));
+        return new ProgramCounter(m.group(1), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)), m.group(4));
     }
 
 }
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkletDescription.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkletDescription.java
index 71ddbe5..32876a7 100644
--- a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkletDescription.java
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkletDescription.java
@@ -16,6 +16,8 @@
 package org.onosproject.workflow.api;
 
 
+import java.util.Optional;
+
 public interface WorkletDescription {
 
     /**
@@ -26,11 +28,17 @@
     String tag();
 
     /**
-     * Gets worklet data model.
+     * Gets label of worklet description.
      *
-     * @return worklet data model
+     * @return worklet label
+     */
+    Optional<Label> label();
+
+    /**
+     * Gets worklet staticData model.
+     *
+     * @return worklet staticData model
      */
     JsonDataModelTree data();
-
-
 }
+