Fix OSGiDOMSchemaService lifecycle
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / OSGiDOMSchemaService.java
index 28d76bd0eca41b04535aa8dce89008e8d141cd75..267c9de96aaf6293476d64f56627b17d7858d83e 100644 (file)
@@ -9,22 +9,25 @@ package org.opendaylight.mdsal.dom.schema.osgi.impl;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.atomic.AtomicReference;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.binding.runtime.api.ModuleInfoSnapshot;
+import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
 import org.opendaylight.mdsal.dom.spi.AbstractDOMSchemaService;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.osgi.service.component.ComponentFactory;
 import org.osgi.service.component.ComponentInstance;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
-import org.osgi.service.component.annotations.FieldOption;
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.osgi.service.component.annotations.ReferencePolicy;
@@ -36,19 +39,20 @@ import org.slf4j.LoggerFactory;
  * OSGi Service Registry-backed implementation of {@link DOMSchemaService}.
  */
 @Component(service = DOMSchemaService.class, immediate = true)
-public final class OSGiDOMSchemaService extends AbstractDOMSchemaService {
+public final class OSGiDOMSchemaService extends AbstractDOMSchemaService.WithYangTextSources {
     private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class);
 
     @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")")
     ComponentFactory listenerFactory = null;
 
     private final List<EffectiveModelContextListener> listeners = new CopyOnWriteArrayList<>();
+    private final AtomicReference<ModuleInfoSnapshot> currentSnapshot = new AtomicReference<>();
 
-    private volatile ModuleInfoSnapshot currentSnapshot;
+    private boolean deactivated;
 
     @Override
-    public EffectiveModelContext getGlobalContext() {
-        return currentSnapshot.getEffectiveModelContext();
+    public @NonNull EffectiveModelContext getGlobalContext() {
+        return currentSnapshot.get().getEffectiveModelContext();
     }
 
     @Override
@@ -57,15 +61,29 @@ public final class OSGiDOMSchemaService extends AbstractDOMSchemaService {
         return registerListener(requireNonNull(listener));
     }
 
-    @Reference(fieldOption = FieldOption.REPLACE)
+    @Override
+    public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
+        return currentSnapshot.get().getSource(sourceIdentifier);
+    }
+
+    @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
     void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
-        LOG.trace("Updating context to generation {}", newContext.getGeneration());
+        LOG.info("Updating context to generation {}", newContext.getGeneration());
         final ModuleInfoSnapshot snapshot = newContext.getService();
         final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
-        currentSnapshot = snapshot;
+        final ModuleInfoSnapshot previous = currentSnapshot.getAndSet(snapshot);
+        LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
+
         listeners.forEach(listener -> notifyListener(ctx, listener));
     }
 
+    void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
+        final ModuleInfoSnapshot snapshot = oldContext.getService();
+        if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
+            LOG.info("Lost final generation {}", oldContext.getGeneration());
+        }
+    }
+
     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
             policyOption = ReferencePolicyOption.GREEDY)
     void addListener(final EffectiveModelContextListener listener) {
@@ -86,9 +104,9 @@ public final class OSGiDOMSchemaService extends AbstractDOMSchemaService {
     }
 
     @Deactivate
-    @SuppressWarnings("static-method")
     void deactivate() {
         LOG.info("DOM Schema services deactivated");
+        deactivated = true;
     }
 
     private @NonNull ListenerRegistration<EffectiveModelContextListener> registerListener(
@@ -108,7 +126,7 @@ public final class OSGiDOMSchemaService extends AbstractDOMSchemaService {
     }
 
     @SuppressWarnings("checkstyle:illegalCatch")
-    private static void notifyListener(final EffectiveModelContext context,
+    private static void notifyListener(final @NonNull EffectiveModelContext context,
             final EffectiveModelContextListener listener) {
         try {
             listener.onModelContextUpdated(context);