blob: f6fc3d106b660ff09f799bfb6a99a2ebd5b220ed [file] [log] [blame]
Jordan Halterman9bdc24f2017-04-19 23:45:12 -07001/*
2 * Copyright 2017-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 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.Map;
19import java.util.concurrent.CompletableFuture;
20import java.util.concurrent.Executor;
21
22import com.google.common.collect.Maps;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070023import org.onosproject.store.service.AsyncDocumentTree;
24import org.onosproject.store.service.DocumentPath;
25import org.onosproject.store.service.DocumentTreeListener;
26import org.onosproject.store.service.Versioned;
27
28/**
29 * {@link AsyncDocumentTree} that executes asynchronous callbacks on a user provided
30 * {@link Executor}.
31 */
32public class ExecutingAsyncDocumentTree<V> extends ExecutingDistributedPrimitive implements AsyncDocumentTree<V> {
33 private final AsyncDocumentTree<V> delegateTree;
Jordan Halterman046faeb2017-05-01 15:10:13 -070034 private final Executor orderedExecutor;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070035 private final Map<DocumentTreeListener<V>, DocumentTreeListener<V>> listenerMap = Maps.newConcurrentMap();
36
Jordan Halterman046faeb2017-05-01 15:10:13 -070037 public ExecutingAsyncDocumentTree(
38 AsyncDocumentTree<V> delegateTree, Executor orderedExecutor, Executor threadPoolExecutor) {
39 super(delegateTree, orderedExecutor, threadPoolExecutor);
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070040 this.delegateTree = delegateTree;
Jordan Halterman046faeb2017-05-01 15:10:13 -070041 this.orderedExecutor = orderedExecutor;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070042 }
43
44 @Override
45 public DocumentPath root() {
46 return delegateTree.root();
47 }
48
49 @Override
50 public CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070051 return asyncFuture(delegateTree.getChildren(path));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070052 }
53
54 @Override
55 public CompletableFuture<Versioned<V>> get(DocumentPath path) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070056 return asyncFuture(delegateTree.get(path));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070057 }
58
59 @Override
60 public CompletableFuture<Versioned<V>> set(DocumentPath path, V value) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070061 return asyncFuture(delegateTree.set(path, value));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070062 }
63
64 @Override
65 public CompletableFuture<Boolean> create(DocumentPath path, V value) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070066 return asyncFuture(delegateTree.create(path, value));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070067 }
68
69 @Override
70 public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070071 return asyncFuture(delegateTree.createRecursive(path, value));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070072 }
73
74 @Override
75 public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070076 return asyncFuture(delegateTree.replace(path, newValue, version));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070077 }
78
79 @Override
80 public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070081 return asyncFuture(delegateTree.replace(path, newValue, currentValue));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070082 }
83
84 @Override
85 public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070086 return asyncFuture(delegateTree.removeNode(path));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070087 }
88
89 @Override
90 public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) {
Jordan Halterman046faeb2017-05-01 15:10:13 -070091 DocumentTreeListener<V> wrappedListener = e -> orderedExecutor.execute(() -> listener.event(e));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070092 listenerMap.put(listener, wrappedListener);
Jordan Halterman046faeb2017-05-01 15:10:13 -070093 return asyncFuture(delegateTree.addListener(path, wrappedListener));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070094 }
95
96 @Override
97 public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) {
98 DocumentTreeListener<V> wrappedListener = listenerMap.remove(listener);
99 if (wrappedListener != null) {
Jordan Halterman046faeb2017-05-01 15:10:13 -0700100 return asyncFuture(delegateTree.removeListener(wrappedListener));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700101 }
102 return CompletableFuture.completedFuture(null);
103 }
104}