Fixes a regression introduced by 23223.

Additionally adds a cleanUp method for the pipeliners
to reset the internal states between different executions.
This was another regression introduced by 23223.

Fixes also a memory leak caused by re-init of the grouphandler
without terminating its internal references

Change-Id: I06e9e005110c5237cb3bdf893cc71975fb94281e
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/XpliantPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/XpliantPipeline.java
index 2dca42d..eced43e 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/XpliantPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/XpliantPipeline.java
@@ -34,6 +34,13 @@
 
     @Override
     protected void initGroupHander(PipelinerContext context) {
+        // Terminate internal references
+        // We are terminating the references here
+        // because when the device is offline the apps
+        // are still sending flowobjectives
+        if (groupHandler != null) {
+            groupHandler.terminate();
+        }
         groupHandler = new XpliantGroupHandler();
         groupHandler.init(deviceId, context);
     }
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java
index 85ce51b..7d9deb1 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java
@@ -182,17 +182,38 @@
 
     @Override
     protected void initGroupHander(PipelinerContext context) {
+        // Terminate internal references
+        // We are terminating the references here
+        // because when the device is offline the apps
+        // are still sending flowobjectives
+        if (groupHandler != null) {
+            groupHandler.terminate();
+        }
         groupHandler = new CpqdOfdpa2GroupHandler();
         groupHandler.init(deviceId, context);
     }
 
     @Override
     public void init(DeviceId deviceId, PipelinerContext context) {
-        if (!ready.getAndSet(true)) {
+        synchronized (this) {
+            if (isReady()) {
+                return;
+            }
+
             if (supportPuntGroup()) {
+                // Terminate internal references
+                // We are terminating the references here
+                // because when the device is offline the apps
+                // are still sending flowobjectives
+                if (groupChecker != null) {
+                    groupChecker.shutdown();
+                }
                 // create a new executor at each init and a new empty queue
                 groupChecker = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/driver",
                         "cpqd-ofdpa-%d", log));
+                if (flowRuleQueue != null) {
+                    flowRuleQueue.clear();
+                }
                 flowRuleQueue = new ConcurrentLinkedQueue<>();
                 groupCheckerLock = new ReentrantLock();
                 groupChecker.scheduleAtFixedRate(new PopVlanPuntGroupChecker(), 20, 50, TimeUnit.MILLISECONDS);
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java
index 53d8fe1..1f28af3 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java
@@ -75,6 +75,13 @@
 
     @Override
     protected void initGroupHander(PipelinerContext context) {
+        // Terminate internal references
+        // We are terminating the references here
+        // because when the device is offline the apps
+        // are still sending flowobjectives
+        if (groupHandler != null) {
+            groupHandler.terminate();
+        }
         groupHandler = new CpqdOfdpa2GroupHandler();
         groupHandler.init(deviceId, context);
     }
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java
index 0b203b0..341798d 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java
@@ -116,6 +116,7 @@
             new ConcurrentHashMap<>();
     private ScheduledExecutorService groupCheckerExecutor =
             Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", "ofdpa-%d", log));
+    private InnerGroupListener innerGroupListener = new InnerGroupListener();
     /**
      * Determines whether this pipeline support copy ttl instructions or not.
      *
@@ -185,7 +186,34 @@
         pendingUpdateNextObjectives = new ConcurrentHashMap<>();
         GroupChecker groupChecker = new GroupChecker(this);
         groupCheckerExecutor.scheduleAtFixedRate(groupChecker, 0, 500, TimeUnit.MILLISECONDS);
-        groupService.addListener(new InnerGroupListener());
+        groupService.addListener(innerGroupListener);
+    }
+
+    // Terminate internal references
+    public void terminate() {
+        if (nextIndex != null) {
+            nextIndex.destroy();
+        }
+        nextIndex = null;
+        if (pendingAddNextObjectives != null) {
+            pendingAddNextObjectives.cleanUp();
+        }
+        pendingAddNextObjectives = null;
+        if (pendingRemoveNextObjectives != null) {
+            pendingRemoveNextObjectives.cleanUp();
+        }
+        pendingRemoveNextObjectives = null;
+        if (pendingGroups != null) {
+            pendingGroups.cleanUp();
+        }
+        pendingGroups = null;
+        if (groupCheckerExecutor != null) {
+            groupCheckerExecutor.shutdown();
+        }
+        groupCheckerExecutor = null;
+        if (groupService != null) {
+            groupService.removeListener(innerGroupListener);
+        }
     }
 
     //////////////////////////////////////
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
index 6731d72..2a400ba 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
@@ -148,7 +148,11 @@
 
     @Override
     public void init(DeviceId deviceId, PipelinerContext context) {
-        if (!ready.getAndSet(true)) {
+        synchronized (this) {
+            if (isReady()) {
+                return;
+            }
+
             this.deviceId = deviceId;
 
             serviceDirectory = context.directory();
@@ -168,6 +172,7 @@
             initGroupHander(context);
 
             initializePipeline();
+            ready.set(true);
         }
     }
 
@@ -176,6 +181,17 @@
         return ready.get();
     }
 
+    @Override
+    public void cleanUp() {
+        synchronized (this) {
+            if (!isReady()) {
+                return;
+            }
+            ready.set(false);
+        }
+        log.info("Cleaning up...");
+    }
+
     void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
         if (accumulator == null) {
             accumulator = new ForwardingObjectiveAccumulator(maxFwd,
@@ -190,6 +206,13 @@
     }
 
     protected void initGroupHander(PipelinerContext context) {
+        // Terminate internal references
+        // We are terminating the references here
+        // because when the device is offline the apps
+        // are still sending flowobjectives
+        if (groupHandler != null) {
+            groupHandler.terminate();
+        }
         groupHandler = new Ofdpa2GroupHandler();
         groupHandler.init(deviceId, context);
     }
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa3Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa3Pipeline.java
index d906953..8f624c4 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa3Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa3Pipeline.java
@@ -83,6 +83,13 @@
 
     @Override
     protected void initGroupHander(PipelinerContext context) {
+        // Terminate internal references
+        // We are terminating the references here
+        // because when the device is offline the apps
+        // are still sending flowobjectives
+        if (groupHandler != null) {
+            groupHandler.terminate();
+        }
         groupHandler = new Ofdpa3GroupHandler();
         groupHandler.init(deviceId, context);
     }
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/OvsOfdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/OvsOfdpa2Pipeline.java
index 97c6143..09a52fb 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/OvsOfdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/OvsOfdpa2Pipeline.java
@@ -32,6 +32,13 @@
 
     @Override
     protected void initGroupHander(PipelinerContext context) {
+        // Terminate internal references
+        // We are terminating the references here
+        // because when the device is offline the apps
+        // are still sending flowobjectives
+        if (groupHandler != null) {
+            groupHandler.terminate();
+        }
         groupHandler = new OvsOfdpa2GroupHandler();
         groupHandler.init(deviceId, context);
     }