/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.mdsal.binding.api; import com.google.common.util.concurrent.MoreExecutors; import java.util.EventListener; import java.util.concurrent.Executor; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.Notification; import org.opendaylight.yangtools.yang.binding.NotificationListener; /** * Notification broker which allows clients to subscribe for and publish YANG-modeled notifications. * *

* Each YANG module which defines notifications results in a generated interface * {ModuleName}Listener which handles all the notifications defined in the YANG model. * Each notification type translates to a specific method of the form * on{NotificationType} on the generated interface. The generated interface also * extends the {@link org.opendaylight.yangtools.yang.binding.NotificationListener} interface and * implementations are registered using * {@link #registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener)} * method. * * Dispatch Listener Example * *

* Lets assume we have following YANG model: * *

 * module example {
 *      ...
 *
 *      notification start {
 *          ...
 *      }
 *
 *      notification stop {
 *           ...
 *      }
 * }
 * 
* *

* The generated interface will be: * *

 * public interface ExampleListener extends NotificationListener {
 *     void onStart(Start notification);
 *
 *     void onStop(Stop notification);
 * }
 * 
* *

* The following defines an implementation of the generated interface: * *

 * public class MyExampleListener implements ExampleListener {
 *     public void onStart(Start notification) {
 *         // do something
 *     }
 *
 *     public void onStop(Stop notification) {
 *         // do something
 *     }
 * }
 * 
* *

* The implementation is registered as follows: * *

 * MyExampleListener listener = new MyExampleListener();
 * ListenerRegistration<NotificationListener> reg = service.registerNotificationListener(listener);
 * 
* *

* The onStart method will be invoked when someone publishes a Start * notification and the onStop method will be invoked when someone publishes a * Stop notification. */ public interface NotificationService extends BindingService { /** * Registers a listener which implements a YANG-generated notification interface derived from * {@link NotificationListener}. The listener is registered for all notifications present in * the implemented interface. * * @param NotificationListener type * @param listener the listener implementation that will receive notifications. * @return a {@link ListenerRegistration} instance that should be used to unregister the listener * by invoking the {@link ListenerRegistration#close()} method when no longer needed. */ @NonNull ListenerRegistration registerNotificationListener(@NonNull T listener); /** * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type. * * @param Notification type * @param type Notification type class * @param listener The listener implementation that will receive notifications * @param executor Executor to use for invoking the listener's methods * @return a {@link Registration} instance that should be used to unregister the listener by invoking the * {@link Registration#close()} method when no longer needed */ @NonNull Registration registerListener(Class type, Listener listener, Executor executor); /** * Registers a {@link Listener} to receive callbacks for {@link Notification}s of a particular type. * * @implSpec * This method is equivalent to {@code registerListener(type, listener, MoreExecutors.directExecutor())}, i.e. * the listener will be invoked on some implementation-specific thread. * * @param Notification type * @param type Notification type class * @param listener The listener implementation that will receive notifications * @return a {@link Registration} instance that should be used to unregister the listener by invoking the * {@link Registration#close()} method when no longer needed */ default @NonNull Registration registerListener(final Class type, final Listener listener) { return registerListener(type, listener, MoreExecutors.directExecutor()); } /** * Interface for listeners on global (YANG 1.0) notifications. Such notifications are identified by their generated * interface which extends {@link Notification}. Each listener instance can listen to only a single notification * type. * * @param N Notification type */ @FunctionalInterface interface Listener extends EventListener { /** * Process a global notification. * * @param notification Notification body */ void onNotification(@NonNull N notification); } }