99bf87141fd93b77b14055134d2352307450770a
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMNotificationPublishService.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.api;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.concurrent.TimeUnit;
12 import org.checkerframework.checker.index.qual.NonNegative;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
15
16 /**
17  * A {@link DOMService} which allows its user to send {@link DOMNotification}s. It
18  * provides two styles of initiating the notification delivery, similar to
19  * {@link java.util.concurrent.BlockingQueue}:
20  * - a put-style method which waits until the implementation can accept the notification
21  *   for delivery, and
22  * - an offer-style method, which attempts to enqueue the notification, but allows
23  *   the caller to specify that it should never wait, or put an upper bound on how
24  *   long it is going to wait.
25  */
26 public interface DOMNotificationPublishService extends DOMService {
27     /**
28      * Well-known value indicating that the implementation is currently not
29      * able to accept a notification.
30      */
31     ListenableFuture<Object> REJECTED = FluentFutures.immediateFailedFluentFuture(
32         new DOMNotificationRejectedException("Unacceptable blocking conditions encountered"));
33
34     /**
35      * Publish a notification. The result of this method is a {@link ListenableFuture} which will
36      * complete once the notification has been delivered to all immediate registrants. The type of
37      * the object resulting from the future is not defined and implementations may use it to convey
38      * additional information related to the publishing process.
39      * Abstract subclasses can refine the return type as returning a promise of a more specific
40      * type, e.g.:
41      * public interface DeliveryStatus { int getListenerCount(); } ListenableFuture&lt;? extends
42      * DeliveryStatus&gt;[ putNotification(DOMNotification notification);
43      * Once the Future succeeds, the resulting object can be queried for traits using instanceof,
44      * e.g:
45      * // Can block when (for example) the implemention's ThreadPool queue is full Object o =
46      * service.putNotification(notif).get(); if (o instanceof DeliveryStatus) { DeliveryStatus ds =
47      * (DeliveryStatus)o; LOG.debug("Notification was received by {} listeners",
48      * ds.getListenerCount();); } }
49      * In case an implementation is running out of resources, it can block the calling thread until
50      * enough resources become available to accept the notification for processing, or it is
51      * interrupted.
52      *
53      * <p>
54      * Caution: completion here means that the implementation has completed processing of the
55      * notification. This does not mean that all existing registrants have seen the notification.
56      * Most importantly, the delivery process at other cluster nodes may have not begun yet.
57      *
58      * @param notification Notification to be published.
59      * @return A listenable future which will report completion when the service has finished
60      *         propagating the notification to its immediate registrants.
61      * @throws InterruptedException if interrupted while waiting
62      * @throws NullPointerException if notification is null.
63      */
64     @NonNull ListenableFuture<? extends Object> putNotification(@NonNull DOMNotification notification)
65             throws InterruptedException;
66
67     /**
68      * Attempt to publish a notification. The result of this method is a {@link ListenableFuture}
69      * which will complete once the notification has been delivered to all immediate registrants.
70      * The type of the object resulting from the future is not defined and implementations may use
71      * it to convey additional information related to the publishing process. Unlike
72      * {@link #putNotification(DOMNotification)}, this method is guaranteed not to block if the
73      * underlying implementation encounters contention.
74      *
75      * @param notification Notification to be published.
76      * @return A listenable future which will report completion when the service has finished
77      *         propagating the notification to its immediate registrants, or {@link #REJECTED} if
78      *         resource constraints prevent the implementation from accepting the notification for
79      *         delivery.
80      * @throws NullPointerException if notification is null.
81      */
82     @NonNull ListenableFuture<? extends Object> offerNotification(@NonNull DOMNotification notification);
83
84     /**
85      * Attempt to publish a notification. The result of this method is a {@link ListenableFuture}
86      * which will complete once the notification has been delivered to all immediate registrants.
87      * The type of the object resulting from the future is not defined and implementations may use
88      * it to convey additional information related to the publishing process. Unlike
89      * {@link #putNotification(DOMNotification)}, this method is guaranteed to block more than the
90      * specified timeout.
91      *
92      * @param notification Notification to be published.
93      * @param timeout how long to wait before giving up, in units of unit
94      * @param unit a TimeUnit determining how to interpret the timeout parameter
95      * @return A listenable future which will report completion when the service has finished
96      *         propagating the notification to its immediate registrants, or {@link #REJECTED} if
97      *         resource constraints prevent the implementation from accepting the notification for
98      *         delivery.
99      * @throws InterruptedException if interrupted while waiting
100      * @throws NullPointerException if notification or unit is null.
101      * @throws IllegalArgumentException if timeout is negative.
102      */
103     @NonNull ListenableFuture<? extends Object> offerNotification(@NonNull DOMNotification notification,
104             @NonNegative long timeout, @NonNull TimeUnit unit) throws InterruptedException;
105 }