2 * Copyright (c) 2015 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 org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.yangtools.concepts.Registration;
12 import org.opendaylight.yangtools.yang.binding.DataObject;
15 * A {@link BindingService} which allows users to register for changes to a subtree.
17 public interface DataTreeChangeService extends BindingService {
19 * Registers a {@link DataTreeChangeListener} to receive notifications when data changes under a given path in the
20 * conceptual data tree.
23 * You are able to register for notifications for any node or subtree which can be represented using
24 * {@link DataTreeIdentifier}.
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.
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.
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.
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.
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.
48 <T extends DataObject> @NonNull Registration registerDataTreeChangeListener(@NonNull DataTreeIdentifier<T> treeId,
49 @NonNull DataTreeChangeListener<T> listener);
52 * Registers a {@link DataTreeChangeListener} to receive notifications when data changes under a given path in the
53 * conceptual data tree.
56 * You are able to register for notifications for any node or subtree which can be represented using
57 * {@link DataTreeIdentifier}.
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.
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.
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.
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.
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));
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.
87 * You are able to register for notifications for any node or subtree which can be represented using
88 * {@link DataTreeIdentifier}.
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.
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.
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.
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.
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));
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);