Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 19 | import java.util.HashMap; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 22 | import java.util.Optional; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 23 | import java.util.concurrent.CompletableFuture; |
| 24 | import java.util.concurrent.Executor; |
| 25 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 26 | import com.google.common.util.concurrent.MoreExecutors; |
| 27 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 28 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 29 | import org.onlab.util.Match; |
| 30 | import org.onlab.util.Tools; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 31 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Get; |
| 32 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GetChildren; |
| 33 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Listen; |
| 34 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Unlisten; |
| 35 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Update; |
| 36 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 37 | import org.onosproject.store.service.AsyncDocumentTree; |
| 38 | import org.onosproject.store.service.DocumentPath; |
| 39 | import org.onosproject.store.service.DocumentTreeEvent; |
| 40 | import org.onosproject.store.service.DocumentTreeListener; |
| 41 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 42 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 43 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 44 | import org.onosproject.store.service.Versioned; |
| 45 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | import static com.google.common.base.Preconditions.checkNotNull; |
| 47 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeEvents.CHANGE; |
| 48 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.ADD_LISTENER; |
| 49 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.CLEAR; |
| 50 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GET; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GET_CHILDREN; |
| 52 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.REMOVE_LISTENER; |
| 53 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.UPDATE; |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 54 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeResult.Status.ILLEGAL_MODIFICATION; |
| 55 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeResult.Status.INVALID_PATH; |
| 56 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeResult.Status.OK; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Distributed resource providing the {@link AsyncDocumentTree} primitive. |
| 60 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 61 | public class AtomixDocumentTree extends AbstractRaftPrimitive implements AsyncDocumentTree<byte[]> { |
| 62 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 63 | .register(KryoNamespaces.BASIC) |
| 64 | .register(AtomixDocumentTreeOperations.NAMESPACE) |
| 65 | .register(AtomixDocumentTreeEvents.NAMESPACE) |
| 66 | .build()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 67 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 68 | private final Map<DocumentTreeListener<byte[]>, InternalListener> eventListeners = new HashMap<>(); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 69 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 70 | public AtomixDocumentTree(RaftProxy proxy) { |
| 71 | super(proxy); |
| 72 | proxy.addStateChangeListener(state -> { |
| 73 | if (state == RaftProxy.State.CONNECTED && isListening()) { |
| 74 | proxy.invoke(ADD_LISTENER, SERIALIZER::encode, new Listen()); |
| 75 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 76 | }); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 77 | proxy.addEventListener(CHANGE, SERIALIZER::decode, this::processTreeUpdates); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Type primitiveType() { |
| 82 | return Type.DOCUMENT_TREE; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public CompletableFuture<Void> destroy() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 87 | return proxy.invoke(CLEAR); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public DocumentPath root() { |
| 92 | return DocumentPath.from("root"); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public CompletableFuture<Map<String, Versioned<byte[]>>> getChildren(DocumentPath path) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 97 | return proxy.<GetChildren, DocumentTreeResult<Map<String, Versioned<byte[]>>>>invoke( |
| 98 | GET_CHILDREN, |
| 99 | SERIALIZER::encode, |
| 100 | new GetChildren(checkNotNull(path)), |
| 101 | SERIALIZER::decode) |
| 102 | .thenCompose(result -> { |
| 103 | if (result.status() == INVALID_PATH) { |
| 104 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
| 105 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
| 106 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 107 | } else { |
| 108 | return CompletableFuture.completedFuture(result); |
| 109 | } |
| 110 | }).thenApply(result -> result.result()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public CompletableFuture<Versioned<byte[]>> get(DocumentPath path) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 115 | return proxy.invoke(GET, SERIALIZER::encode, new Get(checkNotNull(path)), SERIALIZER::decode); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public CompletableFuture<Versioned<byte[]>> set(DocumentPath path, byte[] value) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 120 | return proxy.<Update, DocumentTreeResult<Versioned<byte[]>>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 121 | SERIALIZER::encode, |
| 122 | new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.any()), |
| 123 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 124 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 125 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 126 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 127 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 128 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 129 | } else { |
| 130 | return CompletableFuture.completedFuture(result); |
| 131 | } |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 132 | }).thenApply(result -> result.result()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public CompletableFuture<Boolean> create(DocumentPath path, byte[] value) { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 137 | return createInternal(path, value) |
| 138 | .thenCompose(status -> { |
| 139 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 140 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 141 | } |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 142 | return CompletableFuture.completedFuture(true); |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, byte[] value) { |
| 148 | return createInternal(path, value) |
| 149 | .thenCompose(status -> { |
| 150 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 151 | return createRecursive(path.parent(), null) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 152 | .thenCompose(r -> createInternal(path, value).thenApply(v -> true)); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 153 | } |
| 154 | return CompletableFuture.completedFuture(status == OK); |
| 155 | }); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public CompletableFuture<Boolean> replace(DocumentPath path, byte[] newValue, long version) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 160 | return proxy.<Update, DocumentTreeResult<byte[]>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 161 | SERIALIZER::encode, |
| 162 | new Update(checkNotNull(path), |
| 163 | Optional.ofNullable(newValue), |
| 164 | Match.any(), |
| 165 | Match.ifValue(version)), SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 166 | .thenApply(result -> result.updated()); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public CompletableFuture<Boolean> replace(DocumentPath path, byte[] newValue, byte[] currentValue) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 171 | return proxy.<Update, DocumentTreeResult<byte[]>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 172 | SERIALIZER::encode, |
| 173 | new Update(checkNotNull(path), |
| 174 | Optional.ofNullable(newValue), |
| 175 | Match.ifValue(currentValue), |
| 176 | Match.any()), |
| 177 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 178 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 179 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 180 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 181 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 182 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 183 | } else { |
| 184 | return CompletableFuture.completedFuture(result); |
| 185 | } |
| 186 | }).thenApply(result -> result.updated()); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public CompletableFuture<Versioned<byte[]>> removeNode(DocumentPath path) { |
| 191 | if (path.equals(DocumentPath.from("root"))) { |
| 192 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 193 | } |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 194 | return proxy.<Update, DocumentTreeResult<Versioned<byte[]>>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 195 | SERIALIZER::encode, |
| 196 | new Update(checkNotNull(path), null, Match.any(), Match.ifNotNull()), |
| 197 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 198 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 199 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 200 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 201 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 202 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 203 | } else { |
| 204 | return CompletableFuture.completedFuture(result); |
| 205 | } |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 206 | }).thenApply(result -> result.result()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<byte[]> listener) { |
| 211 | checkNotNull(path); |
| 212 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 213 | InternalListener internalListener = new InternalListener(path, listener, MoreExecutors.directExecutor()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 214 | // TODO: Support API that takes an executor |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 215 | if (!eventListeners.containsKey(listener)) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 216 | return proxy.invoke(ADD_LISTENER, SERIALIZER::encode, new Listen(path)) |
| 217 | .thenRun(() -> eventListeners.put(listener, internalListener)); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 218 | } |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 219 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public CompletableFuture<Void> removeListener(DocumentTreeListener<byte[]> listener) { |
| 224 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 225 | InternalListener internalListener = eventListeners.remove(listener); |
| 226 | if (internalListener != null && eventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 227 | return proxy.invoke(REMOVE_LISTENER, SERIALIZER::encode, new Unlisten(internalListener.path)) |
| 228 | .thenApply(v -> null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 229 | } |
| 230 | return CompletableFuture.completedFuture(null); |
| 231 | } |
| 232 | |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 233 | private CompletableFuture<DocumentTreeResult.Status> createInternal(DocumentPath path, byte[] value) { |
| 234 | return proxy.<Update, DocumentTreeResult<byte[]>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 235 | SERIALIZER::encode, |
| 236 | new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.ifNull()), |
| 237 | SERIALIZER::decode) |
| 238 | .thenApply(result -> result.status()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 241 | private boolean isListening() { |
| 242 | return !eventListeners.isEmpty(); |
| 243 | } |
| 244 | |
| 245 | private void processTreeUpdates(List<DocumentTreeEvent<byte[]>> events) { |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 246 | events.forEach(event -> eventListeners.values().forEach(listener -> listener.event(event))); |
| 247 | } |
| 248 | |
| 249 | private class InternalListener implements DocumentTreeListener<byte[]> { |
| 250 | |
| 251 | private final DocumentPath path; |
| 252 | private final DocumentTreeListener<byte[]> listener; |
| 253 | private final Executor executor; |
| 254 | |
| 255 | public InternalListener(DocumentPath path, DocumentTreeListener<byte[]> listener, Executor executor) { |
| 256 | this.path = path; |
| 257 | this.listener = listener; |
| 258 | this.executor = executor; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void event(DocumentTreeEvent<byte[]> event) { |
| 263 | if (event.path().isDescendentOf(path)) { |
| 264 | executor.execute(() -> listener.event(event)); |
| 265 | } |
| 266 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 267 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 268 | } |