Aaron Kruglikov | d77cc11 | 2016-09-15 17:13:28 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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 com.google.common.base.Throwables; |
| 20 | import org.onosproject.store.service.AsyncDocumentTree; |
| 21 | import org.onosproject.store.service.ConsistentMapException; |
| 22 | import org.onosproject.store.service.DocumentException; |
| 23 | import org.onosproject.store.service.DocumentPath; |
| 24 | import org.onosproject.store.service.DocumentTree; |
| 25 | import org.onosproject.store.service.DocumentTreeListener; |
| 26 | import org.onosproject.store.service.Synchronous; |
| 27 | import org.onosproject.store.service.Versioned; |
| 28 | |
| 29 | import java.util.Map; |
| 30 | import java.util.concurrent.CompletableFuture; |
| 31 | import java.util.concurrent.ExecutionException; |
| 32 | import java.util.concurrent.TimeUnit; |
| 33 | import java.util.concurrent.TimeoutException; |
| 34 | |
| 35 | /** |
| 36 | * Synchronous wrapper for a {@link AsyncDocumentTree}. All operations are |
| 37 | * made by making the equivalent calls to a backing {@link AsyncDocumentTree} |
| 38 | * then blocking until the operations complete or timeout. |
| 39 | * |
| 40 | * @param <V> the type of the values |
| 41 | */ |
| 42 | public class DefaultConsistentDocumentTree<V> extends Synchronous<AsyncDocumentTree<V>> implements DocumentTree<V> { |
| 43 | |
| 44 | private final AsyncDocumentTree<V> backingMap; |
| 45 | private static final int MAX_DELAY_BETWEEN_RETRY_MILLS = 50; |
| 46 | private final long operationTimeoutMillis; |
| 47 | |
| 48 | public DefaultConsistentDocumentTree(AsyncDocumentTree<V> backingMap, |
| 49 | long operationTimeoutMillis) { |
| 50 | super(backingMap); |
| 51 | this.backingMap = backingMap; |
| 52 | this.operationTimeoutMillis = operationTimeoutMillis; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public DocumentPath root() { |
| 57 | return backingMap.root(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Map<String, Versioned<V>> getChildren(DocumentPath path) { |
| 62 | return complete(backingMap.getChildren(path)); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Versioned<V> get(DocumentPath path) { |
| 67 | return complete(backingMap.get(path)); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Versioned<V> set(DocumentPath path, V value) { |
| 72 | return complete(backingMap.set(path, value)); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean create(DocumentPath path, V value) { |
| 77 | return complete(backingMap.create(path, value)); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean createRecursive(DocumentPath path, V value) { |
| 82 | return complete(backingMap.createRecursive(path, value)); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean replace(DocumentPath path, V newValue, long version) { |
| 87 | return complete(backingMap.replace(path, newValue, version)); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean replace(DocumentPath path, V newValue, V currentValue) { |
| 92 | return complete(backingMap.replace(path, newValue, currentValue)); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Versioned<V> removeNode(DocumentPath path) { |
| 97 | return complete(backingMap.removeNode(path)); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
| 102 | complete(backingMap.addListener(path, listener)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void removeListener(DocumentTreeListener<V> listener) { |
| 107 | complete(backingMap.removeListener(listener)); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void addListener(DocumentTreeListener<V> listener) { |
| 112 | complete(backingMap.addListener(listener)); |
| 113 | } |
| 114 | |
| 115 | private <T> T complete(CompletableFuture<T> future) { |
| 116 | try { |
| 117 | return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
| 118 | } catch (InterruptedException e) { |
| 119 | Thread.currentThread().interrupt(); |
| 120 | throw new DocumentException.Interrupted(); |
| 121 | } catch (TimeoutException e) { |
| 122 | throw new DocumentException.Timeout(name()); |
| 123 | } catch (ExecutionException e) { |
| 124 | Throwables.propagateIfPossible(e.getCause()); |
| 125 | throw new ConsistentMapException(e.getCause()); |
| 126 | } |
| 127 | } |
| 128 | } |