Reduce ObjectRegistration use
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / RegularYangModuleInfoRegistry.java
index 244fddd64145052d3d5925e6b2cf237fd46c5092..d1e3717245148f2e062390afd1030c5da36068b9 100644 (file)
@@ -9,13 +9,18 @@ package org.opendaylight.mdsal.dom.schema.osgi.impl;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.collect.ImmutableList;
 import java.util.List;
 import java.util.NoSuchElementException;
 import org.checkerframework.checker.lock.qual.GuardedBy;
 import org.checkerframework.checker.lock.qual.Holding;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
 import org.opendaylight.mdsal.binding.runtime.spi.ModuleInfoSnapshotResolver;
-import org.opendaylight.yangtools.concepts.ObjectRegistration;
+import org.opendaylight.yangtools.concepts.AbstractRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.DataRoot;
+import org.opendaylight.yangtools.yang.binding.YangFeatureProvider;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
 import org.osgi.service.component.ComponentFactory;
@@ -29,11 +34,11 @@ import org.slf4j.LoggerFactory;
 final class RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
     private static final Logger LOG = LoggerFactory.getLogger(RegularYangModuleInfoRegistry.class);
 
-    private final ComponentFactory contextFactory;
-    private final ModuleInfoSnapshotResolver moduleInfoRegistry;
+    private final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory;
+    private final ModuleInfoSnapshotResolver resolver;
 
     @GuardedBy("this")
-    private ComponentInstance currentInstance;
+    private ComponentInstance<OSGiModuleInfoSnapshotImpl> currentInstance;
     @GuardedBy("this")
     private ModuleInfoSnapshot currentSnapshot;
     @GuardedBy("this")
@@ -41,9 +46,10 @@ final class RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
 
     private volatile boolean ignoreScanner = true;
 
-    RegularYangModuleInfoRegistry(final ComponentFactory contextFactory, final YangParserFactory factory) {
+    RegularYangModuleInfoRegistry(final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory,
+            final YangParserFactory factory) {
         this.contextFactory = requireNonNull(contextFactory);
-        moduleInfoRegistry = new ModuleInfoSnapshotResolver("binding-dom-codec", factory);
+        resolver = new ModuleInfoSnapshotResolver("dom-schema-osgi", factory);
     }
 
     // Invocation from scanner, we may want to ignore this in order to not process partial updates
@@ -77,15 +83,31 @@ final class RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
     }
 
     @Override
-    List<ObjectRegistration<YangModuleInfo>> registerInfos(final List<YangModuleInfo> infos) {
-        return moduleInfoRegistry.registerModuleInfos(infos);
+    Registration registerBundle(final List<YangModuleInfo> moduleInfos,
+            final List<YangFeatureProvider<?>> featureProviders) {
+        final var infoRegs = resolver.registerModuleInfos(moduleInfos);
+        final var featureRegs = featureProviders.stream()
+            .map(provider -> {
+                @SuppressWarnings("unchecked")
+                final var raw = (YangFeatureProvider<@NonNull DataRoot>) provider;
+                return resolver.registerModuleFeatures(raw.boundModule(), raw.supportedFeatures());
+            })
+            .collect(ImmutableList.toImmutableList());
+
+        return new AbstractRegistration() {
+            @Override
+            protected void removeRegistration() {
+                featureRegs.forEach(Registration::close);
+                infoRegs.forEach(Registration::close);
+            }
+        };
     }
 
     @Holding("this")
     private void updateService() {
         final ModuleInfoSnapshot newSnapshot;
         try {
-            newSnapshot = moduleInfoRegistry.takeSnapshot();
+            newSnapshot = resolver.takeSnapshot();
         } catch (NoSuchElementException e) {
             LOG.debug("No snapshot available", e);
             return;
@@ -95,8 +117,7 @@ final class RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
             return;
         }
 
-
-        final ComponentInstance newInstance = contextFactory.newInstance(
+        final ComponentInstance<OSGiModuleInfoSnapshotImpl> newInstance = contextFactory.newInstance(
             OSGiModuleInfoSnapshotImpl.props(nextGeneration(), newSnapshot));
         if (currentInstance != null) {
             currentInstance.dispose();