Clean up (DOM)DataTreeIdentifier methods
[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 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.
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
53      * notifications when data changes under a given path in the conceptual data tree.
54      *
55      * <p>
56      * You are able to register for notifications  for any node or subtree
57      * which can be represented using {@link DataTreeIdentifier}.
58      *
59      * <p>
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.
63      *
64      * <p>
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
71      *      in the entire tree.
72      * @param treeId Data tree identifier of the subtree which should be watched for
73      *            changes.
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.
78      */
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));
82     }
83
84     /**
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
87      * tree.
88      *
89      * <p>
90      * You are able to register for notifications  for any node or subtree
91      * which can be represented using {@link DataTreeIdentifier}.
92      *
93      * <p>
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.
97      *
98      * <p>
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.
102      *
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.
106      *
107      * @param treeId Data tree identifier of the subtree which should be watched for
108      *            changes.
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.
113      */
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));
117     }
118
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);
124         }
125         return treeId;
126     }
127 }