[ONOS-6557]Add PiCriterion and PiInstruction builders for flow rules

Change-Id: I9fd8b04f8d0f6c9886825dd6d7e782fd3cce7ae9
diff --git a/core/api/src/main/java/org/onosproject/net/flow/criteria/PiCriterion.java b/core/api/src/main/java/org/onosproject/net/flow/criteria/PiCriterion.java
index ab05f26..c9335c2 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/criteria/PiCriterion.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/criteria/PiCriterion.java
@@ -18,11 +18,22 @@
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Objects;
+import com.google.common.collect.Maps;
 import org.onosproject.net.pi.runtime.PiFieldMatch;
+import org.onosproject.net.pi.runtime.PiHeaderFieldId;
+import org.onosproject.net.pi.runtime.PiExactFieldMatch;
+import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
+import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
+import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
+import org.onosproject.net.pi.runtime.PiValidFieldMatch;
 
 import java.util.Collection;
+import java.util.Map;
 import java.util.StringJoiner;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.onlab.util.ImmutableByteSequence.copyFrom;
+
 /**
  * Protocol-indepedent criterion.
  */
@@ -36,7 +47,7 @@
      *
      * @param fieldMatches fields to match
      */
-    PiCriterion(Collection<PiFieldMatch> fieldMatches) {
+    private PiCriterion(Collection<PiFieldMatch> fieldMatches) {
         this.fieldMatches = fieldMatches;
     }
 
@@ -77,4 +88,250 @@
         fieldMatches.forEach(f -> stringParams.add(f.toString()));
         return stringParams.toString();
     }
-}
+
+    /**
+     * Returns the PiCriterion builder.
+     *
+     * @return PiCriterion builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * PiCriterion Builder.
+     */
+    @Beta
+    public static final class Builder {
+
+        private final Map<PiHeaderFieldId, PiFieldMatch> piFieldMatches = Maps.newHashMap();
+
+        private Builder() {
+            // ban constructor.
+        }
+
+        /**
+         * Adds an exact field match for the given fieldId and value.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  exact match value
+         * @return this
+         */
+        public Builder matchExact(PiHeaderFieldId fieldId, short value) {
+            piFieldMatches.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value)));
+            return this;
+        }
+
+        /**
+         * Adds an exact field match for the given fieldId and value.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  exact match value
+         * @return this
+         */
+        public Builder matchExact(PiHeaderFieldId fieldId, int value) {
+            piFieldMatches.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value)));
+            return this;
+        }
+
+        /**
+         * Adds an exact field match for the given fieldId and value.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  exact match value
+         * @return this
+         */
+        public Builder matchExact(PiHeaderFieldId fieldId, long value) {
+            piFieldMatches.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value)));
+            return this;
+        }
+
+        /**
+         * Adds an exact field match for the given fieldId and value.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  exact match value
+         * @return this
+         */
+        public Builder matchExact(PiHeaderFieldId fieldId, byte[] value) {
+            piFieldMatches.put(fieldId, new PiExactFieldMatch(fieldId, copyFrom(value)));
+            return this;
+        }
+
+        /**
+         * Adds a ternary field match for the given fieldId, value and mask.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  ternary match value
+         * @param mask   ternary match mask
+         * @return this
+         */
+        public Builder matchTernary(PiHeaderFieldId fieldId, short value, short mask) {
+            piFieldMatches.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask)));
+            return this;
+        }
+
+        /**
+         * Adds a ternary field match for the given fieldId, value and mask.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  ternary match value
+         * @param mask   ternary match mask
+         * @return this
+         */
+        public Builder matchTernary(PiHeaderFieldId fieldId, int value, int mask) {
+            piFieldMatches.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask)));
+            return this;
+        }
+
+        /**
+         * Adds a ternary field match for the given fieldId, value and mask.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  ternary match value
+         * @param mask   ternary match mask
+         * @return this
+         */
+        public Builder matchTernary(PiHeaderFieldId fieldId, long value, long mask) {
+            piFieldMatches.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask)));
+            return this;
+        }
+
+        /**
+         * Adds a ternary field match for the given fieldId, value and mask.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param value  ternary match value
+         * @param mask   ternary match mask
+         * @return this
+         */
+        public Builder matchTernary(PiHeaderFieldId fieldId, byte[] value, byte[] mask) {
+            piFieldMatches.put(fieldId, new PiTernaryFieldMatch(fieldId, copyFrom(value), copyFrom(mask)));
+            return this;
+        }
+
+        /**
+         * Adds a longest-prefix field match for the given fieldId, value and prefix length.
+         *
+         * @param fieldId  protocol-independent header field Id
+         * @param value    lpm match value
+         * @param prefixLength lpm match prefix length
+         * @return this
+         */
+        public Builder matchLpm(PiHeaderFieldId fieldId, short value, int prefixLength) {
+            piFieldMatches.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength));
+            return this;
+        }
+
+        /**
+         * Adds a longest-prefix field match for the given fieldId, value and prefix length.
+         *
+         * @param fieldId  protocol-independent header field Id
+         * @param value    lpm match value
+         * @param prefixLength lpm match prefix length
+         * @return this
+         */
+        public Builder matchLpm(PiHeaderFieldId fieldId, int value, int prefixLength) {
+            piFieldMatches.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength));
+            return this;
+        }
+
+        /**
+         * Adds a longest-prefix field match for the given fieldId, value and prefix length.
+         *
+         * @param fieldId  protocol-independent header field Id
+         * @param value    lpm match value
+         * @param prefixLength lpm match prefix length
+         * @return this
+         */
+        public Builder matchLpm(PiHeaderFieldId fieldId, long value, int prefixLength) {
+            piFieldMatches.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength));
+            return this;
+        }
+
+        /**
+         * Adds a longest-prefix field match for the given fieldId, value and prefix length.
+         *
+         * @param fieldId  protocol-independent header field Id
+         * @param value    lpm match value
+         * @param prefixLength lpm match prefix length
+         * @return this
+         */
+        public Builder matchLpm(PiHeaderFieldId fieldId, byte[] value, int prefixLength) {
+            piFieldMatches.put(fieldId, new PiLpmFieldMatch(fieldId, copyFrom(value), prefixLength));
+            return this;
+        }
+
+        /**
+         * Adds a valid field match for the given fieldId and flag.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param flag    a boolean value
+         * @return this
+         */
+        public Builder matchValid(PiHeaderFieldId fieldId, boolean flag) {
+            piFieldMatches.put(fieldId, new PiValidFieldMatch(fieldId, flag));
+            return this;
+        }
+
+        /**
+         * Adds a range field match for the given fieldId, low and high.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param low   range match low value
+         * @param high  range match high value
+         * @return this
+         */
+        public Builder matchRange(PiHeaderFieldId fieldId, short low, short high) {
+            piFieldMatches.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high)));
+            return this;
+        }
+
+        /**
+         * Adds a range field match for the given fieldId, low and high.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param low   range match low value
+         * @param high  range match high value
+         * @return this
+         */
+        public Builder matchRange(PiHeaderFieldId fieldId, int low, int high) {
+            piFieldMatches.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high)));
+            return this;
+        }
+        /**
+         * Adds a range field match for the given fieldId, low and high.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param low   range match low value
+         * @param high  range match high value
+         * @return this
+         */
+        public Builder matchRange(PiHeaderFieldId fieldId, long low, long high) {
+            piFieldMatches.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high)));
+            return this;
+        }
+        /**
+         * Adds a range field match for the given fieldId, low and high.
+         *
+         * @param fieldId protocol-independent header field Id
+         * @param low   range match low value
+         * @param high  range match high value
+         * @return this
+         */
+        public Builder matchRange(PiHeaderFieldId fieldId, byte[] low, byte[] high) {
+            piFieldMatches.put(fieldId, new PiRangeFieldMatch(fieldId, copyFrom(low), copyFrom(high)));
+            return this;
+        }
+
+        /**
+         * Builds a PiCriterion.
+         *
+         * @return PiCriterion
+         */
+        public Criterion build() {
+            checkArgument(piFieldMatches.size() > 0);
+            return new PiCriterion(piFieldMatches.values());
+        }
+    }
+}
\ No newline at end of file