blob: 754366dbdd053b201bafc2d91f2f9625d6fa7de1 [file] [log] [blame]
Madan Jampani79924fa2016-09-13 13:57:03 -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
19import java.util.Map;
20import java.util.concurrent.CompletableFuture;
21
22import javax.annotation.concurrent.NotThreadSafe;
23
24/**
25 * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure.
26 *
27 * @param <V> document tree value type
28 */
29@NotThreadSafe
30public interface AsyncDocumentTree<V> extends DistributedPrimitive {
31
32 /**
33 * Returns the {@link DocumentPath path} to root of the tree.
34 *
35 * @return path to root of the tree
36 */
37 DocumentPath root();
38
39 /**
40 * Returns the child values for this node.
41 *
42 * @param path path to the node
43 * @return future for mapping from a child name to its value
44 * @throws NoSuchDocumentPathException if the path does not point to a valid node
45 */
46 CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path);
47
48 /**
49 * Returns a value (and version) of the tree node at specified path.
50 *
51 * @param path path to node
52 * @return future for node value or {@code null} if path does not point to a valid node
53 */
54 CompletableFuture<Versioned<V>> get(DocumentPath path);
55
56 /**
57 * Creates or updates a document tree node.
58 *
59 * @param path path for the node to create or update
60 * @param value the non-null value to be associated with the key
61 * @return future for the previous mapping or {@code null} if there was no previous mapping. Future will
62 * be completed with a NoSuchDocumentPathException if the parent node (for the node to create/update) does not exist
63 */
64 CompletableFuture<Versioned<V>> set(DocumentPath path, V value);
65
66 /**
67 * Creates a document tree node if one does not exist already.
68 *
69 * @param path path for the node to create
70 * @param value the non-null value to be associated with the key
71 * @return future that is completed with {@code true} if the mapping could be added
72 * successfully; {@code false} otherwise. Future will be completed with a
73 * IllegalDocumentModificationException if the parent node (for the node to create) does not exist
74 */
75 CompletableFuture<Boolean> create(DocumentPath path, V value);
76
77 /**
78 * Conditionally updates a tree node if the current version matches a specified version.
79 *
80 * @param path path for the node to create
81 * @param newValue the non-null value to be associated with the key
82 * @param version current version of the value for update to occur
83 * @return future that is completed with {@code true} if the update was made and the tree was
84 * modified, {@code false} otherwise.
85 */
86 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version);
87
88 /**
89 * Conditionally updates a tree node if the current value matches a specified value.
90 *
91 * @param path path for the node to create
92 * @param newValue the non-null value to be associated with the key
93 * @param currentValue current value for update to occur
94 * @return future that is completed with {@code true} if the update was made and the tree was
95 * modified, {@code false} otherwise.
96 */
97 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue);
98
99 /**
100 * Removes the node with the specified path.
101 *
102 * @param path path for the node to remove
103 * @return future for the previous value. Future will be completed with a
104 * IllegalDocumentModificationException if the node to be removed is either the root
105 * node or has one or more children. Future will be completed with a
106 * NoSuchDocumentPathException if the node to be removed does not exist
107 */
108 CompletableFuture<Versioned<V>> removeNode(DocumentPath path);
109
110 /**
111 * Registers a listener to be notified when a subtree rooted at the specified path
112 * is modified.
113 *
114 * @param path path to root of subtree to monitor for updates
115 * @param listener listener to be notified
116 */
117 CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener);
118
119 /**
120 * Unregisters a previously added listener.
121 *
122 * @param listener listener to unregister
123 */
124 CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener);
125
126 /**
127 * Registers a listener to be notified when the tree is modified.
128 *
129 * @param listener listener to be notified
130 */
131 default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) {
132 return addListener(root(), listener);
133 }
134}