c0e8ea5fee712047c347e14b758228532006ad6d
[controller.git] / opendaylight / md-sal / sal-binding-api / src / main / java / org / opendaylight / controller / md / sal / 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.controller.md.sal.binding.api;
9
10 import org.opendaylight.yangtools.concepts.ListenerRegistration;
11 import org.opendaylight.yangtools.yang.binding.NotificationListener;
12
13 /**
14  * Notification broker which allows clients to subscribe for and publish YANG-modeled notifications.
15  *
16  * Each YANG module which defines notifications results in a generated interface <code>{ModuleName}Listener</code>
17  * which handles all the notifications defined in the YANG model. Each notification type translates to
18  * a specific method of the form <code>on{NotificationType}</code> on the generated interface.
19  * The generated interface also extends the
20  * {@link org.opendaylight.yangtools.yang.binding.NotificationListener} interface and implementations
21  * are registered using {@link #registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener)}
22  * method.
23  *
24  * <h3>Dispatch Listener Example</h3>
25  * <p>
26  * Lets assume we have following YANG model:
27  *
28  * <pre>
29  * module example {
30  *      ...
31  *
32  *      notification start {
33  *          ...
34  *      }
35  *
36  *      notification stop {
37  *           ...
38  *      }
39  * }
40  * </pre>
41  *
42  * The generated interface will be:
43  * {@code
44  *  public interface ExampleListener extends NotificationListener {
45  *      void onStart(Start notification);
46  *      void onStop(Stop notification);
47  *  }
48  * }
49  * The following defines an implementation of the generated interface:
50  * {@code
51  *  public class MyExampleListener implements ExampleListener {
52  *      public void onStart(Start notification) {
53  *          // do something
54  *      }
55  *
56  *      public void onStop(Stop notification) {
57  *          // do something
58  *      }
59  *  }
60  * }
61  * The implementation is registered as follows:
62  * {@code
63  *  MyExampleListener listener = new MyExampleListener();
64  *  ListenerRegistration<NotificationListener> reg = service.registerNotificationListener( listener );
65  * }
66  * The <code>onStart</code> method will be invoked when someone publishes a <code>Start</code> notification and
67  * the <code>onStop</code> method will be invoked when someone publishes a <code>Stop</code> notification.
68  */
69 public interface NotificationService extends BindingService {
70     /**
71      * Registers a listener which implements a YANG-generated notification interface derived from
72      * {@link NotificationListener}. The listener is registered for all notifications present in
73      * the implemented interface.
74      *
75      * @param listener the listener implementation that will receive notifications.
76      * @return a {@link ListenerRegistration} instance that should be used to unregister the listener
77      *         by invoking the {@link ListenerRegistration#close()} method when no longer needed.
78      */
79     <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(T listener);
80 }