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 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 20 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeUpdateResult.Status.ILLEGAL_MODIFICATION; |
| 21 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeUpdateResult.Status.INVALID_PATH; |
| 22 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeUpdateResult.Status.OK; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 23 | import io.atomix.copycat.client.CopycatClient; |
| 24 | import io.atomix.resource.AbstractResource; |
| 25 | import io.atomix.resource.ResourceTypeInfo; |
| 26 | |
| 27 | import java.util.HashMap; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 30 | import java.util.Optional; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 31 | import java.util.Properties; |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | import java.util.concurrent.Executor; |
| 34 | |
| 35 | import org.onlab.util.Match; |
| 36 | import org.onlab.util.Tools; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 37 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeCommands.Clear; |
| 38 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeCommands.Get; |
| 39 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeCommands.GetChildren; |
| 40 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeCommands.Listen; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 41 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeCommands.Unlisten; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 42 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeCommands.Update; |
| 43 | import org.onosproject.store.service.AsyncDocumentTree; |
| 44 | import org.onosproject.store.service.DocumentPath; |
| 45 | import org.onosproject.store.service.DocumentTreeEvent; |
| 46 | import org.onosproject.store.service.DocumentTreeListener; |
| 47 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 48 | import org.onosproject.store.service.NoSuchDocumentPathException; |
| 49 | import org.onosproject.store.service.Versioned; |
| 50 | |
| 51 | import com.google.common.util.concurrent.MoreExecutors; |
| 52 | |
| 53 | /** |
| 54 | * Distributed resource providing the {@link AsyncDocumentTree} primitive. |
| 55 | */ |
| 56 | @ResourceTypeInfo(id = -156, factory = AtomixDocumentTreeFactory.class) |
| 57 | public class AtomixDocumentTree extends AbstractResource<AtomixDocumentTree> |
| 58 | implements AsyncDocumentTree<byte[]> { |
| 59 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 60 | private final Map<DocumentTreeListener<byte[]>, InternalListener> eventListeners = new HashMap<>(); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 61 | public static final String CHANGE_SUBJECT = "changeEvents"; |
| 62 | |
| 63 | protected AtomixDocumentTree(CopycatClient client, Properties options) { |
| 64 | super(client, options); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public CompletableFuture<AtomixDocumentTree> open() { |
| 69 | return super.open().thenApply(result -> { |
| 70 | client.onStateChange(state -> { |
| 71 | if (state == CopycatClient.State.CONNECTED && isListening()) { |
| 72 | client.submit(new Listen()); |
| 73 | } |
| 74 | }); |
| 75 | client.onEvent(CHANGE_SUBJECT, this::processTreeUpdates); |
| 76 | return result; |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public String name() { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Type primitiveType() { |
| 87 | return Type.DOCUMENT_TREE; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public CompletableFuture<Void> destroy() { |
| 92 | return client.submit(new Clear()); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public DocumentPath root() { |
| 97 | return DocumentPath.from("root"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public CompletableFuture<Map<String, Versioned<byte[]>>> getChildren(DocumentPath path) { |
| 102 | return client.submit(new GetChildren(checkNotNull(path))); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public CompletableFuture<Versioned<byte[]>> get(DocumentPath path) { |
| 107 | return client.submit(new Get(checkNotNull(path))); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public CompletableFuture<Versioned<byte[]>> set(DocumentPath path, byte[] value) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 112 | return client.submit(new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.any())) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 113 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 114 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 115 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 116 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 117 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 118 | } else { |
| 119 | return CompletableFuture.completedFuture(result); |
| 120 | } |
| 121 | }).thenApply(result -> result.oldValue()); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Boolean> create(DocumentPath path, byte[] value) { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 126 | return createInternal(path, value) |
| 127 | .thenCompose(status -> { |
| 128 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 129 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 130 | } |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 131 | return CompletableFuture.completedFuture(true); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, byte[] value) { |
| 137 | return createInternal(path, value) |
| 138 | .thenCompose(status -> { |
| 139 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 140 | return createRecursive(path.parent(), null) |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 141 | .thenCompose(r -> createInternal(path, value).thenApply(v -> true)); |
| 142 | } |
| 143 | return CompletableFuture.completedFuture(status == OK); |
| 144 | }); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public CompletableFuture<Boolean> replace(DocumentPath path, byte[] newValue, long version) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 149 | return client.submit(new Update(checkNotNull(path), |
| 150 | Optional.ofNullable(newValue), |
| 151 | Match.any(), |
| 152 | Match.ifValue(version))) |
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) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 158 | return client.submit(new Update(checkNotNull(path), |
| 159 | Optional.ofNullable(newValue), |
| 160 | Match.ifValue(currentValue), |
| 161 | Match.any())) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 162 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 163 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 164 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 165 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 166 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 167 | } else { |
| 168 | return CompletableFuture.completedFuture(result); |
| 169 | } |
| 170 | }).thenApply(result -> result.updated()); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public CompletableFuture<Versioned<byte[]>> removeNode(DocumentPath path) { |
| 175 | if (path.equals(DocumentPath.from("root"))) { |
| 176 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 177 | } |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 178 | return client.submit(new Update(checkNotNull(path), null, Match.any(), Match.ifNotNull())) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 179 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 180 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 181 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 182 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 183 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 184 | } else { |
| 185 | return CompletableFuture.completedFuture(result); |
| 186 | } |
| 187 | }).thenApply(result -> result.oldValue()); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<byte[]> listener) { |
| 192 | checkNotNull(path); |
| 193 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 194 | InternalListener internalListener = new InternalListener(path, listener, MoreExecutors.directExecutor()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 195 | // TODO: Support API that takes an executor |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 196 | if (!eventListeners.containsKey(listener)) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 197 | return client.submit(new Listen(path)) |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 198 | .thenRun(() -> eventListeners.put(listener, internalListener)); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 199 | } |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 200 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public CompletableFuture<Void> removeListener(DocumentTreeListener<byte[]> listener) { |
| 205 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 206 | InternalListener internalListener = eventListeners.remove(listener); |
| 207 | if (internalListener != null && eventListeners.isEmpty()) { |
| 208 | return client.submit(new Unlisten(internalListener.path)).thenApply(v -> null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 209 | } |
| 210 | return CompletableFuture.completedFuture(null); |
| 211 | } |
| 212 | |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 213 | private CompletableFuture<DocumentTreeUpdateResult.Status> createInternal(DocumentPath path, byte[] value) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 214 | return client.submit(new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.ifNull())) |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 215 | .thenApply(result -> result.status()); |
| 216 | } |
| 217 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 218 | private boolean isListening() { |
| 219 | return !eventListeners.isEmpty(); |
| 220 | } |
| 221 | |
| 222 | private void processTreeUpdates(List<DocumentTreeEvent<byte[]>> events) { |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 223 | events.forEach(event -> eventListeners.values().forEach(listener -> listener.event(event))); |
| 224 | } |
| 225 | |
| 226 | private class InternalListener implements DocumentTreeListener<byte[]> { |
| 227 | |
| 228 | private final DocumentPath path; |
| 229 | private final DocumentTreeListener<byte[]> listener; |
| 230 | private final Executor executor; |
| 231 | |
| 232 | public InternalListener(DocumentPath path, DocumentTreeListener<byte[]> listener, Executor executor) { |
| 233 | this.path = path; |
| 234 | this.listener = listener; |
| 235 | this.executor = executor; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void event(DocumentTreeEvent<byte[]> event) { |
| 240 | if (event.path().isDescendentOf(path)) { |
| 241 | executor.execute(() -> listener.event(event)); |
| 242 | } |
| 243 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 244 | } |
| 245 | } |