Various improvements to PI group handling

- Moved group translation logic to core service
- Removed dependency on KRYO
- Fixed bug where tratments with PI instructions where not supported if
	an interpreter was present
- Fixed bug where action profile name was not found during protobuf
	encoding (always perform P4Info lookup by name and alias)
- Improved reading of members by issuing one big request for all
	groups

Change-Id: Ifcf8380b09293e70be15cf4999bd2845caf5d01e
diff --git a/core/net/src/main/java/org/onosproject/net/pi/impl/PiUtils.java b/core/net/src/main/java/org/onosproject/net/pi/impl/PiUtils.java
new file mode 100644
index 0000000..aab8b40
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/pi/impl/PiUtils.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2017-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.net.pi.impl;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.flow.IndexTableId;
+import org.onosproject.net.flow.TableId;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.pi.runtime.PiTableId;
+import org.onosproject.net.pi.runtime.PiTranslationService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static java.lang.String.format;
+
+/**
+ * PI utility class.
+ */
+final class PiUtils {
+
+    private static final Logger log = LoggerFactory.getLogger(PiUtils.class);
+
+    private PiUtils() {
+        // Hides constructor.
+    }
+
+    static PiPipelineInterpreter getInterpreterOrNull(Device device, PiPipeconf pipeconf) {
+        if (device != null) {
+            return device.is(PiPipelineInterpreter.class) ? device.as(PiPipelineInterpreter.class) : null;
+        } else {
+            // The case of device == null should be admitted only during unit testing.
+            // In any other case, the interpreter should be constructed using the device.as() method to make sure that
+            // behaviour's handler/data attributes are correctly populated.
+            // FIXME: modify test class PiFlowRuleTranslatorTest to avoid passing null device
+            // I.e. we need to create a device object that supports is/as method for obtaining the interpreter.
+            log.warn("getInterpreterOrNull() called with device == null, is this a unit test?");
+            try {
+                return (PiPipelineInterpreter) pipeconf.implementation(PiPipelineInterpreter.class)
+                        .orElse(null)
+                        .newInstance();
+            } catch (InstantiationException | IllegalAccessException e) {
+                throw new RuntimeException(format("Unable to instantiate interpreter of pipeconf %s", pipeconf.id()));
+            }
+        }
+    }
+
+    static PiTableId translateTableId(TableId tableId, PiPipelineInterpreter interpreter)
+            throws PiTranslationService.PiTranslationException {
+        switch (tableId.type()) {
+            case PIPELINE_INDEPENDENT:
+                return (PiTableId) tableId;
+            case INDEX:
+                IndexTableId indexId = (IndexTableId) tableId;
+                if (interpreter == null) {
+                    throw new PiTranslationService.PiTranslationException(format(
+                            "Unable to map table ID '%d' from index to PI: missing interpreter", indexId.id()));
+                } else if (!interpreter.mapFlowRuleTableId(indexId.id()).isPresent()) {
+                    throw new PiTranslationService.PiTranslationException(format(
+                            "Unable to map table ID '%d' from index to PI: missing ID in interpreter", indexId.id()));
+                } else {
+                    return interpreter.mapFlowRuleTableId(indexId.id()).get();
+                }
+            default:
+                throw new PiTranslationService.PiTranslationException(format(
+                        "Unrecognized table ID type %s", tableId.type().name()));
+        }
+    }
+}