[ONOS-7829] Implement AbstractGrpcClient and AbstractGrpcClientControl

Change-Id: I39cba6834e7fe8d1b60b576b9934c0b3cfa7104b
diff --git a/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcController.java b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcChannelController.java
similarity index 98%
rename from protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcController.java
rename to protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcChannelController.java
index 33df22b..5b6771c 100644
--- a/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcController.java
+++ b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcChannelController.java
@@ -30,7 +30,7 @@
  * Abstraction of a gRPC controller that stores and manages gRPC channels.
  */
 @Beta
-public interface GrpcController {
+public interface GrpcChannelController {
 
     int CONNECTION_TIMEOUT_SECONDS = 20;
 
diff --git a/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClient.java b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClient.java
new file mode 100644
index 0000000..d040a23
--- /dev/null
+++ b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClient.java
@@ -0,0 +1,36 @@
+/*
+ * 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.grpc.api;
+
+import com.google.common.annotations.Beta;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Abstraction of a gRPC client.
+ *
+ */
+@Beta
+public interface GrpcClient {
+
+    /**
+     * Shutdowns the client by terminating any active RPC.
+     *
+     * @return a completable future to signal the completion of the shutdown
+     * procedure
+     */
+    CompletableFuture<Void> shutdown();
+}
diff --git a/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientController.java b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientController.java
new file mode 100644
index 0000000..087898b
--- /dev/null
+++ b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientController.java
@@ -0,0 +1,77 @@
+/*
+ * 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.grpc.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Abstraction of a gRPC controller which controls specific gRPC
+ * client {@link C} with specific client key {@link K}.
+ *
+ * @param <K> the gRPC client key
+ * @param <C> the gRPC client type
+ */
+@Beta
+public interface GrpcClientController<K extends GrpcClientKey, C extends GrpcClient> {
+
+    /**
+     * Instantiates a new client to operate on a gRPC server identified by
+     * the given information. As a result of this method, a client can be later
+     * obtained by invoking {@link #getClient(DeviceId)}.
+     *
+     * Only one client can exist for the same device ID. Calls to this method are
+     * idempotent fot the same client key, i.e. returns true
+     * if such client already exists but a new one is not created.
+     * If there exists a client with same device ID but different address and port,
+     * removes old one and recreate new one.
+     *
+     * @param clientKey the client key
+     * @return true if the client was created and the channel to the server is open;
+     *         false otherwise
+     */
+    boolean createClient(K clientKey);
+
+    /**
+     * Retrieves the gRPC client to operate on the given device.
+     *
+     * @param deviceId the device identifier
+     * @return the gRPC client of the device if exists; null otherwise
+     */
+    C getClient(DeviceId deviceId);
+
+    /**
+     * Removes the gRPC client for the given device. If no client
+     * exists for the given device, the result is a no-op.
+     *
+     * @param deviceId the device identifier
+     */
+    void removeClient(DeviceId deviceId);
+
+    /**
+     * Check reachability of the gRPC server running on the given device.
+     * Reachability can be tested only if a client is previously created
+     * using {@link #createClient(GrpcClientKey)}.
+     * Different gRPC service may have different ways to test if it is
+     * reachable or not.
+     *
+     * @param deviceId the device identifier
+     * @return true of client was created and is able to contact the gNMI server;
+     *         false otherwise
+     */
+    boolean isReachable(DeviceId deviceId);
+}
diff --git a/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientKey.java b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientKey.java
new file mode 100644
index 0000000..ad8a7da
--- /dev/null
+++ b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientKey.java
@@ -0,0 +1,128 @@
+/*
+ * 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.grpc.api;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onosproject.net.DeviceId;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Key that uniquely identifies a gRPC client.
+ */
+@Beta
+public class GrpcClientKey {
+    private final String serviceName;
+    private final DeviceId deviceId;
+    private final String serverAddr;
+    private final int serverPort;
+
+    /**
+     * Creates a new client key.
+     *
+     * @param serviceName gRPC service name of the client
+     * @param deviceId ONOS device ID
+     * @param serverAddr gRPC server address
+     * @param serverPort gRPC server port
+     */
+    public GrpcClientKey(String serviceName, DeviceId deviceId, String serverAddr, int serverPort) {
+        checkNotNull(serviceName);
+        checkNotNull(deviceId);
+        checkNotNull(serverAddr);
+        checkArgument(!serviceName.isEmpty(),
+                "Service name can not be null");
+        checkArgument(!serverAddr.isEmpty(),
+                "Server address should not be empty");
+        checkArgument(serverPort > 0 && serverPort <= 65535, "Invalid server port");
+        this.serviceName = serviceName;
+        this.deviceId = deviceId;
+        this.serverAddr = serverAddr;
+        this.serverPort = serverPort;
+    }
+
+    /**
+     * Gets the gRPC service name of the client.
+     *
+     * @return the service name
+     */
+    public String serviceName() {
+        return serviceName;
+    }
+
+    /**
+     * Gets the device ID.
+     *
+     * @return the device ID
+     */
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+
+    /**
+     * Gets the gRPC server address.
+     *
+     * @return the gRPC server address.
+     */
+    public String serverAddr() {
+        return serverAddr;
+    }
+
+    /**
+     * Gets the gRPC server port.
+     *
+     * @return the gRPC server port.
+     */
+    public int serverPort() {
+        return serverPort;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        GrpcClientKey that = (GrpcClientKey) o;
+        return serverPort == that.serverPort &&
+                Objects.equal(serviceName, that.serviceName) &&
+                Objects.equal(deviceId, that.deviceId) &&
+                Objects.equal(serverAddr, that.serverAddr);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(serviceName, deviceId, serverAddr, serverPort);
+    }
+
+    protected MoreObjects.ToStringHelper toStringHelper() {
+        return MoreObjects.toStringHelper(this)
+                .add("serviceName", serviceName)
+                .add("deviceId", deviceId)
+                .add("serverAddr", serverAddr)
+                .add("serverPort", serverPort);
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper().toString();
+    }
+}
\ No newline at end of file