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/Ofdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
index d8b1f06..a891e40 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
@@ -155,7 +155,11 @@
 
     @Override
     public void init(DeviceId deviceId, PipelinerContext context) {
-        if (!ready.getAndSet(true)) {
+        synchronized (this) {
+            if (isReady()) {
+                return;
+            }
+
             this.deviceId = deviceId;
 
             serviceDirectory = context.directory();
@@ -175,6 +179,7 @@
             initGroupHander(context);
 
             initializePipeline();
+            ready.set(true);
         }
     }
 
@@ -183,6 +188,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,
@@ -197,6 +213,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);
     }