Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.atomix.primitives.impl; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 17 | |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame] | 18 | import java.util.List; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 19 | import java.util.Map; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | import java.util.stream.Collectors; |
| 22 | |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Maps; |
| 25 | import org.onosproject.store.primitives.NodeUpdate; |
| 26 | import org.onosproject.store.primitives.TransactionId; |
| 27 | import org.onosproject.store.service.AsyncDocumentTree; |
| 28 | import org.onosproject.store.service.DocumentPath; |
| 29 | import org.onosproject.store.service.DocumentTreeEvent; |
| 30 | import org.onosproject.store.service.DocumentTreeListener; |
| 31 | import org.onosproject.store.service.TransactionLog; |
| 32 | import org.onosproject.store.service.Version; |
| 33 | import org.onosproject.store.service.Versioned; |
| 34 | |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 35 | import static org.onosproject.store.atomix.primitives.impl.AtomixFutures.adaptTreeFuture; |
| 36 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Atomix document tree. |
| 39 | */ |
| 40 | public class AtomixDocumentTree<V> implements AsyncDocumentTree<V> { |
| 41 | private final io.atomix.core.tree.AsyncAtomicDocumentTree<V> atomixTree; |
| 42 | private final Map<DocumentTreeListener<V>, io.atomix.core.tree.DocumentTreeEventListener<V>> listenerMap = |
| 43 | Maps.newIdentityHashMap(); |
| 44 | |
| 45 | public AtomixDocumentTree(io.atomix.core.tree.AsyncAtomicDocumentTree<V> atomixTree) { |
| 46 | this.atomixTree = atomixTree; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public String name() { |
| 51 | return atomixTree.name(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public DocumentPath root() { |
| 56 | return toOnosPath(atomixTree.root()); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 61 | return adaptTreeFuture(atomixTree.getChildren(toAtomixPath(path))) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 62 | .thenApply(map -> map.entrySet().stream() |
| 63 | .collect(Collectors.toMap(e -> e.getKey(), |
| 64 | e -> toVersioned(e.getValue())))); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public CompletableFuture<Versioned<V>> get(DocumentPath path) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 69 | return adaptTreeFuture(atomixTree.get(toAtomixPath(path))).thenApply(this::toVersioned); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public CompletableFuture<Versioned<V>> set(DocumentPath path, V value) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 74 | return adaptTreeFuture(atomixTree.set(toAtomixPath(path), value)).thenApply(this::toVersioned); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public CompletableFuture<Boolean> create(DocumentPath path, V value) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 79 | return adaptTreeFuture(atomixTree.create(toAtomixPath(path), value)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 84 | return adaptTreeFuture(atomixTree.createRecursive(toAtomixPath(path), value)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 89 | return adaptTreeFuture(atomixTree.replace(toAtomixPath(path), newValue, version)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 94 | return adaptTreeFuture(atomixTree.replace(toAtomixPath(path), newValue, currentValue)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 99 | return adaptTreeFuture(atomixTree.remove(toAtomixPath(path))).thenApply(this::toVersioned); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public synchronized CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
| 104 | io.atomix.core.tree.DocumentTreeEventListener<V> atomixListener = event -> |
| 105 | listener.event(new DocumentTreeEvent<V>( |
Jordan Halterman | 5409e34 | 2018-09-26 14:31:26 -0700 | [diff] [blame] | 106 | toOnosPath(event.path()), |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 107 | DocumentTreeEvent.Type.valueOf(event.type().name()), |
| 108 | event.newValue().map(this::toVersioned), |
| 109 | event.oldValue().map(this::toVersioned))); |
| 110 | listenerMap.put(listener, atomixListener); |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 111 | return adaptTreeFuture(atomixTree.addListener(toAtomixPath(path), atomixListener)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) { |
| 116 | io.atomix.core.tree.DocumentTreeEventListener<V> atomixListener = listenerMap.remove(listener); |
| 117 | if (atomixListener != null) { |
Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 118 | return adaptTreeFuture(atomixTree.removeListener(atomixListener)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 119 | } |
| 120 | return CompletableFuture.completedFuture(null); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
| 125 | throw new UnsupportedOperationException(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public CompletableFuture<Boolean> prepare(TransactionLog<NodeUpdate<V>> transactionLog) { |
| 130 | throw new UnsupportedOperationException(); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<NodeUpdate<V>> transactionLog) { |
| 135 | throw new UnsupportedOperationException(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 140 | throw new UnsupportedOperationException(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 145 | throw new UnsupportedOperationException(); |
| 146 | } |
| 147 | |
Jordan Halterman | 35a02ea | 2018-08-22 11:57:32 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public CompletableFuture<Void> destroy() { |
| 150 | return adaptTreeFuture(atomixTree.delete()); |
| 151 | } |
| 152 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 153 | private DocumentPath toOnosPath(io.atomix.core.tree.DocumentPath path) { |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame] | 154 | List<String> pathElements = Lists.newArrayList(path.pathElements()); |
| 155 | pathElements.set(0, DocumentPath.ROOT.pathElements().get(0)); |
| 156 | return DocumentPath.from(pathElements); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private io.atomix.core.tree.DocumentPath toAtomixPath(DocumentPath path) { |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame] | 160 | List<String> pathElements = Lists.newArrayList(path.pathElements()); |
Jordan Halterman | 5409e34 | 2018-09-26 14:31:26 -0700 | [diff] [blame] | 161 | // We need to remove the root element here since the Atomix factory method assumes no root is present. |
| 162 | pathElements.remove(0); |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame] | 163 | return io.atomix.core.tree.DocumentPath.from(pathElements); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | private Versioned<V> toVersioned(io.atomix.utils.time.Versioned<V> versioned) { |
| 167 | return versioned != null |
| 168 | ? new Versioned<>(versioned.value(), versioned.version(), versioned.creationTime()) |
| 169 | : null; |
| 170 | } |
| 171 | } |