AnyData feature changes

Change-Id: Iccba22d731321f38b8146bbfc85477d2252a3b10
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AddToDataNodeListAnydataTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AddToDataNodeListAnydataTest.java
new file mode 100644
index 0000000..a6ad4ad
--- /dev/null
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AddToDataNodeListAnydataTest.java
@@ -0,0 +1,240 @@
+/*
+ * 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.yang.runtime.impl.serializerhelper;
+
+import org.junit.Test;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.KeyLeaf;
+import org.onosproject.yang.model.ListKey;
+import org.onosproject.yang.model.NodeKey;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.runtime.HelperContext;
+import org.onosproject.yang.runtime.impl.TestYangSerializerContext;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE;
+import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_NODE;
+import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE;
+import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_NODE;
+import static org.onosproject.yang.runtime.SerializerHelper.addDataNode;
+import static org.onosproject.yang.runtime.SerializerHelper.exitDataNode;
+import static org.onosproject.yang.runtime.SerializerHelper.getModuleNameFromNameSpace;
+import static org.onosproject.yang.runtime.SerializerHelper.getResourceId;
+import static org.onosproject.yang.runtime.SerializerHelper.initializeDataNode;
+import static org.onosproject.yang.runtime.impl.TestUtils.validateDataNode;
+import static org.onosproject.yang.runtime.impl.TestUtils.validateLeafDataNode;
+import static org.onosproject.yang.runtime.impl.TestUtils.validateResourceId;
+import static org.onosproject.yang.runtime.impl.TestUtils.walkINTree;
+
+/**
+ * Tests the serializer helper methods.
+ */
+public class AddToDataNodeListAnydataTest {
+    public static final String LNS = "yrt:list.anydata";
+
+    TestYangSerializerContext context = new TestYangSerializerContext();
+
+    /*
+     * Reference for data node info.
+     */
+    HelperContext info;
+
+    /*
+     * Reference for data node builder.
+     */
+    DataNode.Builder dBlr;
+
+    /*
+     * Reference for resource id.
+     */
+    ResourceId id;
+
+    /*
+     * Reference for the value.
+     */
+    String value;
+
+    /*
+     * Reference for string array to used for resource id testing.
+     */
+    String[] nA;
+    String[] nsA;
+    String[] valA;
+
+    private static final String[] EXPECTED = {
+            "Entry Node is /.",
+            "Entry Node is l1.",
+            "Entry Node is k1.",
+            "Exit Node is k1.",
+            "Entry Node is k2.",
+            "Exit Node is k2.",
+            "Entry Node is c1.",
+            "Entry Node is mydata.",
+            "Exit Node is mydata.",
+            "Entry Node is leaf_c1.",
+            "Exit Node is leaf_c1.",
+            "Exit Node is c1.",
+            "Entry Node is k3.",
+            "Exit Node is k3.",
+            "Exit Node is l1.",
+            "Entry Node is leaf1.",
+            "Exit Node is leaf1.",
+            "Entry Node is leaf1.",
+            "Exit Node is leaf1.",
+            "Entry Node is leaf1.",
+            "Exit Node is leaf1.",
+            "Entry Node is leaf1.",
+            "Exit Node is leaf1.",
+            "Exit Node is /."
+    };
+
+    /**
+     * Test get module name from namespace.
+     */
+    @Test
+    public void getModuleNameTest() {
+
+        String ns = getModuleNameFromNameSpace(context, LNS);
+        assertEquals(ns, "listAnydata");
+    }
+
+    /**
+     * Test add to data node builder.
+     */
+    @Test
+    public void addToDataListTest() throws IOException {
+        ResourceId id;
+        dBlr = initializeDataNode(context);
+        dBlr = addDataNode(dBlr, "l1", LNS, value, null);
+        value = "1";
+        dBlr = addDataNode(dBlr, "k1", null, value, null);
+        dBlr = exitDataNode(dBlr);
+        value = "2";
+        dBlr = addDataNode(dBlr, "k2", null, value, null);
+        dBlr = exitDataNode(dBlr);
+        value = null;
+        dBlr = addDataNode(dBlr, "c1", null, value, null);
+        value = null;
+        dBlr = addDataNode(dBlr, "mydata", null, value, null);
+        dBlr = exitDataNode(dBlr);
+        value = "0";
+        dBlr = addDataNode(dBlr, "leaf_c1", null, value, null);
+
+        info = (HelperContext) dBlr.appInfo();
+        id = getResourceId(dBlr);
+        dBlr = exitDataNode(dBlr);
+
+        ResourceId id1 = getResourceId(dBlr);
+        dBlr = exitDataNode(dBlr);
+
+        value = "3";
+        dBlr = addDataNode(dBlr, "k3", null, value, null);
+
+        info = (HelperContext) dBlr.appInfo();
+        ResourceId id2 = getResourceId(dBlr);
+
+        dBlr = exitDataNode(dBlr);
+        dBlr = exitDataNode(dBlr);
+
+        // Checking leaf list
+        value = "1";
+        dBlr = addDataNode(dBlr, "leaf1", LNS, value, null);
+        dBlr = exitDataNode(dBlr);
+        value = "2";
+        dBlr = addDataNode(dBlr, "leaf1", LNS, value, null);
+        dBlr = exitDataNode(dBlr);
+        value = "3";
+        dBlr = addDataNode(dBlr, "leaf1", LNS, value, null);
+        dBlr = exitDataNode(dBlr);
+        value = null;
+        dBlr = addDataNode(dBlr, "leaf1", LNS, value, null);
+        ResourceId id3 = getResourceId(dBlr);
+        dBlr = exitDataNode(dBlr);
+
+        //Tree validation
+        nA = new String[]{"/", "l1", "k1", "k2", "c1", "leaf_c1"};
+        nsA = new String[]{null, LNS, LNS, LNS, LNS, LNS};
+        valA = new String[]{"1", "2", null, "0"};
+        validateResourceId(nA, nsA, valA, id);
+
+        nA = new String[]{"/", "l1", "k1", "k2", "c1"};
+        nsA = new String[]{null, LNS, LNS, LNS, LNS};
+        valA = new String[]{"1", "2", null};
+        validateResourceId(nA, nsA, valA, id1);
+
+        nA = new String[]{"/", "l1", "k1", "k2", "k3"};
+        nsA = new String[]{null, LNS, LNS, LNS, LNS};
+        valA = new String[]{"1", "2", "3"};
+        validateResourceId(nA, nsA, valA, id2);
+
+        nA = new String[]{"/", "leaf1"};
+        nsA = new String[]{null, LNS};
+        valA = new String[]{null};
+        validateResourceId(nA, nsA, valA, id3);
+
+        // Validating the data node.
+        DataNode node = dBlr.build();
+        validateDataNode(node, "/", null, SINGLE_INSTANCE_NODE, true, null);
+
+        Map<NodeKey, DataNode> childMap = ((InnerNode) node).childNodes();
+        Iterator<Map.Entry<NodeKey, DataNode>> it = childMap.entrySet().iterator();
+        Map.Entry<NodeKey, DataNode> n = it.next();
+        validateDataNode(n.getValue(), "l1", LNS, MULTI_INSTANCE_NODE,
+                         true, null);
+
+        Iterator<KeyLeaf> keyIt = ((ListKey) n.getKey()).keyLeafs().iterator();
+
+        validateLeafDataNode(keyIt.next(), "k1", LNS, "1");
+        validateLeafDataNode(keyIt.next(), "k2", LNS, "2");
+        validateLeafDataNode(keyIt.next(), "k3", LNS, "3");
+
+        Iterator<Map.Entry<NodeKey, DataNode>> it1;
+        it1 = ((InnerNode) n.getValue()).childNodes().entrySet().iterator();
+        validateDataNode(it1.next().getValue(), "k1", LNS,
+                         SINGLE_INSTANCE_LEAF_VALUE_NODE, false, "1");
+        validateDataNode(it1.next().getValue(), "k2", LNS,
+                         SINGLE_INSTANCE_LEAF_VALUE_NODE, false, "2");
+
+        DataNode n1 = it1.next().getValue();
+        validateDataNode(n1, "c1", LNS,
+                         SINGLE_INSTANCE_NODE, true, null);
+        validateDataNode(it1.next().getValue(), "k3", LNS,
+                         SINGLE_INSTANCE_LEAF_VALUE_NODE, false, "3");
+
+        Iterator<Map.Entry<NodeKey, DataNode>> it2;
+        it2 = ((InnerNode) n1).childNodes().entrySet().iterator();
+        DataNode n2 = it2.next().getValue();
+        validateDataNode(n2, "mydata", LNS,
+                         SINGLE_INSTANCE_NODE, true, null);
+        validateDataNode(it2.next().getValue(), "leaf_c1", LNS,
+                         SINGLE_INSTANCE_LEAF_VALUE_NODE, false, "0");
+        validateDataNode(it.next().getValue(), "leaf1", LNS,
+                         MULTI_INSTANCE_LEAF_VALUE_NODE, false, "1");
+        validateDataNode(it.next().getValue(), "leaf1", LNS,
+                         MULTI_INSTANCE_LEAF_VALUE_NODE, false, "2");
+        validateDataNode(it.next().getValue(), "leaf1", LNS,
+                         MULTI_INSTANCE_LEAF_VALUE_NODE, false, "3");
+        validateDataNode(it.next().getValue(), "leaf1", LNS,
+                         MULTI_INSTANCE_LEAF_VALUE_NODE, false, null);
+        walkINTree(dBlr.build(), EXPECTED);
+    }
+}
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AddToResourceIdAnydataTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AddToResourceIdAnydataTest.java
new file mode 100644
index 0000000..96ae07a
--- /dev/null
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AddToResourceIdAnydataTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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.yang.runtime.impl.serializerhelper;
+
+import org.junit.Test;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.runtime.HelperContext;
+import org.onosproject.yang.runtime.impl.TestYangSerializerContext;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.onosproject.yang.runtime.SerializerHelper.addToResourceId;
+import static org.onosproject.yang.runtime.SerializerHelper.initializeResourceId;
+import static org.onosproject.yang.runtime.impl.TestUtils.validateResourceId;
+
+/**
+ * Tests the serializer helper methods.
+ */
+public class AddToResourceIdAnydataTest {
+
+    public static final String LNS = "yrt:list.anydata";
+
+    TestYangSerializerContext context = new TestYangSerializerContext();
+
+    /*
+     * Reference for data node info.
+     */
+    HelperContext info;
+
+    /*
+     * Reference for data node builder.
+     */
+    DataNode.Builder dBlr;
+
+    /*
+     * Reference for resource id.
+     */
+    ResourceId id;
+
+    /*
+     * Reference for the value.
+     */
+    String value;
+
+    /*
+     * Reference for string array to used for resource id testing.
+     */
+    String[] nA;
+    String[] nsA;
+    String[] valA;
+
+    /**
+     * Test add to resource id with anydata.
+     */
+    @Test
+    public void addToDataListTest() {
+
+        ResourceId.Builder rIdBlr = initializeResourceId(context);
+        String value = null;
+        rIdBlr = addToResourceId(rIdBlr, "mydata", LNS, value);
+        ResourceId id = rIdBlr.build();
+
+        //Tree validation
+        nA = new String[]{"/", "mydata"};
+        nsA = new String[]{null, LNS};
+        validateResourceId(nA, nsA, valA, id);
+    }
+
+    /**
+     * Test add to resource id without any key with anydata.
+     */
+    @Test
+    public void addToDataList1Test() {
+        ResourceId.Builder rIdBlr = initializeResourceId(context);
+        String value = null;
+        List<String> valueSet = new LinkedList<>();
+        valueSet.add("1");
+        valueSet.add("2");
+        valueSet.add("3");
+        rIdBlr = addToResourceId(rIdBlr, "l1", LNS, valueSet);
+        rIdBlr = addToResourceId(rIdBlr, "mydata", LNS, value);
+        ResourceId id = rIdBlr.build();
+
+        //Tree validation
+        nA = new String[]{"/", "l1", "k1", "k2", "k3", "mydata"};
+        nsA = new String[]{null, LNS, LNS, LNS, LNS, LNS};
+        valA = new String[]{"1", "2", "3"};
+        validateResourceId(nA, nsA, valA, id);
+    }
+
+    /**
+     * Test add to resource id with anydata as augmented node.
+     */
+    @Test
+    public void addToDataList2Test() {
+        ResourceId.Builder rIdBlr = initializeResourceId(context);
+        String value = null;
+        List<String> valueSet = new LinkedList<>();
+        valueSet.add("1");
+        valueSet.add("2");
+        valueSet.add("3");
+        rIdBlr = addToResourceId(rIdBlr, "l1", LNS, valueSet);
+        rIdBlr = addToResourceId(rIdBlr, "c1", LNS, value);
+        rIdBlr = addToResourceId(rIdBlr, "mydata", LNS, value);
+        ResourceId id = rIdBlr.build();
+
+        //Tree validation
+        nA = new String[]{"/", "l1", "k1", "k2", "k3", "c1", "mydata"};
+        nsA = new String[]{null, LNS, LNS, LNS, LNS, LNS, LNS};
+        valA = new String[]{"1", "2", "3"};
+        validateResourceId(nA, nsA, valA, id);
+    }
+
+    /**
+     * Test add to resource id with augmented anydata under the anydata
+     * augmented node.
+     */
+    @Test
+    public void addToDataList3Test() {
+        ResourceId.Builder rIdBlr = initializeResourceId(context);
+        String value = null;
+        List<String> valueSet = new LinkedList<>();
+        valueSet.add("1");
+        valueSet.add("2");
+        valueSet.add("3");
+        rIdBlr = addToResourceId(rIdBlr, "l1", LNS, valueSet);
+        rIdBlr = addToResourceId(rIdBlr, "c1", LNS, value);
+        rIdBlr = addToResourceId(rIdBlr, "mydata", LNS, value);
+        ResourceId id = rIdBlr.build();
+
+        //Tree validation
+        nA = new String[]{"/", "l1", "k1", "k2", "k3", "c1", "mydata"};
+        nsA = new String[]{null, LNS, LNS, LNS, LNS, LNS, LNS};
+        valA = new String[]{"1", "2", "3"};
+        validateResourceId(nA, nsA, valA, id);
+    }
+}
diff --git a/runtime/src/test/resources/Anydata.yang b/runtime/src/test/resources/Anydata.yang
new file mode 100644
index 0000000..653c4b3
--- /dev/null
+++ b/runtime/src/test/resources/Anydata.yang
@@ -0,0 +1,9 @@
+module anydataTest {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    anydata anydataNode {
+        status current;
+        reference "RFC 7950";
+    }
+}
\ No newline at end of file
diff --git a/runtime/src/test/resources/schemaProviderTestYangFiles/listAnydata.yang b/runtime/src/test/resources/schemaProviderTestYangFiles/listAnydata.yang
new file mode 100644
index 0000000..19f43d9
--- /dev/null
+++ b/runtime/src/test/resources/schemaProviderTestYangFiles/listAnydata.yang
@@ -0,0 +1,53 @@
+module listAnydata {
+
+    yang-version 1.1;
+
+    namespace "yrt:list.anydata";
+
+    prefix "l";
+
+    organization "ON-LAB";
+
+    description "This module defines for list.";
+
+    revision "2016-06-24" {
+        description "Initial revision.";
+    }
+
+    anydata mydata {
+
+    }
+
+    list l1 {
+        key "k1 k2 k3";
+            leaf k1 {
+               type string;
+            }
+
+            leaf k2 {
+              type string;
+            }
+
+            leaf k3 {
+              type string;
+            }
+
+            container c1 {
+                leaf leaf_c1 {
+                  type string;
+                }
+            }
+            anydata mydata {
+
+            }
+    }
+
+    leaf-list leaf1 {
+        type string;
+    }
+
+    augment "/l:l1/l:c1/" {
+        anydata mydata {
+        }
+    }
+}
\ No newline at end of file