Minor changes to PI runtime classes

- New class for action runtime parameter and its identifier
- PiAction builder
- Various indentifier builders

Change-Id: I265f71c868c21dbbbe633622b0c4330712f5a5ad
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiAction.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiAction.java
index 24a7fbe..ef01b9d 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiAction.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiAction.java
@@ -19,11 +19,11 @@
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Objects;
-import com.google.common.collect.ImmutableList;
-import org.onlab.util.ImmutableByteSequence;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 
-import java.util.Collections;
-import java.util.List;
+import java.util.Collection;
+import java.util.Map;
 import java.util.StringJoiner;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -36,7 +36,7 @@
 public final class PiAction implements PiTableAction {
 
     private final PiActionId actionId;
-    private final List<ImmutableByteSequence> runtimeParams;
+    private final Map<PiActionParamId, PiActionParam> runtimeParams;
 
     /**
      * Creates a new action instance for the given action identifier and runtime parameters.
@@ -44,18 +44,9 @@
      * @param actionId      action identifier
      * @param runtimeParams list of runtime parameters
      */
-    public PiAction(PiActionId actionId, List<ImmutableByteSequence> runtimeParams) {
-        this.actionId = checkNotNull(actionId);
-        this.runtimeParams = ImmutableList.copyOf(checkNotNull(runtimeParams));
-    }
-
-    /**
-     * Creates a new action instance for the given action identifier, with no runtime parameters.
-     *
-     * @param actionId action identifier
-     */
-    public PiAction(PiActionId actionId) {
-        this(actionId, Collections.emptyList());
+    private PiAction(PiActionId actionId, Map<PiActionParamId, PiActionParam> runtimeParams) {
+        this.actionId = actionId;
+        this.runtimeParams = ImmutableMap.copyOf(runtimeParams);
     }
 
     @Override
@@ -73,13 +64,13 @@
     }
 
     /**
-     * Returns an immutable view of the list of parameters of this action.
-     * Return an empty list if the action doesn't take any runtime parameters.
+     * Returns all runtime parameters of this action.
+     * Return an empty collection if the action doesn't take any runtime parameters.
      *
      * @return list of byte sequences
      */
-    public List<ImmutableByteSequence> parameters() {
-        return runtimeParams;
+    public Collection<PiActionParam> parameters() {
+        return runtimeParams.values();
     }
 
     @Override
@@ -108,4 +99,71 @@
                 .addValue(this.id().toString() + stringParams.toString())
                 .toString();
     }
+
+    /**
+     * Returns an action builder.
+     *
+     * @return a new builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Builder of protocol-independent actions.
+     */
+    public static final class Builder {
+
+        private PiActionId actionId;
+        private Map<PiActionParamId, PiActionParam> runtimeParams = Maps.newHashMap();
+
+        private Builder() {
+            // hides constructor.
+        }
+
+        /**
+         * Sets the identifier of this action.
+         *
+         * @param actionId action identifier
+         * @return this
+         */
+        public Builder withId(PiActionId actionId) {
+            this.actionId = actionId;
+            return this;
+        }
+
+        /**
+         * Adds a runtime parameter.
+         *
+         * @param param action parameter
+         * @return this
+         */
+        public Builder withParameter(PiActionParam param) {
+            checkNotNull(param);
+            runtimeParams.put(param.id(), param);
+            return this;
+        }
+
+        /**
+         * Adds many runtime parameters.
+         *
+         * @param params collection of action parameters
+         * @return this
+         */
+        public Builder withParameters(Collection<PiActionParam> params) {
+            checkNotNull(params);
+            params.forEach(this::withParameter);
+            return this;
+        }
+
+        /**
+         * Returns a new action instance.
+         *
+         * @return action
+         */
+        public PiAction build() {
+            checkNotNull(actionId);
+            return new PiAction(actionId, runtimeParams);
+        }
+    }
 }