Merge "Bug 7846: ensure switch reconnection succeed in case of connection errors...
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsManagerImpl.java
index 448205d3eba4166c094cc38d7a9d6f6bb080dd87..81628c4f8eba8699b040ec5f6d9853a95c21b681 100644 (file)
@@ -104,11 +104,17 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
                         converterExecutor,
                         this);
 
+        // Clean up stale context if present
+        StatisticsContext staleContext = contexts.remove(deviceInfo);
+        if (staleContext != null){
+            LOG.warn("Previous statistics context for node {} was not closed, closing the context.", deviceInfo);
+            staleContext.close();
+        }
+
         Verify.verify(
                 contexts.putIfAbsent(deviceInfo, statisticsContext) == null,
                 "StatisticsCtx still not closed for Node {}", deviceInfo.getLOGValue()
         );
-
         lifecycleService.setStatContext(statisticsContext);
         lifecycleService.registerDeviceRemovedHandler(this);
         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);