blob: ae2f6402995cc91117c45c4eee32664a4711e1a0 [file] [log] [blame]
Aaron Kruglikovd77cc112016-09-15 17:13:28 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Aaron Kruglikovd77cc112016-09-15 17:13:28 -07003 *
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
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070017package org.onosproject.store.primitives;
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070018
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/**
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070036 * Synchronous wrapper for a {@link AsyncDocumentTree}. All operations are
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070037 * 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 */
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070042public class DefaultDocumentTree<V> extends Synchronous<AsyncDocumentTree<V>> implements DocumentTree<V> {
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070043
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070044 private final AsyncDocumentTree<V> backingTree;
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070045 private final long operationTimeoutMillis;
46
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070047 public DefaultDocumentTree(AsyncDocumentTree<V> backingTree, long operationTimeoutMillis) {
48 super(backingTree);
49 this.backingTree = backingTree;
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070050 this.operationTimeoutMillis = operationTimeoutMillis;
51 }
52
53 @Override
54 public DocumentPath root() {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070055 return backingTree.root();
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070056 }
57
58 @Override
59 public Map<String, Versioned<V>> getChildren(DocumentPath path) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070060 return complete(backingTree.getChildren(path));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070061 }
62
63 @Override
64 public Versioned<V> get(DocumentPath path) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070065 return complete(backingTree.get(path));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070066 }
67
68 @Override
69 public Versioned<V> set(DocumentPath path, V value) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070070 return complete(backingTree.set(path, value));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070071 }
72
73 @Override
74 public boolean create(DocumentPath path, V value) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070075 return complete(backingTree.create(path, value));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070076 }
77
78 @Override
79 public boolean createRecursive(DocumentPath path, V value) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070080 return complete(backingTree.createRecursive(path, value));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070081 }
82
83 @Override
84 public boolean replace(DocumentPath path, V newValue, long version) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070085 return complete(backingTree.replace(path, newValue, version));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070086 }
87
88 @Override
89 public boolean replace(DocumentPath path, V newValue, V currentValue) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070090 return complete(backingTree.replace(path, newValue, currentValue));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070091 }
92
93 @Override
94 public Versioned<V> removeNode(DocumentPath path) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070095 return complete(backingTree.removeNode(path));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -070096 }
97
98 @Override
99 public void addListener(DocumentPath path, DocumentTreeListener<V> listener) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -0700100 complete(backingTree.addListener(path, listener));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -0700101 }
102
103 @Override
104 public void removeListener(DocumentTreeListener<V> listener) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -0700105 complete(backingTree.removeListener(listener));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -0700106 }
107
108 @Override
109 public void addListener(DocumentTreeListener<V> listener) {
Jordan Haltermanb0ac5902017-07-30 12:31:01 -0700110 complete(backingTree.addListener(listener));
Aaron Kruglikovd77cc112016-09-15 17:13:28 -0700111 }
112
113 private <T> T complete(CompletableFuture<T> future) {
114 try {
115 return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
116 } catch (InterruptedException e) {
117 Thread.currentThread().interrupt();
118 throw new DocumentException.Interrupted();
119 } catch (TimeoutException e) {
120 throw new DocumentException.Timeout(name());
121 } catch (ExecutionException e) {
Ray Milkey6a51cb92018-03-06 09:03:03 -0800122 Throwables.throwIfUnchecked(e.getCause());
Aaron Kruglikovd77cc112016-09-15 17:13:28 -0700123 throw new ConsistentMapException(e.getCause());
124 }
125 }
126}