blob: 1f0902cd14ad82cb5b7dbbbc3427ce0ef5f190c1 [file] [log] [blame]
Madan Jampani79924fa2016-09-13 13:57:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani79924fa2016-09-13 13:57:03 -07003 *
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
19import java.util.Map;
20import java.util.concurrent.CompletableFuture;
21
22import javax.annotation.concurrent.NotThreadSafe;
23
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070024import org.onosproject.store.primitives.DefaultDocumentTree;
25
Madan Jampani79924fa2016-09-13 13:57:03 -070026/**
27 * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure.
28 *
29 * @param <V> document tree value type
30 */
31@NotThreadSafe
32public interface AsyncDocumentTree<V> extends DistributedPrimitive {
33
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070034 @Override
35 default Type primitiveType() {
36 return Type.DOCUMENT_TREE;
37 }
38
Madan Jampani79924fa2016-09-13 13:57:03 -070039 /**
40 * Returns the {@link DocumentPath path} to root of the tree.
41 *
42 * @return path to root of the tree
43 */
44 DocumentPath root();
45
46 /**
Madan Jampani86983282016-09-15 14:55:48 -070047 * Returns the children of node at specified path in the form of a mapping from child name to child value.
Madan Jampani79924fa2016-09-13 13:57:03 -070048 *
49 * @param path path to the node
Madan Jampani86983282016-09-15 14:55:48 -070050 * @return future for mapping from child name to child value
Yuta HIGUCHId2db7ad2016-09-16 21:40:13 -070051 * @throws NoSuchDocumentPathException if the path does not point to a valid node
Madan Jampani79924fa2016-09-13 13:57:03 -070052 */
53 CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path);
54
55 /**
Madan Jampani86983282016-09-15 14:55:48 -070056 * Returns the value of the tree node at specified path.
Madan Jampani79924fa2016-09-13 13:57:03 -070057 *
Madan Jampani86983282016-09-15 14:55:48 -070058 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070059 * @return future that will be either be completed with node's {@link Versioned versioned} value
60 * or {@code null} if path does not point to a valid node
Madan Jampani79924fa2016-09-13 13:57:03 -070061 */
62 CompletableFuture<Versioned<V>> get(DocumentPath path);
63
64 /**
65 * Creates or updates a document tree node.
66 *
Madan Jampani86983282016-09-15 14:55:48 -070067 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070068 * @param value value to be associated with the node ({@code null} is a valid value)
69 * @return future that will either be completed with the previous {@link Versioned versioned}
70 * value or {@code null} if there was no node previously at that path.
71 * Future will be completed with a {@code IllegalDocumentModificationException}
Madan Jampani86983282016-09-15 14:55:48 -070072 * if the parent node (for the node to create/update) does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -070073 */
74 CompletableFuture<Versioned<V>> set(DocumentPath path, V value);
75
76 /**
77 * Creates a document tree node if one does not exist already.
78 *
Madan Jampani86983282016-09-15 14:55:48 -070079 * @param path path to the node
80 * @param value the non-null value to be associated with the node
81 * @return future that is completed with {@code true} if the new node was successfully
82 * created. Future will be completed with {@code false} if a node already exists at the specified path.
83 * Future will be completed exceptionally with a {@code IllegalDocumentModificationException} if the parent
84 * node (for the node to create) does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -070085 */
86 CompletableFuture<Boolean> create(DocumentPath path, V value);
87
88 /**
Madan Jampani86983282016-09-15 14:55:48 -070089 * Creates a document tree node recursively by creating all missing intermediate nodes in the path.
90 *
91 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070092 * @param value value to be associated with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -070093 * @return future that is completed with {@code true} if the new node was successfully
94 * created. Future will be completed with {@code false} if a node already exists at the specified path
95 */
96 CompletableFuture<Boolean> createRecursive(DocumentPath path, V value);
97
98 /**
Madan Jampani79924fa2016-09-13 13:57:03 -070099 * Conditionally updates a tree node if the current version matches a specified version.
100 *
Madan Jampani86983282016-09-15 14:55:48 -0700101 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700102 * @param newValue value to associate with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -0700103 * @param version current version of the node for update to occur
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700104 * @return future that is either completed with {@code true} if the update was made
105 * or {@code false} if update did not happen
Madan Jampani79924fa2016-09-13 13:57:03 -0700106 */
107 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version);
108
109 /**
Madan Jampani86983282016-09-15 14:55:48 -0700110 * Conditionally updates a tree node if the current node value matches a specified version.
Madan Jampani79924fa2016-09-13 13:57:03 -0700111 *
Madan Jampani86983282016-09-15 14:55:48 -0700112 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700113 * @param newValue value to associate with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -0700114 * @param currentValue current value of the node for update to occur
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700115 * @return future that is either completed with {@code true} if the update was made
116 * or {@code false} if update did not happen
Madan Jampani79924fa2016-09-13 13:57:03 -0700117 */
118 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue);
119
120 /**
121 * Removes the node with the specified path.
122 *
Madan Jampani86983282016-09-15 14:55:48 -0700123 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700124 * @return future for the previous value. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700125 * {@code IllegalDocumentModificationException} if the node to be removed is either the root
Madan Jampani79924fa2016-09-13 13:57:03 -0700126 * node or has one or more children. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700127 * {@code NoSuchDocumentPathException} if the node to be removed does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -0700128 */
129 CompletableFuture<Versioned<V>> removeNode(DocumentPath path);
130
131 /**
Madan Jampani86983282016-09-15 14:55:48 -0700132 * Registers a listener to be notified when the subtree rooted at the specified path
Madan Jampani79924fa2016-09-13 13:57:03 -0700133 * is modified.
134 *
Madan Jampani86983282016-09-15 14:55:48 -0700135 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700136 * @param listener listener to be notified
Madan Jampani31888032016-09-14 14:37:58 -0700137 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700138 */
139 CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener);
140
141 /**
142 * Unregisters a previously added listener.
143 *
144 * @param listener listener to unregister
Madan Jampani31888032016-09-14 14:37:58 -0700145 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700146 */
147 CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener);
148
149 /**
150 * Registers a listener to be notified when the tree is modified.
151 *
152 * @param listener listener to be notified
Madan Jampani31888032016-09-14 14:37:58 -0700153 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700154 */
155 default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) {
156 return addListener(root(), listener);
157 }
Jordan Haltermanb0ac5902017-07-30 12:31:01 -0700158
159 /**
160 * Returns a new {@link DocumentTree} that is backed by this instance.
161 *
162 * @return new {@code DocumentTree} instance
163 */
164 default DocumentTree<V> asDocumentTree() {
165 return asDocumentTree(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
166 }
167
168 /**
169 * Returns a new {@link DocumentTree} that is backed by this instance.
170 *
171 * @param timeoutMillis timeout duration for the returned DocumentTree operations
172 * @return new {@code DocumentTree} instance
173 */
174 default DocumentTree<V> asDocumentTree(long timeoutMillis) {
175 return new DefaultDocumentTree<V>(this, timeoutMillis);
176 }
Madan Jampani79924fa2016-09-13 13:57:03 -0700177}