Add simple data listeners
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / DataTreeChangeService.java
1 /*
2  * Copyright (c) 2015 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 org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.yangtools.concepts.ListenerRegistration;
12 import org.opendaylight.yangtools.concepts.Registration;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15
16 /**
17  * A {@link BindingService} which allows users to register for changes to a subtree.
18  */
19 public interface DataTreeChangeService extends BindingService {
20     /**
21      * Registers a {@link DataTreeChangeListener} to receive
22      * notifications when data changes under a given path in the conceptual data
23      * tree.
24      *
25      * <p>
26      * You are able to register for notifications  for any node or subtree
27      * which can be represented using {@link DataTreeIdentifier}.
28      *
29      * <p>
30      * You are able to register for data change notifications for a subtree or leaf
31      * even if it does not exist. You will receive notification once that node is
32      * created.
33      *
34      * <p>
35      * If there is any pre-existing data in the data tree for the path for which you are
36      * registering, you will receive an initial data change event, which will
37      * contain all pre-existing data, marked as created.
38      *
39      * <p>
40      * This method returns a {@link ListenerRegistration} object. To
41      * "unregister" your listener for changes call the {@link ListenerRegistration#close()}
42      * method on the returned object.
43      *
44      * <p>
45      * You MUST explicitly unregister your listener when you no longer want to receive
46      * notifications. This is especially true in OSGi environments, where failure to
47      * do so during bundle shutdown can lead to stale listeners being still registered.
48      *
49      * @param treeId
50      *            Data tree identifier of the subtree which should be watched for
51      *            changes.
52      * @param listener
53      *            Listener instance which is being registered
54      * @return Listener registration object, which may be used to unregister
55      *         your listener using {@link ListenerRegistration#close()} to stop
56      *         delivery of change events.
57      */
58     <T extends DataObject, L extends DataTreeChangeListener<T>> @NonNull ListenerRegistration<L>
59             registerDataTreeChangeListener(@NonNull DataTreeIdentifier<T> treeId, @NonNull L listener);
60
61     /**
62      * Registers a {@link DataTreeChangeListener} to receive
63      * notifications when data changes under a given path in the conceptual data tree.
64      *
65      * <p>
66      * You are able to register for notifications  for any node or subtree
67      * which can be represented using {@link DataTreeIdentifier}.
68      *
69      * <p>
70      * This method returns a {@link Registration} object. To
71      * "unregister" your listener for changes call the {@link Registration#close()}
72      * method on the returned object.
73      *
74      * <p>
75      * You MUST explicitly unregister your listener when you no longer want to receive
76      * notifications. This is especially true in OSGi environments, where failure to
77      * do so during bundle shutdown can lead to stale listeners being still registered.
78      * @implSpec This method provides {@link DataListenerAdapter} as listener during
79      *      the registration of {@link DataTreeChangeListener}. This would allow users
80      *      to know the last state of data instead of getting details about what changed
81      *      in the entire tree.
82      * @param treeId Data tree identifier of the subtree which should be watched for
83      *            changes.
84      * @param listener Listener instance which is being registered
85      * @return Listener registration object, which may be used to unregister
86      *         your listener using {@link Registration#close()} to stop
87      *         delivery of change events.
88      */
89     default <T extends DataObject> @NonNull Registration registerDataListener(
90             final @NonNull DataTreeIdentifier<T> treeId, final @NonNull DataListener<T> listener) {
91         return registerDataTreeChangeListener(checkNotWildcard(treeId), new DataListenerAdapter<>(listener));
92     }
93
94     /**
95      * Registers a {@link DataTreeChangeListener} to receive
96      * notifications about the last data state when it changes under a given path in the conceptual data
97      * tree.
98      *
99      * <p>
100      * You are able to register for notifications  for any node or subtree
101      * which can be represented using {@link DataTreeIdentifier}.
102      *
103      * <p>
104      * This method returns a {@link Registration} object. To
105      * "unregister" your listener for changes call the {@link Registration#close()}
106      * method on the returned object.
107      *
108      * <p>
109      * You MUST explicitly unregister your listener when you no longer want to receive
110      * notifications. This is especially true in OSGi environments, where failure to
111      * do so during bundle shutdown can lead to stale listeners being still registered.
112      *
113      * @implSpec This method provides {@link DataChangeListenerAdapter} as listener during
114      *      the registration of {@link DataTreeChangeListener}, which provides a comparison
115      *      of before-value and after-value.
116      *
117      * @param treeId Data tree identifier of the subtree which should be watched for
118      *            changes.
119      * @param listener Listener instance which is being registered
120      * @return Listener registration object, which may be used to unregister
121      *         your listener using {@link Registration#close()} to stop
122      *         delivery of change events.
123      */
124     default <T extends DataObject> @NonNull Registration registerDataChangeListener(
125             final @NonNull DataTreeIdentifier<T> treeId, final @NonNull DataChangeListener<T> listener) {
126         return registerDataTreeChangeListener(checkNotWildcard(treeId), new DataChangeListenerAdapter<>(listener));
127     }
128
129     private static <T extends DataObject> @NonNull DataTreeIdentifier<T> checkNotWildcard(
130             final DataTreeIdentifier<T> treeId) {
131         final @NonNull InstanceIdentifier<T> instanceIdentifier = treeId.getRootIdentifier();
132         if (instanceIdentifier.isWildcarded()) {
133             throw new IllegalArgumentException("Cannot register listener for wildcard " + instanceIdentifier);
134         }
135         return treeId;
136     }
137 }