blob: e175eb782a19cfc2034612cbe83f4b5bd0d38e1b [file] [log] [blame]
Aaron Kruglikovd77cc112016-09-15 17:13:28 -07001/*
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
17package org.onosproject.store.primitives.resources.impl;
18
19import com.google.common.base.Throwables;
20import org.onosproject.store.service.AsyncDocumentTree;
21import org.onosproject.store.service.ConsistentMapException;
22import org.onosproject.store.service.DocumentException;
23import org.onosproject.store.service.DocumentPath;
24import org.onosproject.store.service.DocumentTree;
25import org.onosproject.store.service.DocumentTreeListener;
26import org.onosproject.store.service.Synchronous;
27import org.onosproject.store.service.Versioned;
28
29import java.util.Map;
30import java.util.concurrent.CompletableFuture;
31import java.util.concurrent.ExecutionException;
32import java.util.concurrent.TimeUnit;
33import 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 */
42public 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}