New P4RuntimeClient implementation that supports batching and error reporting

The new client API supports batching and provides detailed response for
write requests (e.g. if entity already exists when inserting), which was
not possible with the old one.

This patch includes:
- New more efficient implementation of P4RuntimeClient (no more locking,
use native gRPC executor, use stub deadlines)
- Ported all codecs to new AbstractCodec-based implementation (needed to
implement codec cache in the future)
- Uses batching in P4RuntimeFlowRuleProgrammable and
P4RuntimeGroupActionProgrammable
- Minor changes to PI framework runtime classes

Change-Id: I3fac42057bb4e1389d761006a32600c786598683
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/utils/PipeconfHelper.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/utils/PipeconfHelper.java
new file mode 100644
index 0000000..dec7438
--- /dev/null
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/utils/PipeconfHelper.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2019-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.p4runtime.ctl.utils;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.collect.Maps;
+import com.google.protobuf.ExtensionRegistry;
+import com.google.protobuf.TextFormat;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipeconfId;
+import org.slf4j.Logger;
+import p4.config.v1.P4InfoOuterClass.P4Info;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Utility class to deal with pipeconfs in the context of P4Runtime.
+ */
+public final class PipeconfHelper {
+
+    private static final int P4INFO_BROWSER_EXPIRE_TIME_IN_MIN = 10;
+    private static final Logger log = getLogger(PipeconfHelper.class);
+
+    private static final Cache<PiPipeconfId, P4InfoBrowser> BROWSERS = CacheBuilder.newBuilder()
+            .expireAfterAccess(P4INFO_BROWSER_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES)
+            .build();
+    private static final Map<PiPipeconfId, P4Info> P4INFOS = Maps.newConcurrentMap();
+
+    private PipeconfHelper() {
+        // hide.
+    }
+
+    /**
+     * Extracts and returns a P4Info protobuf message from the given pipeconf. If the pipeconf does not define any
+     * extension of type {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
+     *
+     * @param pipeconf pipeconf
+     * @return P4Info or null
+     */
+    public static P4Info getP4Info(PiPipeconf pipeconf) {
+        return P4INFOS.computeIfAbsent(pipeconf.id(), piPipeconfId -> {
+            if (!pipeconf.extension(P4_INFO_TEXT).isPresent()) {
+                log.warn("Missing P4Info extension in pipeconf {}", pipeconf.id());
+                return null;
+            }
+
+            InputStream p4InfoStream = pipeconf.extension(P4_INFO_TEXT).get();
+            P4Info.Builder p4iInfoBuilder = P4Info.newBuilder();
+            try {
+                TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), ExtensionRegistry.getEmptyRegistry(),
+                                             p4iInfoBuilder);
+            } catch (IOException ex) {
+                log.warn("Unable to parse P4Info of pipeconf {}: {}", pipeconf.id(), ex.getMessage());
+                return null;
+            }
+
+            return p4iInfoBuilder.build();
+        });
+    }
+
+    /**
+     * Returns a P4Info browser for the given pipeconf. If the pipeconf does not define any extension of type
+     * {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
+     *
+     * @param pipeconf pipeconf
+     * @return P4Info browser or null
+     */
+    public static P4InfoBrowser getP4InfoBrowser(PiPipeconf pipeconf) {
+        try {
+            return BROWSERS.get(pipeconf.id(), () -> {
+                P4Info p4info = PipeconfHelper.getP4Info(pipeconf);
+                if (p4info == null) {
+                    return null;
+                } else {
+                    return new P4InfoBrowser(p4info);
+                }
+            });
+        } catch (ExecutionException e) {
+            log.error("Exception while accessing the P4InfoBrowser cache", e);
+            return null;
+        }
+    }
+}