Madan Jampani
Committed by Gerrit Code Review

Support for listening to DocumentTree modifications.

Change-Id: Ibe7c67e6615f5a19fe4c4c7dea182e1a59dc5eea
......@@ -102,4 +102,29 @@ public interface DocumentTree<V> {
* @throws IllegalDocumentModificationException if the remove to be removed
*/
V removeNode(DocumentPath key);
/**
* Registers a listener to be notified when a subtree rooted at the specified path
* is modified.
*
* @param path path to root of subtree to monitor for updates
* @param listener listener to be notified
*/
void addListener(DocumentPath path, DocumentTreeListener<V> listener);
/**
* Unregisters a previously added listener.
*
* @param listener listener to unregister
*/
void removeListener(DocumentTreeListener<V> listener);
/**
* Registers a listener to be notified when the tree is modified.
*
* @param listener listener to be notified
*/
default void addListener(DocumentTreeListener<V> listener) {
addListener(root(), listener);
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.Optional;
import com.google.common.base.MoreObjects;
/**
* A document tree modification event.
*
* @param <V> tree node value type
*/
public class DocumentTreeEvent<V> {
/**
* Nature of document tree node change.
*/
public enum Type {
/**
* Signifies node being created.
*/
CREATED,
/**
* Signifies the value of an existing node being updated.
*/
UPDATED,
/**
* Signifies an existing node being deleted.
*/
DELETED
}
private final DocumentPath path;
private final Type type;
private final Optional<Versioned<V>> newValue;
private final Optional<Versioned<V>> oldValue;
/**
* Constructs a new {@code DocumentTreeEvent}.
*
* @param path path to the node
* @param type type of change
* @param newValue optional new value; will be empty if node was deleted
* @param oldValue optional old value; will be empty if node was created
*/
public DocumentTreeEvent(DocumentPath path,
Type type,
Optional<Versioned<V>> newValue,
Optional<Versioned<V>> oldValue) {
this.path = path;
this.type = type;
this.newValue = newValue;
this.oldValue = oldValue;
}
/**
* Returns the path to the changed node.
*
* @return node path
*/
public DocumentPath path() {
return path;
}
/**
* Returns the change type.
* @return change type
*/
public Type type() {
return type;
}
/**
* Returns the new value.
*
* @return optional new value; will be empty if node was deleted
*/
public Optional<Versioned<V>> newValue() {
return newValue;
}
/**
* Returns the old value.
*
* @return optional old value; will be empty if node was created
*/
public Optional<Versioned<V>> oldValue() {
return oldValue;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("path", path)
.add("type", type)
.add("newValue", newValue)
.add("oldValue", oldValue)
.toString();
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
/**
* A listener for {@link DocumentTreeEvent}.
*
* @param <V> document tree node value type
*/
public interface DocumentTreeListener<V> {
/**
* Callback notifying about change to document tree node.
*
* @param event event
*/
void event(DocumentTreeEvent<V> event);
}