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