blob: 4255b65401799de2d334edf349ab06499d572b43 [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 Jampani1184bc72016-09-08 08:37:05 -070076 * Conditionally updates a tree node if the current version matches a specified version.
77 *
78 * @param path path for the node to create
79 * @param newValue the non-null value to be associated with the key
80 * @param version current version of the value for update to occur
Madan Jampaniad5b8c72016-09-12 15:05:01 -070081 * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise
Madan Jampani1184bc72016-09-08 08:37:05 -070082 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
83 */
84 boolean replace(DocumentPath path, V newValue, long version);
85
86 /**
87 * Conditionally updates a tree node if the current value matches a specified value.
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 currentValue current 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.
93 * This method returns {@code false} if the newValue and currentValue are same.
Madan Jampani1184bc72016-09-08 08:37:05 -070094 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
95 */
96 boolean replace(DocumentPath path, V newValue, V currentValue);
97
98 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070099 * Removes the node with the specified path.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700100 *
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700101 * @param path path for the node to remove
Madan Jampani5bdebd52016-09-07 16:18:12 -0700102 * @return the previous value of the node or {@code null} if it did not exist
103 * @throws IllegalDocumentModificationException if the remove to be removed
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700104 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700105 Versioned<V> removeNode(DocumentPath path);
Madan Jampani64ff13b2016-09-08 11:18:50 -0700106
107 /**
108 * Registers a listener to be notified when a subtree rooted at the specified path
109 * is modified.
110 *
111 * @param path path to root of subtree to monitor for updates
112 * @param listener listener to be notified
113 */
114 void addListener(DocumentPath path, DocumentTreeListener<V> listener);
115
116 /**
117 * Unregisters a previously added listener.
118 *
119 * @param listener listener to unregister
120 */
121 void removeListener(DocumentTreeListener<V> listener);
122
123 /**
124 * Registers a listener to be notified when the tree is modified.
125 *
126 * @param listener listener to be notified
127 */
128 default void addListener(DocumentTreeListener<V> listener) {
129 addListener(root(), listener);
130 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700131}