Fixed priority mapping in PiFlowRuleTranslotor

Change-Id: Ib68a133eec46a65b7a8f035c607b158116ac0a4a
diff --git a/core/net/src/main/java/org/onosproject/net/pi/impl/PiFlowRuleTranslator.java b/core/net/src/main/java/org/onosproject/net/pi/impl/PiFlowRuleTranslator.java
index a0b76e3..8d8d3c1 100644
--- a/core/net/src/main/java/org/onosproject/net/pi/impl/PiFlowRuleTranslator.java
+++ b/core/net/src/main/java/org/onosproject/net/pi/impl/PiFlowRuleTranslator.java
@@ -68,6 +68,7 @@
  */
 final class PiFlowRuleTranslator {
 
+    public static final int MAX_PI_PRIORITY = (int) Math.pow(2, 24);
     private static final Logger log = LoggerFactory.getLogger(PiFlowRuleTranslationServiceImpl.class);
 
     private PiFlowRuleTranslator() {
@@ -129,10 +130,17 @@
 
         PiTableEntry.Builder tableEntryBuilder = PiTableEntry.builder();
 
-        // In BMv2 0 is the highest priority.
-        // TODO: Move priority change to P4runtimeClient, in the table entry encode phase.
-        // Similarly, original priority should be re-established in the decode phase.
-        int newPriority = Integer.MAX_VALUE - rule.priority();
+        // In the P4 world 0 is the highest priority, in ONOS the lowest one.
+        // FIXME: move priority conversion to the driver, where different constraints might apply
+        // e.g. less bits for encoding priority in TCAM-based implementations.
+        int newPriority;
+        if (rule.priority() > MAX_PI_PRIORITY) {
+            log.warn("Flow rule priority too big, setting translated priority to max value {}: {}",
+                     MAX_PI_PRIORITY, rule);
+            newPriority = 0;
+        } else {
+            newPriority = MAX_PI_PRIORITY - rule.priority();
+        }
 
         tableEntryBuilder
                 .forTable(piTableId)
diff --git a/core/net/src/test/java/org/onosproject/net/pi/impl/PiFlowRuleTranslatorTest.java b/core/net/src/test/java/org/onosproject/net/pi/impl/PiFlowRuleTranslatorTest.java
index 0423331..6874292 100644
--- a/core/net/src/test/java/org/onosproject/net/pi/impl/PiFlowRuleTranslatorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/pi/impl/PiFlowRuleTranslatorTest.java
@@ -45,6 +45,7 @@
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.onosproject.net.pi.impl.MockInterpreter.*;
+import static org.onosproject.net.pi.impl.PiFlowRuleTranslator.MAX_PI_PRIORITY;
 
 /**
  * Tests for {@link PiFlowRuleTranslator}.
@@ -155,7 +156,7 @@
         assertThat("Incorrect ethType match param mask",
                    ethTypeParam.mask().asReadOnlyBuffer().getShort(), is(equalTo(ETH_TYPE_MASK)));
         assertThat("Incorrect priority value",
-                   entry1.priority().get(), is(equalTo(Integer.MAX_VALUE - rule1.priority())));
+                   entry1.priority().get(), is(equalTo(MAX_PI_PRIORITY - rule1.priority())));
         assertThat("Incorrect timeout value",
                    entry1.timeout(), is(equalTo(expectedTimeout)));