AETHER-452 Read P4RT counters only if table supports them

Before, we were reading counters from all tables, even from those that
didn't have any. That was causing certain P4RT implementations to
return error.

Change-Id: I028ecaca46ddc0c9afc16b78ba7754f943a2717b
(cherry picked from commit d172435433e19b10eb6b320823428e67f6205e8a)
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/client/ReadRequestImpl.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/client/ReadRequestImpl.java
index 29a7169..6858800 100644
--- a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/client/ReadRequestImpl.java
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/client/ReadRequestImpl.java
@@ -34,6 +34,7 @@
 import java.util.concurrent.CompletableFuture;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static java.lang.String.format;
 import static java.util.concurrent.CompletableFuture.completedFuture;
 import static org.onosproject.p4runtime.ctl.client.P4RuntimeClientImpl.SHORT_TIMEOUT_SECONDS;
 import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS;
@@ -270,16 +271,15 @@
     private void doTableEntry(PiTableId piTableId, boolean defaultEntries)
             throws InternalRequestException {
         checkNotNull(piTableId);
-        final P4RuntimeOuterClass.Entity entityMsg = P4RuntimeOuterClass.Entity
-                .newBuilder()
-                .setTableEntry(
-                        P4RuntimeOuterClass.TableEntry.newBuilder()
-                                .setTableId(p4TableId(piTableId))
-                                .setIsDefaultAction(defaultEntries)
-                                .setCounterData(P4RuntimeOuterClass.CounterData
-                                                        .getDefaultInstance())
-                                .build())
-                .build();
+        final var builder = P4RuntimeOuterClass.TableEntry.newBuilder()
+                .setTableId(p4TableId(piTableId))
+                .setIsDefaultAction(defaultEntries);
+        if (tableHasCounters(piTableId)) {
+            builder.setCounterData(P4RuntimeOuterClass.CounterData
+                                           .getDefaultInstance());
+        }
+        final var entityMsg = P4RuntimeOuterClass.Entity
+                .newBuilder().setTableEntry(builder.build()).build();
         requestMsg.addEntities(entityMsg);
     }
 
@@ -334,6 +334,13 @@
         }
     }
 
+    private boolean tableHasCounters(PiTableId piTableId) throws InternalRequestException {
+        return pipeconf.pipelineModel().table(piTableId).orElseThrow(
+                () -> new InternalRequestException(format(
+                        "Not such a table in pipeline model: %s", piTableId)))
+                .counters().size() > 0;
+    }
+
     private int p4ActionProfileId(PiActionProfileId piActionProfileId)
             throws InternalRequestException {
         try {