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 MUST explicitly unregister your listener when you no longer want to receive
40 * notifications. This is especially true in OSGi environments, where failure to
41 * do so during bundle shutdown can lead to stale listeners 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
53 * notifications when data changes under a given path in the conceptual data tree.
56 * You are able to register for notifications for any node or subtree
57 * which can be represented using {@link DataTreeIdentifier}.
60 * This method returns a {@link Registration} object. To
61 * "unregister" your listener for changes call the {@link Registration#close()}
62 * method on the returned object.
65 * You MUST explicitly unregister your listener when you no longer want to receive
66 * notifications. This is especially true in OSGi environments, where failure to
67 * do so during bundle shutdown can lead to stale listeners being still registered.
68 * @implSpec This method provides {@link DataListenerAdapter} as listener during
69 * the registration of {@link DataTreeChangeListener}. This would allow users
70 * to know the last state of data instead of getting details about what changed
72 * @param treeId Data tree identifier of the subtree which should be watched for
74 * @param listener Listener instance which is being registered
75 * @return Listener registration object, which may be used to unregister
76 * your listener using {@link Registration#close()} to stop
77 * delivery of change events.
79 default <T extends DataObject> @NonNull Registration registerDataListener(
80 final @NonNull DataTreeIdentifier<T> treeId, final @NonNull DataListener<T> listener) {
81 return registerDataTreeChangeListener(checkNotWildcard(treeId), new DataListenerAdapter<>(listener));
85 * Registers a {@link DataTreeChangeListener} to receive
86 * notifications about the last data state when it changes under a given path in the conceptual data
90 * You are able to register for notifications for any node or subtree
91 * which can be represented using {@link DataTreeIdentifier}.
94 * This method returns a {@link Registration} object. To
95 * "unregister" your listener for changes call the {@link Registration#close()}
96 * method on the returned object.
99 * You MUST explicitly unregister your listener when you no longer want to receive
100 * notifications. This is especially true in OSGi environments, where failure to
101 * do so during bundle shutdown can lead to stale listeners being still registered.
103 * @implSpec This method provides {@link DataChangeListenerAdapter} as listener during
104 * the registration of {@link DataTreeChangeListener}, which provides a comparison
105 * of before-value and after-value.
107 * @param treeId Data tree identifier of the subtree which should be watched for
109 * @param listener Listener instance which is being registered
110 * @return Listener registration object, which may be used to unregister
111 * your listener using {@link Registration#close()} to stop
112 * delivery of change events.
114 default <T extends DataObject> @NonNull Registration registerDataChangeListener(
115 final @NonNull DataTreeIdentifier<T> treeId, final @NonNull DataChangeListener<T> listener) {
116 return registerDataTreeChangeListener(checkNotWildcard(treeId), new DataChangeListenerAdapter<>(listener));
119 private static <T extends DataObject> @NonNull DataTreeIdentifier<T> checkNotWildcard(
120 final DataTreeIdentifier<T> treeId) {
121 final var instanceIdentifier = treeId.path();
122 if (instanceIdentifier.isWildcarded()) {
123 throw new IllegalArgumentException("Cannot register listener for wildcard " + instanceIdentifier);