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();
 }
diff --git a/core/net/src/main/java/org/onosproject/net/pi/impl/PiMulticastGroupTranslatorImpl.java b/core/net/src/main/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImpl.java
similarity index 79%
rename from core/net/src/main/java/org/onosproject/net/pi/impl/PiMulticastGroupTranslatorImpl.java
rename to core/net/src/main/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImpl.java
index 7d48209..1f562a4 100644
--- a/core/net/src/main/java/org/onosproject/net/pi/impl/PiMulticastGroupTranslatorImpl.java
+++ b/core/net/src/main/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImpl.java
@@ -26,7 +26,9 @@
 import org.onosproject.net.group.GroupDescription;
 import org.onosproject.net.pi.model.PiPipeconf;
 import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.pi.runtime.PiCloneSessionEntry;
 import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
+import org.onosproject.net.pi.runtime.PiPreEntry;
 import org.onosproject.net.pi.runtime.PiPreReplica;
 import org.onosproject.net.pi.service.PiTranslationException;
 
@@ -41,19 +43,20 @@
 import static java.lang.String.format;
 
 /**
- * Implementation of multicast group translation logic.
+ * Implementation of replication group translation logic.
  */
-final class PiMulticastGroupTranslatorImpl {
+final class PiReplicationGroupTranslatorImpl {
 
-    private PiMulticastGroupTranslatorImpl() {
+    private PiReplicationGroupTranslatorImpl() {
         // Hides constructor.
     }
 
     /**
-     * Returns a PI PRE multicast group entry equivalent to the given group, for
-     * the given pipeconf and device.
+     * Returns a PI PRE entry equivalent to the given group, for the given
+     * pipeconf and device.
      * <p>
-     * The passed group is expected to have type {@link GroupDescription.Type#ALL}.
+     * The passed group is expected to have type {@link GroupDescription.Type#ALL}
+     * or {@link GroupDescription.Type#CLONE}.
      *
      * @param group    group
      * @param pipeconf pipeconf
@@ -61,16 +64,11 @@
      * @return PI PRE entry
      * @throws PiTranslationException if the group cannot be translated
      */
-    static PiMulticastGroupEntry translate(Group group, PiPipeconf pipeconf, Device device)
+    static PiPreEntry translate(Group group, PiPipeconf pipeconf, Device device)
             throws PiTranslationException {
 
         checkNotNull(group);
 
-        if (!group.type().equals(GroupDescription.Type.ALL)) {
-            throw new PiTranslationException(format(
-                    "group type %s not supported", group.type()));
-        }
-
         final List<Instruction> instructions = group.buckets().buckets().stream()
                 .flatMap(b -> b.treatment().allInstructions().stream())
                 .collect(Collectors.toList());
@@ -88,10 +86,21 @@
                 .map(i -> (OutputInstruction) i)
                 .collect(Collectors.toList());
 
-        return PiMulticastGroupEntry.builder()
-                .withGroupId(group.id().id())
-                .addReplicas(getReplicas(outInstructions, device))
-                .build();
+        switch (group.type()) {
+            case ALL:
+                return PiMulticastGroupEntry.builder()
+                        .withGroupId(group.id().id())
+                        .addReplicas(getReplicas(outInstructions, device))
+                        .build();
+            case CLONE:
+                return PiCloneSessionEntry.builder()
+                        .withSessionId(group.id().id())
+                        .addReplicas(getReplicas(outInstructions, device))
+                        .build();
+            default:
+                throw new PiTranslationException(format(
+                        "group type %s not supported", group.type()));
+        }
     }
 
     private static Set<PiPreReplica> getReplicas(
diff --git a/core/net/src/main/java/org/onosproject/net/pi/impl/PiTranslationServiceImpl.java b/core/net/src/main/java/org/onosproject/net/pi/impl/PiTranslationServiceImpl.java
index 510ad01..18746d3 100644
--- a/core/net/src/main/java/org/onosproject/net/pi/impl/PiTranslationServiceImpl.java
+++ b/core/net/src/main/java/org/onosproject/net/pi/impl/PiTranslationServiceImpl.java
@@ -25,7 +25,7 @@
 import org.onosproject.net.pi.model.PiPipeconf;
 import org.onosproject.net.pi.runtime.PiActionProfileGroup;
 import org.onosproject.net.pi.runtime.PiMeterCellConfig;
-import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
+import org.onosproject.net.pi.runtime.PiPreEntry;
 import org.onosproject.net.pi.runtime.PiTableEntry;
 import org.onosproject.net.pi.service.PiFlowRuleTranslationStore;
 import org.onosproject.net.pi.service.PiFlowRuleTranslator;
@@ -33,8 +33,8 @@
 import org.onosproject.net.pi.service.PiGroupTranslator;
 import org.onosproject.net.pi.service.PiMeterTranslationStore;
 import org.onosproject.net.pi.service.PiMeterTranslator;
-import org.onosproject.net.pi.service.PiMulticastGroupTranslationStore;
-import org.onosproject.net.pi.service.PiMulticastGroupTranslator;
+import org.onosproject.net.pi.service.PiReplicationGroupTranslationStore;
+import org.onosproject.net.pi.service.PiReplicationGroupTranslator;
 import org.onosproject.net.pi.service.PiTranslationException;
 import org.onosproject.net.pi.service.PiTranslationService;
 import org.osgi.service.component.annotations.Activate;
@@ -67,21 +67,21 @@
     private PiGroupTranslationStore groupTranslationStore;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
-    private PiMulticastGroupTranslationStore mcGroupTranslationStore;
+    private PiReplicationGroupTranslationStore repGroupTranslationStore;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
     private PiMeterTranslationStore meterTranslationStore;
 
     private PiFlowRuleTranslator flowRuleTranslator;
     private PiGroupTranslator groupTranslator;
-    private PiMulticastGroupTranslator mcGroupTranslator;
+    private PiReplicationGroupTranslator repGroupTranslator;
     private PiMeterTranslator meterTranslator;
 
     @Activate
     public void activate() {
         flowRuleTranslator = new InternalFlowRuleTranslator(flowRuleTranslationStore);
         groupTranslator = new InternalGroupTranslator(groupTranslationStore);
-        mcGroupTranslator = new InternalMulticastGroupTranslator(mcGroupTranslationStore);
+        repGroupTranslator = new InternalReplicationGroupTranslator(repGroupTranslationStore);
         meterTranslator = new InternalMeterTranslator(meterTranslationStore);
         log.info("Started");
     }
@@ -110,8 +110,8 @@
     }
 
     @Override
-    public PiMulticastGroupTranslator multicastGroupTranslator() {
-        return mcGroupTranslator;
+    public PiReplicationGroupTranslator replicationGroupTranslator() {
+        return repGroupTranslator;
     }
 
     private Device getDevice(DeviceId deviceId) throws PiTranslationException {
@@ -158,20 +158,20 @@
         }
     }
 
-    private final class InternalMulticastGroupTranslator
-            extends AbstractPiTranslatorImpl<Group, PiMulticastGroupEntry>
-            implements PiMulticastGroupTranslator {
+    private final class InternalReplicationGroupTranslator
+            extends AbstractPiTranslatorImpl<Group, PiPreEntry>
+            implements PiReplicationGroupTranslator {
 
-        private InternalMulticastGroupTranslator(PiMulticastGroupTranslationStore store) {
+        private InternalReplicationGroupTranslator(PiReplicationGroupTranslationStore store) {
             super(store);
         }
 
         @Override
-        public PiMulticastGroupEntry translate(Group original, PiPipeconf pipeconf)
+        public PiPreEntry translate(Group original, PiPipeconf pipeconf)
                 throws PiTranslationException {
             checkNotNull(original);
             checkNotNull(pipeconf);
-            return PiMulticastGroupTranslatorImpl.translate(
+            return PiReplicationGroupTranslatorImpl.translate(
                     original, pipeconf, getDevice(original.deviceId()));
         }
     }
diff --git a/core/net/src/test/java/org/onosproject/net/pi/impl/PiMulticastGroupTranslatorImplTest.java b/core/net/src/test/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImplTest.java
similarity index 70%
rename from core/net/src/test/java/org/onosproject/net/pi/impl/PiMulticastGroupTranslatorImplTest.java
rename to core/net/src/test/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImplTest.java
index 9fb7076..7b5e213 100644
--- a/core/net/src/test/java/org/onosproject/net/pi/impl/PiMulticastGroupTranslatorImplTest.java
+++ b/core/net/src/test/java/org/onosproject/net/pi/impl/PiReplicationGroupTranslatorImplTest.java
@@ -30,35 +30,42 @@
 import org.onosproject.net.group.DefaultGroup;
 import org.onosproject.net.group.DefaultGroupBucket;
 import org.onosproject.net.group.DefaultGroupDescription;
+import org.onosproject.net.group.DefaultGroupKey;
 import org.onosproject.net.group.Group;
 import org.onosproject.net.group.GroupBucket;
 import org.onosproject.net.group.GroupBuckets;
 import org.onosproject.net.group.GroupDescription;
-import org.onosproject.net.pi.runtime.PiGroupKey;
+import org.onosproject.net.group.GroupKey;
+import org.onosproject.net.pi.runtime.PiCloneSessionEntry;
 import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
+import org.onosproject.net.pi.runtime.PiPreEntry;
 import org.onosproject.net.pi.runtime.PiPreReplica;
 
 import java.util.List;
 import java.util.Set;
 
 import static org.onosproject.net.group.GroupDescription.Type.ALL;
-import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_WCMP_SELECTOR;
-import static org.onosproject.pipelines.basic.BasicConstants.INGRESS_WCMP_CONTROL_WCMP_TABLE;
+import static org.onosproject.net.group.GroupDescription.Type.CLONE;
 
 /**
- * Test for {@link PiMulticastGroupTranslatorImpl}.
+ * Test for {@link PiReplicationGroupTranslatorImpl}.
  */
-public class PiMulticastGroupTranslatorImplTest {
+public class PiReplicationGroupTranslatorImplTest {
     private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:dummy:1");
     private static final ApplicationId APP_ID = TestApplicationId.create("dummy");
-    private static final GroupId GROUP_ID = GroupId.valueOf(1);
-    private static final PiGroupKey GROUP_KEY = new PiGroupKey(
-            INGRESS_WCMP_CONTROL_WCMP_TABLE, INGRESS_WCMP_CONTROL_WCMP_SELECTOR, GROUP_ID.id());
+    private static final int ENTRY_ID = 99;
+    private static final GroupId GROUP_ID = GroupId.valueOf(ENTRY_ID);
+    private static final GroupKey GROUP_KEY = new DefaultGroupKey(
+            String.valueOf(GROUP_ID.id()).getBytes());
 
     private static final List<GroupBucket> BUCKET_LIST = ImmutableList.of(
             allOutputBucket(1),
             allOutputBucket(2),
             allOutputBucket(3));
+    private static final List<GroupBucket> CLONE_BUCKET_LIST = ImmutableList.of(
+            cloneOutputBucket(1),
+            cloneOutputBucket(2),
+            cloneOutputBucket(3));
     private static final List<GroupBucket> BUCKET_LIST_2 = ImmutableList.of(
             allOutputBucket(1),
             allOutputBucket(2),
@@ -82,6 +89,11 @@
                     .withGroupId(GROUP_ID.id())
                     .addReplicas(REPLICAS)
                     .build();
+    private static final PiCloneSessionEntry PI_CLONE_SESSION_ENTRY =
+            PiCloneSessionEntry.builder()
+                    .withSessionId(ENTRY_ID)
+                    .addReplicas(REPLICAS)
+                    .build();
     private static final PiMulticastGroupEntry PI_MULTICAST_GROUP_2 =
             PiMulticastGroupEntry.builder()
                     .withGroupId(GROUP_ID.id())
@@ -89,12 +101,17 @@
                     .build();
 
     private static final GroupBuckets BUCKETS = new GroupBuckets(BUCKET_LIST);
+    private static final GroupBuckets CLONE_BUCKETS = new GroupBuckets(CLONE_BUCKET_LIST);
     private static final GroupBuckets BUCKETS_2 = new GroupBuckets(BUCKET_LIST_2);
 
     private static final GroupDescription ALL_GROUP_DESC = new DefaultGroupDescription(
             DEVICE_ID, ALL, BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID);
     private static final Group ALL_GROUP = new DefaultGroup(GROUP_ID, ALL_GROUP_DESC);
 
+    private static final GroupDescription CLONE_GROUP_DESC = new DefaultGroupDescription(
+            DEVICE_ID, CLONE, CLONE_BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID);
+    private static final Group CLONE_GROUP = new DefaultGroup(GROUP_ID, CLONE_GROUP_DESC);
+
     private static final GroupDescription ALL_GROUP_DESC_2 = new DefaultGroupDescription(
             DEVICE_ID, ALL, BUCKETS_2, GROUP_KEY, GROUP_ID.id(), APP_ID);
     private static final Group ALL_GROUP_2 = new DefaultGroup(GROUP_ID, ALL_GROUP_DESC_2);
@@ -107,17 +124,27 @@
         return DefaultGroupBucket.createAllGroupBucket(treatment);
     }
 
+    private static GroupBucket cloneOutputBucket(int portNum) {
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+                .setOutput(PortNumber.portNumber(portNum))
+                .build();
+        return DefaultGroupBucket.createCloneGroupBucket(treatment);
+    }
+
     @Test
     public void testTranslatePreGroups() throws Exception {
 
-        PiMulticastGroupEntry multicastGroup = PiMulticastGroupTranslatorImpl
+        PiPreEntry multicastGroup = PiReplicationGroupTranslatorImpl
                 .translate(ALL_GROUP, null, null);
-        PiMulticastGroupEntry multicastGroup2 = PiMulticastGroupTranslatorImpl
+        PiPreEntry multicastGroup2 = PiReplicationGroupTranslatorImpl
                 .translate(ALL_GROUP_2, null, null);
+        PiPreEntry cloneSessionEntry = PiReplicationGroupTranslatorImpl
+                .translate(CLONE_GROUP, null, null);
 
         new EqualsTester()
                 .addEqualityGroup(multicastGroup, PI_MULTICAST_GROUP)
                 .addEqualityGroup(multicastGroup2, PI_MULTICAST_GROUP_2)
+                .addEqualityGroup(cloneSessionEntry, PI_CLONE_SESSION_ENTRY)
                 .testEquals();
     }
 }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/pi/impl/DistributedPiMulticastGroupTranslationStore.java b/core/store/dist/src/main/java/org/onosproject/store/pi/impl/DistributedPiReplicationGroupTranslationStore.java
similarity index 60%
rename from core/store/dist/src/main/java/org/onosproject/store/pi/impl/DistributedPiMulticastGroupTranslationStore.java
rename to core/store/dist/src/main/java/org/onosproject/store/pi/impl/DistributedPiReplicationGroupTranslationStore.java
index dfa6652..82d1f0a 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/pi/impl/DistributedPiMulticastGroupTranslationStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/pi/impl/DistributedPiReplicationGroupTranslationStore.java
@@ -16,20 +16,21 @@
 
 package org.onosproject.store.pi.impl;
 
-import org.osgi.service.component.annotations.Component;
 import org.onosproject.net.group.Group;
-import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
-import org.onosproject.net.pi.service.PiMulticastGroupTranslationStore;
+import org.onosproject.net.pi.runtime.PiPreEntry;
+import org.onosproject.net.pi.service.PiReplicationGroupTranslationStore;
+import org.osgi.service.component.annotations.Component;
 
 /**
- * Distributed implementation of a PI translation store for multicast groups.
+ * Distributed implementation of a PI translation store for groups that require
+ * packet replication.
  */
-@Component(immediate = true, service = PiMulticastGroupTranslationStore.class)
-public class DistributedPiMulticastGroupTranslationStore
-        extends AbstractDistributedPiTranslationStore<Group, PiMulticastGroupEntry>
-        implements PiMulticastGroupTranslationStore {
+@Component(immediate = true, service = PiReplicationGroupTranslationStore.class)
+public class DistributedPiReplicationGroupTranslationStore
+        extends AbstractDistributedPiTranslationStore<Group, PiPreEntry>
+        implements PiReplicationGroupTranslationStore {
 
-    private static final String MAP_SIMPLE_NAME = "mc-group";
+    private static final String MAP_SIMPLE_NAME = "replication-group";
 
     @Override
     protected String mapSimpleName() {
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index e2b826d..db5e28d 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -231,8 +231,11 @@
 import org.onosproject.net.pi.runtime.PiActionProfileMember;
 import org.onosproject.net.pi.runtime.PiActionProfileMemberHandle;
 import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
+import org.onosproject.net.pi.runtime.PiCloneSessionEntry;
+import org.onosproject.net.pi.runtime.PiCloneSessionEntryHandle;
 import org.onosproject.net.pi.runtime.PiCounterCell;
 import org.onosproject.net.pi.runtime.PiCounterCellData;
+import org.onosproject.net.pi.runtime.PiCounterCellHandle;
 import org.onosproject.net.pi.runtime.PiCounterCellId;
 import org.onosproject.net.pi.runtime.PiEntity;
 import org.onosproject.net.pi.runtime.PiEntityType;
@@ -242,10 +245,21 @@
 import org.onosproject.net.pi.runtime.PiHandle;
 import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
 import org.onosproject.net.pi.runtime.PiMatchKey;
+import org.onosproject.net.pi.runtime.PiMeterBand;
+import org.onosproject.net.pi.runtime.PiMeterCellConfig;
+import org.onosproject.net.pi.runtime.PiMeterCellHandle;
 import org.onosproject.net.pi.runtime.PiMeterCellId;
+import org.onosproject.net.pi.runtime.PiMulticastGroupEntry;
+import org.onosproject.net.pi.runtime.PiMulticastGroupEntryHandle;
 import org.onosproject.net.pi.runtime.PiPacketMetadata;
 import org.onosproject.net.pi.runtime.PiPacketOperation;
+import org.onosproject.net.pi.runtime.PiPreEntry;
+import org.onosproject.net.pi.runtime.PiPreEntryHandle;
+import org.onosproject.net.pi.runtime.PiPreEntryType;
+import org.onosproject.net.pi.runtime.PiPreReplica;
 import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
+import org.onosproject.net.pi.runtime.PiRegisterCell;
+import org.onosproject.net.pi.runtime.PiRegisterCellId;
 import org.onosproject.net.pi.runtime.PiTableAction;
 import org.onosproject.net.pi.runtime.PiTableEntry;
 import org.onosproject.net.pi.runtime.PiTableEntryHandle;
@@ -687,16 +701,18 @@
                     PiTableType.class,
                     // PI Runtime
                     PiAction.class,
+                    PiActionParam.class,
                     PiActionProfileGroup.class,
                     PiActionProfileGroupHandle.class,
                     PiActionProfileGroupId.class,
                     PiActionProfileMember.class,
                     PiActionProfileMemberHandle.class,
                     PiActionProfileMemberId.class,
-                    PiActionParam.class,
-                    PiPacketMetadata.class,
+                    PiCloneSessionEntry.class,
+                    PiCloneSessionEntryHandle.class,
                     PiCounterCell.class,
                     PiCounterCellData.class,
+                    PiCounterCellHandle.class,
                     PiCounterCellId.class,
                     PiEntity.class,
                     PiEntityType.class,
@@ -706,13 +722,26 @@
                     PiHandle.class,
                     PiLpmFieldMatch.class,
                     PiMatchKey.class,
+                    PiMeterBand.class,
+                    PiMeterCellConfig.class,
+                    PiMeterCellHandle.class,
+                    PiMeterCellId.class,
+                    PiMulticastGroupEntry.class,
+                    PiMulticastGroupEntryHandle.class,
+                    PiPacketMetadata.class,
                     PiPacketOperation.class,
+                    PiPreEntry.class,
+                    PiPreEntryHandle.class,
+                    PiPreEntryType.class,
+                    PiPreReplica.class,
                     PiRangeFieldMatch.class,
+                    PiRegisterCell.class,
+                    PiRegisterCellId.class,
                     PiTableAction.class,
                     PiTableEntry.class,
+                    PiTableEntryHandle.class,
                     PiTernaryFieldMatch.class,
                     // PI service
-                    PiTableEntryHandle.class,
                     PiTranslatedEntity.class,
                     PiTranslatable.class,
                     // Other