Use bulk-close for internal registrations 27/83627/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 14 Aug 2019 14:49:59 +0000 (16:49 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 14 Aug 2019 15:35:46 +0000 (17:35 +0200)
Rather than using wrapped registrations through the public API,
we really want to first remove all modules and then update the
context (if needed).

JIRA: MDSAL-466
Change-Id: Ib281fff7264b3dd11b222095f8286087cb488002
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit a89133403cedfb24983ef5a9d945ab02232fd1bd)

binding/mdsal-binding-dom-codec-osgi/src/main/java/org/opendaylight/mdsal/binding/dom/codec/osgi/impl/Activator.java
binding/mdsal-binding-dom-codec-osgi/src/main/java/org/opendaylight/mdsal/binding/dom/codec/osgi/impl/ModuleInfoBundleTracker.java
binding/mdsal-binding-dom-codec-osgi/src/main/java/org/opendaylight/mdsal/binding/dom/codec/osgi/impl/OsgiModuleInfoRegistry.java

index a44117f09b1968ff288b0abfa97497b63267a712..14138ee9b688f8e6c153772265aa9e0bba842ab5 100644 (file)
@@ -15,16 +15,21 @@ import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
-import org.osgi.util.tracker.BundleTracker;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class Activator implements BundleActivator {
+    private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
+
     private final List<ServiceRegistration<?>> registrations = new ArrayList<>(2);
 
-    private BundleTracker<?> moduleInfoResolvedBundleTracker = null;
+    private ModuleInfoBundleTracker bundleTracker = null;
     private SimpleBindingRuntimeContextService service = null;
 
     @Override
     public void start(final BundleContext context) {
+        LOG.info("Binding-DOM codec starting");
+
         // XXX: this will use thread-context class loader, which is probably appropriate
         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
 
@@ -33,19 +38,34 @@ public final class Activator implements BundleActivator {
         final OsgiModuleInfoRegistry registry = new OsgiModuleInfoRegistry(moduleInfoBackedContext,
                 moduleInfoBackedContext, service);
 
-        moduleInfoResolvedBundleTracker = new ModuleInfoBundleTracker(context, registry);
-        moduleInfoResolvedBundleTracker.open();
+        LOG.debug("Starting Binding-DOM codec bundle tracker");
+        bundleTracker = new ModuleInfoBundleTracker(context, registry);
+        bundleTracker.open();
 
+        LOG.debug("Starting Binding-DOM runtime context service");
         service.open();
+
+        LOG.debug("Registering Binding-DOM codec services");
         registrations.add(context.registerService(BindingRuntimeContextService.class, service, null));
         registrations.add(context.registerService(ClassLoadingStrategy.class, moduleInfoBackedContext, null));
+
+        LOG.info("Binding-DOM codec started");
     }
 
     @Override
     public void stop(final BundleContext context) {
-        moduleInfoResolvedBundleTracker.close();
-        service.close();
+        LOG.info("Binding-DOM codec stopping");
+
+        LOG.debug("Unregistering Binding-DOM codec services");
         registrations.forEach(ServiceRegistration::unregister);
         registrations.clear();
+
+        LOG.debug("Stopping Binding-DOM codec bundle tracker");
+        bundleTracker.close();
+
+        LOG.debug("Stoping Binding-DOM runtime context service");
+        service.close();
+
+        LOG.info("Binding-DOM codec stopped");
     }
 }
index 0d9df8e4d332e7d11ccb2b5fff428ec52f6fae53..4b39f1072b404d0371612e4eb44e509c3d32f684 100644 (file)
@@ -98,14 +98,14 @@ final class ModuleInfoBundleTracker extends BundleTracker<Collection<ObjectRegis
                 continue;
             }
 
-            registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
+            registrations.add(moduleInfoRegistry.registerInfo(moduleInfo));
         }
 
         if (!deferUpdates) {
             moduleInfoRegistry.updateService();
         }
 
-        LOG.trace("Bundle {} resultend in registrations {}", bundle, registrations);
+        LOG.trace("Bundle {} resulted in registrations {}", bundle, registrations);
         return registrations;
     }
 
@@ -123,11 +123,17 @@ final class ModuleInfoBundleTracker extends BundleTracker<Collection<ObjectRegis
             return;
         }
 
-        for (ObjectRegistration<YangModuleInfo> reg : regs) {
-            try {
-                reg.close();
-            } catch (Exception e) {
-                LOG.warn("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
+        try {
+            regs.forEach(reg -> {
+                try {
+                    reg.close();
+                } catch (Exception e) {
+                    LOG.warn("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
+                }
+            });
+        } finally {
+            if (!deferUpdates) {
+                moduleInfoRegistry.updateService();
             }
         }
     }
index 8fdf9f8c96a48939f9425ce12bf3c6e718dbc7de..f917415e20b22a6b3ce01c5a154f2a739063bf50 100644 (file)
@@ -42,6 +42,11 @@ final class OsgiModuleInfoRegistry implements ModuleInfoRegistry {
         this.runtimeContext = requireNonNull(runtimeContext);
     }
 
+    @Override
+    public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
+        return new ObjectRegistrationWrapper(registerInfo(yangModuleInfo));
+    }
+
     @SuppressWarnings("checkstyle:illegalCatch")
     synchronized void updateService() {
         final SchemaContext context;
@@ -61,9 +66,8 @@ final class OsgiModuleInfoRegistry implements ModuleInfoRegistry {
         }
     }
 
-    @Override
-    public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
-        return new ObjectRegistrationWrapper(moduleInfoRegistry.registerModuleInfo(yangModuleInfo));
+    ObjectRegistration<YangModuleInfo> registerInfo(final YangModuleInfo yangModuleInfo) {
+        return moduleInfoRegistry.registerModuleInfo(yangModuleInfo);
     }
 
     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {