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/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);