blob: 35c6deb07e30b986f30082a8211e2d7661746b9b [file] [log] [blame]
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -07001/*
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
17package org.onosproject.store.service;
18
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070019import java.util.Iterator;
20
Madan Jampani5bdebd52016-09-07 16:18:12 -070021import org.onosproject.store.primitives.DocumentTreeNode;
22
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070023/**
Madan Jampani5bdebd52016-09-07 16:18:12 -070024 * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure.
25 *
Aaron Kruglikov7c998112016-09-07 17:45:07 -070026 * @param <V> document tree value type
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070027 */
28public interface DocumentTree<V> {
29
30 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070031 * Returns the {@link DocumentPath path} to root of the tree.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070032 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070033 * @return path to root of the tree
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070034 */
35 DocumentPath root();
36
37 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070038 * Returns an iterator for all the first order descendants of a node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070039 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070040 * @param path path to the node
41 * @return an iterator for the child nodes of the specified node path
42 * @throws NoSuchDocumentPathException if the path does not point to a valid node
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070043 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070044 Iterator<DocumentTreeNode<V>> getChildren(DocumentPath path);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070045
46 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070047 * Returns a document tree node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070048 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070049 * @param path path to node
50 * @return node value or {@code null} if path does not point to a valid node
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070051 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070052 DocumentTreeNode<V> getNode(DocumentPath path);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070053
54 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070055 * Creates or updates a document tree node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070056 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070057 * @param path path for the node to create or update
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070058 * @param value the non-null value to be associated with the key
Madan Jampani5bdebd52016-09-07 16:18:12 -070059 * @return the previous mapping or {@code null} if there was no previous mapping
60 * @throws NoSuchDocumentPathException if the parent node (for the node to create/update) does not exist
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070061 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070062 V putNode(DocumentPath path, V value);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070063
64 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070065 * Creates a document tree node if one does not exist already.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070066 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070067 * @param path path for the node to create
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070068 * @param value the non-null value to be associated with the key
Madan Jampani5bdebd52016-09-07 16:18:12 -070069 * @return returns {@code true} if the mapping could be added successfully, {@code false} otherwise
70 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070071 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070072 boolean createNode(DocumentPath path, V value);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070073
74 /**
Madan Jampani1184bc72016-09-08 08:37:05 -070075 * Conditionally updates a tree node if the current version matches a specified version.
76 *
77 * @param path path for the node to create
78 * @param newValue the non-null value to be associated with the key
79 * @param version current version of the value for update to occur
80 * @return returns {@code true} if the update was made, {@code false} otherwise
81 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
82 */
83 boolean replace(DocumentPath path, V newValue, long version);
84
85 /**
86 * Conditionally updates a tree node if the current value matches a specified value.
87 *
88 * @param path path for the node to create
89 * @param newValue the non-null value to be associated with the key
90 * @param currentValue current value for update to occur
91 * @return returns {@code true} if the update was made, {@code false} otherwise
92 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
93 */
94 boolean replace(DocumentPath path, V newValue, V currentValue);
95
96 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070097 * Removes the node with the specified path.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070098 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070099 * is not a leaf node i.e has one or more children
Thomas Vachuskaf5e6be42016-09-07 20:58:22 -0700100 * @param key path for the node to remove
Madan Jampani5bdebd52016-09-07 16:18:12 -0700101 * @return the previous value of the node or {@code null} if it did not exist
102 * @throws IllegalDocumentModificationException if the remove to be removed
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700103 */
104 V removeNode(DocumentPath key);
Madan Jampani64ff13b2016-09-08 11:18:50 -0700105
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 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700130}