blob: 46dcdd5b27e5f393eb57397948ff915542951f95 [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
Sithara Punnasseryc3658c72017-08-07 11:16:08 -070019import org.onosproject.store.primitives.NodeUpdate;
20
Madan Jampani79924fa2016-09-13 13:57:03 -070021import java.util.Map;
22import java.util.concurrent.CompletableFuture;
23
24import javax.annotation.concurrent.NotThreadSafe;
25
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070026import org.onosproject.store.primitives.DefaultDocumentTree;
27
Madan Jampani79924fa2016-09-13 13:57:03 -070028/**
29 * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure.
30 *
31 * @param <V> document tree value type
32 */
33@NotThreadSafe
Sithara Punnasseryc3658c72017-08-07 11:16:08 -070034public interface AsyncDocumentTree<V> extends DistributedPrimitive, Transactional<NodeUpdate<V>> {
Madan Jampani79924fa2016-09-13 13:57:03 -070035
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070036 @Override
37 default Type primitiveType() {
38 return Type.DOCUMENT_TREE;
39 }
40
Madan Jampani79924fa2016-09-13 13:57:03 -070041 /**
42 * Returns the {@link DocumentPath path} to root of the tree.
43 *
44 * @return path to root of the tree
45 */
46 DocumentPath root();
47
48 /**
Madan Jampani86983282016-09-15 14:55:48 -070049 * 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 -070050 *
51 * @param path path to the node
Madan Jampani86983282016-09-15 14:55:48 -070052 * @return future for mapping from child name to child value
Yuta HIGUCHId2db7ad2016-09-16 21:40:13 -070053 * @throws NoSuchDocumentPathException if the path does not point to a valid node
Madan Jampani79924fa2016-09-13 13:57:03 -070054 */
55 CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path);
56
57 /**
Madan Jampani86983282016-09-15 14:55:48 -070058 * Returns the value of the tree node at specified path.
Madan Jampani79924fa2016-09-13 13:57:03 -070059 *
Madan Jampani86983282016-09-15 14:55:48 -070060 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070061 * @return future that will be either be completed with node's {@link Versioned versioned} value
62 * or {@code null} if path does not point to a valid node
Madan Jampani79924fa2016-09-13 13:57:03 -070063 */
64 CompletableFuture<Versioned<V>> get(DocumentPath path);
65
66 /**
67 * Creates or updates a document tree node.
68 *
Madan Jampani86983282016-09-15 14:55:48 -070069 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070070 * @param value value to be associated with the node ({@code null} is a valid value)
71 * @return future that will either be completed with the previous {@link Versioned versioned}
72 * value or {@code null} if there was no node previously at that path.
73 * Future will be completed with a {@code IllegalDocumentModificationException}
Madan Jampani86983282016-09-15 14:55:48 -070074 * if the parent node (for the node to create/update) does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -070075 */
76 CompletableFuture<Versioned<V>> set(DocumentPath path, V value);
77
78 /**
79 * Creates a document tree node if one does not exist already.
80 *
Madan Jampani86983282016-09-15 14:55:48 -070081 * @param path path to the node
82 * @param value the non-null value to be associated with the node
83 * @return future that is completed with {@code true} if the new node was successfully
84 * created. Future will be completed with {@code false} if a node already exists at the specified path.
85 * Future will be completed exceptionally with a {@code IllegalDocumentModificationException} if the parent
86 * node (for the node to create) does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -070087 */
88 CompletableFuture<Boolean> create(DocumentPath path, V value);
89
90 /**
Madan Jampani86983282016-09-15 14:55:48 -070091 * Creates a document tree node recursively by creating all missing intermediate nodes in the path.
92 *
93 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070094 * @param value value to be associated with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -070095 * @return future that is completed with {@code true} if the new node was successfully
96 * created. Future will be completed with {@code false} if a node already exists at the specified path
97 */
98 CompletableFuture<Boolean> createRecursive(DocumentPath path, V value);
99
100 /**
Madan Jampani79924fa2016-09-13 13:57:03 -0700101 * Conditionally updates a tree node if the current version matches a specified version.
102 *
Madan Jampani86983282016-09-15 14:55:48 -0700103 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700104 * @param newValue value to associate with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -0700105 * @param version current version of the node for update to occur
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700106 * @return future that is either completed with {@code true} if the update was made
107 * or {@code false} if update did not happen
Madan Jampani79924fa2016-09-13 13:57:03 -0700108 */
109 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version);
110
111 /**
Madan Jampani86983282016-09-15 14:55:48 -0700112 * Conditionally updates a tree node if the current node value matches a specified version.
Madan Jampani79924fa2016-09-13 13:57:03 -0700113 *
Madan Jampani86983282016-09-15 14:55:48 -0700114 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700115 * @param newValue value to associate with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -0700116 * @param currentValue current value of the node for update to occur
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700117 * @return future that is either completed with {@code true} if the update was made
118 * or {@code false} if update did not happen
Madan Jampani79924fa2016-09-13 13:57:03 -0700119 */
120 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue);
121
122 /**
123 * Removes the node with the specified path.
124 *
Madan Jampani86983282016-09-15 14:55:48 -0700125 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700126 * @return future for the previous value. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700127 * {@code IllegalDocumentModificationException} if the node to be removed is either the root
Madan Jampani79924fa2016-09-13 13:57:03 -0700128 * node or has one or more children. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700129 * {@code NoSuchDocumentPathException} if the node to be removed does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -0700130 */
131 CompletableFuture<Versioned<V>> removeNode(DocumentPath path);
132
133 /**
Madan Jampani86983282016-09-15 14:55:48 -0700134 * Registers a listener to be notified when the subtree rooted at the specified path
Madan Jampani79924fa2016-09-13 13:57:03 -0700135 * is modified.
136 *
Madan Jampani86983282016-09-15 14:55:48 -0700137 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700138 * @param listener listener to be notified
Madan Jampani31888032016-09-14 14:37:58 -0700139 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700140 */
141 CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener);
142
143 /**
144 * Unregisters a previously added listener.
145 *
146 * @param listener listener to unregister
Madan Jampani31888032016-09-14 14:37:58 -0700147 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700148 */
149 CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener);
150
151 /**
152 * Registers a listener to be notified when the tree is modified.
153 *
154 * @param listener listener to be notified
Madan Jampani31888032016-09-14 14:37:58 -0700155 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700156 */
157 default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) {
158 return addListener(root(), listener);
159 }
Jordan Haltermanb0ac5902017-07-30 12:31:01 -0700160
161 /**
162 * Returns a new {@link DocumentTree} that is backed by this instance.
163 *
164 * @return new {@code DocumentTree} instance
165 */
166 default DocumentTree<V> asDocumentTree() {
167 return asDocumentTree(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
168 }
169
170 /**
171 * Returns a new {@link DocumentTree} that is backed by this instance.
172 *
173 * @param timeoutMillis timeout duration for the returned DocumentTree operations
174 * @return new {@code DocumentTree} instance
175 */
176 default DocumentTree<V> asDocumentTree(long timeoutMillis) {
177 return new DefaultDocumentTree<V>(this, timeoutMillis);
178 }
Madan Jampani79924fa2016-09-13 13:57:03 -0700179}