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.ListenerRegistration;
12 import org.opendaylight.yangtools.concepts.Registration;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 * A {@link BindingService} which allows users to register for changes to a subtree.
19 public interface DataTreeChangeService extends BindingService {
21 * Registers a {@link DataTreeChangeListener} to receive
22 * notifications when data changes under a given path in the conceptual data
26 * You are able to register for notifications for any node or subtree
27 * which can be represented using {@link DataTreeIdentifier}.
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
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.
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.
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.
50 * Data tree identifier of the subtree which should be watched for
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.
58 <T extends DataObject, L extends DataTreeChangeListener<T>> @NonNull ListenerRegistration<L>
59 registerDataTreeChangeListener(@NonNull DataTreeIdentifier<T> treeId, @NonNull L listener);
62 * Registers a {@link DataTreeChangeListener} to receive
63 * notifications when data changes under a given path in the conceptual data tree.
66 * You are able to register for notifications for any node or subtree
67 * which can be represented using {@link DataTreeIdentifier}.
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.
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
82 * @param treeId Data tree identifier of the subtree which should be watched for
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.
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));
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
100 * You are able to register for notifications for any node or subtree
101 * which can be represented using {@link DataTreeIdentifier}.
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.
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.
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.
117 * @param treeId Data tree identifier of the subtree which should be watched for
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.
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));
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);