[ONOS-7437] Logs more detail from P4Runtime error message

Change-Id: Ia2cc2db332148313c9834cde8bb16484550e18f8
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
index bac0bb9..534ef06 100644
--- 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
@@ -23,6 +23,7 @@
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Sets;
 import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
 import io.grpc.Context;
 import io.grpc.ManagedChannel;
 import io.grpc.Status;
@@ -42,8 +43,8 @@
 import org.onosproject.net.pi.runtime.PiCounterCellData;
 import org.onosproject.net.pi.runtime.PiCounterCellId;
 import org.onosproject.net.pi.runtime.PiPacketOperation;
-import org.onosproject.net.pi.service.PiPipeconfService;
 import org.onosproject.net.pi.runtime.PiTableEntry;
+import org.onosproject.net.pi.service.PiPipeconfService;
 import org.onosproject.p4runtime.api.P4RuntimeClient;
 import org.onosproject.p4runtime.api.P4RuntimeEvent;
 import org.slf4j.Logger;
@@ -159,12 +160,11 @@
             writeLock.lock();
             try {
                 return supplier.get();
+            } catch (StatusRuntimeException ex) {
+                logP4RuntimeErrorStatus(ex, opDescription);
+                throw ex;
             } catch (Throwable ex) {
-                if (ex instanceof StatusRuntimeException) {
-                    log.warn("Unable to execute {} on {}: {}", opDescription, deviceId, ex.toString());
-                } else {
-                    log.error("Exception in client of {}, executing {}", deviceId, opDescription, ex);
-                }
+                log.error("Exception in client of {}, executing {}", deviceId, opDescription, ex);
                 throw ex;
             } finally {
                 writeLock.unlock();
@@ -172,6 +172,18 @@
         }, contextExecutor);
     }
 
+    private void logP4RuntimeErrorStatus(StatusRuntimeException ex, String description) {
+        String statusString = ex.getStatus().getDescription();
+        try {
+            com.google.rpc.Status status = com.google.rpc.Status.parseFrom(statusString.getBytes());
+            log.warn("{} failed on {} due to {}", description, deviceId, status.toString());
+        } catch (InvalidProtocolBufferException e) {
+            log.warn("{} failed on {} due to {}", description, deviceId, statusString);
+        } catch (NullPointerException e) {
+            log.warn("{} failed on {}", description, deviceId);
+        }
+    }
+
     @Override
     public CompletableFuture<Boolean> initStreamChannel() {
         return supplyInContext(this::doInitStreamChannel, "initStreamChannel");
@@ -295,7 +307,11 @@
         try {
             streamRequestObserver.onNext(requestMsg);
             return result.get();
-        } catch (InterruptedException | ExecutionException | StatusRuntimeException e) {
+        } catch (StatusRuntimeException e) {
+            logP4RuntimeErrorStatus(e, "Arbitration update");
+            arbitrationUpdateMap.remove(newElectionId);
+            return false;
+        } catch (InterruptedException | ExecutionException e) {
             log.warn("Arbitration update failed for {} due to {}", deviceId, e);
             arbitrationUpdateMap.remove(newElectionId);
             return false;
@@ -344,7 +360,7 @@
             this.blockingStub.setForwardingPipelineConfig(request);
             return true;
         } catch (StatusRuntimeException ex) {
-            log.warn("Unable to set pipeline config for {}: {}", deviceId, ex.getMessage());
+            logP4RuntimeErrorStatus(ex, "Set pipeline config");
             return false;
         }
     }
@@ -378,7 +394,7 @@
             blockingStub.write(writeRequestBuilder.build());
             return true;
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to write table entries ({}): {}", opType, e.getMessage());
+            logP4RuntimeErrorStatus(e, "Write table entries");
             return false;
         }
     }
@@ -409,7 +425,7 @@
         try {
             responses = blockingStub.read(requestMsg);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to dump table: {}", e.getMessage());
+            logP4RuntimeErrorStatus(e, "Dump table");
             return Collections.emptyList();
         }
 
@@ -518,7 +534,7 @@
         try {
             responses = () -> blockingStub.read(request);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to read counters: {}", e.getMessage());
+            logP4RuntimeErrorStatus(e, "Read counter");
             return Collections.emptyList();
         }
 
@@ -565,7 +581,7 @@
             blockingStub.write(writeRequestMsg);
             return true;
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to write ({}) group members: {}", opType, e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("%s group members", opType));
             return false;
         }
     }
@@ -608,7 +624,8 @@
         try {
             groupResponses = blockingStub.read(groupRequestMsg);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable dump groups from action profile '{}': {}", piActionProfileId.id(), e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("Dump group from action profile %s",
+                                                     piActionProfileId.id()));
             return Collections.emptySet();
         }
 
@@ -656,7 +673,8 @@
         try {
             memberResponses = blockingStub.read(memberRequestMsg);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to read members from action profile {}: {}", piActionProfileId, e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("Read members from action profile %s",
+                                                     piActionProfileId.id()));
             return Collections.emptyList();
         }
 
@@ -715,7 +733,7 @@
             blockingStub.write(writeRequestMsg);
             return true;
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to write groups ({}): {}", opType, e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("%s group", opType));
             return false;
         }
     }