Madan Jampani
Committed by Gerrit Code Review

Support for listening to DocumentTree modifications.

Change-Id: Ibe7c67e6615f5a19fe4c4c7dea182e1a59dc5eea
...@@ -102,4 +102,29 @@ public interface DocumentTree<V> { ...@@ -102,4 +102,29 @@ public interface DocumentTree<V> {
102 * @throws IllegalDocumentModificationException if the remove to be removed 102 * @throws IllegalDocumentModificationException if the remove to be removed
103 */ 103 */
104 V removeNode(DocumentPath key); 104 V removeNode(DocumentPath key);
105 +
106 + /**
107 + * Registers a listener to be notified when a subtree rooted at the specified path
108 + * is modified.
109 + *
110 + * @param path path to root of subtree to monitor for updates
111 + * @param listener listener to be notified
112 + */
113 + void addListener(DocumentPath path, DocumentTreeListener<V> listener);
114 +
115 + /**
116 + * Unregisters a previously added listener.
117 + *
118 + * @param listener listener to unregister
119 + */
120 + void removeListener(DocumentTreeListener<V> listener);
121 +
122 + /**
123 + * Registers a listener to be notified when the tree is modified.
124 + *
125 + * @param listener listener to be notified
126 + */
127 + default void addListener(DocumentTreeListener<V> listener) {
128 + addListener(root(), listener);
129 + }
105 } 130 }
......
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.service;
17 +
18 +import java.util.Optional;
19 +
20 +import com.google.common.base.MoreObjects;
21 +
22 +/**
23 + * A document tree modification event.
24 + *
25 + * @param <V> tree node value type
26 + */
27 +public class DocumentTreeEvent<V> {
28 +
29 + /**
30 + * Nature of document tree node change.
31 + */
32 + public enum Type {
33 +
34 + /**
35 + * Signifies node being created.
36 + */
37 + CREATED,
38 +
39 + /**
40 + * Signifies the value of an existing node being updated.
41 + */
42 + UPDATED,
43 +
44 + /**
45 + * Signifies an existing node being deleted.
46 + */
47 + DELETED
48 + }
49 +
50 + private final DocumentPath path;
51 + private final Type type;
52 + private final Optional<Versioned<V>> newValue;
53 + private final Optional<Versioned<V>> oldValue;
54 +
55 + /**
56 + * Constructs a new {@code DocumentTreeEvent}.
57 + *
58 + * @param path path to the node
59 + * @param type type of change
60 + * @param newValue optional new value; will be empty if node was deleted
61 + * @param oldValue optional old value; will be empty if node was created
62 + */
63 + public DocumentTreeEvent(DocumentPath path,
64 + Type type,
65 + Optional<Versioned<V>> newValue,
66 + Optional<Versioned<V>> oldValue) {
67 + this.path = path;
68 + this.type = type;
69 + this.newValue = newValue;
70 + this.oldValue = oldValue;
71 + }
72 +
73 + /**
74 + * Returns the path to the changed node.
75 + *
76 + * @return node path
77 + */
78 + public DocumentPath path() {
79 + return path;
80 + }
81 +
82 + /**
83 + * Returns the change type.
84 + * @return change type
85 + */
86 + public Type type() {
87 + return type;
88 + }
89 +
90 + /**
91 + * Returns the new value.
92 + *
93 + * @return optional new value; will be empty if node was deleted
94 + */
95 + public Optional<Versioned<V>> newValue() {
96 + return newValue;
97 + }
98 +
99 + /**
100 + * Returns the old value.
101 + *
102 + * @return optional old value; will be empty if node was created
103 + */
104 + public Optional<Versioned<V>> oldValue() {
105 + return oldValue;
106 + }
107 +
108 + @Override
109 + public String toString() {
110 + return MoreObjects.toStringHelper(getClass())
111 + .add("path", path)
112 + .add("type", type)
113 + .add("newValue", newValue)
114 + .add("oldValue", oldValue)
115 + .toString();
116 + }
117 +}
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.service;
17 +
18 +/**
19 + * A listener for {@link DocumentTreeEvent}.
20 + *
21 + * @param <V> document tree node value type
22 + */
23 +public interface DocumentTreeListener<V> {
24 +
25 + /**
26 + * Callback notifying about change to document tree node.
27 + *
28 + * @param event event
29 + */
30 + void event(DocumentTreeEvent<V> event);
31 +}