checkStyleViolationSeverity=error implemented for mdsal-dom-broker
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / DOMNotificationRouter.java
index 5d6010d6e2bae892a3a171a357034a433173c7d9..c1e4ac6a9a8f7f5f672836ceb941392d05f682d5 100644 (file)
@@ -7,13 +7,6 @@
  */
 package org.opendaylight.mdsal.dom.broker;
 
-import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
-import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
-
-import org.opendaylight.mdsal.dom.api.DOMNotification;
-import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
-import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
-import org.opendaylight.mdsal.dom.api.DOMNotificationService;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
@@ -36,6 +29,12 @@ import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
+import org.opendaylight.mdsal.dom.api.DOMNotification;
+import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
+import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
+import org.opendaylight.mdsal.dom.api.DOMNotificationService;
+import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
+import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.util.ListenerRegistry;
@@ -47,14 +46,18 @@ import org.slf4j.LoggerFactory;
  * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
  * routing of notifications from publishers to subscribers.
  *
+ *<p>
  * Internal implementation works by allocating a two-handler Disruptor. The first handler delivers notifications
  * to subscribed listeners and the second one notifies whoever may be listening on the returned future. Registration
  * state tracking is performed by a simple immutable multimap -- when a registration or unregistration occurs we
  * re-generate the entire map from scratch and set it atomically. While registrations/unregistrations synchronize
  * on this instance, notifications do not take any locks here.
  *
- * The fully-blocking {@link #publish(long, DOMNotification, Collection)} and non-blocking {@link #offerNotification(DOMNotification)}
- * are realized using the Disruptor's native operations. The bounded-blocking {@link #offerNotification(DOMNotification, long, TimeUnit)}
+ *<p>
+ * The fully-blocking {@link #publish(long, DOMNotification, Collection)}
+ * and non-blocking {@link #offerNotification(DOMNotification)}
+ * 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,
@@ -62,15 +65,19 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
 
     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>() {
+    private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(
+            1L, 30L, TimeUnit.MILLISECONDS);
+    private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS =
+            new EventHandler<DOMNotificationRouterEvent>() {
         @Override
-        public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) throws Exception {
+        public void onEvent(final DOMNotificationRouterEvent event, final long sequence,
+                final boolean endOfBatch) throws Exception {
             event.deliverNotification();
 
         }
     };
-    private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE = new EventHandler<DOMNotificationRouterEvent>() {
+    private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE =
+            new EventHandler<DOMNotificationRouterEvent>() {
         @Override
         public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) {
             event.setFuture();
@@ -79,14 +86,17 @@ 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 volatile Multimap<SchemaPath, ListenerRegistration<? extends
+            DOMNotificationListener>> listeners = ImmutableMultimap.of();
+    private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
+            ListenerRegistry.create();
 
     @SuppressWarnings("unchecked")
     private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
         this.executor = Preconditions.checkNotNull(executor);
 
-        disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, strategy);
+        disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY,
+                queueDepth, executor, ProducerType.MULTI, strategy);
         disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
         disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
         disruptor.start();
@@ -98,7 +108,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
         return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
     }
 
-    public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime, final TimeUnit unit) {
+    public static DOMNotificationRouter create(final int queueDepth, final long spinTime,
+            final long parkTime, final TimeUnit unit) {
         final ExecutorService executor = Executors.newCachedThreadPool();
         final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit);
 
@@ -106,25 +117,28 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
     }
 
     @Override
-    public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener, final Collection<SchemaPath> types) {
+    public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
+            final T listener, final Collection<SchemaPath> types) {
         final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
             @Override
             protected void removeRegistration() {
                 final ListenerRegistration<T> me = this;
 
                 synchronized (DOMNotificationRouter.this) {
-                    replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
-                        @Override
-                        public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
-                            return input != me;
-                        }
-                    })));
+                    replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners,
+                            new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
+                            @Override
+                            public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
+                                return input != me;
+                            }
+                        })));
                 }
             }
         };
 
         if (!types.isEmpty()) {
-            final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b = ImmutableMultimap.builder();
+            final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b =
+                    ImmutableMultimap.builder();
             b.putAll(listeners);
 
             for (final SchemaPath t : types) {
@@ -138,14 +152,15 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
     }
 
     @Override
-    public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener, final SchemaPath... types) {
+    public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
+            final T listener, final SchemaPath... types) {
         return registerNotificationListener(listener, Arrays.asList(types));
     }
 
     /**
-     * Swaps registered listeners and triggers notification update
+     * Swaps registered listeners and triggers notification update.
      *
-     * @param newListeners
+     * @param newListeners is used to notify listenerTypes changed
      */
     private void replaceListeners(
             final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
@@ -153,8 +168,10 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
         notifyListenerTypesChanged(newListeners.keySet());
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
-        final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =ImmutableList.copyOf(subscriptionListeners.getListeners());
+        final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =
+                ImmutableList.copyOf(subscriptionListeners.getListeners());
         executor.submit(new Runnable() {
 
             @Override
@@ -184,7 +201,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
         return subscriptionListeners.registerWithType(listener);
     }
 
-    private ListenableFuture<Void> publish(final long seq, final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
+    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);
         disruptor.getRingBuffer().publish(seq);
@@ -192,8 +210,10 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
     }
 
     @Override
-    public ListenableFuture<? extends Object> putNotification(final DOMNotification notification) throws InterruptedException {
-        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
+    public ListenableFuture<? extends Object> putNotification(final DOMNotification notification)
+            throws InterruptedException {
+        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
+                listeners.get(notification.getType());
         if (subscribers.isEmpty()) {
             return NO_LISTENERS;
         }
@@ -202,10 +222,12 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
         return publish(seq, notification, subscribers);
     }
 
-    private ListenableFuture<? extends Object> tryPublish(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    private ListenableFuture<? extends Object> tryPublish(final DOMNotification notification,
+            final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
         final long seq;
         try {
-             seq = disruptor.getRingBuffer().tryNext();
+            seq = disruptor.getRingBuffer().tryNext();
         } catch (final InsufficientCapacityException e) {
             return DOMNotificationPublishService.REJECTED;
         }
@@ -215,7 +237,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
 
     @Override
     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
-        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
+        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
+                listeners.get(notification.getType());
         if (subscribers.isEmpty()) {
             return NO_LISTENERS;
         }
@@ -226,7 +249,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
     @Override
     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification, final long timeout,
             final TimeUnit unit) throws InterruptedException {
-        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
+        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
+                listeners.get(notification.getType());
         if (subscribers.isEmpty()) {
             return NO_LISTENERS;
         }