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