[ONOS-7831] Implement GnmiHandshaker

Change-Id: I2232a724a86955483321f9fda571907aa2cb615a
diff --git a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
index e363f4a..fda0bb5 100644
--- a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
+++ b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
@@ -16,6 +16,7 @@
 
 package org.onosproject.drivers.gnmi;
 
+import io.grpc.StatusRuntimeException;
 import org.onosproject.gnmi.api.GnmiClient;
 import org.onosproject.gnmi.api.GnmiClientKey;
 import org.onosproject.gnmi.api.GnmiController;
@@ -26,11 +27,20 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
 /**
  * Abstract implementation of a behaviour handler for a gNMI device.
  */
 public class AbstractGnmiHandlerBehaviour extends AbstractHandlerBehaviour {
 
+    // Default timeout in seconds for device operations.
+    private static final String DEVICE_REQ_TIMEOUT = "deviceRequestTimeout";
+    private static final int DEFAULT_DEVICE_REQ_TIMEOUT = 60;
+
     public static final String GNMI_SERVER_ADDR_KEY = "gnmi_ip";
     public static final String GNMI_SERVER_PORT_KEY = "gnmi_port";
     private static final String GNMI_SERVICE_NAME = "gnmi";
@@ -43,19 +53,13 @@
     protected GnmiClient client;
 
     protected boolean setupBehaviour() {
-        // FIXME: Should create GnmiHandshaker which initialize the client
-        // instead of create client here.
         deviceId = handler().data().deviceId();
 
         controller = handler().get(GnmiController.class);
         client = controller.getClient(deviceId);
 
         if (client == null) {
-            client = createClient();
-        }
-
-        if (client == null) {
-            log.warn("Can not create client for {} (see log above)", deviceId);
+            log.warn("Unable to find client for {}, aborting operation", deviceId);
             return false;
         }
 
@@ -90,4 +94,58 @@
         }
         return controller.getClient(deviceId);
     }
+
+    /**
+     * Returns the device request timeout driver property, or a default value
+     * if the property is not present or cannot be parsed.
+     *
+     * @return timeout value
+     */
+    private int getDeviceRequestTimeout() {
+        final String timeout = handler().driver()
+                .getProperty(DEVICE_REQ_TIMEOUT);
+        if (timeout == null) {
+            return DEFAULT_DEVICE_REQ_TIMEOUT;
+        } else {
+            try {
+                return Integer.parseInt(timeout);
+            } catch (NumberFormatException e) {
+                log.error("{} driver property '{}' is not a number, using default value {}",
+                        DEVICE_REQ_TIMEOUT, timeout, DEFAULT_DEVICE_REQ_TIMEOUT);
+                return DEFAULT_DEVICE_REQ_TIMEOUT;
+            }
+        }
+    }
+
+    /**
+     * Convenience method to get the result of a completable future while
+     * setting a timeout and checking for exceptions.
+     *
+     * @param future        completable future
+     * @param opDescription operation description to use in log messages. Should
+     *                      be a sentence starting with a verb ending in -ing,
+     *                      e.g. "reading...", "writing...", etc.
+     * @param defaultValue  value to return if operation fails
+     * @param <U>           type of returned value
+     * @return future result or default value
+     */
+    <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription,
+                                U defaultValue) {
+        try {
+            return future.get(getDeviceRequestTimeout(), TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            log.error("Exception while {} on {}", opDescription, deviceId);
+        } catch (ExecutionException e) {
+            final Throwable cause = e.getCause();
+            if (cause instanceof StatusRuntimeException) {
+                final StatusRuntimeException grpcError = (StatusRuntimeException) cause;
+                log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage());
+            } else {
+                log.error("Exception while {} on {}", opDescription, deviceId, e.getCause());
+            }
+        } catch (TimeoutException e) {
+            log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId);
+        }
+        return defaultValue;
+    }
 }
diff --git a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiHandshaker.java b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiHandshaker.java
new file mode 100644
index 0000000..d1ee1b8
--- /dev/null
+++ b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiHandshaker.java
@@ -0,0 +1,97 @@
+/*
+ * 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.drivers.gnmi;
+
+import org.onosproject.gnmi.api.GnmiClient;
+import org.onosproject.gnmi.api.GnmiController;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.device.DeviceHandshaker;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Implementation of DeviceHandshaker for gNMI.
+ */
+public class GnmiHandshaker extends AbstractGnmiHandlerBehaviour implements DeviceHandshaker {
+
+    @Override
+    public CompletableFuture<Boolean> isReachable() {
+        return CompletableFuture
+                // gNMI requires a client to be created to
+                // check for reachability.
+                .supplyAsync(super::createClient)
+                .thenApplyAsync(client -> {
+                    if (client == null) {
+                        return false;
+                    }
+                    return handler()
+                            .get(GnmiController.class)
+                            .isReachable(handler().data().deviceId());
+                });
+    }
+
+    @Override
+    public void roleChanged(MastershipRole newRole) {
+        throw new UnsupportedOperationException("Mastership operation not supported");
+    }
+
+    @Override
+    public MastershipRole getRole() {
+        throw new UnsupportedOperationException("Mastership operation not supported");
+    }
+
+    @Override
+    public CompletableFuture<Boolean> connect() {
+        return CompletableFuture
+                .supplyAsync(super::createClient)
+                .thenComposeAsync(client -> {
+                    if (client == null) {
+                        return CompletableFuture.completedFuture(false);
+                    }
+                    return CompletableFuture.completedFuture(true);
+                });
+    }
+
+    @Override
+    public boolean isConnected() {
+        final GnmiController controller = handler().get(GnmiController.class);
+        final DeviceId deviceId = handler().data().deviceId();
+        final GnmiClient client = controller.getClient(deviceId);
+
+        if (client == null) {
+            return false;
+        }
+
+        return getFutureWithDeadline(client.isServiceAvailable(), "getting availability", false);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> disconnect() {
+        final GnmiController controller = handler().get(GnmiController.class);
+        final DeviceId deviceId = handler().data().deviceId();
+        final GnmiClient client = controller.getClient(deviceId);
+        if (client == null) {
+            return CompletableFuture.completedFuture(true);
+        }
+        return client.shutdown()
+                .thenApplyAsync(v -> {
+                    controller.removeClient(deviceId);
+                    return true;
+                });
+    }
+}
diff --git a/drivers/gnmi/src/main/resources/gnmi-drivers.xml b/drivers/gnmi/src/main/resources/gnmi-drivers.xml
index 4ca539e..70c0b20 100644
--- a/drivers/gnmi/src/main/resources/gnmi-drivers.xml
+++ b/drivers/gnmi/src/main/resources/gnmi-drivers.xml
@@ -18,6 +18,8 @@
     <driver name="gnmi" manufacturer="gnmi" hwVersion="master" swVersion="master">
         <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
                    impl="org.onosproject.drivers.gnmi.OpenConfigGnmiDeviceDescriptionDiscovery"/>
+        <behaviour api="org.onosproject.net.device.DeviceHandshaker"
+                   impl="org.onosproject.drivers.gnmi.GnmiHandshaker"/>
     </driver>
 </drivers>
 
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
index 7f43d1f..242bc94 100644
--- a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
@@ -57,5 +57,13 @@
      */
     CompletableFuture<SetResponse> set(SetRequest request);
 
+    /**
+     * Check weather the gNMI service is available or not by sending a
+     * dummy get request message.
+     *
+     * @return true if gNMI service available; false otherwise
+     */
+    CompletableFuture<Boolean> isServiceAvailable();
+
     // TODO: Support gNMI subscription
 }
diff --git a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
index 74a9ec0..b8179a9 100644
--- a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
+++ b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
@@ -19,10 +19,13 @@
 import gnmi.Gnmi.CapabilityResponse;
 import gnmi.Gnmi.GetRequest;
 import gnmi.Gnmi.GetResponse;
+import gnmi.Gnmi.Path;
+import gnmi.Gnmi.PathElem;
 import gnmi.Gnmi.SetRequest;
 import gnmi.Gnmi.SetResponse;
 import gnmi.gNMIGrpc;
 import io.grpc.ManagedChannel;
+import io.grpc.Status;
 import io.grpc.StatusRuntimeException;
 import org.onosproject.gnmi.api.GnmiClientKey;
 import org.onosproject.grpc.ctl.AbstractGrpcClient;
@@ -37,6 +40,9 @@
  * Implementation of gNMI client.
  */
 public class GnmiClientImpl extends AbstractGrpcClient implements GnmiClient {
+    private static final PathElem DUMMY_PATH_ELEM = PathElem.newBuilder().setName("onos-gnmi-test").build();
+    private static final Path DUMMY_PATH = Path.newBuilder().addElem(DUMMY_PATH_ELEM).build();
+    private static final GetRequest DUMMY_REQUEST = GetRequest.newBuilder().addPath(DUMMY_PATH).build();
     private final Logger log = getLogger(getClass());
     private final gNMIGrpc.gNMIBlockingStub blockingStub;
 
@@ -60,6 +66,11 @@
         return supplyInContext(() -> doSet(request), "set");
     }
 
+    @Override
+    public CompletableFuture<Boolean> isServiceAvailable() {
+        return supplyInContext(this::doServiceAvailable, "isServiceAvailable");
+    }
+
     private CapabilityResponse doCapability() {
         CapabilityRequest request = CapabilityRequest.newBuilder().build();
         try {
@@ -87,4 +98,17 @@
             return null;
         }
     }
+
+    private boolean doServiceAvailable() {
+        try {
+            blockingStub.get(DUMMY_REQUEST);
+            return true;
+        } catch (StatusRuntimeException e) {
+            // This gRPC call should throw INVALID_ARGUMENT status exception
+            // since "/onos-gnmi-test" path does not exists in any config model
+            // For other status code such as UNIMPLEMENT, means the gNMI
+            // service is not available on the device.
+            return e.getStatus().getCode().equals(Status.Code.INVALID_ARGUMENT);
+        }
+    }
 }
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
index 087898b..2e3ea5d 100644
--- 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
@@ -66,11 +66,12 @@
      * 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.
+     * Note that this only checks the reachability instead of checking service
+     * availability, different gRPC client checks service availability with
+     * different way.
      *
      * @param deviceId the device identifier
-     * @return true of client was created and is able to contact the gNMI server;
+     * @return true if client was created and is able to contact the gNMI server;
      *         false otherwise
      */
     boolean isReachable(DeviceId deviceId);