X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fbroker%2Fimpl%2FDOMNotificationRouter.java;h=b5371fc289dd0a4cacf0195becb70d849557d13c;hb=5e55825865a186cb4909b5a9d5c2c91571314cd9;hp=b9972fc0a09f9773db0270e51198209438df010e;hpb=7f44a5812395762479de68d339c3d017db6c0e9d;p=controller.git
diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java
index b9972fc0a0..b5371fc289 100644
--- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java
+++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java
@@ -7,185 +7,174 @@
*/
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.ImmutableMultimap;
-import com.google.common.collect.ImmutableMultimap.Builder;
-import com.google.common.collect.Multimap;
-import com.google.common.collect.Multimaps;
-import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
-import com.lmax.disruptor.EventHandler;
-import com.lmax.disruptor.InsufficientCapacityException;
-import com.lmax.disruptor.SleepingWaitStrategy;
-import com.lmax.disruptor.WaitStrategy;
-import com.lmax.disruptor.dsl.Disruptor;
-import com.lmax.disruptor.dsl.ProducerType;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.util.Date;
import java.util.concurrent.TimeUnit;
+import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
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.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
/**
* Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
* routing of notifications from publishers to subscribers.
*
+ *
* 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)}
+ *
+ * The fully-blocking {@link #offerNotification(DOMNotification)}
+ * is 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 {
- private static final ListenableFuture NO_LISTENERS = Futures.immediateFuture(null);
- private static final WaitStrategy DEFAULT_STRATEGY = new SleepingWaitStrategy();
- private static final EventHandler DISPATCH_NOTIFICATIONS = new EventHandler() {
- @Override
- public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) throws Exception {
- event.deliverNotification();
-
- }
- };
- private static final EventHandler NOTIFY_FUTURE = new EventHandler() {
- @Override
- public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) {
- event.setFuture();
- }
- };
-
- private final Disruptor disruptor;
- private final ExecutorService executor;
- private volatile Multimap> listeners = ImmutableMultimap.of();
-
- private DOMNotificationRouter(final ExecutorService executor, final Disruptor disruptor) {
- this.executor = Preconditions.checkNotNull(executor);
- this.disruptor = Preconditions.checkNotNull(disruptor);
+@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Void is the only allowed value")
+public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
+ DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
+
+ private final org.opendaylight.mdsal.dom.api.DOMNotificationService delegateNotificationService;
+ private final org.opendaylight.mdsal.dom.api.DOMNotificationPublishService delegateNotificationPublishService;
+ private final org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry delegateListenerRegistry;
+
+ private DOMNotificationRouter(
+ org.opendaylight.mdsal.dom.api.DOMNotificationService delegateNotificationService,
+ org.opendaylight.mdsal.dom.api.DOMNotificationPublishService delegateNotificationPublishService,
+ org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry delegateListenerRegistry) {
+ this.delegateNotificationService = delegateNotificationService;
+ this.delegateNotificationPublishService = delegateNotificationPublishService;
+ this.delegateListenerRegistry = delegateListenerRegistry;
}
- @SuppressWarnings("unchecked")
public static DOMNotificationRouter create(final int queueDepth) {
- final ExecutorService executor = Executors.newCachedThreadPool();
- final Disruptor disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, DEFAULT_STRATEGY);
+ final org.opendaylight.mdsal.dom.broker.DOMNotificationRouter delegate =
+ org.opendaylight.mdsal.dom.broker.DOMNotificationRouter.create(queueDepth);
+ return create(delegate, delegate, delegate);
+ }
- disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
- disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
- disruptor.start();
+ public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime,
+ final TimeUnit unit) {
+ final org.opendaylight.mdsal.dom.broker.DOMNotificationRouter delegate =
+ org.opendaylight.mdsal.dom.broker.DOMNotificationRouter.create(queueDepth, spinTime, parkTime, unit);
+ return create(delegate, delegate, delegate);
+ }
- return new DOMNotificationRouter(executor, disruptor);
+ public static DOMNotificationRouter create(
+ final org.opendaylight.mdsal.dom.api.DOMNotificationService delegateNotificationService,
+ final org.opendaylight.mdsal.dom.api.DOMNotificationPublishService delegateNotificationPublishService,
+ final org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry delegateListenerRegistry) {
+ return new DOMNotificationRouter(delegateNotificationService, delegateNotificationPublishService,
+ delegateListenerRegistry);
}
@Override
- public synchronized ListenerRegistration registerNotificationListener(final T listener, final Collection types) {
- final ListenerRegistration reg = new AbstractListenerRegistration(listener) {
- @Override
- protected void removeRegistration() {
- final ListenerRegistration me = this;
-
- synchronized (DOMNotificationRouter.this) {
- listeners = ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate>() {
- @Override
- public boolean apply(final ListenerRegistration extends DOMNotificationListener> input) {
- return input != me;
- }
- }));
- }
+ public synchronized ListenerRegistration registerNotificationListener(
+ final T listener, final Collection types) {
+ org.opendaylight.mdsal.dom.api.DOMNotificationListener delegateListener = notification -> {
+ if (notification instanceof DOMNotification) {
+ listener.onNotification((DOMNotification)notification);
+ return;
}
- };
-
- if (!types.isEmpty()) {
- final Builder> b = ImmutableMultimap.builder();
- b.putAll(listeners);
- for (SchemaPath t : types) {
- b.put(t, reg);
+ if (notification instanceof org.opendaylight.mdsal.dom.api.DOMEvent) {
+ listener.onNotification(new DefaultDOMEvent(notification,
+ (org.opendaylight.mdsal.dom.api.DOMEvent)notification));
+ return;
}
- listeners = b.build();
- }
+ listener.onNotification(new DefaultDOMNotification(notification));
+ };
+
+ final ListenerRegistration reg =
+ delegateNotificationService.registerNotificationListener(delegateListener, types);
- return reg;
+ return new AbstractListenerRegistration(listener) {
+ @Override
+ protected void removeRegistration() {
+ reg.close();
+ }
+ };
}
@Override
- public ListenerRegistration registerNotificationListener(final T listener, final SchemaPath... types) {
+ public ListenerRegistration registerNotificationListener(final T listener,
+ final SchemaPath... types) {
return registerNotificationListener(listener, Arrays.asList(types));
}
- private ListenableFuture publish(final long seq, final DOMNotification notification, final Collection> subscribers) {
- final DOMNotificationRouterEvent event = disruptor.get(seq);
- final ListenableFuture future = event.initialize(notification, subscribers);
- disruptor.getRingBuffer().publish(seq);
- return future;
+ @Override
+ public ListenerRegistration registerSubscriptionListener(
+ final L listener) {
+ return delegateListenerRegistry.registerSubscriptionListener(listener);
}
@Override
- public ListenableFuture extends Object> putNotification(final DOMNotification notification) throws InterruptedException {
- final Collection> subscribers = listeners.get(notification.getType());
- if (subscribers.isEmpty()) {
- return NO_LISTENERS;
- }
-
- final long seq = disruptor.getRingBuffer().next();
- return publish(seq, notification, subscribers);
+ public ListenableFuture> putNotification(final DOMNotification notification) throws InterruptedException {
+ return delegateNotificationPublishService.putNotification(notification);
}
- private ListenableFuture extends Object> tryPublish(final DOMNotification notification, final Collection> subscribers) {
- final long seq;
- try {
- seq = disruptor.getRingBuffer().tryNext();
- } catch (InsufficientCapacityException e) {
- return DOMNotificationPublishService.REJECTED;
- }
-
- return publish(seq, notification, subscribers);
+ @Override
+ public ListenableFuture> offerNotification(final DOMNotification notification) {
+ return delegateNotificationPublishService.offerNotification(notification);
}
@Override
- public ListenableFuture extends Object> offerNotification(final DOMNotification notification) {
- final Collection> subscribers = listeners.get(notification.getType());
- if (subscribers.isEmpty()) {
- return NO_LISTENERS;
- }
-
- return tryPublish(notification, subscribers);
+ public ListenableFuture> offerNotification(final DOMNotification notification, final long timeout,
+ final TimeUnit unit) throws InterruptedException {
+ return delegateNotificationPublishService.offerNotification(notification, timeout, unit);
}
@Override
- public ListenableFuture extends Object> offerNotification(final DOMNotification notification, final long timeout,
- final TimeUnit unit) throws InterruptedException {
- final Collection> subscribers = listeners.get(notification.getType());
- if (subscribers.isEmpty()) {
- return NO_LISTENERS;
+ public void close() {
+ }
+
+ private static class DefaultDOMNotification implements DOMNotification {
+ private final SchemaPath schemaPath;
+ private final ContainerNode body;
+
+ DefaultDOMNotification(org.opendaylight.mdsal.dom.api.DOMNotification from) {
+ this.schemaPath = from.getType();
+ this.body = from.getBody();
}
- // Attempt to perform a non-blocking publish first
- final ListenableFuture extends Object> noBlock = tryPublish(notification, subscribers);
- if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
- return noBlock;
+ @Override
+ public SchemaPath getType() {
+ return schemaPath;
}
- /*
- * FIXME: we need a background thread, which will watch out for blocking too long. Here
- * we will arm a tasklet for it and synchronize delivery of interrupt properly.
- */
- throw new UnsupportedOperationException("Not implemented yet");
+ @Override
+ public ContainerNode getBody() {
+ return body;
+ }
}
- @Override
- public void close() {
- disruptor.shutdown();
- executor.shutdown();
+ private static class DefaultDOMEvent extends DefaultDOMNotification implements DOMEvent {
+ private final Date eventTime;
+
+ DefaultDOMEvent(org.opendaylight.mdsal.dom.api.DOMNotification fromNotification,
+ org.opendaylight.mdsal.dom.api.DOMEvent fromEvent) {
+ super(fromNotification);
+ final Instant eventInstant = fromEvent.getEventInstant();
+ this.eventTime = eventInstant != null ? Date.from(eventInstant) : null;
+ }
+
+ @Override
+ public Date getEventTime() {
+ return eventTime;
+ }
}
}