ONOS-6562 Implement P4RuntimePacketProvider

Change-Id: Ib6398544b514db9c8ab6ba1d991930d624a6b438
diff --git a/modules.defs b/modules.defs
index ba172d6..1ed85cc 100644
--- a/modules.defs
+++ b/modules.defs
@@ -75,6 +75,7 @@
     '//providers/lisp/device:onos-providers-lisp-device',
     '//providers/tl1/device:onos-providers-tl1-device',
     '//providers/general/device:onos-providers-general-device',
+    '//providers/p4runtime/packet:onos-providers-p4runtime-packet',
 
     '//web/api:onos-rest',
     '//web/gui:onos-gui',
@@ -132,6 +133,7 @@
     '//providers/lisp:onos-providers-lisp-oar',
     '//providers/tl1:onos-providers-tl1-oar',
     '//providers/general:onos-providers-general-oar',
+    '//providers/p4runtime/packet:onos-providers-p4runtime-packet-oar',
 #    '//providers/ietfte:onos-providers-ietfte-oar',
 ]
 
diff --git a/providers/p4runtime/packet/BUCK b/providers/p4runtime/packet/BUCK
new file mode 100644
index 0000000..29eace1
--- /dev/null
+++ b/providers/p4runtime/packet/BUCK
@@ -0,0 +1,21 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//protocols/p4runtime/api:onos-protocols-p4runtime-api',
+]
+
+BUNDLES = [
+    ':onos-providers-p4runtime-packet',
+]
+
+osgi_jar (
+    deps = COMPILE_DEPS,
+)
+
+onos_app (
+    app_name = 'org.onosproject.provider.p4runtime.packet',
+    title = 'P4Runtime Packet Provider',
+    category = 'Provider',
+    url = 'http://onosproject.org',
+    description = 'ONOS P4Runtime packet provider.',
+    included_bundles = BUNDLES,
+)
\ No newline at end of file
diff --git a/providers/p4runtime/packet/src/main/java/org/onosproject/provider/p4runtime/packet/impl/P4RuntimePacketProvider.java b/providers/p4runtime/packet/src/main/java/org/onosproject/provider/p4runtime/packet/impl/P4RuntimePacketProvider.java
new file mode 100644
index 0000000..998e4e6
--- /dev/null
+++ b/providers/p4runtime/packet/src/main/java/org/onosproject/provider/p4runtime/packet/impl/P4RuntimePacketProvider.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.provider.p4runtime.packet.impl;
+
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.packet.DefaultOutboundPacket;
+import org.onosproject.net.packet.DefaultPacketContext;
+import org.onosproject.net.packet.InboundPacket;
+import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.packet.PacketProgrammable;
+import org.onosproject.net.packet.PacketProvider;
+import org.onosproject.net.packet.PacketProviderRegistry;
+import org.onosproject.net.packet.PacketProviderService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.p4runtime.api.P4RuntimeController;
+import org.onosproject.p4runtime.api.P4RuntimeEvent;
+import org.onosproject.p4runtime.api.P4RuntimeEventListener;
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+
+import static org.onosproject.net.flow.DefaultTrafficTreatment.emptyTreatment;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of a packet provider for P4Runtime device.
+ */
+@Component(immediate = true)
+public class P4RuntimePacketProvider extends AbstractProvider implements PacketProvider {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected P4RuntimeController controller;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected PacketProviderRegistry providerRegistry;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    private PacketProviderService providerService;
+
+    private InternalPacketListener packetListener = new InternalPacketListener();
+
+    /**
+     * Creates a new P4Runtime packet provider.
+     */
+    public P4RuntimePacketProvider() {
+        super(new ProviderId("p4runtime", "org.onosproject.provider.p4runtime.packet"));
+    }
+
+    @Activate
+    protected void activate() {
+        providerService = providerRegistry.register(this);
+        controller.addListener(packetListener);
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        controller.removeListener(packetListener);
+        providerRegistry.unregister(this);
+        providerService = null;
+        log.info("Stopped");
+    }
+
+    @Override
+    public void emit(OutboundPacket packet) {
+        if (packet != null) {
+            DeviceId deviceId = packet.sendThrough();
+            Device device = deviceService.getDevice(deviceId);
+            if (device.is(PacketProgrammable.class)) {
+                PacketProgrammable packetProgrammable = device.as(PacketProgrammable.class);
+                packetProgrammable.emit(packet);
+            } else {
+                log.warn("No PacketProgrammable behavior for device {}", deviceId);
+            }
+        }
+    }
+
+    /**
+     * Internal packet context implementation.
+     */
+    private class P4RuntimePacketContext extends DefaultPacketContext {
+
+        P4RuntimePacketContext(long time, InboundPacket inPkt, OutboundPacket outPkt, boolean block) {
+            super(time, inPkt, outPkt, block);
+        }
+
+        @Override
+        public void send() {
+
+            if (this.block()) {
+                log.info("Unable to send, packet context is blocked");
+                return;
+            }
+
+            DeviceId deviceId = outPacket().sendThrough();
+            ByteBuffer rawData = outPacket().data();
+
+            TrafficTreatment treatment;
+            if (outPacket().treatment() == null) {
+                treatment = (treatmentBuilder() == null) ? emptyTreatment() : treatmentBuilder().build();
+            } else {
+                treatment = outPacket().treatment();
+            }
+
+            emit(new DefaultOutboundPacket(deviceId, treatment, rawData));
+        }
+    }
+
+    /**
+     * Internal packet listener to handle packet-in events received from the P4Runtime controller.
+     */
+    private class InternalPacketListener implements P4RuntimeEventListener {
+
+
+        @Override
+        public void event(P4RuntimeEvent event) {
+            /**
+             * TODO: handle packet-in event.
+             * The inputport need parse from metadata() of packet_in event,
+             * but support for parsing metadata() is not ready yet.
+             */
+
+        }
+    }
+}
diff --git a/providers/p4runtime/packet/src/main/java/org/onosproject/provider/p4runtime/packet/impl/package-info.java b/providers/p4runtime/packet/src/main/java/org/onosproject/provider/p4runtime/packet/impl/package-info.java
new file mode 100644
index 0000000..ee61af4
--- /dev/null
+++ b/providers/p4runtime/packet/src/main/java/org/onosproject/provider/p4runtime/packet/impl/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Provider that uses P4Runtime controller as a means of handling and
+ * emitting packets.
+ */
+package org.onosproject.provider.p4runtime.packet.impl;
\ No newline at end of file