Add basic error handling to gRPC Device SB service

Change-Id: Idd19bd4e0d35d8eda06acb4cb631c5d4bcd21980
diff --git a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderServiceClientProxy.java b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderServiceClientProxy.java
index a6712b9..c93a42a 100644
--- a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderServiceClientProxy.java
+++ b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderServiceClientProxy.java
@@ -65,6 +65,8 @@
 
     private final Channel channel;
 
+    private Throwable error;
+
     DeviceProviderServiceClientProxy(DeviceProvider provider, Channel channel) {
         super(provider);
         this.channel = channel;
@@ -194,10 +196,28 @@
             log.error("Shutting down session over {}", channel.authority());
             // initiate abnormal termination from client
             devProvService.onError(t);
-            invalidate();
+            invalidate(t);
         }
     }
 
+    /**
+     * Invalidates the ProviderService indicating Failure.
+     * @param t {@link Throwable} describing last failure
+     */
+    private void invalidate(Throwable t) {
+        this.error = t;
+        invalidate();
+    }
+
+    @Override
+    public void checkValidity() {
+        if (error != null) {
+            throw new IllegalStateException("DeviceProviderService no longer valid",
+                                            error);
+        }
+        super.checkValidity();
+    }
+
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this)
@@ -272,17 +292,14 @@
         public void onCompleted() {
             log.info("DeviceProviderClientProxy completed");
             // session terminated from remote
-            // TODO unregister...? how?
-
-            //devProvService.onCompleted();
+            invalidate();
         }
 
         @Override
         public void onError(Throwable t) {
             log.error("DeviceProviderClientProxy#onError", t);
             // session terminated from remote
-            // TODO unregister...? how?
-            //devProvService.onError(t);
+            invalidate(t);
         }
 
         @Override
diff --git a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java
index a44d539..099ed20 100644
--- a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java
+++ b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java
@@ -397,6 +397,16 @@
 
         @Override
         public void triggerProbe(DeviceId deviceId) {
+            try {
+                onTriggerProbe(deviceId);
+            } catch (Exception e) {
+                log.error("Exception caught handling triggerProbe({})",
+                          deviceId, e);
+                toDeviceProvider.onError(e);
+            }
+        }
+
+        private void onTriggerProbe(DeviceId deviceId) {
             log.trace("triggerProbe({})", deviceId);
             DeviceProviderMsg.Builder msgBuilder = DeviceProviderMsg.newBuilder();
             msgBuilder.setTriggerProbe(msgBuilder.getTriggerProbeBuilder()
@@ -404,11 +414,20 @@
                                        .build());
             DeviceProviderMsg triggerProbeMsg = msgBuilder.build();
             toDeviceProvider.onNext(triggerProbeMsg);
-            // TODO Catch Exceptions and call onError()
         }
 
         @Override
         public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
+            try {
+                onRoleChanged(deviceId, newRole);
+            } catch (Exception e) {
+                log.error("Exception caught handling onRoleChanged({}, {})",
+                          deviceId, newRole, e);
+                toDeviceProvider.onError(e);
+            }
+        }
+
+        private void onRoleChanged(DeviceId deviceId, MastershipRole newRole) {
             log.trace("roleChanged({}, {})", deviceId, newRole);
             DeviceProviderMsg.Builder msgBuilder = DeviceProviderMsg.newBuilder();
             msgBuilder.setRoleChanged(msgBuilder.getRoleChangedBuilder()
@@ -416,11 +435,22 @@
                                           .setNewRole(translate(newRole))
                                           .build());
             toDeviceProvider.onNext(msgBuilder.build());
-            // TODO Catch Exceptions and call onError()
         }
 
         @Override
         public boolean isReachable(DeviceId deviceId) {
+            try {
+                return onIsReachable(deviceId);
+            } catch (Exception e) {
+                log.error("Exception caught handling onIsReachable({})",
+                          deviceId, e);
+                toDeviceProvider.onError(e);
+                return false;
+            }
+        }
+
+        private boolean onIsReachable(DeviceId deviceId) {
+
             log.trace("isReachable({})", deviceId);
             CompletableFuture<Boolean> result = new CompletableFuture<>();
             final int xid = xidPool.incrementAndGet();
@@ -450,10 +480,10 @@
                 log.warn("isReachable({}) Timed out", deviceId, e);
             } catch (ExecutionException e) {
                 log.error("isReachable({}) Execution failed", deviceId, e);
-                // close session?
+                // close session
+                toDeviceProvider.onError(e);
             }
             return false;
-            // TODO Catch Exceptions and call onError()
         }
 
         @Override
@@ -464,8 +494,9 @@
         @Override
         public void changePortState(DeviceId deviceId, PortNumber portNumber,
                                     boolean enable) {
-            // TODO if required
-
+            // TODO Implement if required
+            log.error("changePortState not supported yet");
+            toDeviceProvider.onError(new UnsupportedOperationException("not implemented yet"));
         }
 
     }