blob: 1b5974d5a6bfe670fa060f0b06979d201062429d [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
18import java.util.Map;
19import java.util.concurrent.CompletableFuture;
20import java.util.stream.Collectors;
21
22import com.google.common.collect.Maps;
23import org.onosproject.store.primitives.NodeUpdate;
24import org.onosproject.store.primitives.TransactionId;
25import org.onosproject.store.service.AsyncDocumentTree;
26import org.onosproject.store.service.DocumentPath;
27import org.onosproject.store.service.DocumentTreeEvent;
28import org.onosproject.store.service.DocumentTreeListener;
29import org.onosproject.store.service.TransactionLog;
30import org.onosproject.store.service.Version;
31import org.onosproject.store.service.Versioned;
32
33/**
34 * Atomix document tree.
35 */
36public class AtomixDocumentTree<V> implements AsyncDocumentTree<V> {
37 private final io.atomix.core.tree.AsyncAtomicDocumentTree<V> atomixTree;
38 private final Map<DocumentTreeListener<V>, io.atomix.core.tree.DocumentTreeEventListener<V>> listenerMap =
39 Maps.newIdentityHashMap();
40
41 public AtomixDocumentTree(io.atomix.core.tree.AsyncAtomicDocumentTree<V> atomixTree) {
42 this.atomixTree = atomixTree;
43 }
44
45 @Override
46 public String name() {
47 return atomixTree.name();
48 }
49
50 @Override
51 public DocumentPath root() {
52 return toOnosPath(atomixTree.root());
53 }
54
55 @Override
56 public CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path) {
57 return atomixTree.getChildren(toAtomixPath(path))
58 .thenApply(map -> map.entrySet().stream()
59 .collect(Collectors.toMap(e -> e.getKey(),
60 e -> toVersioned(e.getValue()))));
61 }
62
63 @Override
64 public CompletableFuture<Versioned<V>> get(DocumentPath path) {
65 return atomixTree.get(toAtomixPath(path)).thenApply(this::toVersioned);
66 }
67
68 @Override
69 public CompletableFuture<Versioned<V>> set(DocumentPath path, V value) {
70 return atomixTree.set(toAtomixPath(path), value).thenApply(this::toVersioned);
71 }
72
73 @Override
74 public CompletableFuture<Boolean> create(DocumentPath path, V value) {
75 return atomixTree.create(toAtomixPath(path), value);
76 }
77
78 @Override
79 public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) {
80 return atomixTree.createRecursive(toAtomixPath(path), value);
81 }
82
83 @Override
84 public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) {
85 return atomixTree.replace(toAtomixPath(path), newValue, version);
86 }
87
88 @Override
89 public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) {
90 return atomixTree.replace(toAtomixPath(path), newValue, currentValue);
91 }
92
93 @Override
94 public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) {
95 return atomixTree.removeNode(toAtomixPath(path)).thenApply(this::toVersioned);
96 }
97
98 @Override
99 public synchronized CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) {
100 io.atomix.core.tree.DocumentTreeEventListener<V> atomixListener = event ->
101 listener.event(new DocumentTreeEvent<V>(
102 DocumentPath.from(event.path().pathElements()),
103 DocumentTreeEvent.Type.valueOf(event.type().name()),
104 event.newValue().map(this::toVersioned),
105 event.oldValue().map(this::toVersioned)));
106 listenerMap.put(listener, atomixListener);
107 return atomixTree.addListener(toAtomixPath(path), atomixListener);
108 }
109
110 @Override
111 public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) {
112 io.atomix.core.tree.DocumentTreeEventListener<V> atomixListener = listenerMap.remove(listener);
113 if (atomixListener != null) {
114 return atomixTree.removeListener(atomixListener);
115 }
116 return CompletableFuture.completedFuture(null);
117 }
118
119 @Override
120 public CompletableFuture<Version> begin(TransactionId transactionId) {
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public CompletableFuture<Boolean> prepare(TransactionLog<NodeUpdate<V>> transactionLog) {
126 throw new UnsupportedOperationException();
127 }
128
129 @Override
130 public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<NodeUpdate<V>> transactionLog) {
131 throw new UnsupportedOperationException();
132 }
133
134 @Override
135 public CompletableFuture<Void> commit(TransactionId transactionId) {
136 throw new UnsupportedOperationException();
137 }
138
139 @Override
140 public CompletableFuture<Void> rollback(TransactionId transactionId) {
141 throw new UnsupportedOperationException();
142 }
143
144 private DocumentPath toOnosPath(io.atomix.core.tree.DocumentPath path) {
145 return DocumentPath.from(path.pathElements());
146 }
147
148 private io.atomix.core.tree.DocumentPath toAtomixPath(DocumentPath path) {
149 return io.atomix.core.tree.DocumentPath.from(path.pathElements());
150 }
151
152 private Versioned<V> toVersioned(io.atomix.utils.time.Versioned<V> versioned) {
153 return versioned != null
154 ? new Versioned<>(versioned.value(), versioned.version(), versioned.creationTime())
155 : null;
156 }
157}