[SDFAB-188] Remove buffer drainer from UpfProgrammable

Change-Id: Id10d8b41d203b4af99867d169255a63fe99b25a0
(cherry picked from commit ac94678fb6204b76f44feef3e6098b18c84359bd)
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfDevice.java b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfDevice.java
index ebb9f68..dd7528e 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfDevice.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfDevice.java
@@ -17,7 +17,6 @@
 package org.onosproject.net.behaviour.upf;
 
 import com.google.common.annotations.Beta;
-import org.onlab.packet.Ip4Address;
 
 import java.nio.ByteBuffer;
 import java.util.Collection;
@@ -151,7 +150,7 @@
 
     /**
      * Read the counter contents for all cell indices that are valid on the hardware switch.
-     * @code maxCounterId} parameter is used to limit the number of counters
+     * {@code maxCounterId} parameter is used to limit the number of counters
      * retrieved from the UPF device. If the limit given is larger than the
      * physical limit, the physical limit will be used. A limit of -1 removes
      * limitations.
@@ -163,34 +162,6 @@
     Collection<PdrStats> readAllCounters(long maxCounterId) throws UpfProgrammableException;
 
     /**
-     * Set the source and destination of the GTPU tunnel used to send packets to a dbuf buffering
-     * device.
-     *
-     * @param switchAddr the address on the switch that sends and receives packets to and from dbuf
-     * @param dbufAddr   the dataplane address of dbuf
-     */
-    void setDbufTunnel(Ip4Address switchAddr, Ip4Address dbufAddr);
-
-    /**
-     * Removes the dbuf tunnel info if they were previously set using {@link
-     * #setDbufTunnel(Ip4Address, Ip4Address)}.
-     */
-    void unsetDbufTunnel();
-
-    /**
-     * Install a BufferDrainer reference that can be used to trigger the draining of a specific dbuf
-     * buffer back into the UPF device.
-     *
-     * @param drainer the BufferDrainer reference
-     */
-    void setBufferDrainer(UpfProgrammable.BufferDrainer drainer);
-
-    /**
-     * Removes the buffer drainer if one was set using {@link #setBufferDrainer(UpfProgrammable.BufferDrainer)}.
-     */
-    void unsetBufferDrainer();
-
-    /**
      * Instructs the UPF-programmable device to use GTP-U extension PDU Session Container (PSC) when
      * doing encap of downlink packets, with the given QoS Flow Identifier (QFI).
      *
@@ -213,7 +184,7 @@
     /**
      * Sends the given data as a data plane packet-out through this device. Data is expected to
      * contain an Ethernet frame.
-     *
+     * <p>
      * The device should process the packet through the pipeline tables to select an output port
      * and to apply eventual modifications (e.g., MAC rewrite for routing, pushing a VLAN tag,
      * etc.).
@@ -221,18 +192,4 @@
      * @param data Ethernet frame bytes
      */
     void sendPacketOut(ByteBuffer data);
-
-    /**
-     * Used by the UpfProgrammable to trigger buffer draining as needed. Install an instance using
-     * {@link UpfProgrammable#setBufferDrainer(UpfProgrammable.BufferDrainer)}
-     */
-    //TODO: remove from UpfDevice
-    interface BufferDrainer {
-        /**
-         * Drain the buffer that contains packets for the UE with the given address.
-         *
-         * @param ueAddr the address of the UE for which we should drain a buffer
-         */
-        void drain(Ip4Address ueAddr);
-    }
 }
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfProgrammable.java b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfProgrammable.java
index 6dcce30..5c98f09 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfProgrammable.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfProgrammable.java
@@ -19,7 +19,6 @@
 import com.google.common.annotations.Beta;
 import org.onosproject.net.driver.HandlerBehaviour;
 
-
 /**
  * Provides means to update the device forwarding state to implement a 3GPP
  * User Plane Function. An implementation of this API should not write state
diff --git a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/DistributedFabricUpfStore.java b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/DistributedFabricUpfStore.java
index 1f40f3b..02923c2 100644
--- a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/DistributedFabricUpfStore.java
+++ b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/DistributedFabricUpfStore.java
@@ -19,13 +19,10 @@
 import com.google.common.collect.BiMap;
 import com.google.common.collect.ImmutableBiMap;
 import com.google.common.collect.Maps;
-import org.onlab.packet.Ip4Address;
 import org.onlab.util.ImmutableByteSequence;
 import org.onlab.util.KryoNamespace;
-import org.onosproject.net.behaviour.upf.PacketDetectionRule;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.ConsistentMap;
-import org.onosproject.store.service.DistributedSet;
 import org.onosproject.store.service.MapEvent;
 import org.onosproject.store.service.MapEventListener;
 import org.onosproject.store.service.Serializer;
@@ -38,12 +35,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.HashSet;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Set;
-
-import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * Distributed implementation of FabricUpfStore.
@@ -58,8 +51,6 @@
     protected StorageService storageService;
 
     protected static final String FAR_ID_MAP_NAME = "fabric-upf-far-id";
-    protected static final String BUFFER_FAR_ID_SET_NAME = "fabric-upf-buffer-far-id";
-    protected static final String FAR_ID_UE_MAP_NAME = "fabric-upf-far-id-ue";
     protected static final KryoNamespace.Builder SERIALIZER = KryoNamespace.newBuilder()
             .register(KryoNamespaces.API)
             .register(UpfRuleIdentifier.class);
@@ -83,9 +74,6 @@
     protected Map<Integer, UpfRuleIdentifier> reverseFarIdMap;
     private int nextGlobalFarId = 1;
 
-    protected DistributedSet<UpfRuleIdentifier> bufferFarIds;
-    protected ConsistentMap<UpfRuleIdentifier, Set<Ip4Address>> farIdToUeAddrs;
-
     @Activate
     protected void activate() {
         // Allow unit test to inject farIdMap here.
@@ -95,17 +83,6 @@
                     .withRelaxedReadConsistency()
                     .withSerializer(Serializer.using(SERIALIZER.build()))
                     .build();
-            this.bufferFarIds = storageService.<UpfRuleIdentifier>setBuilder()
-                    .withName(BUFFER_FAR_ID_SET_NAME)
-                    .withRelaxedReadConsistency()
-                    .withSerializer(Serializer.using(SERIALIZER.build()))
-                    .build().asDistributedSet();
-            this.farIdToUeAddrs = storageService.<UpfRuleIdentifier, Set<Ip4Address>>consistentMapBuilder()
-                    .withName(FAR_ID_UE_MAP_NAME)
-                    .withRelaxedReadConsistency()
-                    .withSerializer(Serializer.using(SERIALIZER.build()))
-                    .build();
-
         }
         farIdMapListener = new FarIdMapListener();
         farIdMap.addListener(farIdMapListener);
@@ -129,8 +106,6 @@
     public void reset() {
         farIdMap.clear();
         reverseFarIdMap.clear();
-        bufferFarIds.clear();
-        farIdToUeAddrs.clear();
         nextGlobalFarId = 0;
     }
 
@@ -171,59 +146,6 @@
         return reverseFarIdMap.get(globalFarId);
     }
 
-    public void learnFarIdToUeAddrs(PacketDetectionRule pdr) {
-        UpfRuleIdentifier ruleId = UpfRuleIdentifier.of(pdr.sessionId(), pdr.farId());
-        farIdToUeAddrs.compute(ruleId, (k, set) -> {
-            if (set == null) {
-                set = new HashSet<>();
-            }
-            set.add(pdr.ueAddress());
-            return set;
-        });
-    }
-
-    @Override
-    public boolean isFarIdBuffering(UpfRuleIdentifier farId) {
-        checkNotNull(farId);
-        return bufferFarIds.contains(farId);
-    }
-
-    @Override
-    public void learBufferingFarId(UpfRuleIdentifier farId) {
-        checkNotNull(farId);
-        bufferFarIds.add(farId);
-    }
-
-    @Override
-    public void forgetBufferingFarId(UpfRuleIdentifier farId) {
-        checkNotNull(farId);
-        bufferFarIds.remove(farId);
-    }
-
-    @Override
-    public void forgetUeAddr(Ip4Address ueAddr) {
-        farIdToUeAddrs.keySet().forEach(
-                farId -> farIdToUeAddrs.computeIfPresent(farId, (farIdz, ueAddrs) -> {
-                    ueAddrs.remove(ueAddr);
-                    return ueAddrs;
-                }));
-    }
-
-    @Override
-    public Set<Ip4Address> ueAddrsOfFarId(UpfRuleIdentifier farId) {
-        return farIdToUeAddrs.getOrDefault(farId, Set.of()).value();
-    }
-
-    @Override
-    public Set<UpfRuleIdentifier> getBufferFarIds() {
-        return Set.copyOf(bufferFarIds);
-    }
-
-    @Override
-    public Map<UpfRuleIdentifier, Set<Ip4Address>> getFarIdToUeAddrs() {
-        return Map.copyOf(farIdToUeAddrs.asJavaMap());
-    }
-
     // NOTE: FarIdMapListener is run on the same thread intentionally in order to ensure that
     //       reverseFarIdMap update always finishes right after farIdMap is updated
     private class FarIdMapListener implements MapEventListener<UpfRuleIdentifier, Integer> {
diff --git a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfProgrammable.java b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfProgrammable.java
index 77ad732..61345c4 100644
--- a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfProgrammable.java
+++ b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfProgrammable.java
@@ -18,14 +18,12 @@
 
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Maps;
-import org.onlab.packet.Ip4Address;
 import org.onlab.packet.Ip4Prefix;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.drivers.p4runtime.AbstractP4RuntimeHandlerBehaviour;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.behaviour.upf.ForwardingActionRule;
-import org.onosproject.net.behaviour.upf.GtpTunnel;
 import org.onosproject.net.behaviour.upf.PacketDetectionRule;
 import org.onosproject.net.behaviour.upf.PdrStats;
 import org.onosproject.net.behaviour.upf.UpfInterface;
@@ -99,15 +97,6 @@
 
     private ApplicationId appId;
 
-    // FIXME: remove, buffer drain should be triggered by Up4Service
-    private BufferDrainer bufferDrainer;
-
-    // FIXME: dbuf tunnel should be managed by Up4Service
-    //  Up4Service should be responsible of setting up such tunnel, then transforming FARs for this
-    //  device accordingly. When the tunnel endpoint change, it should be up to Up4Service to update
-    //  the FAR on the device.
-    private GtpTunnel dbufTunnel;
-
     @Override
     protected boolean setupBehaviour(String opName) {
         if (!super.setupBehaviour(opName)) {
@@ -201,22 +190,6 @@
     }
 
     @Override
-    public void setBufferDrainer(BufferDrainer drainer) {
-        if (!setupBehaviour("setBufferDrainer()")) {
-            return;
-        }
-        this.bufferDrainer = drainer;
-    }
-
-    @Override
-    public void unsetBufferDrainer() {
-        if (!setupBehaviour("unsetBufferDrainer()")) {
-            return;
-        }
-        this.bufferDrainer = null;
-    }
-
-    @Override
     public void enablePscEncap(int defaultQfi) throws UpfProgrammableException {
         throw new UpfProgrammableException("PSC encap is not supported in fabric-v1model",
                                            UNSUPPORTED_OPERATION);
@@ -244,46 +217,6 @@
     }
 
     @Override
-    public void setDbufTunnel(Ip4Address switchAddr, Ip4Address dbufAddr) {
-        if (!setupBehaviour("setDbufTunnel()")) {
-            return;
-        }
-        this.dbufTunnel = GtpTunnel.builder()
-                .setSrc(switchAddr)
-                .setDst(dbufAddr)
-                .setSrcPort((short) 2152)
-                .setTeid(0)
-                .build();
-    }
-
-    @Override
-    public void unsetDbufTunnel() {
-        if (!setupBehaviour("unsetDbufTunnel()")) {
-            return;
-        }
-        this.dbufTunnel = null;
-    }
-
-    /**
-     * Convert the given buffering FAR to a FAR that tunnels the packet to dbuf.
-     *
-     * @param far the FAR to convert
-     * @return the converted FAR
-     */
-    private ForwardingActionRule convertToDbufFar(ForwardingActionRule far) {
-        if (!far.buffers()) {
-            throw new IllegalArgumentException("Converting a non-buffering FAR to a dbuf FAR! This shouldn't happen.");
-        }
-        return ForwardingActionRule.builder()
-                .setFarId(far.farId())
-                .withSessionId(far.sessionId())
-                .setNotifyFlag(far.notifies())
-                .setBufferFlag(true)
-                .setTunnel(dbufTunnel)
-                .build();
-    }
-
-    @Override
     public void cleanUp() {
         if (!setupBehaviour("cleanUp()")) {
             return;
@@ -475,11 +408,6 @@
         log.info("Installing {}", pdr.toString());
         flowRuleService.applyFlowRules(fabricPdr);
         log.debug("PDR added with flowID {}", fabricPdr.id().value());
-
-        // If the flow rule was applied and the PDR is downlink, add the PDR to the farID->PDR mapping
-        if (pdr.matchesUnencapped()) {
-            fabricUpfStore.learnFarIdToUeAddrs(pdr);
-        }
     }
 
 
@@ -488,28 +416,10 @@
         if (!setupBehaviour("addFar()")) {
             return;
         }
-        UpfRuleIdentifier ruleId = UpfRuleIdentifier.of(far.sessionId(), far.farId());
-        if (far.buffers()) {
-            // If the far has the buffer flag, modify its tunnel so it directs to dbuf
-            far = convertToDbufFar(far);
-            fabricUpfStore.learBufferingFarId(ruleId);
-        }
         FlowRule fabricFar = upfTranslator.farToFabricEntry(far, deviceId, appId, DEFAULT_PRIORITY);
         log.info("Installing {}", far.toString());
         flowRuleService.applyFlowRules(fabricFar);
         log.debug("FAR added with flowID {}", fabricFar.id().value());
-        if (!far.buffers() && fabricUpfStore.isFarIdBuffering(ruleId)) {
-            // If this FAR does not buffer but used to, then drain the buffer for every UE address
-            // that hits this FAR.
-            fabricUpfStore.forgetBufferingFarId(ruleId);
-            for (var ueAddr : fabricUpfStore.ueAddrsOfFarId(ruleId)) {
-                if (bufferDrainer == null) {
-                    log.warn("Unable to drain downlink buffer for UE {}, bufferDrainer is null", ueAddr);
-                } else {
-                    bufferDrainer.drain(ueAddr);
-                }
-            }
-        }
     }
 
     @Override
@@ -620,13 +530,6 @@
         }
         log.info("Removing {}", pdr.toString());
         removeEntry(match, tableId, false);
-
-        // Remove the PDR from the farID->PDR mapping
-        // This is an inefficient hotfix FIXME: remove UE addrs from the mapping in sublinear time
-        if (pdr.matchesUnencapped()) {
-            // Should we remove just from the map entry with key == far ID?
-            fabricUpfStore.forgetUeAddr(pdr.ueAddress());
-        }
     }
 
     @Override
diff --git a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfStore.java b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfStore.java
index 786ef3c..9b5d853 100644
--- a/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfStore.java
+++ b/pipelines/fabric/impl/src/main/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/FabricUpfStore.java
@@ -16,12 +16,9 @@
 
 package org.onosproject.pipelines.fabric.impl.behaviour.upf;
 
-import org.onlab.packet.Ip4Address;
 import org.onlab.util.ImmutableByteSequence;
-import org.onosproject.net.behaviour.upf.PacketDetectionRule;
 
 import java.util.Map;
-import java.util.Set;
 
 /**
  * Stores state required for translation of UPF entities to pipeline-specific ones.
@@ -80,62 +77,6 @@
      *
      * @param queueId Tofino queue Id
      * @return the corresponding scheduling priroity
-    */
+     */
     String schedulingPriorityOf(int queueId);
-
-    /**
-     * Stores the mapping between FAR ID and UE address as defined by the given PDR.
-     *
-     * @param pdr PDR
-     */
-    void learnFarIdToUeAddrs(PacketDetectionRule pdr);
-
-    /**
-     * Returns true if the given FAR IDs is known to be a buffering one.
-     *
-     * @param farId FAR ID
-     * @return boolean
-     */
-    boolean isFarIdBuffering(UpfRuleIdentifier farId);
-
-    /**
-     * Learns the given FAR ID as being a buffering one.
-     *
-     * @param farId FAR ID
-     */
-    void learBufferingFarId(UpfRuleIdentifier farId);
-
-    /**
-     * Forgets the given FAR ID as being a buffering one.
-     *
-     * @param farId FAR ID
-     */
-    void forgetBufferingFarId(UpfRuleIdentifier farId);
-
-    /**
-     * Returns the set of UE addresses associated with the given FAR ID.
-     *
-     * @param farId FAR ID
-     * @return Set of Ip4Address
-     */
-    Set<Ip4Address> ueAddrsOfFarId(UpfRuleIdentifier farId);
-
-    /**
-     * Removes the given UE address from the FAR ID to UE address map.
-     * @param ueAddr UE address
-     */
-    void forgetUeAddr(Ip4Address ueAddr);
-
-    /**
-     * Returns the set of known buffering FAR IDs.
-     * @return set
-     */
-    Set<UpfRuleIdentifier> getBufferFarIds();
-
-    /**
-     * Returns the FAR ID to UE addresses map.
-     *
-     * @return map
-     */
-    Map<UpfRuleIdentifier, Set<Ip4Address>> getFarIdToUeAddrs();
 }
diff --git a/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/TestDistributedFabricUpfStore.java b/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/TestDistributedFabricUpfStore.java
index 8500b9a..cf09c02 100644
--- a/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/TestDistributedFabricUpfStore.java
+++ b/pipelines/fabric/impl/src/test/java/org/onosproject/pipelines/fabric/impl/behaviour/upf/TestDistributedFabricUpfStore.java
@@ -16,16 +16,10 @@
 
 package org.onosproject.pipelines.fabric.impl.behaviour.upf;
 
-import org.onlab.packet.Ip4Address;
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.TestConsistentMap;
-import org.onosproject.store.service.TestDistributedSet;
 
-import java.util.Set;
-
-import static org.onosproject.pipelines.fabric.impl.behaviour.upf.DistributedFabricUpfStore.BUFFER_FAR_ID_SET_NAME;
 import static org.onosproject.pipelines.fabric.impl.behaviour.upf.DistributedFabricUpfStore.FAR_ID_MAP_NAME;
-import static org.onosproject.pipelines.fabric.impl.behaviour.upf.DistributedFabricUpfStore.FAR_ID_UE_MAP_NAME;
 import static org.onosproject.pipelines.fabric.impl.behaviour.upf.DistributedFabricUpfStore.SERIALIZER;
 
 
@@ -43,22 +37,6 @@
                 .withSerializer(Serializer.using(SERIALIZER.build()));
         store.farIdMap = farIdMapBuilder.build();
 
-        TestDistributedSet.Builder<UpfRuleIdentifier> bufferFarIdsBuilder =
-                TestDistributedSet.builder();
-        bufferFarIdsBuilder
-                .withName(BUFFER_FAR_ID_SET_NAME)
-                .withRelaxedReadConsistency()
-                .withSerializer(Serializer.using(SERIALIZER.build()));
-        store.bufferFarIds = bufferFarIdsBuilder.build().asDistributedSet();
-
-        TestConsistentMap.Builder<UpfRuleIdentifier, Set<Ip4Address>> farIdToUeAddrsBuilder =
-                TestConsistentMap.builder();
-        farIdToUeAddrsBuilder
-                .withName(FAR_ID_UE_MAP_NAME)
-                .withRelaxedReadConsistency()
-                .withSerializer(Serializer.using(SERIALIZER.build()));
-        store.farIdToUeAddrs = farIdToUeAddrsBuilder.build();
-
         store.activate();
 
         // Init with some translation state.