Merge "Add timeout to netconf IT test"
[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
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 = Futures.immediateFailedFuture(new Throwable("Unacceptable blocking conditions encountered"));
32
33     /**
34      * Publish a notification. The result of this method is a {@link ListenableFuture}
35      * which will complete once the notification has been delivered to all immediate
36      * registrants. The type of the object resulting from the future is not defined
37      * and implementations may use it to convey additional information related to the
38      * publishing process.
39      *
40      * Abstract subclasses can refine the return type as returning a promise of a
41      * more specific type, e.g.:
42      *
43      *     public interface DeliveryStatus { int getListenerCount(); }
44      *     ListenableFuture<? extends DeliveryStatus> putNotification(DOMNotification notification);
45      *
46      * Once the Future succeeds, the resulting object can be queried for traits using
47      * instanceof, e.g:
48      *
49      *     // Can block when (for example) the implemention's ThreadPool queue is full
50      *     Object o = service.putNotification(notif).get();
51      *     if (o instanceof DeliveryStatus) {
52      *         DeliveryStatus ds = (DeliveryStatus)o;
53      *         LOG.debug("Notification was received by {} listeners", ds.getListenerCount(););
54      *     }
55      * }
56      *
57      * In case an implementation is running out of resources, it can block the calling
58      * thread until enough resources become available to accept the notification for
59      * processing, or it is interrupted.
60      *
61      * Caution: completion here means that the implementation has completed processing
62      *          of the notification. This does not mean that all existing registrants
63      *          have seen the notification. Most importantly, the delivery process at
64      *          other cluster nodes may have not begun yet.
65      *
66      * @param notification Notification to be published.
67      * @return A listenable future which will report completion when the service
68      *         has finished propagating the notification to its immediate registrants.
69      * @throws InterruptedException if interrupted while waiting
70      * @throws NullPointerException if notification is null.
71      */
72     @Nonnull ListenableFuture<? extends Object> putNotification(@Nonnull DOMNotification notification) throws InterruptedException;
73
74     /**
75      * Attempt to publish a notification. The result of this method is a {@link ListenableFuture}
76      * which will complete once the notification has been delivered to all immediate
77      * registrants. The type of the object resulting from the future is not defined
78      * and implementations may use it to convey additional information related to the
79      * publishing process. Unlike {@link #putNotification(DOMNotification)}, this method
80      * is guaranteed not to block if the underlying implementation encounters contention.
81      *
82      * @param notification Notification to be published.
83      * @return A listenable future which will report completion when the service
84      *         has finished propagating the notification to its immediate registrants,
85      *         or {@value #REJECTED} if resource constraints prevent
86      *         the implementation from accepting the notification for delivery.
87      * @throws NullPointerException if notification is null.
88      */
89     @Nonnull ListenableFuture<? extends Object> offerNotification(@Nonnull DOMNotification notification);
90
91     /**
92      * Attempt to publish a notification. The result of this method is a {@link ListenableFuture}
93      * which will complete once the notification has been delivered to all immediate
94      * registrants. The type of the object resulting from the future is not defined
95      * and implementations may use it to convey additional information related to the
96      * publishing process. Unlike {@link #putNotification(DOMNotification)}, this method
97      * is guaranteed to block more than the specified timeout.
98      *
99      * @param notification Notification to be published.
100      * @param timeout how long to wait before giving up, in units of unit
101      * @param unit a TimeUnit determining how to interpret the timeout parameter
102      * @return A listenable future which will report completion when the service
103      *         has finished propagating the notification to its immediate registrants,
104      *         or {@value #REJECTED} if resource constraints prevent
105      *         the implementation from accepting the notification for delivery.
106      * @throws InterruptedException if interrupted while waiting
107      * @throws NullPointerException if notification or unit is null.
108      * @throws IllegalArgumentException if timeout is negative.
109      */
110     @Nonnull ListenableFuture<? extends Object> offerNotification(@Nonnull DOMNotification notification,
111         @Nonnegative long timeout, @Nonnull TimeUnit unit) throws InterruptedException;
112 }