2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.dom.broker.impl;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Collection;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
15 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
18 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener;
19 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
20 import org.opendaylight.controller.sal.core.compat.LegacyDOMNotificationServiceAdapter;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
26 * routing of notifications from publishers to subscribers.
29 * Internal implementation works by allocating a two-handler Disruptor. The first handler delivers notifications
30 * to subscribed listeners and the second one notifies whoever may be listening on the returned future. Registration
31 * state tracking is performed by a simple immutable multimap -- when a registration or unregistration occurs we
32 * re-generate the entire map from scratch and set it atomically. While registrations/unregistrations synchronize
33 * on this instance, notifications do not take any locks here.
36 * The fully-blocking {@link #offerNotification(DOMNotification)}
37 * is realized using the Disruptor's native operations. The bounded-blocking
38 * {@link #offerNotification(DOMNotification, long, TimeUnit)}
39 * is realized by arming a background wakeup interrupt.
41 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Void is the only allowed value")
42 public final class DOMNotificationRouter extends LegacyDOMNotificationServiceAdapter implements AutoCloseable,
43 DOMNotificationPublishService, DOMNotificationSubscriptionListenerRegistry {
45 private final org.opendaylight.mdsal.dom.api.DOMNotificationPublishService delegateNotificationPublishService;
46 private final org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry delegateListenerRegistry;
48 private DOMNotificationRouter(
49 final org.opendaylight.mdsal.dom.api.DOMNotificationService delegateNotificationService,
50 final org.opendaylight.mdsal.dom.api.DOMNotificationPublishService delegateNotificationPublishService,
51 final org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry delegateListenerRegistry) {
52 super(delegateNotificationService);
53 this.delegateNotificationPublishService = delegateNotificationPublishService;
54 this.delegateListenerRegistry = delegateListenerRegistry;
57 public static DOMNotificationRouter create(final int queueDepth) {
58 final org.opendaylight.mdsal.dom.broker.DOMNotificationRouter delegate =
59 org.opendaylight.mdsal.dom.broker.DOMNotificationRouter.create(queueDepth);
60 return create(delegate, delegate, delegate);
63 public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime,
64 final TimeUnit unit) {
65 final org.opendaylight.mdsal.dom.broker.DOMNotificationRouter delegate =
66 org.opendaylight.mdsal.dom.broker.DOMNotificationRouter.create(queueDepth, spinTime, parkTime, unit);
67 return create(delegate, delegate, delegate);
70 public static DOMNotificationRouter create(
71 final org.opendaylight.mdsal.dom.api.DOMNotificationService delegateNotificationService,
72 final org.opendaylight.mdsal.dom.api.DOMNotificationPublishService delegateNotificationPublishService,
73 final org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry delegateListenerRegistry) {
74 return new DOMNotificationRouter(delegateNotificationService, delegateNotificationPublishService,
75 delegateListenerRegistry);
79 public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
80 final T listener, final Collection<SchemaPath> types) {
81 return super.registerNotificationListener(listener, types);
85 public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
87 return delegateListenerRegistry.registerSubscriptionListener(listener);
91 public ListenableFuture<?> putNotification(final DOMNotification notification) throws InterruptedException {
92 return delegateNotificationPublishService.putNotification(notification);
96 public ListenableFuture<?> offerNotification(final DOMNotification notification) {
97 return delegateNotificationPublishService.offerNotification(notification);
101 public ListenableFuture<?> offerNotification(final DOMNotification notification, final long timeout,
102 final TimeUnit unit) throws InterruptedException {
103 return delegateNotificationPublishService.offerNotification(notification, timeout, unit);
107 public void close() {