Fix ModuleFactory not found errors
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ConfigManagerActivator.java
index 7966b8983aab814ca310be6d660b5f8e1ba4fc1c..1f538e5c6dc848bb5f9ef334d8d12df0d50807c9 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,10 +24,14 @@ 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.concepts.ObjectRegistration;
 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
 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.util.tracker.BundleTracker;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -49,7 +54,7 @@ public class ConfigManagerActivator implements BundleActivator {
             RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
                     moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, bindingContextProvider, context);
 
-            ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
+            ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(context, moduleInfoRegistryWrapper);
 
             // start config registry
             BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
@@ -60,13 +65,25 @@ public class ConfigManagerActivator implements BundleActivator {
             // track bundles containing factories
             BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
                     configRegistry);
-            ModuleFactoryBundleTracker primaryModuleFactoryBundleTracker = new ModuleFactoryBundleTracker(
+            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<?> bundleTracker = new ExtensibleBundleTracker<>(context,
-                    primaryModuleFactoryBundleTracker, moduleInfoBundleTracker);
-            bundleTracker.open();
+            moduleFactoryBundleTracker.open();
 
             // Wrap config registry with JMX notification publishing adapter
             final JMXNotifierConfigRegistry notifyingConfigRegistry =
@@ -97,11 +114,17 @@ public class ConfigManagerActivator implements BundleActivator {
                     blankTransactionServiceTracker);
             serviceTracker.open();
 
-            List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg, configRegistry, wrap(bundleTracker),
+            List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg, configRegistry,
+                    wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker,
                     configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications, wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry);
             autoCloseable = OsgiRegistrationUtil.aggregate(list);
         } catch(Exception e) {
             LOG.warn("Error starting config manager", e);
+        } catch(Error e) {
+            // Log JVM Error and re-throw. The OSGi container may silently fail the bundle and not always log
+            // the exception. This has been seen on initial feature install.
+            LOG.error("Error starting config manager", e);
+            throw e;
         }
     }