ONOS-6559 P4Runtime protocol library

Change-Id: I7070b69507dcf2ca47ee1c446bcc2505ca868fb1
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java
index 99754a8..f94ae6e 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java
@@ -19,7 +19,7 @@
 import com.google.common.annotations.Beta;
 import org.onosproject.net.driver.Behaviour;
 
-import java.nio.ByteBuffer;
+import java.io.InputStream;
 import java.util.Collection;
 import java.util.Optional;
 
@@ -68,28 +68,33 @@
     boolean hasBehaviour(Class<? extends Behaviour> behaviourClass);
 
     /**
-     * Returns, if present, an arbitrary sequence of bytes representing a device-specific or control
+     * Returns, if present, an input stream of ad device-specific or control
      * protocol-specific extension of this configuration. For example, if requesting a
      * target-specific P4 binary, this will return the same bytes produced by the P4 compiler.
      *
      * @param type extension type
-     * @return extension bytes
+     * @return extension input stream
      */
     // FIXME: this is a sloppy way of handling extensions.
-    Optional<ByteBuffer> extension(ExtensionType type);
+    Optional<InputStream> extension(ExtensionType type);
 
     /**
      * Type of extension of a protocol-independent pipeline configuration.
      */
     enum ExtensionType {
         /**
-         * The P4Info as returned by the p4c compiler (in binary format).
+         * The P4Info as returned by the p4c compiler in text format.
          */
-        P4_INFO_BINARY,
+        P4_INFO_TEXT,
 
         /**
          * BMv2 JSON configuration.
          */
-        BMV2_JSON
+        BMV2_JSON,
+
+        /**
+         * Barefoot's Tofino configuration binary.
+         */
+        TOFINO_BIN
     }
 }
diff --git a/core/net/src/test/java/org/onosproject/net/pi/impl/MockPipeconf.java b/core/net/src/test/java/org/onosproject/net/pi/impl/MockPipeconf.java
index c5f90eb..4e98567 100644
--- a/core/net/src/test/java/org/onosproject/net/pi/impl/MockPipeconf.java
+++ b/core/net/src/test/java/org/onosproject/net/pi/impl/MockPipeconf.java
@@ -27,8 +27,8 @@
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.nio.ByteBuffer;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
@@ -84,7 +84,7 @@
     }
 
     @Override
-    public Optional<ByteBuffer> extension(ExtensionType type) {
+    public Optional<InputStream> extension(ExtensionType type) {
         return Optional.empty();
     }
 }
diff --git a/incubator/bmv2/model/src/main/java/org/onosproject/bmv2/model/Bmv2Pipeconf.java b/incubator/bmv2/model/src/main/java/org/onosproject/bmv2/model/Bmv2Pipeconf.java
index 6c86d61..8987650 100644
--- a/incubator/bmv2/model/src/main/java/org/onosproject/bmv2/model/Bmv2Pipeconf.java
+++ b/incubator/bmv2/model/src/main/java/org/onosproject/bmv2/model/Bmv2Pipeconf.java
@@ -22,7 +22,7 @@
 import org.onosproject.net.pi.model.PiPipeconfId;
 import org.onosproject.net.pi.model.PiPipelineModel;
 
-import java.nio.ByteBuffer;
+import java.io.InputStream;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
@@ -37,7 +37,7 @@
     private final PiPipeconfId id;
     private final Bmv2PipelineModel pipelineModel;
     private final Set<Class<? extends Behaviour>> behaviours;
-    private final Map<ExtensionType, ByteBuffer> extensions;
+    private final Map<ExtensionType, InputStream> extensions;
 
     /**
      * Builds a new BMv2 pipeline configuration (pipeconf) by given information.
@@ -50,7 +50,7 @@
     public Bmv2Pipeconf(PiPipeconfId id,
                         Bmv2PipelineModel pipelineModel,
                         Set<Class<? extends Behaviour>> behaviours,
-                        Map<ExtensionType, ByteBuffer> extensions) {
+                        Map<ExtensionType, InputStream> extensions) {
         checkNotNull(id, "Pipeconf Id can't be null");
         checkNotNull(pipelineModel, "Pipeline model can't be null");
 
@@ -89,7 +89,7 @@
     }
 
     @Override
-    public Optional<ByteBuffer> extension(ExtensionType type) {
+    public Optional<InputStream> extension(ExtensionType type) {
         return Optional.ofNullable(extensions.get(type));
     }
 }
diff --git a/lib/BUCK b/lib/BUCK
index fff5916..38c65e9 100644
--- a/lib/BUCK
+++ b/lib/BUCK
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Mon, 10 Jul 2017 20:01:19 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Mon, 10 Jul 2017 20:46:48 GMT. Do not edit this file manually. *****
 # ***** Use onos-lib-gen *****
 
 pass_thru_pom(
@@ -1336,6 +1336,15 @@
 )
 
 remote_jar (
+  name = 'grpc-protobuf-lite-1.3.0',
+  out = 'grpc-protobuf-lite-1.3.0.jar',
+  url = 'mvn:io.grpc:grpc-protobuf-lite:jar:1.3.0',
+  sha1 = '0aea7ea9cd12ad1938231769738016622461b1ed',
+  maven_coords = 'io.grpc:grpc-protobuf-lite:jar:NON-OSGI:1.3.0',
+  visibility = [ 'PUBLIC' ],
+)
+
+remote_jar (
   name = 'grpc-stub-1.3.0',
   out = 'grpc-stub-1.3.0.jar',
   url = 'mvn:io.grpc:grpc-stub:jar:1.3.0',
diff --git a/lib/deps.json b/lib/deps.json
index 5751bc9..8eae68a 100644
--- a/lib/deps.json
+++ b/lib/deps.json
@@ -245,6 +245,7 @@
     "sshd-core": "mvn:org.apache.sshd:sshd-core:1.4.0",
     "grpc-core-1.3.0": "mvn:io.grpc:grpc-core:1.3.0",
     "grpc-protobuf-1.3.0": "mvn:io.grpc:grpc-protobuf:1.3.0",
+    "grpc-protobuf-lite-1.3.0": "mvn:io.grpc:grpc-protobuf-lite:1.3.0",
     "grpc-stub-1.3.0": "mvn:io.grpc:grpc-stub:1.3.0",
     "grpc-netty-1.3.0": "mvn:io.grpc:grpc-netty:1.3.0",
     "grpc-auth-1.3.0": "mvn:io.grpc:grpc-auth:1.3.0",
diff --git a/modules.defs b/modules.defs
index 3c51154..30fbc3e 100644
--- a/modules.defs
+++ b/modules.defs
@@ -42,6 +42,9 @@
     '//protocols/ovsdb/rfc:onos-protocols-ovsdb-rfc',
     '//protocols/ovsdb/api:onos-protocols-ovsdb-api',
     '//protocols/ovsdb/ctl:onos-protocols-ovsdb-ctl',
+    '//protocols/p4runtime/api:onos-protocols-p4runtime-api',
+    '//protocols/p4runtime/ctl:onos-protocols-p4runtime-ctl',
+    '//protocols/p4runtime/proto:onos-protocols-p4runtime-proto',
     '//protocols/pcep/pcepio:onos-protocols-pcep-pcepio',
     '//protocols/pcep/api:onos-protocols-pcep-api',
     '//protocols/pcep/ctl:onos-protocols-pcep-ctl',
diff --git a/protocols/p4runtime/api/BUCK b/protocols/p4runtime/api/BUCK
new file mode 100644
index 0000000..21506be
--- /dev/null
+++ b/protocols/p4runtime/api/BUCK
@@ -0,0 +1,15 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//incubator/grpc-dependencies:grpc-core-repkg-1.3.0',
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+    '//core/api:onos-api-tests',
+]
+
+
+osgi_jar_with_tests(
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+)
\ No newline at end of file
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeClient.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeClient.java
new file mode 100644
index 0000000..ee622cb
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeClient.java
@@ -0,0 +1,83 @@
+/*
+ * 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.p4runtime.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.pi.runtime.PiTableEntry;
+import org.onosproject.net.pi.runtime.PiTableId;
+
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Client to control a P4Runtime device.
+ */
+@Beta
+public interface P4RuntimeClient {
+
+    /**
+     * Type of write operation.
+     */
+    enum WriteOperationType {
+        UNSPECIFIED,
+        INSERT,
+        UPDATE,
+        DELETE
+    }
+
+    /**
+     * Sets the pipeline configuration. This method should be called before any other method of this client.
+     *
+     * @param p4Info       input stream of a P4Info message in text format
+     * @param targetConfig input stream of the target-specific configuration (e.g. BMv2 JSON)
+     * @return a completable future of a boolean, true if the operations was successful, false otherwise.
+     */
+    CompletableFuture<Boolean> setPipelineConfig(InputStream p4Info, InputStream targetConfig);
+
+    /**
+     * Initializes the stream channel, after which all messages received from the device will be notified using the
+     * {@link P4RuntimeController} event listener.
+     *
+     * @return a completable future of a boolean, true if the operations was successful, false otherwise.
+     */
+    CompletableFuture<Boolean> initStreamChannel();
+
+    /**
+     * Performs the given write operation for the given table entries.
+     *
+     * @param entries table entries
+     * @param opType  operation type.
+     * @return true if the operation was successful, false otherwise.
+     */
+    boolean writeTableEntries(Collection<PiTableEntry> entries, WriteOperationType opType);
+
+    /**
+     * Dumps all entries currently installed in the given table.
+     *
+     * @param tableId table identifier
+     * @return completable future of a collection of table entries
+     */
+    CompletableFuture<Collection<PiTableEntry>> dumpTable(PiTableId tableId);
+
+    /**
+     * Shutdown the client by terminating any active RPC such as the stream channel.
+     */
+    void shutdown();
+
+    // TODO: work in progress.
+}
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeController.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeController.java
new file mode 100644
index 0000000..3b8074f
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeController.java
@@ -0,0 +1,68 @@
+/*
+ * 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.p4runtime.api;
+
+import com.google.common.annotations.Beta;
+import io.grpc.ManagedChannelBuilder;
+import org.onosproject.event.ListenerService;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Controller of P4Runtime devices.
+ */
+@Beta
+public interface P4RuntimeController extends ListenerService<P4RuntimeEvent, P4RuntimeEventListener> {
+
+    /**
+     * Instantiates a new client to operate on the device identified by the given information and reachable using the
+     * given gRPC channel builder. As a result of this method, a {@link P4RuntimeClient} can be later obtained by
+     * invoking {@link #getClient(DeviceId)}. Only one client can exist for the same device identifier. Returns true if
+     * the client was created and the channel to the device is open, false otherwise.
+     *
+     * @param deviceId       device identifier
+     * @param p4DeviceId     P4Runtime-specific device identifier
+     * @param channelBuilder gRPC channel builder pointing at the P4Runtime server in execution on the device
+     * @return true if the client was created and the channel to the device is open
+     * @throws IllegalStateException if a client already exists for the given device identifier
+     */
+    boolean createClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder);
+
+    /**
+     * Returns a client to operate on the given device.
+     *
+     * @param deviceId device identifier
+     * @return client instance
+     * @throws IllegalStateException if no client exists for the given device identifier
+     */
+    P4RuntimeClient getClient(DeviceId deviceId);
+
+    /**
+     * Removes the client for the given device. If no client exists for the given device identifier, the
+     * result is a no-op.
+     *
+     * @param deviceId device identifier
+     */
+    void removeClient(DeviceId deviceId);
+
+    /**
+     * Returns true if a client exists for the given device identifier, false otherwise.
+     *
+     * @param deviceId device identifier
+     * @return true if client exists, false otherwise.
+     */
+    boolean hasClient(DeviceId deviceId);
+}
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEvent.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEvent.java
new file mode 100644
index 0000000..9ba3d58
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEvent.java
@@ -0,0 +1,28 @@
+/*
+ * 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.p4runtime.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.event.Event;
+
+/**
+ * Representation of an event received from a P4Runtime device.
+ */
+@Beta
+public interface P4RuntimeEvent extends Event<P4RuntimeEventListener.Type, P4RuntimeEventSubject> {
+
+}
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEventListener.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEventListener.java
new file mode 100644
index 0000000..0a63355
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEventListener.java
@@ -0,0 +1,38 @@
+/*
+ * 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.p4runtime.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.event.EventListener;
+
+/**
+ * A listener of events received from P4Runtime devices.
+ */
+@Beta
+public interface P4RuntimeEventListener extends EventListener<P4RuntimeEvent> {
+
+    /**
+     * Type of event.
+     */
+    enum Type {
+        /**
+         * A packet-in.
+         */
+        PACKET_IN,
+        // TODO: add mastership, device as soon as we define those.
+    }
+}
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEventSubject.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEventSubject.java
new file mode 100644
index 0000000..23b6540
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeEventSubject.java
@@ -0,0 +1,27 @@
+/*
+ * 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.p4runtime.api;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Information about an event generated by a P4Runtime device .
+ */
+@Beta
+public interface P4RuntimeEventSubject {
+
+}
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimePacketIn.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimePacketIn.java
new file mode 100644
index 0000000..338b19a
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimePacketIn.java
@@ -0,0 +1,52 @@
+/*
+ * 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.p4runtime.api;
+
+import com.google.common.annotations.Beta;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.net.DeviceId;
+
+import java.util.List;
+
+/**
+ * Information about a packet-in received from a P4Runtime device.
+ */
+@Beta
+public interface P4RuntimePacketIn extends P4RuntimeEventSubject {
+
+    /**
+     * Returns the identifier of the device that generated this packet-in.
+     *
+     * @return device identifier
+     */
+    DeviceId deviceId();
+
+    /**
+     * Returns the packet raw data.
+     *
+     * @return byte sequence
+     */
+    ImmutableByteSequence data();
+
+    /**
+     * Returns the list of metadata associated with this packet-in, to be parsed by a
+     * {@link org.onosproject.net.pi.model.PiPipelineInterpreter}.
+     *
+     * @return list of byte sequences
+     */
+    List<ImmutableByteSequence> metadata();
+}
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/package-info.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/package-info.java
new file mode 100644
index 0000000..c5d10ca
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * P4Runtime protocol API.
+ */
+package org.onosproject.p4runtime.api;
\ No newline at end of file
diff --git a/protocols/p4runtime/ctl/BUCK b/protocols/p4runtime/ctl/BUCK
new file mode 100644
index 0000000..baf1645
--- /dev/null
+++ b/protocols/p4runtime/ctl/BUCK
@@ -0,0 +1,46 @@
+GRPC_DEPS = [
+    '//incubator/grpc-dependencies:grpc-core-repkg-1.3.0',
+    '//lib:grpc-protobuf-1.3.0',
+    '//lib:grpc-protobuf-lite-1.3.0',
+    '//lib:grpc-stub-1.3.0',
+    '//lib:grpc-netty-1.3.0',
+    '//lib:grpc-auth-1.3.0',
+    '//lib:google-instrumentation-0.3.0',
+    '//lib:protobuf-java-3.0.2',
+    # Lazily adding all netty-related packages.
+    # Some of them might not be necessary.
+    '//lib:netty',
+    '//lib:netty-buffer',
+    '//lib:netty-codec',
+    '//lib:netty-codec-http',
+    '//lib:netty-codec-http2',
+    '//lib:netty-common',
+    '//lib:netty-handler',
+    '//lib:netty-transport',
+    '//lib:netty-transport-native-epoll',
+    '//lib:netty-resolver',
+]
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//protocols/grpc/api:onos-protocols-grpc-api',
+    '//protocols/p4runtime/api:onos-protocols-p4runtime-api',
+    '//protocols/p4runtime/proto:onos-protocols-p4runtime-proto'
+] + GRPC_DEPS
+
+TEST_DEPS = [
+    '//lib:TEST',
+    '//core/api:onos-api-tests',
+]
+
+BUNDLES = [
+    '//protocols/grpc/api:onos-protocols-grpc-api',
+    '//protocols/p4runtime/api:onos-protocols-p4runtime-api',
+    '//protocols/p4runtime/proto:onos-protocols-p4runtime-proto'
+] + GRPC_DEPS
+
+
+osgi_jar_with_tests(
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+)
\ No newline at end of file
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/DefaultPacketInEvent.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/DefaultPacketInEvent.java
new file mode 100644
index 0000000..dd2f021
--- /dev/null
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/DefaultPacketInEvent.java
@@ -0,0 +1,93 @@
+/*
+ * 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.p4runtime.ctl;
+
+import com.google.common.base.Objects;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.event.AbstractEvent;
+import org.onosproject.net.DeviceId;
+import org.onosproject.p4runtime.api.P4RuntimeEvent;
+import org.onosproject.p4runtime.api.P4RuntimeEventListener;
+import org.onosproject.p4runtime.api.P4RuntimeEventSubject;
+import org.onosproject.p4runtime.api.P4RuntimePacketIn;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Default implementation of a packet-in event.
+ */
+final class DefaultPacketInEvent
+        extends AbstractEvent<P4RuntimeEventListener.Type, P4RuntimeEventSubject>
+        implements P4RuntimeEvent {
+
+    DefaultPacketInEvent(DeviceId deviceId, ImmutableByteSequence data,
+                                   List<ImmutableByteSequence> metadata) {
+        super(P4RuntimeEventListener.Type.PACKET_IN, new DefaultPacketIn(deviceId, data, metadata));
+    }
+
+    /**
+     * Default implementation of a packet-in in P4Runtime.
+     */
+    private static final class DefaultPacketIn implements P4RuntimePacketIn {
+
+        private final DeviceId deviceId;
+        private final ImmutableByteSequence data;
+        private final List<ImmutableByteSequence> metadata;
+
+        private DefaultPacketIn(DeviceId deviceId, ImmutableByteSequence data, List<ImmutableByteSequence> metadata) {
+            this.deviceId = checkNotNull(deviceId);
+            this.data = checkNotNull(data);
+            this.metadata = checkNotNull(metadata);
+        }
+
+        @Override
+        public DeviceId deviceId() {
+            return deviceId;
+        }
+
+        @Override
+        public ImmutableByteSequence data() {
+            return data;
+        }
+
+        @Override
+        public List<ImmutableByteSequence> metadata() {
+            return metadata;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+            DefaultPacketIn that = (DefaultPacketIn) o;
+            return Objects.equal(deviceId, that.deviceId) &&
+                    Objects.equal(data, that.data) &&
+                    Objects.equal(metadata, that.metadata);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hashCode(deviceId, data, metadata);
+        }
+    }
+}
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
new file mode 100644
index 0000000..e6603ea
--- /dev/null
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
@@ -0,0 +1,228 @@
+/*
+ * 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.p4runtime.ctl;
+
+import com.google.common.collect.ImmutableList;
+import com.google.protobuf.ByteString;
+import com.google.protobuf.ExtensionRegistry;
+import com.google.protobuf.TextFormat;
+import io.grpc.ManagedChannel;
+import io.grpc.Status;
+import io.grpc.StatusRuntimeException;
+import io.grpc.stub.StreamObserver;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.pi.runtime.PiTableEntry;
+import org.onosproject.net.pi.runtime.PiTableId;
+import org.onosproject.p4runtime.api.P4RuntimeClient;
+import org.onosproject.p4runtime.api.P4RuntimeEvent;
+import org.slf4j.Logger;
+import p4.P4RuntimeGrpc;
+import p4.config.P4InfoOuterClass;
+import p4.tmp.P4Config;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static org.onlab.util.ImmutableByteSequence.copyFrom;
+import static org.slf4j.LoggerFactory.getLogger;
+import static p4.P4RuntimeOuterClass.*;
+import static p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest.Action.VERIFY_AND_COMMIT;
+
+/**
+ * Implementation of a P4Runtime client.
+ */
+public class P4RuntimeClientImpl implements P4RuntimeClient {
+
+    private static final int DEADLINE_SECONDS = 15;
+
+    private final Logger log = getLogger(getClass());
+
+    private final DeviceId deviceId;
+    private final int p4DeviceId;
+    private final P4RuntimeControllerImpl controller;
+    private final P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub;
+    private final P4RuntimeGrpc.P4RuntimeStub asyncStub;
+    private ExecutorService executorService;
+    private StreamObserver<StreamMessageRequest> streamRequestObserver;
+
+
+    P4RuntimeClientImpl(DeviceId deviceId, int p4DeviceId, ManagedChannel channel, P4RuntimeControllerImpl controller,
+                        ExecutorService executorService) {
+        this.deviceId = deviceId;
+        this.p4DeviceId = p4DeviceId;
+        this.controller = controller;
+        this.executorService = executorService;
+        this.blockingStub = P4RuntimeGrpc.newBlockingStub(channel)
+                .withDeadlineAfter(DEADLINE_SECONDS, TimeUnit.SECONDS);
+        this.asyncStub = P4RuntimeGrpc.newStub(channel)
+                .withDeadlineAfter(DEADLINE_SECONDS, TimeUnit.SECONDS);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> initStreamChannel() {
+        return CompletableFuture.supplyAsync(this::doInitStreamChannel, executorService);
+    }
+
+    private boolean doInitStreamChannel() {
+        if (this.streamRequestObserver == null) {
+            this.streamRequestObserver = this.asyncStub.streamChannel(new StreamChannelResponseObserver());
+            // To listen for packets and other events, we need to start the RPC.
+            // Here we do it by sending an empty packet out.
+            try {
+                this.streamRequestObserver.onNext(StreamMessageRequest.newBuilder()
+                                                          .setPacket(PacketOut.getDefaultInstance())
+                                                          .build());
+            } catch (StatusRuntimeException e) {
+                log.warn("Unable to initialize stream channel for {}: {}", deviceId, e);
+                return false;
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> setPipelineConfig(InputStream p4info, InputStream targetConfig) {
+        return CompletableFuture.supplyAsync(() -> doSetPipelineConfig(p4info, targetConfig), executorService);
+    }
+
+    private boolean doSetPipelineConfig(InputStream p4info, InputStream targetConfig) {
+
+        log.debug("Setting pipeline config for {}", deviceId);
+
+        P4InfoOuterClass.P4Info.Builder p4iInfoBuilder = P4InfoOuterClass.P4Info.newBuilder();
+
+        try {
+            TextFormat.getParser().merge(new InputStreamReader(p4info),
+                                         ExtensionRegistry.getEmptyRegistry(),
+                                         p4iInfoBuilder);
+        } catch (IOException ex) {
+            log.warn("Unable to load p4info for {}: {}", deviceId, ex.getMessage());
+            return false;
+        }
+
+        P4Config.P4DeviceConfig deviceIdConfig;
+        try {
+            deviceIdConfig = P4Config.P4DeviceConfig
+                    .newBuilder()
+                    .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance())
+                    .setReassign(true)
+                    .setDeviceData(ByteString.readFrom(targetConfig))
+                    .build();
+        } catch (IOException ex) {
+            log.warn("Unable to load target-specific config for {}: {}", deviceId, ex.getMessage());
+            return false;
+        }
+
+        SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest
+                .newBuilder()
+                .setAction(VERIFY_AND_COMMIT)
+                .addConfigs(ForwardingPipelineConfig
+                                    .newBuilder()
+                                    .setDeviceId(p4DeviceId)
+                                    .setP4Info(p4iInfoBuilder.build())
+                                    .setP4DeviceConfig(deviceIdConfig.toByteString())
+                                    .build())
+                .build();
+        try {
+            this.blockingStub.setForwardingPipelineConfig(request);
+        } catch (StatusRuntimeException ex) {
+            log.warn("Unable to set pipeline config for {}: {}", deviceId, ex.getMessage());
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public boolean writeTableEntries(Collection<PiTableEntry> entries, WriteOperationType opType) {
+
+        throw new UnsupportedOperationException("writeTableEntries not implemented.");
+    }
+
+    @Override
+    public CompletableFuture<Collection<PiTableEntry>> dumpTable(PiTableId tableId) {
+
+        throw new UnsupportedOperationException("dumpTable not implemented.");
+    }
+
+    @Override
+    public void shutdown() {
+
+        if (this.streamRequestObserver != null) {
+            this.streamRequestObserver.onError(new StatusRuntimeException(Status.CANCELLED));
+            this.streamRequestObserver.onCompleted();
+        }
+
+        this.executorService.shutdownNow();
+        try {
+            executorService.awaitTermination(5, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            log.warn("Executor service didn't shutdown in time.");
+        }
+
+        // Prevent the execution of other tasks.
+        executorService = null;
+    }
+
+    private class StreamChannelResponseObserver implements StreamObserver<StreamMessageResponse> {
+
+        @Override
+        public void onNext(StreamMessageResponse message) {
+
+            P4RuntimeEvent event;
+
+            if (message.getPacket().isInitialized()) {
+                // Packet-in
+                PacketIn packetIn = message.getPacket();
+                ImmutableByteSequence data = copyFrom(packetIn.getPayload().asReadOnlyByteBuffer());
+                ImmutableList.Builder<ImmutableByteSequence> metadataBuilder = ImmutableList.builder();
+                packetIn.getMetadataList().stream()
+                        .map(m -> m.getValue().asReadOnlyByteBuffer())
+                        .map(ImmutableByteSequence::copyFrom)
+                        .forEach(metadataBuilder::add);
+                event = new DefaultPacketInEvent(deviceId, data, metadataBuilder.build());
+
+            } else if (message.getArbitration().isInitialized()) {
+                // Arbitration.
+                throw new UnsupportedOperationException("Arbitration not implemented.");
+
+            } else {
+                log.warn("Unrecognized stream message from {}: {}", deviceId, message);
+                return;
+            }
+
+            controller.postEvent(event);
+        }
+
+        @Override
+        public void onError(Throwable throwable) {
+            log.warn("Error on stream channel for {}: {}", deviceId, throwable);
+        }
+
+        @Override
+        public void onCompleted() {
+            // TODO: declare the device as disconnected?
+        }
+    }
+
+}
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeControllerImpl.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeControllerImpl.java
new file mode 100644
index 0000000..cd4151c
--- /dev/null
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeControllerImpl.java
@@ -0,0 +1,173 @@
+/*
+ * 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.p4runtime.ctl;
+
+import com.google.common.collect.Maps;
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+import io.grpc.NameResolverProvider;
+import io.grpc.internal.DnsNameResolverProvider;
+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.apache.felix.scr.annotations.Service;
+import org.onosproject.event.AbstractListenerManager;
+import org.onosproject.grpc.api.GrpcChannelId;
+import org.onosproject.grpc.api.GrpcController;
+import org.onosproject.net.DeviceId;
+import org.onosproject.p4runtime.api.P4RuntimeClient;
+import org.onosproject.p4runtime.api.P4RuntimeController;
+import org.onosproject.p4runtime.api.P4RuntimeEvent;
+import org.onosproject.p4runtime.api.P4RuntimeEventListener;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.lang.String.format;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * P4Runtime controller implementation.
+ */
+@Component(immediate = true)
+@Service
+public class P4RuntimeControllerImpl
+        extends AbstractListenerManager<P4RuntimeEvent, P4RuntimeEventListener>
+        implements P4RuntimeController {
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected GrpcController grpcController;
+
+    private final Logger log = getLogger(getClass());
+
+    private final NameResolverProvider nameResolverProvider = new DnsNameResolverProvider();
+    private final Map<DeviceId, P4RuntimeClient> clients = Maps.newConcurrentMap();
+    private final Map<DeviceId, GrpcChannelId> channelIds = Maps.newConcurrentMap();
+    // TODO: should use a cache to delete unused locks.
+    private final Map<DeviceId, Lock> deviceLocks = Maps.newConcurrentMap();
+
+    @Activate
+    public void activate() {
+        log.info("Started");
+    }
+
+
+    @Deactivate
+    public void deactivate() {
+        grpcController = null;
+        log.info("Stopped");
+    }
+
+
+    @Override
+    public boolean createClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) {
+        checkNotNull(deviceId);
+        checkNotNull(channelBuilder);
+
+        deviceLocks.putIfAbsent(deviceId, new ReentrantLock());
+        deviceLocks.get(deviceId).lock();
+
+        log.info("Creating client for {} (with internal device id {})...", deviceId, p4DeviceId);
+
+        try {
+            if (clients.containsKey(deviceId)) {
+                throw new IllegalStateException(format("A client already exists for %s", deviceId));
+            } else {
+                return doCreateClient(deviceId, p4DeviceId, channelBuilder);
+            }
+        } finally {
+            deviceLocks.get(deviceId).unlock();
+        }
+    }
+
+    private boolean doCreateClient(DeviceId deviceId, int p4DeviceId, ManagedChannelBuilder channelBuilder) {
+        GrpcChannelId channelId = GrpcChannelId.of(deviceId, "p4runtime");
+
+        // Channel defaults.
+        channelBuilder.nameResolverFactory(nameResolverProvider);
+
+        ManagedChannel channel;
+        try {
+            channel = grpcController.connectChannel(channelId, channelBuilder);
+        } catch (IOException e) {
+            log.warn("Unable to connect to gRPC server of {}: {}", deviceId, e.getMessage());
+            return false;
+        }
+
+        P4RuntimeClient client = new P4RuntimeClientImpl(deviceId, p4DeviceId, channel, this,
+                                                         newSingleThreadExecutor());
+
+        channelIds.put(deviceId, channelId);
+        clients.put(deviceId, client);
+
+        return true;
+    }
+
+    @Override
+    public P4RuntimeClient getClient(DeviceId deviceId) {
+
+        deviceLocks.putIfAbsent(deviceId, new ReentrantLock());
+        deviceLocks.get(deviceId).lock();
+
+        try {
+            return clients.get(deviceId);
+        } finally {
+            deviceLocks.get(deviceId).unlock();
+        }
+    }
+
+    @Override
+    public void removeClient(DeviceId deviceId) {
+
+        deviceLocks.putIfAbsent(deviceId, new ReentrantLock());
+        deviceLocks.get(deviceId).lock();
+
+        try {
+            if (clients.containsKey(deviceId)) {
+                clients.get(deviceId).shutdown();
+                grpcController.disconnectChannel(channelIds.get(deviceId));
+                clients.remove(deviceId);
+            }
+        } finally {
+            deviceLocks.get(deviceId).unlock();
+        }
+    }
+
+    @Override
+    public boolean hasClient(DeviceId deviceId) {
+
+        deviceLocks.putIfAbsent(deviceId, new ReentrantLock());
+        deviceLocks.get(deviceId).lock();
+
+        try {
+            return clients.containsKey(deviceId);
+        } finally {
+            deviceLocks.get(deviceId).unlock();
+        }
+    }
+
+    void postEvent(P4RuntimeEvent event) {
+        post(event);
+    }
+}
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/package-info.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/package-info.java
new file mode 100644
index 0000000..f7503a5
--- /dev/null
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Implementation classes of the P4Runtime protocol subsystem.
+ */
+package org.onosproject.p4runtime.ctl;
\ No newline at end of file
diff --git a/drivers/p4runtime/proto/BUCK b/protocols/p4runtime/proto/BUCK
similarity index 96%
rename from drivers/p4runtime/proto/BUCK
rename to protocols/p4runtime/proto/BUCK
index 5ef002b..f7ef33e 100644
--- a/drivers/p4runtime/proto/BUCK
+++ b/protocols/p4runtime/proto/BUCK
@@ -51,5 +51,5 @@
 )
 
 project_config(
-    src_target = ':onos-drivers-p4runtime-proto'
+    src_target = ':onos-protocols-p4runtime-proto'
 )
\ No newline at end of file