[ONOS-7829] Implement AbstractGrpcClient and AbstractGrpcClientControl

Change-Id: I39cba6834e7fe8d1b60b576b9934c0b3cfa7104b
diff --git a/protocols/p4runtime/api/BUCK b/protocols/p4runtime/api/BUCK
index fd05763..c775aea 100644
--- a/protocols/p4runtime/api/BUCK
+++ b/protocols/p4runtime/api/BUCK
@@ -3,6 +3,7 @@
 COMPILE_DEPS = [
     '//lib:CORE_DEPS',
     '//incubator/grpc-dependencies:grpc-core-repkg-' + GRPC_VER,
+    '//protocols/grpc/api:onos-protocols-grpc-api',
 ]
 
 TEST_DEPS = [
diff --git a/protocols/p4runtime/api/BUILD b/protocols/p4runtime/api/BUILD
index 531d734..2fb15ba 100644
--- a/protocols/p4runtime/api/BUILD
+++ b/protocols/p4runtime/api/BUILD
@@ -1,4 +1,5 @@
 COMPILE_DEPS = CORE_DEPS + [
+    "//protocols/grpc/api:onos-protocols-grpc-api",
     "@io_grpc_grpc_java//core",
 ]
 
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
index f44c629..7a08668 100644
--- 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
@@ -17,6 +17,7 @@
 package org.onosproject.p4runtime.api;
 
 import com.google.common.annotations.Beta;
+import org.onosproject.grpc.api.GrpcClient;
 import org.onosproject.net.pi.model.PiActionProfileId;
 import org.onosproject.net.pi.model.PiCounterId;
 import org.onosproject.net.pi.model.PiMeterId;
@@ -42,7 +43,7 @@
  * Client to control a P4Runtime device.
  */
 @Beta
-public interface P4RuntimeClient {
+public interface P4RuntimeClient extends GrpcClient {
 
     /**
      * Type of write operation.
@@ -70,15 +71,6 @@
     boolean isStreamChannelOpen();
 
     /**
-     * Shutdowns the client by terminating any active RPC such as the Stream
-     * one.
-     *
-     * @return a completable future to signal the completion of the shutdown
-     * procedure
-     */
-    CompletableFuture<Void> shutdown();
-
-    /**
      * Sends a master arbitration update to the device with a new election ID
      * that is guaranteed to be the highest value between all clients.
      *
diff --git a/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeClientKey.java b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeClientKey.java
new file mode 100644
index 0000000..70bf33c
--- /dev/null
+++ b/protocols/p4runtime/api/src/main/java/org/onosproject/p4runtime/api/P4RuntimeClientKey.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.grpc.api.GrpcClientKey;
+import org.onosproject.net.DeviceId;
+
+import java.util.Objects;
+
+/**
+ * Key that uniquely identifies a P4Runtime client.
+ */
+@Beta
+public final class P4RuntimeClientKey extends GrpcClientKey {
+    private static final String P4R_SERVICE_NAME = "p4runtime";
+    private final long p4DeviceId;
+
+    /**
+     * Creates a new client key.
+     *
+     * @param deviceId   ONOS device ID
+     * @param serverAddr P4Runtime server address
+     * @param serverPort P4Runtime server port
+     * @param p4DeviceId P4Runtime server-internal device ID
+     */
+    public P4RuntimeClientKey(DeviceId deviceId, String serverAddr,
+                              int serverPort, long p4DeviceId) {
+        super(P4R_SERVICE_NAME, deviceId, serverAddr, serverPort);
+        this.p4DeviceId = p4DeviceId;
+    }
+
+    /**
+     * Returns the P4Runtime server-internal device ID.
+     *
+     * @return P4Runtime server-internal device ID
+     */
+    public long p4DeviceId() {
+        return p4DeviceId;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        if (!super.equals(o)) {
+            return false;
+        }
+        P4RuntimeClientKey that = (P4RuntimeClientKey) o;
+        return p4DeviceId == that.p4DeviceId;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(super.hashCode(), p4DeviceId);
+    }
+
+    @Override
+    public String toString() {
+        return super.toStringHelper()
+                .add("p4DeviceId", p4DeviceId)
+                .toString();
+    }
+}
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
index 159e313..0bc8ed6 100644
--- 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
@@ -18,6 +18,7 @@
 
 import com.google.common.annotations.Beta;
 import org.onosproject.event.ListenerService;
+import org.onosproject.grpc.api.GrpcClientController;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.device.DeviceAgentListener;
 import org.onosproject.net.provider.ProviderId;
@@ -27,73 +28,8 @@
  */
 @Beta
 public interface P4RuntimeController
-        extends ListenerService<P4RuntimeEvent, P4RuntimeEventListener> {
-
-    /**
-     * Instantiates a new client to operate on a P4Runtime device identified by
-     * the given information. As a result of this method, a {@link
-     * P4RuntimeClient} can be later obtained by invoking {@link
-     * #getClient(DeviceId)}. Returns true if the client was created and the
-     * channel to the device is open, false otherwise.
-     * <p>
-     * Only one client can exist for the same device ID. Calls to this method
-     * are idempotent for the same [device ID, address, port, p4DeviceId]
-     * triplet, i.e. returns true if such client already exists but a new one is
-     * not created. Throws an {@link IllegalStateException} if a client for
-     * device ID already exists but for different [address, port, p4DeviceId].
-     *
-     * @param deviceId   device identifier
-     * @param serverAddr address of the P4Runtime server
-     * @param serverPort port of the P4Runtime server
-     * @param p4DeviceId P4Runtime-specific device identifier
-     * @return true if the client was created and the channel to the device is
-     * open
-     * @throws IllegalStateException if a client already exists for this device
-     *                               ID but for different [address, port,
-     *                               p4DeviceId] triplet.
-     */
-    boolean createClient(DeviceId deviceId, String serverAddr, int serverPort,
-                         long p4DeviceId);
-
-    /**
-     * Returns a client to operate on the given device, or null if a client for
-     * such device does not exist in this controller.
-     *
-     * @param deviceId device identifier
-     * @return client instance or null
-     */
-    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);
-
-    /**
-     * Returns true if the P4Runtime server running on the given device is
-     * reachable, i.e. the channel is open and the server is able to respond to
-     * RPCs, false otherwise. Reachability can be tested only if a client was
-     * previously created using {@link #createClient(DeviceId, String, int,
-     * long)}, otherwise this method returns false.
-     *
-     * @param deviceId device identifier.
-     * @return true if a client was created and is able to contact the P4Runtime
-     * server, false otherwise.
-     */
-    boolean isReachable(DeviceId deviceId);
-
+        extends GrpcClientController<P4RuntimeClientKey, P4RuntimeClient>,
+                ListenerService<P4RuntimeEvent, P4RuntimeEventListener> {
     /**
      * Adds a listener for device agent events for the given provider.
      *