[ONOS-6870] Refactor DistributedFlowRuleStore to use DocumentTree for persistence

Change-Id: I5f0eccfeb0050ccf1959f3ca95bbd0a90406e4ba
diff --git a/core/api/src/test/java/org/onosproject/store/service/AsyncDocumentTreeAdapter.java b/core/api/src/test/java/org/onosproject/store/service/AsyncDocumentTreeAdapter.java
new file mode 100644
index 0000000..6ff99ee
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/store/service/AsyncDocumentTreeAdapter.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.store.service;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+
+import org.onosproject.store.primitives.NodeUpdate;
+import org.onosproject.store.primitives.TransactionId;
+
+/**
+ * Async document tree adapter.
+ */
+public class AsyncDocumentTreeAdapter<V> implements AsyncDocumentTree<V> {
+    @Override
+    public String name() {
+        return null;
+    }
+
+    @Override
+    public DocumentPath root() {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Versioned<V>> get(DocumentPath path) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Versioned<V>> set(DocumentPath path, V value) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> create(DocumentPath path, V value) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Version> begin(TransactionId transactionId) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> prepare(TransactionLog<NodeUpdate<V>> transactionLog) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<NodeUpdate<V>> transactionLog) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Void> commit(TransactionId transactionId) {
+        return null;
+    }
+
+    @Override
+    public CompletableFuture<Void> rollback(TransactionId transactionId) {
+        return null;
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestDocumentTree.java b/core/api/src/test/java/org/onosproject/store/service/TestDocumentTree.java
new file mode 100644
index 0000000..c9100f5
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/store/service/TestDocumentTree.java
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.store.service;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicLong;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+
+/**
+ * Simple implementation of a {@link DocumentTree}.
+ *
+ * @param <V> tree node value type
+ */
+public class TestDocumentTree<V> implements DocumentTree<V> {
+
+    private static final DocumentPath ROOT_PATH = DocumentPath.from("root");
+    private final String name;
+    final TestDocumentTreeNode<V> root;
+    private final AtomicLong version = new AtomicLong();
+
+    public TestDocumentTree(String name) {
+        this(name, null);
+    }
+
+    public TestDocumentTree(String name, V value) {
+        this.name = name;
+        this.root = new TestDocumentTreeNode<>(ROOT_PATH, value, version.incrementAndGet(), null);
+    }
+
+    @Override
+    public String name() {
+        return name;
+    }
+
+    @Override
+    public DocumentPath root() {
+        return ROOT_PATH;
+    }
+
+    @Override
+    public Map<String, Versioned<V>> getChildren(DocumentPath path) {
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null) {
+            Map<String, Versioned<V>> childrenValues = Maps.newHashMap();
+            node.children().forEachRemaining(n -> childrenValues.put(simpleName(n.path()), n.value()));
+            return childrenValues;
+        }
+        throw new NoSuchDocumentPathException();
+    }
+
+    @Override
+    public Versioned<V> get(DocumentPath path) {
+        DocumentTreeNode<V> currentNode = getNode(path);
+        return currentNode != null ? currentNode.value() : null;
+    }
+
+    @Override
+    public Versioned<V> set(DocumentPath path, V value) {
+        checkRootModification(path);
+        TestDocumentTreeNode<V> node = getNode(path);
+        if (node != null) {
+            return node.update(value, version.incrementAndGet());
+        } else {
+            create(path, value);
+            return null;
+        }
+    }
+
+    @Override
+    public boolean create(DocumentPath path, V value) {
+        checkRootModification(path);
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null) {
+            return false;
+        }
+        DocumentPath parentPath = path.parent();
+        TestDocumentTreeNode<V> parentNode =  getNode(parentPath);
+        if (parentNode == null) {
+            throw new IllegalDocumentModificationException();
+        }
+        parentNode.addChild(simpleName(path), value, version.incrementAndGet());
+        return true;
+    }
+
+    @Override
+    public boolean createRecursive(DocumentPath path, V value) {
+        checkRootModification(path);
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null) {
+            return false;
+        }
+        DocumentPath parentPath = path.parent();
+        if (getNode(parentPath) == null) {
+            createRecursive(parentPath, null);
+        }
+        TestDocumentTreeNode<V> parentNode =  getNode(parentPath);
+        if (parentNode == null) {
+            throw new IllegalDocumentModificationException();
+        }
+        parentNode.addChild(simpleName(path), value, version.incrementAndGet());
+        return true;
+    }
+
+    @Override
+    public boolean replace(DocumentPath path, V newValue, long version) {
+        checkRootModification(path);
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null && node.value() != null && node.value().version() == version) {
+            set(path, newValue);
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public boolean replace(DocumentPath path, V newValue, V currentValue) {
+        checkRootModification(path);
+        if (Objects.equals(newValue, currentValue)) {
+            return false;
+        }
+        DocumentTreeNode<V> node = getNode(path);
+        if (node != null && Objects.equals(Versioned.valueOrNull(node.value()), currentValue)) {
+            set(path, newValue);
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public Versioned<V> removeNode(DocumentPath path) {
+        checkRootModification(path);
+        TestDocumentTreeNode<V> nodeToRemove = getNode(path);
+        if (nodeToRemove == null) {
+            throw new NoSuchDocumentPathException();
+        }
+        if (nodeToRemove.hasChildren()) {
+            throw new IllegalDocumentModificationException();
+        }
+        TestDocumentTreeNode<V> parent = (TestDocumentTreeNode<V>) nodeToRemove.parent();
+        parent.removeChild(simpleName(path));
+        return nodeToRemove.value();
+    }
+
+    @Override
+    public void addListener(DocumentPath path, DocumentTreeListener<V> listener) {
+        // TODO Auto-generated method stub
+    }
+
+    @Override
+    public void removeListener(DocumentTreeListener<V> listener) {
+        // TODO Auto-generated method stub
+    }
+
+    private TestDocumentTreeNode<V> getNode(DocumentPath path) {
+        Iterator<String> pathElements = path.pathElements().iterator();
+        TestDocumentTreeNode<V> currentNode = root;
+        Preconditions.checkState("root".equals(pathElements.next()), "Path should start with root");
+        while (pathElements.hasNext() &&  currentNode != null) {
+            currentNode = (TestDocumentTreeNode<V>) currentNode.child(pathElements.next());
+        }
+        return currentNode;
+    }
+
+    private String simpleName(DocumentPath path) {
+        return path.pathElements().get(path.pathElements().size() - 1);
+    }
+
+    private void checkRootModification(DocumentPath path) {
+        if (ROOT_PATH.equals(path)) {
+            throw new IllegalDocumentModificationException();
+        }
+    }
+}
\ No newline at end of file
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestDocumentTreeNode.java b/core/api/src/test/java/org/onosproject/store/service/TestDocumentTreeNode.java
new file mode 100644
index 0000000..508fad2
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/store/service/TestDocumentTreeNode.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.store.service;
+
+import java.util.Iterator;
+import java.util.Objects;
+import java.util.TreeMap;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * A {@code DocumentTree} node.
+ */
+public class TestDocumentTreeNode<V> implements DocumentTreeNode<V> {
+    private final DocumentPath key;
+    private Versioned<V> value;
+    private final TreeMap<String, DocumentTreeNode<V>> children = Maps.newTreeMap();
+    private final DocumentTreeNode<V> parent;
+
+    public TestDocumentTreeNode(DocumentPath key,
+            V value,
+            long version,
+            DocumentTreeNode<V> parent) {
+        this.key = checkNotNull(key);
+        this.value = new Versioned<>(value, version);
+        this.parent = parent;
+    }
+
+    @Override
+    public DocumentPath path() {
+        return key;
+    }
+
+    @Override
+    public Versioned<V> value() {
+        return value;
+    }
+
+    @Override
+    public Iterator<DocumentTreeNode<V>> children() {
+        return ImmutableList.copyOf(children.values()).iterator();
+    }
+
+    @Override
+    public DocumentTreeNode<V> child(String name) {
+        return children.get(name);
+    }
+
+
+    public DocumentTreeNode<V> parent() {
+        return parent;
+    }
+
+    /**
+     * Adds a new child only if one does not exist with the name.
+     * @param name relative path name of the child node
+     * @param newValue new value to set
+     * @param newVersion new version to set
+     * @return previous value; can be {@code null} if no child currently exists with that relative path name.
+     * a non null return value indicates child already exists and no modification occured.
+     */
+    public Versioned<V> addChild(String name, V newValue, long newVersion) {
+        TestDocumentTreeNode<V> child = (TestDocumentTreeNode<V>) children.get(name);
+        if (child != null) {
+            return child.value();
+        }
+        children.put(name, new TestDocumentTreeNode<>(new DocumentPath(name, path()), newValue, newVersion, this));
+        return null;
+    }
+
+    /**
+     * Updates the node value.
+     *
+     * @param newValue new value to set
+     * @param newVersion new version to set
+     * @return previous value
+     */
+    public Versioned<V> update(V newValue, long newVersion) {
+        Versioned<V> previousValue = value;
+        value = new Versioned<>(newValue, newVersion);
+        return previousValue;
+    }
+
+
+    /**
+     * Removes a child node.
+     *
+     * @param name the name of child node to be removed
+     * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise
+     */
+    public boolean removeChild(String name) {
+        return children.remove(name) != null;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(this.key);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof TestDocumentTreeNode) {
+            TestDocumentTreeNode<V> that = (TestDocumentTreeNode<V>) obj;
+            if (this.parent.equals(that.parent)) {
+                if (this.children.size() == that.children.size()) {
+                    return Sets.symmetricDifference(this.children.keySet(), that.children.keySet()).isEmpty();
+                }
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        MoreObjects.ToStringHelper helper =
+                MoreObjects.toStringHelper(getClass())
+                        .add("parent", this.parent)
+                        .add("key", this.key)
+                        .add("value", this.value);
+        for (DocumentTreeNode<V> child : children.values()) {
+            helper = helper.add("child", "\n" + child.path().pathElements()
+                    .get(child.path().pathElements().size() - 1) +
+                    " : " + child.value());
+        }
+        return helper.toString();
+    }
+}
\ No newline at end of file