AnyData feature changes

Change-Id: Iccba22d731321f38b8146bbfc85477d2252a3b10
diff --git a/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/AnydataListenerTest.java b/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/AnydataListenerTest.java
new file mode 100644
index 0000000..d3ed0eb
--- /dev/null
+++ b/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/AnydataListenerTest.java
@@ -0,0 +1,216 @@
+/*
+ * 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.compiler.parser.impl.listeners;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yang.compiler.datamodel.YangAnydata;
+import org.onosproject.yang.compiler.datamodel.YangList;
+import org.onosproject.yang.compiler.datamodel.YangModule;
+import org.onosproject.yang.compiler.datamodel.YangNode;
+import org.onosproject.yang.compiler.datamodel.YangNodeType;
+import org.onosproject.yang.compiler.datamodel.YangStatusType;
+import org.onosproject.yang.compiler.parser.exceptions.ParserException;
+import org.onosproject.yang.compiler.parser.impl.YangUtilsParserManager;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test cases for testing anydata listener.
+ */
+public class AnydataListenerTest {
+
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    private final YangUtilsParserManager manager = new YangUtilsParserManager();
+
+    /**
+     * Checks anydata statement as sub-statement of module.
+     */
+    @Test
+    public void processModuleSubStatementAnydata() throws IOException,
+            ParserException {
+
+        YangNode node = manager.getDataModel(
+                "src/test/resources/ModuleSubStatementAnydata.yang");
+
+        assertThat((node instanceof YangModule), is(true));
+
+        // Check whether the node type is set properly to module.
+        assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+        // Check whether the module name is set correctly.
+        YangModule yangNode = (YangModule) node;
+        assertThat(yangNode.getName(), is("Test"));
+
+        // Check whether the anydata is child of module
+        YangAnydata anyData = (YangAnydata) yangNode.getChild();
+        assertThat(anyData.getName(), is("valid"));
+    }
+
+    /**
+     * Checks if anydata identifier in module is duplicate.
+     */
+    @Test(expected = ParserException.class)
+    public void processModuleDuplicateAnydata() throws IOException,
+            ParserException {
+
+        YangNode node = manager.getDataModel(
+                "src/test/resources/ModuleDuplicateAnydata.yang");
+    }
+
+    /**
+     * Checks if anydata identifier in list is duplicate.
+     */
+    @Test(expected = ParserException.class)
+    public void processListDuplicateAnydata() throws IOException,
+            ParserException {
+
+        YangNode node = manager.getDataModel(
+                "src/test/resources/ListDuplicateAnydata.yang");
+    }
+
+    /**
+     * Checks if anydata identifier collides with list at same level.
+     */
+    @Test(expected = ParserException.class)
+    public void processDuplicateAnydataAndList() throws IOException,
+            ParserException {
+
+        YangNode node = manager.getDataModel(
+                "src/test/resources/DuplicateAnydataAndList.yang");
+    }
+
+    /**
+     * Checks anydata statement as sub-statement of list.
+     */
+    @Test
+    public void processListSubStatementAnydata() throws IOException,
+            ParserException {
+
+        YangNode node = manager.getDataModel(
+                "src/test/resources/ListSubStatementAnydata.yang");
+
+        assertThat((node instanceof YangModule), is(true));
+
+        // Check whether the node type is set properly to module.
+        assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+        // Check whether the module name is set correctly.
+        YangModule yangNode = (YangModule) node;
+        assertThat(yangNode.getName(), is("Test"));
+
+        // Check whether the list is child of module
+        YangList yangList1 = (YangList) yangNode.getChild();
+        assertThat(yangList1.getName(), is("ospf"));
+
+        Iterator<String> keyList = yangList1.getKeyList().iterator();
+        assertThat(keyList.next(), is("process-id"));
+
+        // Check whether the list is child of list
+        YangAnydata yangAnydata = (YangAnydata) yangList1.getChild();
+        assertThat(yangAnydata.getName(), is("interface"));
+    }
+
+    /**
+     * Checks anydata with all its sub-statements.
+     */
+    @Test
+    public void processAnydataSubStatements() throws IOException,
+            ParserException {
+
+        YangNode node = manager.getDataModel(
+                "src/test/resources/AnydataSubStatements.yang");
+
+        assertThat((node instanceof YangModule), is(true));
+
+        // Check whether the node type is set properly to module.
+        assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
+
+        // Check whether the module name is set correctly.
+        YangModule yangNode = (YangModule) node;
+        assertThat(yangNode.getName(), is("Test"));
+
+        // Check whether the anydata is child of module
+        YangAnydata yangAnydata = (YangAnydata) yangNode.getChild();
+
+        // Check whether anydata properties as set correctly.
+        assertThat(yangAnydata.getName(), is("ospf"));
+
+        assertThat(yangAnydata.isConfig(), is(true));
+        assertThat(yangAnydata.getDescription(), is("\"anydata description\""));
+        assertThat(yangAnydata.getStatus(), is(YangStatusType.CURRENT));
+        assertThat(yangAnydata.getReference(), is("\"anydata reference\""));
+    }
+
+    /**
+     * Checks cardinality of sub-statements of anydata.
+     */
+    @Test
+    public void processAnydataSubStatementCardinality() throws IOException,
+            ParserException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("YANG file error: \"reference\" is defined more" +
+                                     " than once in \"anydata valid\".");
+        YangNode node = manager.getDataModel(
+                "src/test/resources/AnydataSubStatementCardinality.yang");
+    }
+
+    /**
+     * Checks anydata as root node.
+     */
+    @Test
+    public void processAnydataRootNode() throws IOException, ParserException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage("no viable alternative at input 'anydata'");
+        YangNode node = manager.getDataModel(
+                "src/test/resources/AnydataRootNode.yang");
+    }
+
+    /**
+     * Checks invalid identifier for anydata statement.
+     */
+    @Test
+    public void processAnydataInvalidIdentifier() throws IOException,
+            ParserException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage(
+                "YANG file error : anydata name 1valid is not valid.");
+        YangNode node = manager.getDataModel(
+                "src/test/resources/AnydataInvalidIdentifier.yang");
+    }
+
+    /**
+     * Checks invalid identifier for anydata statement.
+     */
+    @Test
+    public void processAnydataInvalidVersionTest() throws IOException,
+            ParserException {
+        thrown.expect(ParserException.class);
+        thrown.expectMessage(
+                "YANG file error : anydata with name valid at line number 5 " +
+                        "in src/test/resources/AnydataInvalidVersion.yang is feature of YANG version 1.1");
+        YangNode node = manager.getDataModel(
+                "src/test/resources/AnydataInvalidVersion.yang");
+    }
+}
diff --git a/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/SubModuleListenerTest.java b/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/SubModuleListenerTest.java
index 19fdd5f..8371e88 100644
--- a/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/SubModuleListenerTest.java
+++ b/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/SubModuleListenerTest.java
@@ -53,7 +53,7 @@
         // Check whether the module name is set correctly.
         assertThat(yangNode.getName(), is("Test"));
         // Checks for the version value in data model tree.
-        assertThat(yangNode.getVersion(), is((byte) 1));
+        assertThat(yangNode.getVersion(), is("1"));
         // Checks identifier of belongsto in data model tree.
         assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
         // Checks for the version value in data model tree.
@@ -79,7 +79,7 @@
         // Check whether the module name is set correctly.
         assertThat(yangNode.getName(), is("Test"));
         // Checks for the version value in data model tree.
-        assertThat(yangNode.getVersion(), is((byte) 1));
+        assertThat(yangNode.getVersion(), is("1"));
         // Checks identifier of belongsto in data model tree.
         assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
         // Checks for the version value in data model tree.
diff --git a/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/VersionListenerTest.java b/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/VersionListenerTest.java
index 21bd740..bd775c8 100644
--- a/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/VersionListenerTest.java
+++ b/compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/VersionListenerTest.java
@@ -52,7 +52,7 @@
         YangNode node = manager.getDataModel("src/test/resources/VersionValidEntry.yang");
 
         // Checks for the version value in data model tree.
-        assertThat(((YangModule) node).getVersion(), is((byte) 1));
+        assertThat(((YangModule) node).getVersion(), is("1"));
     }
 
     /**
@@ -64,7 +64,7 @@
         YangNode node = manager.getDataModel("src/test/resources/ValidVersionWithDoubleQuotes.yang");
 
         // Checks for the version value in data model tree.
-        assertThat(((YangModule) node).getVersion(), is((byte) 1));
+        assertThat(((YangModule) node).getVersion(), is("1"));
     }
 
     /**
@@ -76,7 +76,7 @@
         YangNode node = manager.getDataModel("src/test/resources/VersionNotPresent.yang");
 
         // Checks for the version value in data model tree.
-        assertThat(((YangModule) node).getVersion(), is((byte) 1));
+        assertThat(((YangModule) node).getVersion(), is("1"));
     }
 
     /**
@@ -97,7 +97,7 @@
         YangNode node = manager.getDataModel("src/test/resources/VersionOrder.yang");
 
         // Checks for the version value in data model tree.
-        assertThat(((YangModule) node).getVersion(), is((byte) 1));
+        assertThat(((YangModule) node).getVersion(), is("1"));
     }
 
     /**
diff --git a/compiler/base/parser/src/test/resources/AnydataInvalidIdentifier.yang b/compiler/base/parser/src/test/resources/AnydataInvalidIdentifier.yang
new file mode 100644
index 0000000..83b7547
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/AnydataInvalidIdentifier.yang
@@ -0,0 +1,9 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    anydata 1valid {
+            status current;
+            reference "RFC 7950";
+    }
+}
\ No newline at end of file
diff --git a/compiler/base/parser/src/test/resources/AnydataInvalidVersion.yang b/compiler/base/parser/src/test/resources/AnydataInvalidVersion.yang
new file mode 100644
index 0000000..74d1d3c
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/AnydataInvalidVersion.yang
@@ -0,0 +1,9 @@
+module Test {
+    yang-version 1;
+    namespace http://huawei.com;
+    prefix Ant;
+    anydata valid {
+            status current;
+            reference "RFC 7950";
+    }
+}
\ No newline at end of file
diff --git a/compiler/base/parser/src/test/resources/AnydataRootNode.yang b/compiler/base/parser/src/test/resources/AnydataRootNode.yang
new file mode 100644
index 0000000..393e8e3
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/AnydataRootNode.yang
@@ -0,0 +1,4 @@
+anydata valid {
+    status current;
+    reference "RFC 7950";
+}
\ No newline at end of file
diff --git a/compiler/base/parser/src/test/resources/AnydataSubStatementCardinality.yang b/compiler/base/parser/src/test/resources/AnydataSubStatementCardinality.yang
new file mode 100644
index 0000000..84c8f06
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/AnydataSubStatementCardinality.yang
@@ -0,0 +1,10 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    anydata valid {
+        reference "RFC 7950";
+        reference "RFC 7950";
+        status current;
+    }
+}
\ No newline at end of file
diff --git a/compiler/base/parser/src/test/resources/AnydataSubStatements.yang b/compiler/base/parser/src/test/resources/AnydataSubStatements.yang
new file mode 100644
index 0000000..fb075c4
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/AnydataSubStatements.yang
@@ -0,0 +1,11 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    anydata ospf {
+        config true;
+        description "anydata description";
+        status current;
+        reference "anydata reference";
+    }
+}
\ No newline at end of file
diff --git a/compiler/base/parser/src/test/resources/DuplicateAnydataAndList.yang b/compiler/base/parser/src/test/resources/DuplicateAnydataAndList.yang
new file mode 100644
index 0000000..5574243
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/DuplicateAnydataAndList.yang
@@ -0,0 +1,25 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    container ospf {
+        anydata valid {
+            status current;
+            reference "RFC 7950";
+        }
+        list valid {
+            key "process-id";
+            container interface {
+                leaf invalid-interval {
+                    type "uint16";
+                    units "seconds";
+                    status current;
+                    reference "RFC 6020";
+                }
+            }
+            leaf process-id {
+                type "string";
+            }
+        }
+    }
+}
diff --git a/compiler/base/parser/src/test/resources/ListDuplicateAnydata.yang b/compiler/base/parser/src/test/resources/ListDuplicateAnydata.yang
new file mode 100644
index 0000000..6313c69
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/ListDuplicateAnydata.yang
@@ -0,0 +1,19 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    list ospf {
+        key "process-id";
+        anydata interface {
+            status current;
+            reference "RFC 7950";
+        }
+        leaf process-id {
+            type "string";
+        }
+        anydata interface {
+            status current;
+            reference "RFC 7950";
+        }
+    }
+}
diff --git a/compiler/base/parser/src/test/resources/ListSubStatementAnydata.yang b/compiler/base/parser/src/test/resources/ListSubStatementAnydata.yang
new file mode 100644
index 0000000..375c42a
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/ListSubStatementAnydata.yang
@@ -0,0 +1,15 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    list ospf {
+        key "process-id";
+        anydata interface {
+            status current;
+            reference "RFC 7950";
+        }
+        leaf process-id {
+            type "string";
+        }
+    }
+}
\ No newline at end of file
diff --git a/compiler/base/parser/src/test/resources/ModuleDuplicateAnydata.yang b/compiler/base/parser/src/test/resources/ModuleDuplicateAnydata.yang
new file mode 100644
index 0000000..16fe694
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/ModuleDuplicateAnydata.yang
@@ -0,0 +1,17 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    anydata valid {
+        status current;
+        reference "RFC 7950";
+    }
+    anydata invalid {
+        status current;
+        reference "RFC 7950";
+    }
+    anydata valid {
+        status current;
+        reference "RFC 7950";
+    }
+}
diff --git a/compiler/base/parser/src/test/resources/ModuleSubStatementAnydata.yang b/compiler/base/parser/src/test/resources/ModuleSubStatementAnydata.yang
new file mode 100644
index 0000000..947f420
--- /dev/null
+++ b/compiler/base/parser/src/test/resources/ModuleSubStatementAnydata.yang
@@ -0,0 +1,9 @@
+module Test {
+    yang-version 1.1;
+    namespace http://huawei.com;
+    prefix Ant;
+    anydata valid {
+        status current;
+        reference "RFC 7950";
+    }
+}
\ No newline at end of file