Improve cleanup after device disconnected event
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.java
index 964ad0ba6b9fb41f3064361ee2f3d13ae05a34a3..f81ae826fe2b162d3fd1334cf59c627112ccb2e5 100644 (file)
@@ -149,14 +149,15 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
         LOG.debug("Final phase of DeviceContextLevelUp for Node: {} ", deviceInfo.getNodeId());
         DeviceContext deviceContext = Preconditions.checkNotNull(deviceContexts.get(deviceInfo));
         deviceContext.onPublished();
+        lifecycleService.registerDeviceRemovedHandler(this);
         lifecycleService.registerService(this.singletonServiceProvider);
     }
 
     @Override
     public ConnectionStatus deviceConnected(@CheckForNull final ConnectionContext connectionContext) throws Exception {
         Preconditions.checkArgument(connectionContext != null);
+        final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
 
-        DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
         /*
          * This part prevent destroy another device context. Throwing here an exception result to propagate close connection
          * in {@link org.opendaylight.openflowplugin.impl.connection.org.opendaylight.openflowplugin.impl.connection.HandshakeContextImpl}
@@ -178,11 +179,12 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
                 connectionContext.getConnectionAdapter().getRemoteAddress(), deviceInfo.getNodeId());
 
         // Add Disconnect handler
-        connectionContext.setDeviceDisconnectedHandler(DeviceManagerImpl.this);
+        connectionContext.setDeviceDisconnectedHandler(this);
+
         // Cache this for clarity
         final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
 
-        //FIXME: as soon as auxiliary connection are fully supported then this is needed only before device context published
+        // FIXME: as soon as auxiliary connection are fully supported then this is needed only before device context published
         connectionAdapter.setPacketInFiltering(true);
 
         final OutboundQueueProvider outboundQueueProvider = new OutboundQueueProviderImpl(deviceInfo.getVersion());
@@ -256,27 +258,8 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
 
     @Override
     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
-
-        LifecycleService lifecycleService = lifecycleServices.remove(deviceInfo);
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Lifecycle service removed for node {}", deviceInfo.getLOGValue());
-        }
-
         updatePacketInRateLimiters();
-        if (Objects.nonNull(lifecycleService)) {
-            try {
-                lifecycleService.close();
-                LOG.debug("Lifecycle service successfully closed for node {}", deviceInfo.getLOGValue());
-            } catch (Exception e) {
-                LOG.warn("Closing lifecycle service for node {} was unsuccessful ", deviceInfo.getLOGValue(), e);
-            }
-        }
-
-        deviceContexts.remove(deviceInfo);
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Device context removed for node {}", deviceInfo.getLOGValue());
-        }
-
+        Optional.ofNullable(lifecycleServices.get(deviceInfo)).ifPresent(OFPContext::close);
     }
 
     @Override
@@ -305,7 +288,7 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
         final DeviceContext deviceCtx = this.deviceContexts.get(deviceInfo);
 
-        if (null == deviceCtx) {
+        if (Objects.isNull(deviceCtx)) {
             LOG.info("DeviceContext for Node {} was not found. Connection is terminated without OFP context suite.", deviceInfo.getLOGValue());
             return;
         }
@@ -315,18 +298,18 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
             return;
         }
 
-        deviceCtx.setState(OFPContext.CONTEXT_STATE.TERMINATION);
+        deviceCtx.close();
 
         if (!connectionContext.equals(deviceCtx.getPrimaryConnectionContext())) {
             LOG.debug("Node {} disconnected, but not primary connection.", connectionContext.getDeviceInfo().getLOGValue());
-            /* Connection is not PrimaryConnection so try to remove from Auxiliary Connections */
+            // Connection is not PrimaryConnection so try to remove from Auxiliary Connections
             deviceCtx.removeAuxiliaryConnectionContext(connectionContext);
         }
-        //TODO: Auxiliary connections supported ?
-            /* Device is disconnected and so we need to close TxManager */
+
+        // TODO: Auxiliary connections supported ?
+        // Device is disconnected and so we need to close TxManager
         final ListenableFuture<Void> future = deviceCtx.shuttingDownDataStoreTransactions();
         Futures.addCallback(future, new FutureCallback<Void>() {
-
             @Override
             public void onSuccess(final Void result) {
                 LOG.debug("TxChainManager for device {} is closed successful.", deviceInfo.getLOGValue());
@@ -340,13 +323,15 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
                 deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
             }
         });
-        /* Add timer for Close TxManager because it could fain ind cluster without notification */
+
+        // Add timer for Close TxManager because it could fail in cluster without notification
         final TimerTask timerTask = timeout -> {
             if (!future.isDone()) {
                 LOG.warn("Shutting down TxChain for node {} not completed during 10 sec. Continue anyway.", deviceInfo.getLOGValue());
                 future.cancel(false);
             }
         };
+
         hashedWheelTimer.newTimeout(timerTask, 10, TimeUnit.SECONDS);
     }
 
@@ -440,15 +425,11 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
         }
     }
 
-    @VisibleForTesting
-    void setDeviceContext(final DeviceInfo deviceInfo, final DeviceContext deviceContext) {
-        this.deviceContexts.putIfAbsent(deviceInfo, deviceContext);
-    }
+    public void onDeviceRemoved(DeviceInfo deviceInfo) {
+        deviceContexts.remove(deviceInfo);
+        LOG.debug("Device context removed for node {}", deviceInfo.getLOGValue());
 
-    @VisibleForTesting
-    int getDeviceContextCount() {
-        return this.deviceContexts.size();
+        lifecycleServices.remove(deviceInfo);
+        LOG.debug("Lifecycle service removed for node {}", deviceInfo.getLOGValue());
     }
-
-
 }