ONOS-6605 PI flow rule translator implementation

Change-Id: Icac66f17677c494152207f4b52355ad647e1227b
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineInterpreter.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineInterpreter.java
index 443c15b..8d06100 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineInterpreter.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineInterpreter.java
@@ -18,15 +18,17 @@
 
 import com.google.common.annotations.Beta;
 import org.onosproject.net.driver.HandlerBehaviour;
+import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.net.flow.criteria.Criterion;
 import org.onosproject.net.pi.runtime.PiHeaderFieldId;
 import org.onosproject.net.pi.runtime.PiTableAction;
+import org.onosproject.net.pi.runtime.PiTableId;
 
 import java.util.Optional;
 
 /**
- * An interpreter of a protocol-independent pipeline.
+ * An interpreter of a protocol-independent pipeline model.
  */
 @Beta
 public interface PiPipelineInterpreter extends HandlerBehaviour {
@@ -52,6 +54,16 @@
     Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId);
 
     /**
+     * Returns a protocol-independent table id equivalent to the given numeric table id (as in
+     * {@link FlowRule#tableId()}). If not present, it means that the given numeric table id cannot
+     * be mapped to any table of the pipeline model.
+     *
+     * @param flowRuleTableId a numeric table id
+     * @return a protocol-independent table id
+     */
+    Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId);
+
+    /**
      * Returns a table action of a protocol-independent pipeline that is functionally equivalent to
      * the given ONOS traffic treatment for the given pipeline configuration.
      *
@@ -72,4 +84,4 @@
             super(message);
         }
     }
-}
+}
\ No newline at end of file
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java
index 420b5f6..5709e4e 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java
@@ -19,6 +19,7 @@
 import com.google.common.annotations.Beta;
 
 import java.util.Collection;
+import java.util.Optional;
 
 /**
  * Model of a match+action table in a protocol-independent pipeline.
@@ -67,4 +68,14 @@
      * @return a collection of action models
      */
     Collection<PiActionModel> actions();
+
+    /**
+     * Returns the action model associated with the given name, if present.
+     * If not present, it means that this table does not support such an action.
+     *
+     * @param name string value
+     * @return optional action model
+     */
+    Optional<PiActionModel> action(String name);
+
 }
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiActionId.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiActionId.java
index d0655da..7a04318 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiActionId.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiActionId.java
@@ -38,6 +38,15 @@
     }
 
     /**
+     * Returns the name of the action.
+     *
+     * @return action name
+     */
+    public String name() {
+        return this.identifier;
+    }
+
+    /**
      * Returns an action identifier with the given name.
      *
      * @param name action name
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
index c6f6094..63e478d 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
@@ -19,7 +19,7 @@
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Objects;
-import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
 
 import java.util.Collection;
@@ -39,7 +39,7 @@
     private static final double NO_TIMEOUT = -1;
 
     private final PiTableId tableId;
-    private final Collection<PiFieldMatch> fieldMatches;
+    private final Map<PiHeaderFieldId, PiFieldMatch> fieldMatches;
     private final PiTableAction tableAction;
     private final long cookie;
     private final int priority;
@@ -48,7 +48,7 @@
     private PiTableEntry(PiTableId tableId, Map<PiHeaderFieldId, PiFieldMatch> fieldMatches,
                          PiTableAction tableAction, long cookie, int priority, double timeout) {
         this.tableId = tableId;
-        this.fieldMatches = ImmutableSet.copyOf(fieldMatches.values());
+        this.fieldMatches = ImmutableMap.copyOf(fieldMatches);
         this.tableAction = tableAction;
         this.cookie = cookie;
         this.priority = priority;
@@ -70,7 +70,17 @@
      * @return collection of field matches
      */
     public Collection<PiFieldMatch> fieldMatches() {
-        return fieldMatches;
+        return fieldMatches.values();
+    }
+
+    /**
+     * If present, returns the field match associated with the given header field identifier.
+     *
+     * @param fieldId field identifier
+     * @return optional field match
+     */
+    public Optional<PiFieldMatch> fieldMatch(PiHeaderFieldId fieldId) {
+        return Optional.ofNullable(fieldMatches.get(fieldId));
     }
 
     /**
@@ -171,7 +181,7 @@
          * @param tableId table identifier
          * @return this
          */
-        Builder forTable(PiTableId tableId) {
+        public Builder forTable(PiTableId tableId) {
             this.tableId = checkNotNull(tableId);
             return this;
         }
@@ -182,7 +192,7 @@
          * @param tableAction table action
          * @return this
          */
-        Builder withAction(PiTableAction tableAction) {
+        public Builder withAction(PiTableAction tableAction) {
             this.tableAction = checkNotNull(tableAction);
             return this;
         }
@@ -193,7 +203,7 @@
          * @param fieldMatch field match
          * @return this
          */
-        Builder withFieldMatch(PiFieldMatch fieldMatch) {
+        public Builder withFieldMatch(PiFieldMatch fieldMatch) {
             this.fieldMatches.put(fieldMatch.fieldId(), fieldMatch);
             return this;
         }
@@ -204,7 +214,7 @@
          * @param fieldMatches collection of field matches
          * @return this
          */
-        Builder withFieldMatches(Collection<PiFieldMatch> fieldMatches) {
+        public Builder withFieldMatches(Collection<PiFieldMatch> fieldMatches) {
             fieldMatches.forEach(f -> this.fieldMatches.put(f.fieldId(), f));
             return this;
         }
@@ -215,7 +225,7 @@
          * @param cookie cookie
          * @return this
          */
-        Builder withCookie(long cookie) {
+        public Builder withCookie(long cookie) {
             this.cookie = cookie;
             return this;
         }
@@ -226,7 +236,7 @@
          * @param priority priority
          * @return this
          */
-        Builder withPriority(int priority) {
+        public Builder withPriority(int priority) {
             checkArgument(priority >= 0, "Priority must be a positive integer.");
             this.priority = priority;
             return this;
@@ -238,7 +248,7 @@
          * @param seconds timeout in seconds
          * @return this
          */
-        Builder withTimeout(double seconds) {
+        public Builder withTimeout(double seconds) {
             checkArgument(seconds > 0, "Timeout must be greater than zero.");
             this.timeout = seconds;
             return this;
@@ -249,7 +259,7 @@
          *
          * @return a new table entry
          */
-        PiTableEntry build() {
+        public PiTableEntry build() {
             checkNotNull(tableId);
             checkNotNull(tableAction);
             return new PiTableEntry(tableId, fieldMatches, tableAction, cookie, priority, timeout);