ONOS-7066 ONOS-7067 PI abstractions refactoring and P4Info model parser

Includes changes previously reviewed in #15607, #15877, and #15955.

Change-Id: Ie2ff62e415f2099832ebfe05961a879b7b188fc3
diff --git a/pipelines/basic/BUCK b/pipelines/basic/BUCK
index 2f27732..1b6cc47 100644
--- a/pipelines/basic/BUCK
+++ b/pipelines/basic/BUCK
@@ -1,15 +1,13 @@
 COMPILE_DEPS = [
     '//lib:CORE_DEPS',
     '//lib:minimal-json',
-    '//incubator/bmv2/model:onos-incubator-bmv2-model',
     '//drivers/default:onos-drivers-default',
+    '//protocols/p4runtime/model:onos-protocols-p4runtime-model',
     '//protocols/p4runtime/api:onos-protocols-p4runtime-api',
 ]
 
 BUNDLES = [
     '//pipelines/basic:onos-pipelines-basic',
-    '//drivers/default:onos-drivers-default',
-    '//incubator/bmv2/model:onos-incubator-bmv2-model',
 ]
 
 osgi_jar(
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicConstants.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicConstants.java
index df0923a..b71acbf 100644
--- a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicConstants.java
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicConstants.java
@@ -16,14 +16,13 @@
 
 package org.onosproject.pipelines.basic;
 
-import org.onosproject.net.pi.runtime.PiActionId;
-import org.onosproject.net.pi.runtime.PiActionParamId;
-import org.onosproject.net.pi.runtime.PiActionProfileId;
-import org.onosproject.net.pi.runtime.PiCounterId;
-import org.onosproject.net.pi.runtime.PiCounterType;
-import org.onosproject.net.pi.runtime.PiHeaderFieldId;
-import org.onosproject.net.pi.runtime.PiPacketMetadataId;
-import org.onosproject.net.pi.runtime.PiTableId;
+import org.onosproject.net.pi.model.PiActionId;
+import org.onosproject.net.pi.model.PiActionParamId;
+import org.onosproject.net.pi.model.PiActionProfileId;
+import org.onosproject.net.pi.model.PiControlMetadataId;
+import org.onosproject.net.pi.model.PiCounterId;
+import org.onosproject.net.pi.model.PiMatchFieldId;
+import org.onosproject.net.pi.model.PiTableId;
 
 /**
  * Constants for the basic.p4 program.
@@ -32,29 +31,36 @@
 
     // TODO: constants could be auto-generated starting from the P4info.
 
+    private static final String TABLE0_CONTROL = "table0_control";
+    private static final String WCMP_CONTROL = "wcmp_control";
+
     // Header field IDs
+    public static final String DOT =  ".";
+    public static final String HDR = "hdr";
     public static final String ETHERNET = "ethernet";
     public static final String LOCAL_METADATA = "local_metadata";
     public static final String STANDARD_METADATA = "standard_metadata";
-    public static final PiHeaderFieldId HDR_IN_PORT_ID = PiHeaderFieldId.of(STANDARD_METADATA, "ingress_port");
-    public static final PiHeaderFieldId HDR_ETH_DST_ID = PiHeaderFieldId.of(ETHERNET, "dst_addr");
-    public static final PiHeaderFieldId HDR_ETH_SRC_ID = PiHeaderFieldId.of(ETHERNET, "src_addr");
-    public static final PiHeaderFieldId HDR_ETH_TYPE_ID = PiHeaderFieldId.of(ETHERNET, "ether_type");
-    public static final PiHeaderFieldId HDR_NEXT_HOP_ID = PiHeaderFieldId.of(LOCAL_METADATA, "next_hop_id");
-    public static final PiHeaderFieldId HDR_SELECTOR_ID = PiHeaderFieldId.of(LOCAL_METADATA, "selector");
+    public static final PiMatchFieldId HDR_IN_PORT_ID = PiMatchFieldId.of(STANDARD_METADATA + DOT + "ingress_port");
+    public static final PiMatchFieldId HDR_ETH_DST_ID = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + "dst_addr");
+    public static final PiMatchFieldId HDR_ETH_SRC_ID = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + "src_addr");
+    public static final PiMatchFieldId HDR_ETH_TYPE_ID = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + "ether_type");
+    public static final PiMatchFieldId HDR_NEXT_HOP_ID = PiMatchFieldId.of(LOCAL_METADATA + DOT + "next_hop_id");
+    public static final PiMatchFieldId HDR_SELECTOR_ID = PiMatchFieldId.of(LOCAL_METADATA + DOT + "selector");
     // Table IDs
-    public static final PiTableId TBL_TABLE0_ID = PiTableId.of("table0_control.table0");
-    public static final PiTableId TBL_WCMP_TABLE_ID = PiTableId.of("wcmp_control.wcmp_table");
+    public static final PiTableId TBL_TABLE0_ID = PiTableId.of(TABLE0_CONTROL + DOT  + "table0");
+    public static final PiTableId TBL_WCMP_TABLE_ID = PiTableId.of(WCMP_CONTROL + DOT  + "wcmp_table");
     // Counter IDs
-    public static final PiCounterId CNT_TABLE0_ID = PiCounterId.of("table0_control.table0_counter",
-                                                                   PiCounterType.DIRECT);
-    public static final PiCounterId CNT_WCMP_TABLE_ID = PiCounterId.of("wcmp_control.wcmp_table_counter",
-                                                                       PiCounterType.DIRECT);
+    public static final PiCounterId CNT_EGRESS_PORT_COUNTER_ID =
+            PiCounterId.of("port_counters_egress.egress_port_counter");
+    public static final PiCounterId CNT_INGRESS_PORT_COUNTER_ID =
+            PiCounterId.of("port_counters_ingress.ingress_port_counter");
+    public static final PiCounterId CNT_TABLE0_ID = PiCounterId.of(TABLE0_CONTROL + DOT  + "table0_counter");
+    public static final PiCounterId CNT_WCMP_TABLE_ID = PiCounterId.of(WCMP_CONTROL + DOT  + "wcmp_table_counter");
     // Action IDs
     public static final PiActionId ACT_NOACTION_ID = PiActionId.of("NoAction");
     public static final PiActionId ACT_DROP_ID = PiActionId.of("_drop");
     public static final PiActionId ACT_SET_EGRESS_PORT_ID = PiActionId.of("set_egress_port");
-    public static final PiActionId ACT_SET_NEXT_HOP_ID = PiActionId.of("table0_control.set_next_hop_id");
+    public static final PiActionId ACT_SET_NEXT_HOP_ID = PiActionId.of(TABLE0_CONTROL + DOT  + "set_next_hop_id");
     public static final PiActionId ACT_SEND_TO_CPU_ID = PiActionId.of("send_to_cpu");
     // Action Param IDs
     public static final PiActionParamId ACT_PRM_PORT_ID = PiActionParamId.of("port");
@@ -62,8 +68,8 @@
     // Action Profile IDs
     public static final PiActionProfileId ACT_PRF_WCMP_SELECTOR_ID = PiActionProfileId.of("wcmp_selector");
     // Packet Metadata IDs
-    public static final PiPacketMetadataId PKT_META_EGRESS_PORT_ID = PiPacketMetadataId.of("egress_port");
-    public static final PiPacketMetadataId PKT_META_INGRESS_PORT_ID = PiPacketMetadataId.of("ingress_port");
+    public static final PiControlMetadataId PKT_META_EGRESS_PORT_ID = PiControlMetadataId.of("egress_port");
+    public static final PiControlMetadataId PKT_META_INGRESS_PORT_ID = PiControlMetadataId.of("ingress_port");
     // Bitwidths
     public static final int PORT_BITWIDTH = 9;
 
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicInterpreterImpl.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicInterpreterImpl.java
index f9ff527..cc0ef46 100644
--- a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicInterpreterImpl.java
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicInterpreterImpl.java
@@ -22,7 +22,6 @@
 import org.onlab.packet.Ethernet;
 import org.onlab.util.ImmutableByteSequence;
 import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DeviceId;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
@@ -33,14 +32,14 @@
 import org.onosproject.net.packet.DefaultInboundPacket;
 import org.onosproject.net.packet.InboundPacket;
 import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.pi.model.PiCounterId;
+import org.onosproject.net.pi.model.PiMatchFieldId;
 import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.pi.model.PiTableId;
 import org.onosproject.net.pi.runtime.PiAction;
 import org.onosproject.net.pi.runtime.PiActionParam;
-import org.onosproject.net.pi.runtime.PiCounterId;
-import org.onosproject.net.pi.runtime.PiHeaderFieldId;
-import org.onosproject.net.pi.runtime.PiPacketMetadata;
+import org.onosproject.net.pi.runtime.PiControlMetadata;
 import org.onosproject.net.pi.runtime.PiPacketOperation;
-import org.onosproject.net.pi.runtime.PiTableId;
 
 import java.nio.ByteBuffer;
 import java.util.Collection;
@@ -55,7 +54,7 @@
 import static org.onosproject.net.PortNumber.FLOOD;
 import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
 import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
-import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT;
+import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
 import static org.onosproject.pipelines.basic.BasicConstants.ACT_DROP_ID;
 import static org.onosproject.pipelines.basic.BasicConstants.ACT_NOACTION_ID;
 import static org.onosproject.pipelines.basic.BasicConstants.ACT_PRM_PORT_ID;
@@ -88,8 +87,8 @@
                     .put(TBL_TABLE0_ID, CNT_TABLE0_ID)
                     .put(TBL_WCMP_TABLE_ID, CNT_WCMP_TABLE_ID)
                     .build();
-    private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP =
-            new ImmutableBiMap.Builder<Criterion.Type, PiHeaderFieldId>()
+    private static final ImmutableBiMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
+            new ImmutableBiMap.Builder<Criterion.Type, PiMatchFieldId>()
                     .put(Criterion.Type.IN_PORT, HDR_IN_PORT_ID)
                     .put(Criterion.Type.ETH_DST, HDR_ETH_DST_ID)
                     .put(Criterion.Type.ETH_SRC, HDR_ETH_SRC_ID)
@@ -183,7 +182,7 @@
     }
 
     @Override
-    public InboundPacket mapInboundPacket(DeviceId deviceId, PiPacketOperation packetIn)
+    public InboundPacket mapInboundPacket(PiPacketOperation packetIn)
             throws PiInterpreterException {
         // Assuming that the packet is ethernet, which is fine since basic.p4
         // can deparse only ethernet packets.
@@ -196,36 +195,37 @@
         }
 
         // Returns the ingress port packet metadata.
-        Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas()
+        Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
                 .stream().filter(m -> m.id().equals(PKT_META_INGRESS_PORT_ID))
                 .findFirst();
 
         if (packetMetadata.isPresent()) {
             ImmutableByteSequence portByteSequence = packetMetadata.get().value();
             short s = portByteSequence.asReadOnlyBuffer().getShort();
-            ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
+            ConnectPoint receivedFrom = new ConnectPoint(packetIn.deviceId(), PortNumber.portNumber(s));
             ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
             return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
         } else {
             throw new PiInterpreterException(format(
                     "Missing metadata '%s' in packet-in received from '%s': %s",
-                    PKT_META_INGRESS_PORT_ID, deviceId, packetIn));
+                    PKT_META_INGRESS_PORT_ID, packetIn.deviceId(), packetIn));
         }
     }
 
     private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber)
             throws PiInterpreterException {
-        PiPacketMetadata metadata = createPacketMetadata(portNumber);
+        PiControlMetadata metadata = createPacketMetadata(portNumber);
         return PiPacketOperation.builder()
+                .forDevice(this.data().deviceId())
                 .withType(PACKET_OUT)
                 .withData(copyFrom(data))
                 .withMetadatas(ImmutableList.of(metadata))
                 .build();
     }
 
-    private PiPacketMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
+    private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
         try {
-            return PiPacketMetadata.builder()
+            return PiControlMetadata.builder()
                     .withId(PKT_META_EGRESS_PORT_ID)
                     .withValue(fit(copyFrom(portNumber), PORT_BITWIDTH))
                     .build();
@@ -236,12 +236,12 @@
     }
 
     @Override
-    public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
+    public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
         return Optional.ofNullable(CRITERION_MAP.get(type));
     }
 
     @Override
-    public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
+    public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId headerFieldId) {
         return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
     }
 
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpConstants.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpConstants.java
index 284d5cd..1671d0a 100644
--- a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpConstants.java
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpConstants.java
@@ -16,7 +16,7 @@
 
 package org.onosproject.pipelines.basic;
 
-import org.onosproject.net.pi.runtime.PiTableId;
+import org.onosproject.net.pi.model.PiTableId;
 
 /**
  * Constants for the ecmp.p4 program.
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpInterpreterImpl.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpInterpreterImpl.java
index 2596ee9..d103cdb 100644
--- a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpInterpreterImpl.java
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/EcmpInterpreterImpl.java
@@ -17,7 +17,7 @@
 package org.onosproject.pipelines.basic;
 
 import com.google.common.collect.ImmutableBiMap;
-import org.onosproject.net.pi.runtime.PiTableId;
+import org.onosproject.net.pi.model.PiTableId;
 
 import java.util.Optional;
 
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PipeconfLoader.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PipeconfLoader.java
index bcfb7d0..9e298d3 100644
--- a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PipeconfLoader.java
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PipeconfLoader.java
@@ -22,7 +22,6 @@
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
 import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
 import org.onosproject.net.behaviour.Pipeliner;
 import org.onosproject.net.device.PortStatisticsDiscovery;
@@ -32,6 +31,8 @@
 import org.onosproject.net.pi.model.PiPipelineInterpreter;
 import org.onosproject.net.pi.model.PiPipelineModel;
 import org.onosproject.net.pi.runtime.PiPipeconfService;
+import org.onosproject.p4runtime.model.P4InfoParser;
+import org.onosproject.p4runtime.model.P4InfoParserException;
 
 import java.net.URL;
 import java.util.Collection;
@@ -75,10 +76,10 @@
     private static PiPipeconf buildBasicPipeconf() {
         final URL jsonUrl = PipeconfLoader.class.getResource(BASIC_JSON_PATH);
         final URL p4InfoUrl = PipeconfLoader.class.getResource(BASIC_P4INFO);
-        final PiPipelineModel model = Bmv2PipelineModelParser.parse(jsonUrl);
+
         return DefaultPiPipeconf.builder()
                 .withId(BASIC_PIPECONF_ID)
-                .withPipelineModel(model)
+                .withPipelineModel(parseP4Info(p4InfoUrl))
                 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
                 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
                 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
@@ -92,10 +93,10 @@
     private static PiPipeconf buildEcmpPipeconf() {
         final URL jsonUrl = PipeconfLoader.class.getResource(ECMP_JSON_PATH);
         final URL p4InfoUrl = PipeconfLoader.class.getResource(ECMP_P4INFO);
-        final PiPipelineModel model = Bmv2PipelineModelParser.parse(jsonUrl);
+
         return DefaultPiPipeconf.builder()
                 .withId(ECMP_PIPECONF_ID)
-                .withPipelineModel(model)
+                .withPipelineModel(parseP4Info(p4InfoUrl))
                 .addBehaviour(PiPipelineInterpreter.class, EcmpInterpreterImpl.class)
                 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
                 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
@@ -103,4 +104,12 @@
                 .addExtension(BMV2_JSON, jsonUrl)
                 .build();
     }
+
+    private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
+        try {
+            return P4InfoParser.parse(p4InfoUrl);
+        } catch (P4InfoParserException e) {
+            throw new RuntimeException(e);
+        }
+    }
 }
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java
index 425f8fe..bfcdd86 100644
--- a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java
@@ -24,11 +24,10 @@
 import org.onosproject.net.device.PortStatistics;
 import org.onosproject.net.device.PortStatisticsDiscovery;
 import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.pi.model.PiCounterId;
 import org.onosproject.net.pi.model.PiPipeconf;
 import org.onosproject.net.pi.runtime.PiCounterCellData;
 import org.onosproject.net.pi.runtime.PiCounterCellId;
-import org.onosproject.net.pi.runtime.PiCounterId;
-import org.onosproject.net.pi.runtime.PiIndirectCounterCellId;
 import org.onosproject.net.pi.runtime.PiPipeconfService;
 import org.onosproject.p4runtime.api.P4RuntimeClient;
 import org.onosproject.p4runtime.api.P4RuntimeController;
@@ -42,7 +41,9 @@
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 
-import static org.onosproject.net.pi.runtime.PiCounterType.INDIRECT;
+import static org.onosproject.net.pi.model.PiCounterType.INDIRECT;
+import static org.onosproject.pipelines.basic.BasicConstants.CNT_EGRESS_PORT_COUNTER_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.CNT_INGRESS_PORT_COUNTER_ID;
 
 /**
  * Implementation of the PortStatisticsBehaviour for basic.p4.
@@ -51,19 +52,13 @@
 
     protected final Logger log = LoggerFactory.getLogger(getClass());
 
-    private static final String SCOPE = "port_counters_control";
-    private static final PiCounterId INGRESS_COUNTER_ID = PiCounterId.of("port_counters_ingress",
-                                                                         "ingress_port_counter", INDIRECT);
-    private static final PiCounterId EGRESS_COUNTER_ID = PiCounterId.of("port_counters_egress",
-                                                                        "egress_port_counter", INDIRECT);
-
     /**
      * Returns the ID of the ingress port counter.
      *
      * @return counter ID
      */
     public PiCounterId ingressCounterId() {
-        return INGRESS_COUNTER_ID;
+        return CNT_INGRESS_PORT_COUNTER_ID;
     }
 
     /**
@@ -72,7 +67,7 @@
      * @return counter ID
      */
     public PiCounterId egressCounterId() {
-        return EGRESS_COUNTER_ID;
+        return CNT_EGRESS_PORT_COUNTER_ID;
     }
 
     @Override
@@ -106,8 +101,8 @@
         Set<PiCounterCellId> counterCellIds = Sets.newHashSet();
         portStatBuilders.keySet().forEach(p -> {
             // Counter cell/index = port number.
-            counterCellIds.add(PiIndirectCounterCellId.of(ingressCounterId(), p));
-            counterCellIds.add(PiIndirectCounterCellId.of(egressCounterId(), p));
+            counterCellIds.add(PiCounterCellId.ofIndirect(ingressCounterId(), p));
+            counterCellIds.add(PiCounterCellId.ofIndirect(egressCounterId(), p));
         });
 
         Collection<PiCounterCellData> counterEntryResponse;
@@ -120,11 +115,11 @@
         }
 
         counterEntryResponse.forEach(counterData -> {
-            if (counterData.cellId().type() != INDIRECT) {
-                log.warn("Invalid counter data type {}, skipping", counterData.cellId().type());
+            if (counterData.cellId().counterType() != INDIRECT) {
+                log.warn("Invalid counter data type {}, skipping", counterData.cellId().counterType());
                 return;
             }
-            PiIndirectCounterCellId indCellId = (PiIndirectCounterCellId) counterData.cellId();
+            PiCounterCellId indCellId = counterData.cellId();
             if (!portStatBuilders.containsKey(indCellId.index())) {
                 log.warn("Unrecognized counter index {}, skipping", counterData);
                 return;