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/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java b/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java
index c9ea8a9..22e89d8 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java
@@ -90,4 +90,11 @@
         return true;
     }
 
+    /**
+     * Clean up internal state of the pipeliner.
+     * Implementation is pipeliner specific.
+     */
+    default void cleanUp() {
+    }
+
 }
diff --git a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
index 84820cf..3c5c571 100644
--- a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
+++ b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
@@ -496,7 +496,10 @@
     private void invalidatePipeliner(DeviceId id) {
         log.info("Invalidating cached pipeline behaviour for {}", id);
         driverHandlers.remove(id);
-        pipeliners.remove(id);
+        Pipeliner pipeliner = pipeliners.remove(id);
+        if (pipeliner != null) {
+            pipeliner.cleanUp();
+        }
         if (deviceService.isAvailable(id)) {
             getAndInitDevicePipeliner(id);
         }
@@ -518,6 +521,7 @@
                         getAndInitDevicePipeliner(event.subject().id());
                       } else {
                         log.debug("Device is no longer available {}", event.subject().id());
+                        getDevicePipeliner(event.subject().id()).cleanUp();
                       }
                     });
                     break;
@@ -534,8 +538,11 @@
                     // before removing device, especially if they intend to
                     // replace driver/pipeliner assigned to the device.
                     devEventExecutor.execute(() -> {
-                      driverHandlers.remove(event.subject().id());
-                      pipeliners.remove(event.subject().id());
+                        driverHandlers.remove(event.subject().id());
+                        Pipeliner pipeliner = pipeliners.remove(event.subject().id());
+                        if (pipeliner != null) {
+                            pipeliner.cleanUp();
+                        }
                     });
                     break;
                 case DEVICE_SUSPENDED: