New PiInterpreter API to map logical port to long type

Introduce a new API for PiInterpreter to map logical port number to a long
port number and deprecate the old one.
The reason we need to translate logical port number to a long type is that
the mapped number will become negative when translating port number to
an unsigned integer with MSB (e.g., 0xFFFFFFFD) and the low level driver
or translator is expecting to get a positive number.

Change-Id: I751e69461b0cf97280f12fd98ca2b15b83cbe84f
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 1ba85e0..11ca7c9 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
@@ -106,12 +106,26 @@
      *
      * @param port port number
      * @return optional integer
+     * @deprecated in ONOS 3.0 using {@link #mapLogicalPort} instead
      */
+    @Deprecated
     default Optional<Integer> mapLogicalPortNumber(PortNumber port) {
         return Optional.empty();
     }
 
     /**
+     * Maps the given logical port number to the data plane port ID (long)
+     * identifying the same port for this pipeconf, if such mapping is
+     * possible.
+     *
+     * @param port port number
+     * @return optional long
+     */
+    default Optional<Long> mapLogicalPort(PortNumber port) {
+        return mapLogicalPortNumber(port).map(integer -> (long) integer);
+    }
+
+    /**
      * If the given table allows for mutable default actions, this method
      * returns an action instance to be used when ONOS tries to remove a
      * different default action previously set.
diff --git a/core/net/src/main/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImpl.java b/core/net/src/main/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImpl.java
index 3407cf2..112aab2 100644
--- a/core/net/src/main/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImpl.java
+++ b/core/net/src/main/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImpl.java
@@ -156,7 +156,7 @@
                     "missing interpreter, cannot map logical port " + logicalPort.toString());
         }
         final PiPipelineInterpreter interpreter = device.as(PiPipelineInterpreter.class);
-        Optional<Integer> mappedPort = interpreter.mapLogicalPortNumber(logicalPort);
+        Optional<Long> mappedPort = interpreter.mapLogicalPort(logicalPort);
         if (!mappedPort.isPresent()) {
             throw new PiTranslationException(
                     "interpreter cannot map logical port " + logicalPort.toString());
diff --git a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricCapabilities.java b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricCapabilities.java
index 93605e2..fc9fe21 100644
--- a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricCapabilities.java
+++ b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricCapabilities.java
@@ -49,7 +49,7 @@
                 .table(FabricConstants.FABRIC_INGRESS_NEXT_HASHED).isPresent();
     }
 
-    public Optional<Integer> cpuPort() {
+    public Optional<Long> cpuPort() {
         // This is probably brittle, but needed to dynamically get the CPU port
         // for different platforms.
         if (!pipeconf.extension(CPU_PORT_TXT).isPresent()) {
@@ -67,7 +67,7 @@
                 return Optional.empty();
             }
             try {
-                return Optional.of(Integer.parseInt(str));
+                return Optional.of(Long.parseLong(str));
             } catch (NumberFormatException e) {
                 log.error("Invalid CPU port for {}: {}", pipeconf.id(), str);
                 return Optional.empty();
diff --git a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricInterpreter.java b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricInterpreter.java
index 9f848ed..46b584d 100644
--- a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricInterpreter.java
+++ b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/FabricInterpreter.java
@@ -308,7 +308,7 @@
     }
 
     @Override
-    public Optional<Integer> mapLogicalPortNumber(PortNumber port) {
+    public Optional<Long> mapLogicalPort(PortNumber port) {
         if (!port.equals(CONTROLLER)) {
             return Optional.empty();
         }
diff --git a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipeliner.java b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipeliner.java
index e3bc567..cea5c47 100644
--- a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipeliner.java
+++ b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipeliner.java
@@ -148,7 +148,7 @@
 
     protected void initializePipeline() {
         // Set up rules for packet-out forwarding. We support only IPv4 routing.
-        final int cpuPort = capabilities.cpuPort().get();
+        final long cpuPort = capabilities.cpuPort().get();
         flowRuleService.applyFlowRules(
                 ingressVlanRule(cpuPort, false, DEFAULT_VLAN),
                 fwdClassifierRule(cpuPort, null, Ethernet.TYPE_IPV4, FWD_IPV4_ROUTING,
@@ -390,7 +390,7 @@
                 .build();
     }
 
-    public FlowRule fwdClassifierRule(int port, Short ethType, short ipEthType, byte fwdType, int priority) {
+    public FlowRule fwdClassifierRule(long port, Short ethType, short ipEthType, byte fwdType, int priority) {
         final TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
                 .matchInPort(PortNumber.portNumber(port))
                 .matchPi(PiCriterion.builder()
diff --git a/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipelinerTest.java b/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipelinerTest.java
index d3a7c5c..ad988f5 100644
--- a/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipelinerTest.java
+++ b/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/pipeliner/FabricPipelinerTest.java
@@ -56,7 +56,7 @@
     private static final ApplicationId APP_ID = TestApplicationId.create("FabricPipelinerTest");
     private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:1");
     private static final int DEFAULT_FLOW_PRIORITY = 100;
-    private static final int CPU_PORT = 320;
+    private static final long CPU_PORT = 320;
     private static final byte FWD_IPV4_ROUTING = 2;
     private static final int DEFAULT_VLAN = 4094;
     public static final byte[] ONE = new byte[]{1};