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