6ff74ca34159dd8a8d2b5ff8e8b9b2f28500b282
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / NotificationService.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.binding.api;
9
10 import com.google.common.util.concurrent.MoreExecutors;
11 import java.util.EventListener;
12 import java.util.concurrent.Executor;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15 import org.opendaylight.yangtools.concepts.Registration;
16 import org.opendaylight.yangtools.yang.binding.Notification;
17 import org.opendaylight.yangtools.yang.binding.NotificationListener;
18
19 /**
20  * Notification broker which allows clients to subscribe for and publish YANG-modeled notifications.
21  *
22  *<p>
23  * Each YANG module which defines notifications results in a generated interface
24  * <code>{ModuleName}Listener</code> which handles all the notifications defined in the YANG model.
25  * Each notification type translates to a specific method of the form
26  * <code>on{NotificationType}</code> on the generated interface. The generated interface also
27  * extends the {@link org.opendaylight.yangtools.yang.binding.NotificationListener} interface and
28  * implementations are registered using
29  * {@link #registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener)}
30  * method.
31  *
32  * <b>Dispatch Listener Example</b>
33  *
34  * <p>
35  * Lets assume we have following YANG model:
36  *
37  * <pre>
38  * module example {
39  *      ...
40  *
41  *      notification start {
42  *          ...
43  *      }
44  *
45  *      notification stop {
46  *           ...
47  *      }
48  * }
49  * </pre>
50  *
51  * <p>
52  * The generated interface will be:
53  *
54  * <pre>
55  * public interface ExampleListener extends NotificationListener {
56  *     void onStart(Start notification);
57  *
58  *     void onStop(Stop notification);
59  * }
60  * </pre>
61  *
62  * <p>
63  * The following defines an implementation of the generated interface:
64  *
65  * <pre>
66  * public class MyExampleListener implements ExampleListener {
67  *     public void onStart(Start notification) {
68  *         // do something
69  *     }
70  *
71  *     public void onStop(Stop notification) {
72  *         // do something
73  *     }
74  * }
75  * </pre>
76  *
77  * <p>
78  * The implementation is registered as follows:
79  *
80  * <pre>
81  * MyExampleListener listener = new MyExampleListener();
82  * ListenerRegistration&lt;NotificationListener&gt; reg = service.registerNotificationListener(listener);
83  * </pre>
84  *
85  * <p>
86  * The <code>onStart</code> method will be invoked when someone publishes a <code>Start</code>
87  * notification and the <code>onStop</code> method will be invoked when someone publishes a
88  * <code>Stop</code> notification.
89  */
90 public interface NotificationService extends BindingService {
91     /**
92      * Registers a listener which implements a YANG-generated notification interface derived from
93      * {@link NotificationListener}. The listener is registered for all notifications present in
94      * the implemented interface.
95      *
96      * @param <T> NotificationListener type
97      * @param listener the listener implementation that will receive notifications.
98      * @return a {@link ListenerRegistration} instance that should be used to unregister the listener
99      *         by invoking the {@link ListenerRegistration#close()} method when no longer needed.
100      */
101     <T extends NotificationListener> @NonNull ListenerRegistration<T> registerNotificationListener(@NonNull T listener);
102
103     /**
104      * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type.
105      *
106      * @param <N> Notification type
107      * @param type Notification type class
108      * @param listener The listener implementation that will receive notifications
109      * @param executor Executor to use for invoking the listener's methods
110      * @return a {@link Registration} instance that should be used to unregister the listener by invoking the
111      *        {@link Registration#close()} method when no longer needed
112      */
113     <N extends Notification> @NonNull Registration registerListener(Class<N> type, Listener<N> listener,
114         Executor executor);
115
116     /**
117      * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type.
118      *
119      * @implSpec
120      *     This method is equivalent to {@code registerListener(type, listener, MoreExecutors.directExecutor())}, i.e.
121      *     the listener will be invoked on some implementation-specific thread.
122      *
123      * @param <N> Notification type
124      * @param type Notification type class
125      * @param listener The listener implementation that will receive notifications
126      * @return a {@link Registration} instance that should be used to unregister the listener by invoking the
127      *        {@link Registration#close()} method when no longer needed
128      */
129     default <N extends Notification> @NonNull Registration registerListener(final Class<N> type,
130             final Listener<N> listener) {
131         return registerListener(type, listener, MoreExecutors.directExecutor());
132     }
133
134     /**
135      * Interface for listeners on global (YANG 1.0) notifications. Such notifications are identified by their generated
136      * interface which extends {@link Notification}. Each listener instance can listen to only a single notification
137      * type.
138      *
139      * @param N Notification type
140      */
141     @FunctionalInterface
142     interface Listener<N extends Notification> extends EventListener {
143         /**
144          * Process a global notification.
145          *
146          * @param notification Notification body
147          */
148         void onNotification(@NonNull N notification);
149     }
150 }