Modify ModuleInfoBundleTracker to track RESOLVED bundles (round 2)
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / ModuleInfoBundleTracker.java
index 56535e797b46bb7b6be4005f2fe8705c83d51fa3..1006513fe9bcf7efeff49074f57ac8f4256b8407 100644 (file)
@@ -16,11 +16,12 @@ import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import org.opendaylight.yangtools.concepts.ObjectRegistration;
-import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
+import org.osgi.util.tracker.BundleTracker;
 import org.osgi.util.tracker.BundleTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -28,17 +29,39 @@ import org.slf4j.LoggerFactory;
 /**
  * Tracks bundles and attempts to retrieve YangModuleInfo, which is then fed into ModuleInfoRegistry
  */
-public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
+public final class ModuleInfoBundleTracker implements AutoCloseable,
+        BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
 
     private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
 
     public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
 
 
-    private final ModuleInfoRegistry moduleInfoRegistry;
+    private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
+    private final BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
+    private boolean starting;
 
-    public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
+    public ModuleInfoBundleTracker(BundleContext context, RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
         this.moduleInfoRegistry = moduleInfoRegistry;
+        bundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING |
+                Bundle.STOPPING | Bundle.ACTIVE, this);
+    }
+
+    public void open() {
+        LOG.debug("ModuleInfoBundleTracker open starting");
+
+        starting = true;
+        bundleTracker.open();
+
+        starting = false;
+        moduleInfoRegistry.updateService();
+
+        LOG.debug("ModuleInfoBundleTracker open complete");
+    }
+
+    @Override
+    public void close() {
+        bundleTracker.close();
     }
 
     @Override
@@ -56,9 +79,14 @@ public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Co
                 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
                 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
             }
+
+            if(!starting) {
+                moduleInfoRegistry.updateService();
+            }
         } catch (IOException e) {
-            LOG.error("Error while reading {}", resource, e);
-            throw new RuntimeException(e);
+            LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
+        } catch (RuntimeException e) {
+            LOG.error("Failed to process {} for bundle {}", resource, bundle, e);
         }
 
         LOG.trace("Got following registrations {}", registrations);
@@ -79,7 +107,7 @@ public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Co
             try {
                 reg.close();
             } catch (Exception e) {
-                throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
+                LOG.error("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
             }
         }
     }
@@ -104,13 +132,11 @@ public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Co
                     moduleInfoClass, bundle, e);
             throw new IllegalStateException(errorMessage, e);
         }
+
         try{
             return instance.getModuleInfo();
-        } catch (NoClassDefFoundError e) {
-
-
-            LOG.error("Error while executing getModuleInfo on {}", instance, e);
-            throw e;
+        } catch (NoClassDefFoundError | ExceptionInInitializerError e) {
+            throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e);
         }
     }