blob: e8e47461651179dd63750f07876601997ddf4fe9 [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
Madan Jampaniad5b8c72016-09-12 15:05:01 -070019import java.util.Map;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070020
Madan Jampaniad5b8c72016-09-12 15:05:01 -070021import javax.annotation.concurrent.NotThreadSafe;
Madan Jampani5bdebd52016-09-07 16:18:12 -070022
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 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070028@NotThreadSafe
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070029public interface DocumentTree<V> {
30
31 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070032 * Returns the {@link DocumentPath path} to root of the tree.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070033 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070034 * @return path to root of the tree
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070035 */
36 DocumentPath root();
37
38 /**
Madan Jampaniad5b8c72016-09-12 15:05:01 -070039 * Returns the child values for this node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070040 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070041 * @param path path to the node
Madan Jampaniad5b8c72016-09-12 15:05:01 -070042 * @return mapping from a child name to its value
Madan Jampani5bdebd52016-09-07 16:18:12 -070043 * @throws NoSuchDocumentPathException if the path does not point to a valid node
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070044 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070045 Map<String, Versioned<V>> getChildren(DocumentPath path);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070046
47 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070048 * Returns a document tree node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070049 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070050 * @param path path to node
51 * @return node value or {@code null} if path does not point to a valid node
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070052 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070053 Versioned<V> get(DocumentPath path);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070054
55 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070056 * Creates or updates a document tree node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070057 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070058 * @param path path for the node to create or update
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070059 * @param value the non-null value to be associated with the key
Madan Jampani5bdebd52016-09-07 16:18:12 -070060 * @return the previous mapping or {@code null} if there was no previous mapping
61 * @throws NoSuchDocumentPathException if the parent node (for the node to create/update) does not exist
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070062 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070063 Versioned<V> set(DocumentPath path, V value);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070064
65 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070066 * Creates a document tree node if one does not exist already.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070067 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070068 * @param path path for the node to create
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070069 * @param value the non-null value to be associated with the key
Madan Jampani5bdebd52016-09-07 16:18:12 -070070 * @return returns {@code true} if the mapping could be added successfully, {@code false} otherwise
71 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070072 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070073 boolean create(DocumentPath path, V value);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070074
75 /**
Madan Jampani86983282016-09-15 14:55:48 -070076 * Creates a document tree node by first creating any missing intermediate nodes in the path.
77 *
78 * @param path path for the node to create
79 * @param value the non-null value to be associated with the key
80 * @return returns {@code true} if the mapping could be added successfully, {@code false} if
81 * a node already exists at that path
82 * @throws IllegalDocumentModificationException if {@code path} points to root
83 */
84 boolean createRecursive(DocumentPath path, V value);
85
86 /**
Madan Jampani1184bc72016-09-08 08:37:05 -070087 * Conditionally updates a tree node if the current version matches a specified version.
88 *
89 * @param path path for the node to create
90 * @param newValue the non-null value to be associated with the key
91 * @param version current version of the value for update to occur
Madan Jampaniad5b8c72016-09-12 15:05:01 -070092 * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise
Madan Jampani1184bc72016-09-08 08:37:05 -070093 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
94 */
95 boolean replace(DocumentPath path, V newValue, long version);
96
97 /**
98 * Conditionally updates a tree node if the current value matches a specified value.
99 *
100 * @param path path for the node to create
101 * @param newValue the non-null value to be associated with the key
102 * @param currentValue current value for update to occur
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700103 * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise.
104 * This method returns {@code false} if the newValue and currentValue are same.
Madan Jampani1184bc72016-09-08 08:37:05 -0700105 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
106 */
107 boolean replace(DocumentPath path, V newValue, V currentValue);
108
109 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -0700110 * Removes the node with the specified path.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700111 *
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700112 * @param path path for the node to remove
Madan Jampani5bdebd52016-09-07 16:18:12 -0700113 * @return the previous value of the node or {@code null} if it did not exist
114 * @throws IllegalDocumentModificationException if the remove to be removed
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700115 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700116 Versioned<V> removeNode(DocumentPath path);
Madan Jampani64ff13b2016-09-08 11:18:50 -0700117
118 /**
119 * Registers a listener to be notified when a subtree rooted at the specified path
120 * is modified.
121 *
122 * @param path path to root of subtree to monitor for updates
123 * @param listener listener to be notified
124 */
125 void addListener(DocumentPath path, DocumentTreeListener<V> listener);
126
127 /**
128 * Unregisters a previously added listener.
129 *
130 * @param listener listener to unregister
131 */
132 void removeListener(DocumentTreeListener<V> listener);
133
134 /**
135 * Registers a listener to be notified when the tree is modified.
136 *
137 * @param listener listener to be notified
138 */
139 default void addListener(DocumentTreeListener<V> listener) {
140 addListener(root(), listener);
141 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700142}