Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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; |
| 54 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeUpdateResult.Status.ILLEGAL_MODIFICATION; |
| 55 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeUpdateResult.Status.INVALID_PATH; |
| 56 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeUpdateResult.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 | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 97 | return proxy.invoke(GET_CHILDREN, SERIALIZER::encode, new GetChildren(checkNotNull(path)), SERIALIZER::decode); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public CompletableFuture<Versioned<byte[]>> get(DocumentPath path) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 102 | return proxy.invoke(GET, SERIALIZER::encode, new Get(checkNotNull(path)), SERIALIZER::decode); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public CompletableFuture<Versioned<byte[]>> set(DocumentPath path, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 107 | return proxy.<Update, DocumentTreeUpdateResult<byte[]>>invoke(UPDATE, |
| 108 | SERIALIZER::encode, |
| 109 | new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.any()), |
| 110 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 111 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 112 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 113 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 114 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 115 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 116 | } else { |
| 117 | return CompletableFuture.completedFuture(result); |
| 118 | } |
| 119 | }).thenApply(result -> result.oldValue()); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public CompletableFuture<Boolean> create(DocumentPath path, byte[] value) { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 124 | return createInternal(path, value) |
| 125 | .thenCompose(status -> { |
| 126 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 127 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 128 | } |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 129 | return CompletableFuture.completedFuture(true); |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, byte[] value) { |
| 135 | return createInternal(path, value) |
| 136 | .thenCompose(status -> { |
| 137 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 138 | return createRecursive(path.parent(), null) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 139 | .thenCompose(r -> createInternal(path, value).thenApply(v -> true)); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 140 | } |
| 141 | return CompletableFuture.completedFuture(status == OK); |
| 142 | }); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public CompletableFuture<Boolean> replace(DocumentPath path, byte[] newValue, long version) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 147 | return proxy.<Update, DocumentTreeUpdateResult<byte[]>>invoke(UPDATE, |
| 148 | SERIALIZER::encode, |
| 149 | new Update(checkNotNull(path), |
| 150 | Optional.ofNullable(newValue), |
| 151 | Match.any(), |
| 152 | Match.ifValue(version)), SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 153 | .thenApply(result -> result.updated()); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public CompletableFuture<Boolean> replace(DocumentPath path, byte[] newValue, byte[] currentValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 158 | return proxy.<Update, DocumentTreeUpdateResult<byte[]>>invoke(UPDATE, |
| 159 | SERIALIZER::encode, |
| 160 | new Update(checkNotNull(path), |
| 161 | Optional.ofNullable(newValue), |
| 162 | Match.ifValue(currentValue), |
| 163 | Match.any()), |
| 164 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 165 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 166 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 167 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 168 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 169 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 170 | } else { |
| 171 | return CompletableFuture.completedFuture(result); |
| 172 | } |
| 173 | }).thenApply(result -> result.updated()); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public CompletableFuture<Versioned<byte[]>> removeNode(DocumentPath path) { |
| 178 | if (path.equals(DocumentPath.from("root"))) { |
| 179 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 180 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 181 | return proxy.<Update, DocumentTreeUpdateResult<byte[]>>invoke(UPDATE, |
| 182 | SERIALIZER::encode, |
| 183 | new Update(checkNotNull(path), null, Match.any(), Match.ifNotNull()), |
| 184 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 185 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 186 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 187 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 188 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 189 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 190 | } else { |
| 191 | return CompletableFuture.completedFuture(result); |
| 192 | } |
| 193 | }).thenApply(result -> result.oldValue()); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<byte[]> listener) { |
| 198 | checkNotNull(path); |
| 199 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 200 | InternalListener internalListener = new InternalListener(path, listener, MoreExecutors.directExecutor()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 201 | // TODO: Support API that takes an executor |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 202 | if (!eventListeners.containsKey(listener)) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 203 | return proxy.invoke(ADD_LISTENER, SERIALIZER::encode, new Listen(path)) |
| 204 | .thenRun(() -> eventListeners.put(listener, internalListener)); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 205 | } |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 206 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public CompletableFuture<Void> removeListener(DocumentTreeListener<byte[]> listener) { |
| 211 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 212 | InternalListener internalListener = eventListeners.remove(listener); |
| 213 | if (internalListener != null && eventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 214 | return proxy.invoke(REMOVE_LISTENER, SERIALIZER::encode, new Unlisten(internalListener.path)) |
| 215 | .thenApply(v -> null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 216 | } |
| 217 | return CompletableFuture.completedFuture(null); |
| 218 | } |
| 219 | |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 220 | private CompletableFuture<DocumentTreeUpdateResult.Status> createInternal(DocumentPath path, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 221 | return proxy.<Update, DocumentTreeUpdateResult<byte[]>>invoke(UPDATE, |
| 222 | SERIALIZER::encode, |
| 223 | new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.ifNull()), |
| 224 | SERIALIZER::decode) |
| 225 | .thenApply(result -> result.status()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 228 | private boolean isListening() { |
| 229 | return !eventListeners.isEmpty(); |
| 230 | } |
| 231 | |
| 232 | private void processTreeUpdates(List<DocumentTreeEvent<byte[]>> events) { |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 233 | events.forEach(event -> eventListeners.values().forEach(listener -> listener.event(event))); |
| 234 | } |
| 235 | |
| 236 | private class InternalListener implements DocumentTreeListener<byte[]> { |
| 237 | |
| 238 | private final DocumentPath path; |
| 239 | private final DocumentTreeListener<byte[]> listener; |
| 240 | private final Executor executor; |
| 241 | |
| 242 | public InternalListener(DocumentPath path, DocumentTreeListener<byte[]> listener, Executor executor) { |
| 243 | this.path = path; |
| 244 | this.listener = listener; |
| 245 | this.executor = executor; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void event(DocumentTreeEvent<byte[]> event) { |
| 250 | if (event.path().isDescendentOf(path)) { |
| 251 | executor.execute(() -> listener.event(event)); |
| 252 | } |
| 253 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 254 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 255 | } |