[ONOS-6869] Move blocking DocumentTree to core primitives package and implement default methods for constructing blocking primitives
Change-Id: I9c227a690a120dba7d9d89c6c9178c8b357b52aa
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultConsistentDocumentTree.java b/core/api/src/main/java/org/onosproject/store/primitives/DefaultDocumentTree.java
similarity index 70%
rename from core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultConsistentDocumentTree.java
rename to core/api/src/main/java/org/onosproject/store/primitives/DefaultDocumentTree.java
index e175eb7..6f90df4 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultConsistentDocumentTree.java
+++ b/core/api/src/main/java/org/onosproject/store/primitives/DefaultDocumentTree.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 Open Networking Laboratory
+ * Copyright 2017-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.onosproject.store.primitives.resources.impl;
+package org.onosproject.store.primitives;
import com.google.common.base.Throwables;
import org.onosproject.store.service.AsyncDocumentTree;
@@ -33,83 +33,81 @@
import java.util.concurrent.TimeoutException;
/**
- * Synchronous wrapper for a {@link AsyncDocumentTree}. All operations are
+ * Synchronous wrapper for a {@link AsyncDocumentTree}. All operations are
* made by making the equivalent calls to a backing {@link AsyncDocumentTree}
* then blocking until the operations complete or timeout.
*
* @param <V> the type of the values
*/
-public class DefaultConsistentDocumentTree<V> extends Synchronous<AsyncDocumentTree<V>> implements DocumentTree<V> {
+public class DefaultDocumentTree<V> extends Synchronous<AsyncDocumentTree<V>> implements DocumentTree<V> {
- private final AsyncDocumentTree<V> backingMap;
- private static final int MAX_DELAY_BETWEEN_RETRY_MILLS = 50;
+ private final AsyncDocumentTree<V> backingTree;
private final long operationTimeoutMillis;
- public DefaultConsistentDocumentTree(AsyncDocumentTree<V> backingMap,
- long operationTimeoutMillis) {
- super(backingMap);
- this.backingMap = backingMap;
+ public DefaultDocumentTree(AsyncDocumentTree<V> backingTree, long operationTimeoutMillis) {
+ super(backingTree);
+ this.backingTree = backingTree;
this.operationTimeoutMillis = operationTimeoutMillis;
}
@Override
public DocumentPath root() {
- return backingMap.root();
+ return backingTree.root();
}
@Override
public Map<String, Versioned<V>> getChildren(DocumentPath path) {
- return complete(backingMap.getChildren(path));
+ return complete(backingTree.getChildren(path));
}
@Override
public Versioned<V> get(DocumentPath path) {
- return complete(backingMap.get(path));
+ return complete(backingTree.get(path));
}
@Override
public Versioned<V> set(DocumentPath path, V value) {
- return complete(backingMap.set(path, value));
+ return complete(backingTree.set(path, value));
}
@Override
public boolean create(DocumentPath path, V value) {
- return complete(backingMap.create(path, value));
+ return complete(backingTree.create(path, value));
}
@Override
public boolean createRecursive(DocumentPath path, V value) {
- return complete(backingMap.createRecursive(path, value));
+ return complete(backingTree.createRecursive(path, value));
}
@Override
public boolean replace(DocumentPath path, V newValue, long version) {
- return complete(backingMap.replace(path, newValue, version));
+ return complete(backingTree.replace(path, newValue, version));
}
@Override
public boolean replace(DocumentPath path, V newValue, V currentValue) {
- return complete(backingMap.replace(path, newValue, currentValue));
+ return complete(backingTree.replace(path, newValue, currentValue));
}
@Override
public Versioned<V> removeNode(DocumentPath path) {
- return complete(backingMap.removeNode(path));
+ return complete(backingTree.removeNode(path));
}
@Override
public void addListener(DocumentPath path, DocumentTreeListener<V> listener) {
- complete(backingMap.addListener(path, listener));
+ complete(backingTree.addListener(path, listener));
}
@Override
public void removeListener(DocumentTreeListener<V> listener) {
- complete(backingMap.removeListener(listener));
+ complete(backingTree.removeListener(listener));
}
@Override
public void addListener(DocumentTreeListener<V> listener) {
- complete(backingMap.addListener(listener));
+ complete(backingTree.addListener(listener));
}
private <T> T complete(CompletableFuture<T> future) {
diff --git a/core/api/src/main/java/org/onosproject/store/service/AsyncDocumentTree.java b/core/api/src/main/java/org/onosproject/store/service/AsyncDocumentTree.java
index 00bccda..04f015a 100644
--- a/core/api/src/main/java/org/onosproject/store/service/AsyncDocumentTree.java
+++ b/core/api/src/main/java/org/onosproject/store/service/AsyncDocumentTree.java
@@ -21,6 +21,8 @@
import javax.annotation.concurrent.NotThreadSafe;
+import org.onosproject.store.primitives.DefaultDocumentTree;
+
/**
* A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure.
*
@@ -29,6 +31,11 @@
@NotThreadSafe
public interface AsyncDocumentTree<V> extends DistributedPrimitive {
+ @Override
+ default Type primitiveType() {
+ return Type.DOCUMENT_TREE;
+ }
+
/**
* Returns the {@link DocumentPath path} to root of the tree.
*
@@ -148,4 +155,23 @@
default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) {
return addListener(root(), listener);
}
+
+ /**
+ * Returns a new {@link DocumentTree} that is backed by this instance.
+ *
+ * @return new {@code DocumentTree} instance
+ */
+ default DocumentTree<V> asDocumentTree() {
+ return asDocumentTree(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
+ }
+
+ /**
+ * Returns a new {@link DocumentTree} that is backed by this instance.
+ *
+ * @param timeoutMillis timeout duration for the returned DocumentTree operations
+ * @return new {@code DocumentTree} instance
+ */
+ default DocumentTree<V> asDocumentTree(long timeoutMillis) {
+ return new DefaultDocumentTree<V>(this, timeoutMillis);
+ }
}
diff --git a/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java b/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java
index 0a4906e..90b9863 100644
--- a/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java
+++ b/core/api/src/main/java/org/onosproject/store/service/DocumentPath.java
@@ -21,6 +21,7 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
@@ -95,6 +96,39 @@
}
/**
+ * Creates a new {@code DocumentPath} from a list of path elements.
+ *
+ * @param elements path elements
+ * @return {@code DocumentPath} instance
+ */
+ public static DocumentPath from(String... elements) {
+ return from(Arrays.asList(elements));
+ }
+
+ /**
+ * Creates a new {@code DocumentPath} from a list of path elements.
+ *
+ * @param elements path elements
+ * @return {@code DocumentPath} instance
+ */
+ public static DocumentPath from(List<String> elements) {
+ return new DocumentPath(elements);
+ }
+
+ /**
+ * Creates a new {@code DocumentPath} from a list of path elements.
+ *
+ * @param elements path elements
+ * @param child child element
+ * @return {@code DocumentPath} instance
+ */
+ public static DocumentPath from(List<String> elements, String child) {
+ elements = new ArrayList<>(elements);
+ elements.add(child);
+ return from(elements);
+ }
+
+ /**
* Returns the relative path to the given node.
*
* @return relative path to the given node.
diff --git a/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java b/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java
index e8e4746..ad6d405 100644
--- a/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java
+++ b/core/api/src/main/java/org/onosproject/store/service/DocumentTree.java
@@ -26,7 +26,12 @@
* @param <V> document tree value type
*/
@NotThreadSafe
-public interface DocumentTree<V> {
+public interface DocumentTree<V> extends DistributedPrimitive {
+
+ @Override
+ default Type primitiveType() {
+ return Type.DOCUMENT_TREE;
+ }
/**
* Returns the {@link DocumentPath path} to root of the tree.
diff --git a/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java b/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java
index d267814..353cf30 100644
--- a/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java
+++ b/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java
@@ -37,6 +37,8 @@
assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
assertEquals(path("root.a"), path.parent());
assertEquals(path("b"), path.childPath());
+ path = DocumentPath.from("root", "a", "b");
+ assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
}
@Test
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTree.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTree.java
index ba248ba..295635d 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTree.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/DefaultDocumentTree.java
@@ -61,6 +61,11 @@
}
@Override
+ public String name() {
+ return null;
+ }
+
+ @Override
public DocumentPath root() {
return ROOT_PATH;
}