Bug 3135 - Fixed support for InterestListener
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMNotificationRouter.java
index e814dd2..d623c3e 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.controller.md.sal.dom.broker.impl;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMultimap;
 import com.google.common.collect.ImmutableMultimap.Builder;
 import com.google.common.collect.Multimap;
@@ -23,6 +24,8 @@ import com.lmax.disruptor.dsl.Disruptor;
 import com.lmax.disruptor.dsl.ProducerType;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.List;
+import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
@@ -30,9 +33,14 @@ import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
+import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener;
+import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.util.ListenerRegistry;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
@@ -48,7 +56,10 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
  * are realized using the Disruptor's native operations. The bounded-blocking {@link #offerNotification(DOMNotification, long, TimeUnit)}
  * is realized by arming a background wakeup interrupt.
  */
-public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService, DOMNotificationService {
+public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
+        DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
     private static final ListenableFuture<Void> NO_LISTENERS = Futures.immediateFuture(null);
     private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(1L, 30L, TimeUnit.MILLISECONDS);
     private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS = new EventHandler<DOMNotificationRouterEvent>() {
@@ -68,6 +79,7 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
     private final Disruptor<DOMNotificationRouterEvent> disruptor;
     private final ExecutorService executor;
     private volatile Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> listeners = ImmutableMultimap.of();
+    private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners = ListenerRegistry.create();
 
     private DOMNotificationRouter(final ExecutorService executor, final Disruptor<DOMNotificationRouterEvent> disruptor) {
         this.executor = Preconditions.checkNotNull(executor);
@@ -94,12 +106,12 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
                 final ListenerRegistration<T> me = this;
 
                 synchronized (DOMNotificationRouter.this) {
-                    listeners = ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
+                    replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
                         @Override
                         public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
                             return input != me;
                         }
-                    }));
+                    })));
                 }
             }
         };
@@ -108,11 +120,11 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
             final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b = ImmutableMultimap.builder();
             b.putAll(listeners);
 
-            for (SchemaPath t : types) {
+            for (final SchemaPath t : types) {
                 b.put(t, reg);
             }
 
-            listeners = b.build();
+            replaceListeners(b.build());
         }
 
         return reg;
@@ -123,6 +135,48 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
         return registerNotificationListener(listener, Arrays.asList(types));
     }
 
+    /**
+     * Swaps registered listeners and triggers notification update
+     *
+     * @param newListeners
+     */
+    private void replaceListeners(
+            final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
+        listeners = newListeners;
+        notifyListenerTypesChanged(newListeners.keySet());
+    }
+
+    private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
+        final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =ImmutableList.copyOf(subscriptionListeners.getListeners());
+        executor.submit(new Runnable() {
+
+            @Override
+            public void run() {
+                for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
+                    try {
+                        subListener.getInstance().onSubscriptionChanged(typesAfter);
+                    } catch (final Exception e) {
+                        LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
+                    }
+                }
+            }
+        });
+    }
+
+    @Override
+    public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
+            final L listener) {
+        final Set<SchemaPath> initialTypes = listeners.keySet();
+        executor.submit(new Runnable() {
+
+            @Override
+            public void run() {
+                listener.onSubscriptionChanged(initialTypes);
+            }
+        });
+        return subscriptionListeners.registerWithType(listener);
+    }
+
     private ListenableFuture<Void> publish(final long seq, final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
         final DOMNotificationRouterEvent event = disruptor.get(seq);
         final ListenableFuture<Void> future = event.initialize(notification, subscribers);
@@ -145,7 +199,7 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
         final long seq;
         try {
              seq = disruptor.getRingBuffer().tryNext();
-        } catch (InsufficientCapacityException e) {
+        } catch (final InsufficientCapacityException e) {
             return DOMNotificationPublishService.REJECTED;
         }
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.