de0c8649b5cc61baf4cc5a0e319e6f62599d6bbb
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / InstanceNotificationPublishService.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.binding.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.concurrent.TimeUnit;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
18
19 /**
20  * A {@link BindingService} which allows its users to submit YANG-modeled top-level (YANG 1.1)
21  * {@link InstanceNotification}s for delivery.
22  */
23 @Beta
24 public interface InstanceNotificationPublishService extends BindingService {
25     /**
26      * Create a new {@link Publisher} for a {@link InstanceNotificationSpec}. Returned interface may be freely reused
27      * and accessed concurrently from multiple threads.
28      *
29      * @param <N> Generated InstanceNotification interface type
30      * @param <P> Notification parent type
31      * @param notificationSpec Specification of an {@link InstanceNotification}
32      * @return A {@link Publisher} handle
33      * @throws NullPointerException if {@code notificationSpec} is {@code null}
34      * @throws IllegalArgumentException when {@code notificationSpec} cannot be resolved
35      */
36     <N extends InstanceNotification<N, P>, P extends DataObject> @NonNull Publisher<N, P> newPublisher(
37         InstanceNotificationSpec<N, P> notificationSpec);
38
39     /**
40      * Interface for publishing {@link InstanceNotification} of a particular type bound to an instantiation. There are
41      * three methods of submission, following the patters from {@link java.util.concurrent.BlockingQueue}:
42      * <ul>
43      *   <li>{@link #putNotification(InstanceNotification)}, which may block indefinitely if the implementation cannot
44      *       allocate resources to accept the notification,</li>
45      *   <li>{@link #offerNotification(InstanceNotification)}, which does not block if face of resource starvation,</li>
46      *   <li>{@link #offerNotification(InstanceNotification, int, TimeUnit)}, which may block for specified time if
47      *       resources are thin.</li>
48      * </ul>
49      *
50      * <p>
51      * The actual delivery to listeners is asynchronous and implementation-specific. Users of this interface should not
52      * make any assumptions as to whether the notification has or has not been seen.
53      *
54      * @param <N> Generated InstanceNotification interface type
55      * @param <P> Notification parent type
56      */
57     @Beta
58     interface Publisher<N extends InstanceNotification<N, P>, P extends DataObject> {
59         /**
60          * Well-known value indicating that the binding-aware implementation is currently not able to accept a
61          * notification.
62          */
63         @NonNull ListenableFuture<Object> REJECTED = FluentFutures.immediateFailedFluentFuture(
64                 new NotificationRejectedException("Rejected due to resource constraints."));
65
66         /**
67          * Publishes a notification to subscribed listeners. This initiates the process of sending the notification, but
68          * delivery to the listeners can happen asynchronously, potentially after a call to this method returns.
69          *
70          * <b>Note:</b> This call will block when the notification queue is full.
71          *
72          * @param path parent path
73          * @param notification the notification to publish.
74          * @throws InterruptedException if interrupted while waiting
75          * @throws NullPointerException if any argument is null
76          */
77         void putNotification(@NonNull InstanceIdentifier<P> path, @NonNull N notification) throws InterruptedException;
78
79         /**
80          * Publishes a notification to subscribed listeners. This initiates the process of sending the notification, but
81          * delivery to the listeners can happen asynchronously, potentially after a call to this method returns.
82          *
83          * <p>
84          * Still guaranteed not to block. Returns Listenable Future which will complete once the delivery is completed.
85          *
86          * @param path parent path
87          * @param notification the notification to publish.
88          * @return A listenable future which will report completion when the service has finished propagating the
89          *         notification to its immediate registrants, or {@link #REJECTED} if resource constraints prevent
90          * @throws NullPointerException if any argument is null
91          */
92         @NonNull ListenableFuture<? extends Object> offerNotification(@NonNull InstanceIdentifier<P> path,
93             @NonNull N notification);
94
95         /**
96          * Publishes a notification to subscribed listeners. This initiates the process of sending the notification, but
97          * delivery to the listeners can happen asynchronously, potentially after a call to this method returns. This
98          * method is guaranteed not to block more than the specified timeout.
99          *
100          * @param path parent path
101          * @param notification the notification to publish.
102          * @param timeout how long to wait before giving up, in units of unit
103          * @param unit a TimeUnit determining how to interpret the timeout parameter
104          * @return A listenable future which will report completion when the service has finished propagating the
105          *         notification to its immediate registrants, or {@link #REJECTED} if resource constraints prevent
106          * @throws InterruptedException if interrupted while waiting
107          * @throws NullPointerException if any argument is null
108          * @throws IllegalArgumentException if timeout is negative.
109          */
110         @NonNull ListenableFuture<? extends Object> offerNotification(@NonNull InstanceIdentifier<P> path,
111             @NonNull N notification, long timeout, @NonNull TimeUnit unit) throws InterruptedException;
112     }
113 }