blob: 1989607474243cc1f4507f1cc0a852a8b49cf9b5 [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
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 Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
Jordan Halterman6aca84c2018-07-31 13:33:13 -070018import java.util.List;
Jordan Halterman00e92da2018-05-22 23:05:52 -070019import java.util.Map;
20import java.util.concurrent.CompletableFuture;
21import java.util.stream.Collectors;
22
Jordan Halterman6aca84c2018-07-31 13:33:13 -070023import com.google.common.base.Throwables;
24import com.google.common.collect.Lists;
Jordan Halterman00e92da2018-05-22 23:05:52 -070025import com.google.common.collect.Maps;
26import org.onosproject.store.primitives.NodeUpdate;
27import org.onosproject.store.primitives.TransactionId;
28import org.onosproject.store.service.AsyncDocumentTree;
29import org.onosproject.store.service.DocumentPath;
30import org.onosproject.store.service.DocumentTreeEvent;
31import org.onosproject.store.service.DocumentTreeListener;
Jordan Halterman6aca84c2018-07-31 13:33:13 -070032import org.onosproject.store.service.IllegalDocumentModificationException;
33import org.onosproject.store.service.NoSuchDocumentPathException;
Jordan Halterman00e92da2018-05-22 23:05:52 -070034import org.onosproject.store.service.TransactionLog;
35import org.onosproject.store.service.Version;
36import org.onosproject.store.service.Versioned;
37
38/**
39 * Atomix document tree.
40 */
41public 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 Halterman6aca84c2018-07-31 13:33:13 -070080 return convertException(atomixTree.create(toAtomixPath(path), value));
Jordan Halterman00e92da2018-05-22 23:05:52 -070081 }
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 Halterman6aca84c2018-07-31 13:33:13 -0700100 return atomixTree.remove(toAtomixPath(path)).thenApply(this::toVersioned);
Jordan Halterman00e92da2018-05-22 23:05:52 -0700101 }
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 Halterman6aca84c2018-07-31 13:33:13 -0700149 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 Halterman00e92da2018-05-22 23:05:52 -0700168 private DocumentPath toOnosPath(io.atomix.core.tree.DocumentPath path) {
Jordan Halterman6aca84c2018-07-31 13:33:13 -0700169 List<String> pathElements = Lists.newArrayList(path.pathElements());
170 pathElements.set(0, DocumentPath.ROOT.pathElements().get(0));
171 return DocumentPath.from(pathElements);
Jordan Halterman00e92da2018-05-22 23:05:52 -0700172 }
173
174 private io.atomix.core.tree.DocumentPath toAtomixPath(DocumentPath path) {
Jordan Halterman6aca84c2018-07-31 13:33:13 -0700175 List<String> pathElements = Lists.newArrayList(path.pathElements());
176 pathElements.set(0, "");
177 return io.atomix.core.tree.DocumentPath.from(pathElements);
Jordan Halterman00e92da2018-05-22 23:05:52 -0700178 }
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}