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.base.Throwables; |
| 24 | import com.google.common.collect.Lists; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
| 26 | import org.onosproject.store.primitives.NodeUpdate; |
| 27 | import org.onosproject.store.primitives.TransactionId; |
| 28 | import org.onosproject.store.service.AsyncDocumentTree; |
| 29 | import org.onosproject.store.service.DocumentPath; |
| 30 | import org.onosproject.store.service.DocumentTreeEvent; |
| 31 | import org.onosproject.store.service.DocumentTreeListener; |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame^] | 32 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 33 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.TransactionLog; |
| 35 | import org.onosproject.store.service.Version; |
| 36 | import org.onosproject.store.service.Versioned; |
| 37 | |
| 38 | /** |
| 39 | * Atomix document tree. |
| 40 | */ |
| 41 | public class AtomixDocumentTree<V> implements AsyncDocumentTree<V> { |
| 42 | private final io.atomix.core.tree.AsyncAtomicDocumentTree<V> atomixTree; |
| 43 | private final Map<DocumentTreeListener<V>, io.atomix.core.tree.DocumentTreeEventListener<V>> listenerMap = |
| 44 | Maps.newIdentityHashMap(); |
| 45 | |
| 46 | public AtomixDocumentTree(io.atomix.core.tree.AsyncAtomicDocumentTree<V> atomixTree) { |
| 47 | this.atomixTree = atomixTree; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public String name() { |
| 52 | return atomixTree.name(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public DocumentPath root() { |
| 57 | return toOnosPath(atomixTree.root()); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path) { |
| 62 | return atomixTree.getChildren(toAtomixPath(path)) |
| 63 | .thenApply(map -> map.entrySet().stream() |
| 64 | .collect(Collectors.toMap(e -> e.getKey(), |
| 65 | e -> toVersioned(e.getValue())))); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public CompletableFuture<Versioned<V>> get(DocumentPath path) { |
| 70 | return atomixTree.get(toAtomixPath(path)).thenApply(this::toVersioned); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public CompletableFuture<Versioned<V>> set(DocumentPath path, V value) { |
| 75 | return atomixTree.set(toAtomixPath(path), value).thenApply(this::toVersioned); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public CompletableFuture<Boolean> create(DocumentPath path, V value) { |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame^] | 80 | return convertException(atomixTree.create(toAtomixPath(path), value)); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) { |
| 85 | return atomixTree.createRecursive(toAtomixPath(path), value); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) { |
| 90 | return atomixTree.replace(toAtomixPath(path), newValue, version); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) { |
| 95 | return atomixTree.replace(toAtomixPath(path), newValue, currentValue); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) { |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame^] | 100 | return atomixTree.remove(toAtomixPath(path)).thenApply(this::toVersioned); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public synchronized CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
| 105 | io.atomix.core.tree.DocumentTreeEventListener<V> atomixListener = event -> |
| 106 | listener.event(new DocumentTreeEvent<V>( |
| 107 | DocumentPath.from(event.path().pathElements()), |
| 108 | DocumentTreeEvent.Type.valueOf(event.type().name()), |
| 109 | event.newValue().map(this::toVersioned), |
| 110 | event.oldValue().map(this::toVersioned))); |
| 111 | listenerMap.put(listener, atomixListener); |
| 112 | return atomixTree.addListener(toAtomixPath(path), atomixListener); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) { |
| 117 | io.atomix.core.tree.DocumentTreeEventListener<V> atomixListener = listenerMap.remove(listener); |
| 118 | if (atomixListener != null) { |
| 119 | return atomixTree.removeListener(atomixListener); |
| 120 | } |
| 121 | return CompletableFuture.completedFuture(null); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
| 126 | throw new UnsupportedOperationException(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public CompletableFuture<Boolean> prepare(TransactionLog<NodeUpdate<V>> transactionLog) { |
| 131 | throw new UnsupportedOperationException(); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<NodeUpdate<V>> transactionLog) { |
| 136 | throw new UnsupportedOperationException(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 141 | throw new UnsupportedOperationException(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 146 | throw new UnsupportedOperationException(); |
| 147 | } |
| 148 | |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame^] | 149 | private <T> CompletableFuture<T> convertException(CompletableFuture<T> future) { |
| 150 | CompletableFuture<T> newFuture = new CompletableFuture<>(); |
| 151 | future.whenComplete((result, error) -> { |
| 152 | if (error == null) { |
| 153 | newFuture.complete(result); |
| 154 | } else { |
| 155 | Throwable cause = Throwables.getRootCause(error); |
| 156 | if (cause instanceof io.atomix.core.tree.NoSuchDocumentPathException) { |
| 157 | newFuture.completeExceptionally(new NoSuchDocumentPathException()); |
| 158 | } else if (cause instanceof io.atomix.core.tree.IllegalDocumentModificationException) { |
| 159 | newFuture.completeExceptionally(new IllegalDocumentModificationException()); |
| 160 | } else { |
| 161 | newFuture.completeExceptionally(cause); |
| 162 | } |
| 163 | } |
| 164 | }); |
| 165 | return newFuture; |
| 166 | } |
| 167 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 168 | private DocumentPath toOnosPath(io.atomix.core.tree.DocumentPath path) { |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame^] | 169 | List<String> pathElements = Lists.newArrayList(path.pathElements()); |
| 170 | pathElements.set(0, DocumentPath.ROOT.pathElements().get(0)); |
| 171 | return DocumentPath.from(pathElements); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private io.atomix.core.tree.DocumentPath toAtomixPath(DocumentPath path) { |
Jordan Halterman | 6aca84c | 2018-07-31 13:33:13 -0700 | [diff] [blame^] | 175 | List<String> pathElements = Lists.newArrayList(path.pathElements()); |
| 176 | pathElements.set(0, ""); |
| 177 | return io.atomix.core.tree.DocumentPath.from(pathElements); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private Versioned<V> toVersioned(io.atomix.utils.time.Versioned<V> versioned) { |
| 181 | return versioned != null |
| 182 | ? new Versioned<>(versioned.value(), versioned.version(), versioned.creationTime()) |
| 183 | : null; |
| 184 | } |
| 185 | } |