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.Notification;
17 import org.opendaylight.yangtools.yang.binding.NotificationListener;
20 * Notification broker which allows clients to subscribe for and publish YANG-modeled notifications.
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)}
32 * <b>Dispatch Listener Example</b>
35 * Lets assume we have following YANG model:
41 * notification start {
52 * The generated interface will be:
55 * public interface ExampleListener extends NotificationListener {
56 * void onStart(Start notification);
58 * void onStop(Stop notification);
63 * The following defines an implementation of the generated interface:
66 * public class MyExampleListener implements ExampleListener {
67 * public void onStart(Start notification) {
71 * public void onStop(Stop notification) {
78 * The implementation is registered as follows:
81 * MyExampleListener listener = new MyExampleListener();
82 * ListenerRegistration<NotificationListener> reg = service.registerNotificationListener(listener);
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.
90 public interface NotificationService extends BindingService {
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.
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.
101 <T extends NotificationListener> @NonNull ListenerRegistration<T> registerNotificationListener(@NonNull T listener);
104 * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type.
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
113 <N extends Notification> @NonNull Registration registerListener(Class<N> type, Listener<N> listener,
117 * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type.
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.
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
129 default <N extends Notification> @NonNull Registration registerListener(final Class<N> type,
130 final Listener<N> listener) {
131 return registerListener(type, listener, MoreExecutors.directExecutor());
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
139 * @param N Notification type
142 interface Listener<N extends Notification> extends EventListener {
144 * Process a global notification.
146 * @param notification Notification body
148 void onNotification(@NonNull N notification);