2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.api;
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;
21 * Notification broker which allows clients to subscribe for and publish YANG-modeled notifications.
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)}
33 * <b>Dispatch Listener Example</b>
36 * Lets assume we have following YANG model:
42 * notification start {
53 * The generated interface will be:
56 * public interface ExampleListener extends NotificationListener {
57 * void onStart(Start notification);
59 * void onStop(Stop notification);
64 * The following defines an implementation of the generated interface:
67 * public class MyExampleListener implements ExampleListener {
68 * public void onStart(Start notification) {
72 * public void onStop(Stop notification) {
79 * The implementation is registered as follows:
82 * MyExampleListener listener = new MyExampleListener();
83 * ListenerRegistration<NotificationListener> reg = service.registerNotificationListener(listener);
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.
91 public interface NotificationService extends BindingService {
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.
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.
102 <T extends NotificationListener> @NonNull ListenerRegistration<T> registerNotificationListener(@NonNull T listener);
105 * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type.
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
114 <N extends Notification<N> & DataObject> @NonNull Registration registerListener(Class<N> type, Listener<N> listener,
118 * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type.
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.
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
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());
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
140 * @param <N> Notification type
143 interface Listener<N extends Notification<N> & DataObject> extends EventListener {
145 * Process a global notification.
147 * @param notification Notification body
149 void onNotification(@NonNull N notification);