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