Merge "Inventory status check"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImpl.java
index a37ce0267b487e35b9fd2fe1b1dbcd8f10ab2799..8a61ca1a5c954871e3fa388568ff56e0fbe2281d 100644 (file)
@@ -19,7 +19,6 @@ import io.netty.util.Timer;
 import java.lang.management.ManagementFactory;
 import java.util.Collection;
 import java.util.List;
-import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
@@ -27,7 +26,6 @@ import java.util.concurrent.SynchronousQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
-import javax.annotation.Nonnull;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
@@ -128,6 +126,7 @@ public class OpenFlowPluginProviderImpl implements
     private ListeningExecutorService executorService;
     private ContextChainHolderImpl contextChainHolder;
     private final OpenflowDiagStatusProvider openflowDiagStatusProvider;
+    private final SystemReadyMonitor systemReadyMonitor;
     private final SettableFuture<Void> fullyStarted = SettableFuture.create();
     private static final String OPENFLOW_SERVICE_NAME = "OPENFLOW";
 
@@ -158,8 +157,7 @@ public class OpenFlowPluginProviderImpl implements
         config = new OpenFlowProviderConfigImpl(configurationService);
         this.mastershipChangeServiceManager = mastershipChangeServiceManager;
         this.openflowDiagStatusProvider = openflowDiagStatusProvider;
-        systemReadyMonitor.registerListener(this);
-        LOG.info("registered onSystemBootReady() listener for deferred startSwitchConnections()");
+        this.systemReadyMonitor = systemReadyMonitor;
     }
 
     @Override
@@ -180,14 +178,14 @@ public class OpenFlowPluginProviderImpl implements
             return switchConnectionProvider.startup();
         }).collect(Collectors.toSet())), new FutureCallback<List<Boolean>>() {
             @Override
-            public void onSuccess(@Nonnull final List<Boolean> result) {
+            public void onSuccess(final List<Boolean> result) {
                 LOG.info("All switchConnectionProviders are up and running ({}).", result.size());
                 openflowDiagStatusProvider.reportStatus(OPENFLOW_SERVICE_NAME, ServiceState.OPERATIONAL);
                 fullyStarted.set(null);
             }
 
             @Override
-            public void onFailure(@Nonnull final Throwable throwable) {
+            public void onFailure(final Throwable throwable) {
                 LOG.warn("Some switchConnectionProviders failed to start.", throwable);
                 openflowDiagStatusProvider.reportStatus(OPENFLOW_SERVICE_NAME, throwable);
                 fullyStarted.setException(throwable);
@@ -214,12 +212,12 @@ public class OpenFlowPluginProviderImpl implements
 
         Futures.addCallback(listListenableFuture, new FutureCallback<List<Boolean>>() {
             @Override
-            public void onSuccess(@Nonnull final List<Boolean> result) {
+            public void onSuccess(final List<Boolean> result) {
                 LOG.info("All switchConnectionProviders were successfully shut down ({}).", result.size());
             }
 
             @Override
-            public void onFailure(@Nonnull final Throwable throwable) {
+            public void onFailure(final Throwable throwable) {
                 LOG.warn("Some switchConnectionProviders failed to shutdown.", throwable);
             }
         }, MoreExecutors.directExecutor());
@@ -240,9 +238,9 @@ public class OpenFlowPluginProviderImpl implements
         // constructed threads when they are available.
         // Threads that have not been used for x seconds are terminated and removed from the cache.
         executorService = MoreExecutors.listeningDecorator(new ThreadPoolLoggingExecutor(
-                config.getThreadPoolMinThreads(),
-                config.getThreadPoolMaxThreads().getValue(),
-                config.getThreadPoolTimeout(),
+                config.getThreadPoolMinThreads().toJava(),
+                config.getThreadPoolMaxThreads().getValue().toJava(),
+                config.getThreadPoolTimeout().toJava(),
                 TimeUnit.SECONDS, new SynchronousQueue<>(), POOL_NAME));
 
         deviceManager = new DeviceManagerImpl(
@@ -252,7 +250,8 @@ public class OpenFlowPluginProviderImpl implements
                 notificationPublishService,
                 hashedWheelTimer,
                 convertorManager,
-                deviceInitializerProvider);
+                deviceInitializerProvider,
+                executorService);
 
         TranslatorLibraryUtil.injectBasicTranslatorLibrary(deviceManager, convertorManager);
         ((ExtensionConverterProviderKeeper) deviceManager).setExtensionConverterProvider(extensionConverterManager);
@@ -270,25 +269,28 @@ public class OpenFlowPluginProviderImpl implements
                 convertorManager,
                 executorService);
 
-        roleManager = new RoleManagerImpl(hashedWheelTimer, config);
+        roleManager = new RoleManagerImpl(hashedWheelTimer, config, executorService);
 
         contextChainHolder = new ContextChainHolderImpl(
                 executorService,
                 singletonServicesProvider,
                 entityOwnershipService,
-                mastershipChangeServiceManager);
+                mastershipChangeServiceManager,
+                config);
 
         contextChainHolder.addManager(deviceManager);
         contextChainHolder.addManager(statisticsManager);
         contextChainHolder.addManager(rpcManager);
         contextChainHolder.addManager(roleManager);
 
-        connectionManager = new ConnectionManagerImpl(config, executorService);
+        connectionManager = new ConnectionManagerImpl(config, executorService, dataBroker, notificationPublishService);
         connectionManager.setDeviceConnectedHandler(contextChainHolder);
         connectionManager.setDeviceDisconnectedHandler(contextChainHolder);
 
         deviceManager.setContextChainHolder(contextChainHolder);
         deviceManager.initialize();
+        systemReadyMonitor.registerListener(this);
+        LOG.info("registered onSystemBootReady() listener for deferred startSwitchConnections()");
     }
 
     @Override
@@ -298,6 +300,7 @@ public class OpenFlowPluginProviderImpl implements
 
     @Override
     @PreDestroy
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void close() {
         try {
             shutdownSwitchConnections().get(10, TimeUnit.SECONDS);
@@ -306,6 +309,7 @@ public class OpenFlowPluginProviderImpl implements
         }
 
         gracefulShutdown(contextChainHolder);
+        gracefulShutdown(connectionManager);
         gracefulShutdown(deviceManager);
         gracefulShutdown(rpcManager);
         gracefulShutdown(statisticsManager);
@@ -314,42 +318,44 @@ public class OpenFlowPluginProviderImpl implements
         gracefulShutdown(hashedWheelTimer);
         unregisterMXBean(MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME);
         openflowDiagStatusProvider.reportStatus(ServiceState.UNREGISTERED);
+        try {
+            if (connectionManager != null) {
+                connectionManager.close();
+                connectionManager = null;
+            }
+        } catch (Exception e) {
+            LOG.error("Failed to close ConnectionManager", e);
+        }
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     private static void gracefulShutdown(final AutoCloseable closeable) {
-        if (Objects.isNull(closeable)) {
-            return;
-        }
-
-        try {
-            closeable.close();
-        } catch (Exception e) {
-            LOG.warn("Failed to shutdown {} gracefully.", closeable);
+        if (closeable != null) {
+            try {
+                closeable.close();
+            } catch (Exception e) {
+                LOG.warn("Failed to shutdown {} gracefully.", closeable);
+            }
         }
     }
 
     private static void gracefulShutdown(final Timer timer) {
-        if (Objects.isNull(timer)) {
-            return;
-        }
-
-        try {
-            timer.stop();
-        } catch (IllegalStateException e) {
-            LOG.warn("Failed to shutdown {} gracefully.", timer);
+        if (timer != null) {
+            try {
+                timer.stop();
+            } catch (IllegalStateException e) {
+                LOG.warn("Failed to shutdown {} gracefully.", timer);
+            }
         }
     }
 
     private static void gracefulShutdown(final ExecutorService executorService) {
-        if (Objects.isNull(executorService)) {
-            return;
-        }
-
-        try {
-            executorService.shutdownNow();
-        } catch (SecurityException e) {
-            LOG.warn("Failed to shutdown {} gracefully.", executorService);
+        if (executorService != null) {
+            try {
+                executorService.shutdownNow();
+            } catch (SecurityException e) {
+                LOG.warn("Failed to shutdown {} gracefully.", executorService);
+            }
         }
     }
 
@@ -362,7 +368,7 @@ public class OpenFlowPluginProviderImpl implements
                 | NotCompliantMBeanException
                 | MBeanRegistrationException
                 | InstanceAlreadyExistsException e) {
-            LOG.warn("Error registering MBean {}", e);
+            LOG.warn("Error registering MBean {}", beanName, e);
         }
     }
 
@@ -374,7 +380,7 @@ public class OpenFlowPluginProviderImpl implements
         } catch (InstanceNotFoundException
                 | MBeanRegistrationException
                 | MalformedObjectNameException e) {
-            LOG.warn("Error unregistering MBean {}", e);
+            LOG.warn("Error unregistering MBean {}", beanName, e);
         }
     }
 }