Add support for P4Runtime clone sessions via Group API

Clone sessions can now be created by defining groups with new type CLONE

The PI framework has been refactored to abstract commonality between
multicast groups and clone sessions as both are managed as part of the
P4Runtime packet replication engine (PRE).

Change-Id: I2f23c629b7de1931d5cab96ec76aef26130ce418
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCloneSessionEntry.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCloneSessionEntry.java
new file mode 100644
index 0000000..656a809
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCloneSessionEntry.java
@@ -0,0 +1,242 @@
+/*
+ * 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.net.pi.runtime;
+
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.net.DeviceId;
+
+import java.util.Collection;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Representation of a clone session entry of a protocol-independent packet
+ * replication engine (PRE).
+ */
+@Beta
+public final class PiCloneSessionEntry implements PiPreEntry {
+
+    public static final int DEFAULT_CLASS_OF_SERVICE = 0;
+    public static final int DO_NOT_TRUNCATE = 0;
+
+    private final int sessionId;
+    private final Set<PiPreReplica> replicas;
+    private final int classOfService;
+    private final int maxPacketLengthBytes;
+
+    private PiCloneSessionEntry(int sessionId, Set<PiPreReplica> replicas,
+                                int classOfService, int maxPacketBytes) {
+        this.sessionId = sessionId;
+        this.replicas = replicas;
+        this.classOfService = classOfService;
+        this.maxPacketLengthBytes = maxPacketBytes;
+    }
+
+    /**
+     * Returns the identifier of this clone session, unique in the scope of a
+     * PRE instance.
+     *
+     * @return clone session ID
+     */
+    public int sessionId() {
+        return sessionId;
+    }
+
+    /**
+     * Returns the packet replicas provided by this clone session.
+     *
+     * @return packet replicas
+     */
+    public Set<PiPreReplica> replicas() {
+        return replicas;
+    }
+
+    /**
+     * Returns the class of service associated to the replicas produced by this
+     * clone session.
+     *
+     * @return class of service
+     */
+    public int classOfService() {
+        return classOfService;
+    }
+
+    /**
+     * Returns the maximum length in bytes of cloned packets. If a larger packet
+     * is cloned, then the PRE is expected to truncate clones to the given size.
+     * 0 means that no truncation on the clone(s) will be performed.
+     *
+     * @return maximum length in bytes of clones packets
+     */
+    public int maxPacketLengthBytes() {
+        return maxPacketLengthBytes;
+    }
+
+    @Override
+    public PiEntityType piEntityType() {
+        return PiEntityType.PRE_ENTRY;
+    }
+
+    @Override
+    public PiPreEntryType preEntryType() {
+        return PiPreEntryType.CLONE_SESSION;
+    }
+
+    @Override
+    public PiCloneSessionEntryHandle handle(DeviceId deviceId) {
+        return PiCloneSessionEntryHandle.of(deviceId, this);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(sessionId, replicas, classOfService,
+                                maxPacketLengthBytes);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final PiCloneSessionEntry other = (PiCloneSessionEntry) obj;
+        return Objects.equal(this.sessionId, other.sessionId)
+                && Objects.equal(this.replicas, other.replicas)
+                && Objects.equal(this.classOfService, other.classOfService)
+                && Objects.equal(this.maxPacketLengthBytes, other.maxPacketLengthBytes);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("sessionId", sessionId)
+                .add("replicas", replicas)
+                .add("classOfService", classOfService)
+                .add("maxPacketLengthBytes", maxPacketLengthBytes)
+                .toString();
+    }
+
+    /**
+     * Returns a new builder of clone session entries.
+     *
+     * @return builder
+     */
+    public static PiCloneSessionEntry.Builder builder() {
+        return new PiCloneSessionEntry.Builder();
+    }
+
+    /**
+     * Builder of PI clone session entries.
+     */
+    public static final class Builder {
+
+        private Integer sessionId;
+        private ImmutableSet.Builder<PiPreReplica> replicaSetBuilder = ImmutableSet.builder();
+        private int classOfService = DEFAULT_CLASS_OF_SERVICE;
+        private int maxPacketLengthBytes = DO_NOT_TRUNCATE;
+
+        private Builder() {
+            // Hide constructor.
+        }
+
+        /**
+         * Sets the identifier of this clone session.
+         *
+         * @param sessionId session ID
+         * @return this
+         */
+        public PiCloneSessionEntry.Builder withSessionId(int sessionId) {
+            this.sessionId = sessionId;
+            return this;
+        }
+
+        /**
+         * Adds the given packet replica to this clone session.
+         *
+         * @param replica packet replica
+         * @return this
+         */
+        public PiCloneSessionEntry.Builder addReplica(PiPreReplica replica) {
+            checkNotNull(replica);
+            replicaSetBuilder.add(replica);
+            return this;
+        }
+
+        /**
+         * Adds the given packet replicas to this clone session.
+         *
+         * @param replicas packet replicas
+         * @return this
+         */
+        public PiCloneSessionEntry.Builder addReplicas(Collection<PiPreReplica> replicas) {
+            checkNotNull(replicas);
+            replicaSetBuilder.addAll(replicas);
+            return this;
+        }
+
+        /**
+         * Sets the class of service of this clone session. If not set, the
+         * default value {@link PiCloneSessionEntry#DEFAULT_CLASS_OF_SERVICE}
+         * will be used.
+         *
+         * @param classOfService class of service value
+         * @return this
+         */
+        public PiCloneSessionEntry.Builder withClassOfService(
+                int classOfService) {
+            this.classOfService = classOfService;
+            return this;
+        }
+
+        /**
+         * Sets the maximum length in bytes of cloned packets. If not set, the
+         * default value {@link PiCloneSessionEntry#DO_NOT_TRUNCATE} will be
+         * used.
+         *
+         * @param maxPacketLengthBytes max length in bytes of cloned packets
+         * @return this
+         */
+        public PiCloneSessionEntry.Builder withMaxPacketLengthBytes(
+                int maxPacketLengthBytes) {
+            checkArgument(maxPacketLengthBytes >= 0,
+                          "maxPacketLengthBytes must be a positive integer");
+            this.maxPacketLengthBytes = maxPacketLengthBytes;
+            return this;
+        }
+
+        /**
+         * Returns a new clone session entry.
+         *
+         * @return clone session entry
+         */
+        public PiCloneSessionEntry build() {
+            checkNotNull(sessionId, "Clone session ID must be set");
+            final ImmutableSet<PiPreReplica> replicas = replicaSetBuilder.build();
+            return new PiCloneSessionEntry(
+                    sessionId, replicas, classOfService, maxPacketLengthBytes);
+        }
+    }
+}
+
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCloneSessionEntryHandle.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCloneSessionEntryHandle.java
new file mode 100644
index 0000000..cd2df4a
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCloneSessionEntryHandle.java
@@ -0,0 +1,105 @@
+/*
+ * 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.net.pi.runtime;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onosproject.net.DeviceId;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Global identifier of a PI clone session entry applied to the packet
+ * replication engine (PRE) of a device, uniquely defined by a device ID, and
+ * session ID.
+ */
+@Beta
+public final class PiCloneSessionEntryHandle extends PiPreEntryHandle {
+
+    private final int sessionId;
+
+    private PiCloneSessionEntryHandle(DeviceId deviceId, int sessionId) {
+        super(deviceId);
+        this.sessionId = sessionId;
+    }
+
+    /**
+     * Creates a new handle for the given device ID and PI clone session ID.
+     *
+     * @param deviceId  device ID
+     * @param sessionId clone session ID
+     * @return PI clone session entry handle
+     */
+    public static PiCloneSessionEntryHandle of(DeviceId deviceId,
+                                               int sessionId) {
+        return new PiCloneSessionEntryHandle(deviceId, sessionId);
+    }
+
+    /**
+     * Creates a new handle for the given device ID and PI clone session entry.
+     *
+     * @param deviceId device ID
+     * @param entry    PI clone session entry
+     * @return PI clone session entry handle
+     */
+    public static PiCloneSessionEntryHandle of(DeviceId deviceId,
+                                               PiCloneSessionEntry entry) {
+        checkNotNull(entry);
+        return new PiCloneSessionEntryHandle(deviceId, entry.sessionId());
+    }
+
+    /**
+     * Returns the clone session ID associated with this handle.
+     *
+     * @return session ID
+     */
+    public int sessionId() {
+        return sessionId;
+    }
+
+    @Override
+    public PiPreEntryType preEntryType() {
+        return PiPreEntryType.CLONE_SESSION;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(deviceId(), sessionId);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        PiCloneSessionEntryHandle that = (PiCloneSessionEntryHandle) o;
+        return Objects.equal(deviceId(), that.deviceId()) &&
+                Objects.equal(sessionId, that.sessionId);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("deviceId", deviceId())
+                .add("sessionId", sessionId)
+                .toString();
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java
index 0e99188..2dc7235 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java
@@ -54,14 +54,9 @@
     COUNTER_CELL("counter cell"),
 
     /**
-     * Packet Replication Engine (PRE) multicast group entry.
+     * Packet Replication Engine (PRE) entry.
      */
-    PRE_MULTICAST_GROUP_ENTRY("PRE multicast group entry"),
-
-    /**
-     * Packet Replication Engine (PRE) clone session entry.
-     */
-    PRE_CLONE_SESSION_ENTRY("PRE clone session entry");
+    PRE_ENTRY("PRE entry");
 
     private final String humanReadableName;
 
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntry.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntry.java
index 7a833aa..73898f5 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntry.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntry.java
@@ -68,7 +68,7 @@
 
     @Override
     public PiEntityType piEntityType() {
-        return PiEntityType.PRE_MULTICAST_GROUP_ENTRY;
+        return PiEntityType.PRE_ENTRY;
     }
 
     @Override
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntryHandle.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntryHandle.java
index b74ca8e..b18cb51 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntryHandle.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiMulticastGroupEntryHandle.java
@@ -29,7 +29,7 @@
  * ID.
  */
 @Beta
-public final class PiMulticastGroupEntryHandle extends PiHandle {
+public final class PiMulticastGroupEntryHandle extends PiPreEntryHandle {
 
     private final int groupId;
 
@@ -74,8 +74,8 @@
     }
 
     @Override
-    public PiEntityType entityType() {
-        return PiEntityType.PRE_MULTICAST_GROUP_ENTRY;
+    public PiPreEntryType preEntryType() {
+        return PiPreEntryType.MULTICAST_GROUP;
     }
 
     @Override
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntry.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntry.java
index fb6c5e0..a774ff9 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntry.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntry.java
@@ -26,20 +26,6 @@
 public interface PiPreEntry extends PiEntity {
 
     /**
-     * Type of PRE entry.
-     */
-    enum PiPreEntryType {
-        /**
-         * Multicast group entry.
-         */
-        MULTICAST_GROUP,
-        /**
-         * Clone session entry.
-         */
-        CLONE_SESSION
-    }
-
-    /**
      * Returns the type of this PRE entry.
      *
      * @return PRE entry type
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntryHandle.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntryHandle.java
new file mode 100644
index 0000000..e159593
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntryHandle.java
@@ -0,0 +1,41 @@
+/*
+ * 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.net.pi.runtime;
+
+import org.onosproject.net.DeviceId;
+
+/**
+ * Abstract implementation of a PI handle for PRE entries.
+ */
+public abstract class PiPreEntryHandle extends PiHandle {
+
+    PiPreEntryHandle(DeviceId deviceId) {
+        super(deviceId);
+    }
+
+    /**
+     * Returns the type of PRE entry associated with this handle.
+     *
+     * @return PRE entry type
+     */
+    public abstract PiPreEntryType preEntryType();
+
+    @Override
+    public PiEntityType entityType() {
+        return PiEntityType.PRE_ENTRY;
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntryType.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntryType.java
new file mode 100644
index 0000000..4448834
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiPreEntryType.java
@@ -0,0 +1,32 @@
+/*
+ * 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.net.pi.runtime;
+
+/**
+ * Type of entry of the packet replication engine (PRE) or a
+ * protocol-independent pipeline.
+ */
+public enum PiPreEntryType {
+    /**
+     * Multicast group entry.
+     */
+    MULTICAST_GROUP,
+    /**
+     * Clone session entry.
+     */
+    CLONE_SESSION
+}
diff --git a/core/api/src/main/java/org/onosproject/net/pi/service/PiMulticastGroupTranslationStore.java b/core/api/src/main/java/org/onosproject/net/pi/service/PiReplicationGroupTranslationStore.java
similarity index 78%
rename from core/api/src/main/java/org/onosproject/net/pi/service/PiMulticastGroupTranslationStore.java
rename to core/api/src/main/java/org/onosproject/net/pi/service/PiReplicationGroupTranslationStore.java
index f961d84..60ef15c 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/service/PiMulticastGroupTranslationStore.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/service/PiReplicationGroupTranslationStore.java
@@ -18,13 +18,13 @@
 
 import com.google.common.annotations.Beta;
 import org.onosproject.net.group.Group;
-import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
+import org.onosproject.net.pi.runtime.PiPreEntry;
 
 /**
  * A PI translation store that keeps track of which groups have been
- * translated to which PI PRE multicast groups.
+ * translated to which PI packet replication engine (PRE) entry.
  */
 @Beta
-public interface PiMulticastGroupTranslationStore
-        extends PiTranslationStore<Group, PiMulticastGroupEntry> {
+public interface PiReplicationGroupTranslationStore
+        extends PiTranslationStore<Group, PiPreEntry> {
 }
diff --git a/core/api/src/main/java/org/onosproject/net/pi/service/PiMulticastGroupTranslator.java b/core/api/src/main/java/org/onosproject/net/pi/service/PiReplicationGroupTranslator.java
similarity index 75%
rename from core/api/src/main/java/org/onosproject/net/pi/service/PiMulticastGroupTranslator.java
rename to core/api/src/main/java/org/onosproject/net/pi/service/PiReplicationGroupTranslator.java
index e687189..960f98e 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/service/PiMulticastGroupTranslator.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/service/PiReplicationGroupTranslator.java
@@ -18,12 +18,13 @@
 
 import com.google.common.annotations.Beta;
 import org.onosproject.net.group.Group;
-import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
+import org.onosproject.net.pi.runtime.PiPreEntry;
 
 /**
- * A translator of groups to PI multicast group.
+ * A translator of groups for packet replication to PI packet replication engine
+ * (PRE) entries.
  */
 @Beta
-public interface PiMulticastGroupTranslator
-        extends PiTranslator<Group, PiMulticastGroupEntry> {
+public interface PiReplicationGroupTranslator
+        extends PiTranslator<Group, PiPreEntry> {
 }
diff --git a/core/api/src/main/java/org/onosproject/net/pi/service/PiTranslationService.java b/core/api/src/main/java/org/onosproject/net/pi/service/PiTranslationService.java
index 20ea14d..4871140 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/service/PiTranslationService.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/service/PiTranslationService.java
@@ -48,9 +48,9 @@
 
     /**
      * Returns a group translator for packet replication engine (PRE)
-     * multicast groups.
+     * entries.
      *
-     * @return multicast group translator
+     * @return replication group translator
      */
-    PiMulticastGroupTranslator multicastGroupTranslator();
+    PiReplicationGroupTranslator replicationGroupTranslator();
 }