Support alternative ordering specifications for DocumentTree primitive

Change-Id: I89a99435bff44f8d37d6b529fbf735940e7d7210
diff --git a/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java b/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java
index 162da9f..6b2a54c 100644
--- a/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java
+++ b/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java
@@ -27,6 +27,7 @@
 import org.onosproject.store.service.AsyncDistributedSet;
 import org.onosproject.store.service.AsyncDocumentTree;
 import org.onosproject.store.service.AsyncLeaderElector;
+import org.onosproject.store.service.Ordering;
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.WorkQueue;
 
@@ -139,7 +140,20 @@
      * @param serializer serializer
      * @return document tree
      */
-    <V> AsyncDocumentTree<V> newAsyncDocumentTree(String name, Serializer serializer);
+    default <V> AsyncDocumentTree<V> newAsyncDocumentTree(String name, Serializer serializer) {
+        return newAsyncDocumentTree(name, serializer, Ordering.NATURAL);
+    }
+
+    /**
+     * Creates a new {@code AsyncDocumentTree}.
+     *
+     * @param <V> document tree node value type
+     * @param name tree name
+     * @param serializer serializer
+     * @param ordering tree node ordering
+     * @return document tree
+     */
+    <V> AsyncDocumentTree<V> newAsyncDocumentTree(String name, Serializer serializer, Ordering ordering);
 
     /**
      * Returns the names of all created {@code AsyncConsistentMap} instances.
diff --git a/core/api/src/main/java/org/onosproject/store/service/DocumentTreeBuilder.java b/core/api/src/main/java/org/onosproject/store/service/DocumentTreeBuilder.java
index e0e0063..14ecd5c 100644
--- a/core/api/src/main/java/org/onosproject/store/service/DocumentTreeBuilder.java
+++ b/core/api/src/main/java/org/onosproject/store/service/DocumentTreeBuilder.java
@@ -25,6 +25,7 @@
         extends DistributedPrimitiveBuilder<DocumentTreeBuilder<V>, AsyncDocumentTree<V>> {
 
     private boolean purgeOnUninstall = false;
+    private Ordering ordering = Ordering.NATURAL;
 
     public DocumentTreeBuilder() {
         super(DistributedPrimitive.Type.DOCUMENT_TREE);
@@ -50,6 +51,32 @@
     }
 
     /**
+     * Sets the ordering of the tree nodes.
+     * <p>
+     * When {@link AsyncDocumentTree#getChildren(DocumentPath)} is called, children will be returned according to
+     * the specified sort order.
+     *
+     * @param ordering ordering of the tree nodes
+     * @return this builder
+     */
+    public DocumentTreeBuilder<V> withOrdering(Ordering ordering) {
+        this.ordering = ordering;
+        return this;
+    }
+
+    /**
+     * Returns the ordering of tree nodes.
+     * <p>
+     * When {@link AsyncDocumentTree#getChildren(DocumentPath)} is called, children will be returned according to
+     * the specified sort order.
+     *
+     * @return the ordering of tree nodes
+     */
+    public Ordering ordering() {
+        return ordering;
+    }
+
+    /**
      * Builds the distributed Document tree based on the configuration options supplied
      * to this builder.
      *
diff --git a/core/api/src/main/java/org/onosproject/store/service/Ordering.java b/core/api/src/main/java/org/onosproject/store/service/Ordering.java
new file mode 100644
index 0000000..64f41ec
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/Ordering.java
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+/**
+ * Describes the order of a primitive data structure.
+ */
+public enum Ordering {
+
+    /**
+     * Indicates that items should be ordered in their natural order.
+     */
+    NATURAL,
+
+    /**
+     * Indicates that items should be ordered in insertion order.
+     */
+    INSERTION,
+}