Add single layer deserialization support
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.java
index 964ad0ba6b9fb41f3064361ee2f3d13ae05a34a3..df49b1443769a6145fa754673ab673114b22c2b1 100644 (file)
@@ -51,9 +51,10 @@ import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.Messa
 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
 import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
+import org.opendaylight.openflowplugin.impl.device.initialization.DeviceInitializerProvider;
 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
 import org.opendaylight.openflowplugin.impl.lifecycle.LifecycleServiceImpl;
-import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl;
+import org.opendaylight.openflowplugin.impl.services.sal.SalRoleServiceImpl;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
@@ -73,11 +74,12 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
 
     private final long globalNotificationQuota;
     private final boolean switchFeaturesMandatory;
-    private boolean isNotificationFlowRemovedOff;
+    private boolean isFlowRemovedNotificationOn;
     private boolean skipTableFeatures;
     private static final int SPY_RATE = 10;
 
     private final DataBroker dataBroker;
+    private final DeviceInitializerProvider deviceInitializerProvider;
     private final ConvertorExecutor convertorExecutor;
     private TranslatorLibrary translatorLibrary;
     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
@@ -95,6 +97,7 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
     private final NotificationPublishService notificationPublishService;
     private final MessageSpy messageSpy;
     private final HashedWheelTimer hashedWheelTimer;
+    private final boolean useSingleLayerSerialization;
 
     public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
                              final long globalNotificationQuota,
@@ -102,14 +105,17 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
                              final long barrierInterval,
                              final int barrierCountLimit,
                              final MessageSpy messageSpy,
-                             final boolean isNotificationFlowRemovedOff,
+                             final boolean isFlowRemovedNotificationOn,
                              final ClusterSingletonServiceProvider singletonServiceProvider,
                              final NotificationPublishService notificationPublishService,
                              final HashedWheelTimer hashedWheelTimer,
                              final ConvertorExecutor convertorExecutor,
-                             final boolean skipTableFeatures) {
+                             final boolean skipTableFeatures,
+                             final boolean useSingleLayerSerialization,
+                             final DeviceInitializerProvider deviceInitializerProvider) {
 
         this.dataBroker = dataBroker;
+        this.deviceInitializerProvider = deviceInitializerProvider;
 
         /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
@@ -125,7 +131,7 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
 
         this.switchFeaturesMandatory = switchFeaturesMandatory;
         this.globalNotificationQuota = globalNotificationQuota;
-        this.isNotificationFlowRemovedOff = isNotificationFlowRemovedOff;
+        this.isFlowRemovedNotificationOn = isFlowRemovedNotificationOn;
         this.skipTableFeatures = skipTableFeatures;
         this.convertorExecutor = convertorExecutor;
         this.hashedWheelTimer = hashedWheelTimer;
@@ -135,6 +141,7 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
         this.singletonServiceProvider = singletonServiceProvider;
         this.notificationPublishService = notificationPublishService;
         this.messageSpy = messageSpy;
+        this.useSingleLayerSerialization = useSingleLayerSerialization;
     }
 
 
@@ -149,14 +156,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 +186,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());
@@ -193,7 +202,6 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
         connectionContext.setOutboundQueueHandleRegistration(outboundQueueHandlerRegistration);
 
         final LifecycleService lifecycleService = new LifecycleServiceImpl();
-
         final DeviceContext deviceContext = new DeviceContextImpl(
                 connectionContext,
                 dataBroker,
@@ -203,7 +211,9 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
                 convertorExecutor,
                 skipTableFeatures,
                 hashedWheelTimer,
-                this);
+                this,
+                useSingleLayerSerialization,
+                deviceInitializerProvider);
 
         deviceContext.setSalRoleService(new SalRoleServiceImpl(deviceContext, deviceContext));
         deviceContexts.put(deviceInfo, deviceContext);
@@ -256,27 +266,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 +296,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 +306,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 +331,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);
     }
 
@@ -356,13 +349,13 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
     }
 
     @Override
-    public void setIsNotificationFlowRemovedOff(boolean isNotificationFlowRemovedOff) {
-        this.isNotificationFlowRemovedOff = isNotificationFlowRemovedOff;
+    public void setFlowRemovedNotificationOn(boolean isNotificationFlowRemovedOff) {
+        this.isFlowRemovedNotificationOn = isNotificationFlowRemovedOff;
     }
 
     @Override
-    public boolean getIsNotificationFlowRemovedOff() {
-        return this.isNotificationFlowRemovedOff;
+    public boolean isFlowRemovedNotificationOn() {
+        return this.isFlowRemovedNotificationOn;
     }
 
 
@@ -440,15 +433,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());
     }
-
-
 }