Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.concurrent.Executor; |
| 21 | |
| 22 | import com.google.common.collect.Maps; |
| 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.store.service.AsyncDocumentTree; |
| 25 | import org.onosproject.store.service.DocumentPath; |
| 26 | import org.onosproject.store.service.DocumentTreeListener; |
| 27 | import org.onosproject.store.service.Versioned; |
| 28 | |
| 29 | /** |
| 30 | * {@link AsyncDocumentTree} that executes asynchronous callbacks on a user provided |
| 31 | * {@link Executor}. |
| 32 | */ |
| 33 | public class ExecutingAsyncDocumentTree<V> extends ExecutingDistributedPrimitive implements AsyncDocumentTree<V> { |
| 34 | private final AsyncDocumentTree<V> delegateTree; |
| 35 | private final Executor executor; |
| 36 | private final Map<DocumentTreeListener<V>, DocumentTreeListener<V>> listenerMap = Maps.newConcurrentMap(); |
| 37 | |
| 38 | public ExecutingAsyncDocumentTree(AsyncDocumentTree<V> delegateTree, Executor executor) { |
| 39 | super(delegateTree, executor); |
| 40 | this.delegateTree = delegateTree; |
| 41 | this.executor = executor; |
| 42 | } |
| 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) { |
| 51 | return Tools.asyncFuture(delegateTree.getChildren(path), executor); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public CompletableFuture<Versioned<V>> get(DocumentPath path) { |
| 56 | return Tools.asyncFuture(delegateTree.get(path), executor); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public CompletableFuture<Versioned<V>> set(DocumentPath path, V value) { |
| 61 | return Tools.asyncFuture(delegateTree.set(path, value), executor); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public CompletableFuture<Boolean> create(DocumentPath path, V value) { |
| 66 | return Tools.asyncFuture(delegateTree.create(path, value), executor); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) { |
| 71 | return Tools.asyncFuture(delegateTree.createRecursive(path, value), executor); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) { |
| 76 | return Tools.asyncFuture(delegateTree.replace(path, newValue, version), executor); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) { |
| 81 | return Tools.asyncFuture(delegateTree.replace(path, newValue, currentValue), executor); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) { |
| 86 | return Tools.asyncFuture(delegateTree.removeNode(path), executor); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
| 91 | DocumentTreeListener<V> wrappedListener = e -> executor.execute(() -> listener.event(e)); |
| 92 | listenerMap.put(listener, wrappedListener); |
| 93 | return Tools.asyncFuture(delegateTree.addListener(path, wrappedListener), executor); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) { |
| 98 | DocumentTreeListener<V> wrappedListener = listenerMap.remove(listener); |
| 99 | if (wrappedListener != null) { |
| 100 | return Tools.asyncFuture(delegateTree.removeListener(wrappedListener), executor); |
| 101 | } |
| 102 | return CompletableFuture.completedFuture(null); |
| 103 | } |
| 104 | } |