Add blueprint wiring to sal-binding-broker-impl
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ConfigManagerActivator.java
index c15c6c82203458e0284ca30e088e1f029d76faa6..ec27bc724fda542578113ecdd19074791ea30c91 100644 (file)
@@ -11,6 +11,7 @@ import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrat
 import static org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil.wrap;
 import java.lang.management.ManagementFactory;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
 import javax.management.InstanceAlreadyExistsException;
 import javax.management.MBeanServer;
@@ -23,22 +24,32 @@ import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBu
 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
 import org.opendaylight.controller.config.spi.ModuleFactory;
-import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
+import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
+import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.SynchronousBundleListener;
+import org.osgi.util.tracker.BundleTracker;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class ConfigManagerActivator implements BundleActivator {
+public class ConfigManagerActivator implements BundleActivator, SynchronousBundleListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(ConfigManagerActivator.class);
 
+    private static final long SYSTEM_BUNDLE_ID = 0;
+
     private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
 
     private AutoCloseable autoCloseable;
 
+    private ConfigRegistryImpl configRegistry;
+
     @Override
     public void start(final BundleContext context) {
         try {
@@ -54,7 +65,7 @@ public class ConfigManagerActivator implements BundleActivator {
             // start config registry
             BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
                     context);
-            ConfigRegistryImpl configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
+            configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
                     bindingContextProvider);
 
             // track bundles containing factories
@@ -63,19 +74,29 @@ public class ConfigManagerActivator implements BundleActivator {
             ModuleFactoryBundleTracker moduleFactoryTracker = new ModuleFactoryBundleTracker(
                     blankTransactionServiceTracker);
 
+            boolean scanResolvedBundlesForModuleInfo = true;
+            BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker = null;
+            ExtensibleBundleTracker<?> moduleFactoryBundleTracker;
+            if(scanResolvedBundlesForModuleInfo) {
+                moduleInfoResolvedBundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING |
+                            Bundle.STOPPING | Bundle.ACTIVE, moduleInfoBundleTracker);
+                moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context, moduleFactoryTracker);
+            } else {
+                moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context,
+                        moduleFactoryTracker, moduleInfoBundleTracker);
+            }
+
+            moduleInfoBundleTracker.open(moduleInfoResolvedBundleTracker);
+
             // start extensible tracker
-            ExtensibleBundleTracker<?> moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context,
-                    moduleFactoryTracker);
             moduleFactoryBundleTracker.open();
 
-            moduleInfoBundleTracker.open();
-
             // Wrap config registry with JMX notification publishing adapter
             final JMXNotifierConfigRegistry notifyingConfigRegistry =
                     new JMXNotifierConfigRegistry(configRegistry, configMBeanServer);
 
             // register config registry to OSGi
-            AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, GeneratedClassLoadingStrategy.class);
+            AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, ClassLoadingStrategy.class);
             AutoCloseable configRegReg = registerService(context, notifyingConfigRegistry, ConfigRegistry.class);
 
             // register config registry to jmx
@@ -83,6 +104,7 @@ public class ConfigManagerActivator implements BundleActivator {
             try {
                 configRegistryJMXRegistrator.registerToJMXNoNotifications(configRegistry);
             } catch (InstanceAlreadyExistsException e) {
+                configRegistryJMXRegistrator.close();
                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
             }
 
@@ -91,6 +113,8 @@ public class ConfigManagerActivator implements BundleActivator {
             try {
                 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
             } catch (InstanceAlreadyExistsException e) {
+                configRegistryJMXRegistrator.close();
+                configRegistryJMXRegistratorWithNotifications.close();
                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
             }
 
@@ -99,10 +123,12 @@ public class ConfigManagerActivator implements BundleActivator {
                     blankTransactionServiceTracker);
             serviceTracker.open();
 
-            List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg, configRegistry,
+            List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg,
                     wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker,
                     configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications, wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry);
             autoCloseable = OsgiRegistrationUtil.aggregate(list);
+
+            context.addBundleListener(this);
         } catch(Exception e) {
             LOG.warn("Error starting config manager", e);
         } catch(Error e) {
@@ -115,6 +141,20 @@ public class ConfigManagerActivator implements BundleActivator {
 
     @Override
     public void stop(final BundleContext context) throws Exception {
+        context.removeBundleListener(this);
         autoCloseable.close();
     }
+
+    @Override
+    public void bundleChanged(BundleEvent event) {
+        if(configRegistry == null) {
+            return;
+        }
+
+        // If the system bundle (id 0) is stopping close the ConfigRegistry so it destroys all modules. On
+        // shutdown the system bundle is stopped first.
+        if(event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) {
+            configRegistry.close();
+        }
+    }
 }