[ONOS-5102] Value validation in YANG utils for YMS + schema node map changes

Change-Id: I9aafe857e50e06e57d0ab254dbe44c365052b65d
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
index 7feb947..eeeeca0 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
@@ -436,8 +436,13 @@
     }
 
     @Override
+    public void isValueValid(String value) throws DataModelException {
+        getDataType().isValidValue(value);
+    }
+
+    @Override
     public int getMandatoryChildCount() throws DataModelException {
-        throw new DataModelException("TODO");
+        throw new DataModelException("Leaf can't have child.");
     }
 
     @Override
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java
index 59c0111..4a7ea54 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java
@@ -454,6 +454,11 @@
     }
 
     @Override
+    public void isValueValid(String value) throws DataModelException {
+        getDataType().isValidValue(value);
+    }
+
+    @Override
     public int getMandatoryChildCount() throws DataModelException {
         throw new DataModelException("TODO");
     }
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangList.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangList.java
index bf3f8ea..c754b3e 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangList.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangList.java
@@ -237,7 +237,7 @@
 
     @Override
     public YangSchemaNodeType getYangSchemaNodeType() {
-        return YangSchemaNodeType.YANG_MULTI_INSTANCE_LEAF_NODE;
+        return YangSchemaNodeType.YANG_MULTI_INSTANCE_NODE;
     }
 
     /**
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java
index e40f81d..ba0fdf4 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java
@@ -311,7 +311,7 @@
      */
     public void processAdditionOfSchemaNodeToCurNodeMap(String name, String namespace, YangSchemaNode
             yangSchemaNode) {
-        processAdditionOfSchemaNodeToMap(getName(), getNameSpace().getUri(), this, this);
+        processAdditionOfSchemaNodeToMap(name, namespace, yangSchemaNode, this);
     }
 
     /**
@@ -664,4 +664,9 @@
             ((YangLeavesHolder) this).setLeafNameSpaceAndAddToParentSchemaMap();
         }
     }
+
+    @Override
+    public void isValueValid(String value) throws DataModelException {
+        throw new DataModelException("Value validation asked for YANG node.");
+    }
 }
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
index 8a48637..4b527c0 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
@@ -42,6 +42,15 @@
     YangSchemaNodeContextInfo getChildSchema(YangSchemaNodeIdentifier dataNodeIdentifier);
 
     /**
+     * Validates whether the leaf/leaf-list value is valid as per YANG. It is
+     * used by YMS to validate input value.
+     *
+     * @param value value of leaf/leaf-list
+     * @throws DataModelException a violation in data model rule
+     */
+    void isValueValid(String value) throws DataModelException;
+
+    /**
      * Returns count of mandatory child nodes, this is used by YMS to identify whether
      * in request all mandatory child nodes are available.
      *
@@ -58,4 +67,5 @@
      * @return map of default child nodes
      */
     Map<YangSchemaNodeIdentifier, YangSchemaNode> getDefaultChild(YangSchemaNodeIdentifier dataNodeIdentifier);
+
 }
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNodeIdentifier.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNodeIdentifier.java
index acdb61e..08dd398 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNodeIdentifier.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNodeIdentifier.java
@@ -17,6 +17,7 @@
 package org.onosproject.yangutils.datamodel;
 
 import java.io.Serializable;
+import java.util.Objects;
 
 /**
  * Represents YANG data node identifier which is a combination of name and namespace.
@@ -73,4 +74,22 @@
     public void setNamespace(String namespace) {
         this.namespace = namespace;
     }
+
+    @Override
+    public boolean equals(Object obj) {
+
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof YangSchemaNodeIdentifier) {
+            final YangSchemaNodeIdentifier other = (YangSchemaNodeIdentifier) obj;
+            return Objects.equals(name, other.name) && Objects.equals(namespace, other.namespace);
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(name, namespace);
+    }
 }
diff --git a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListener.java b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListener.java
index f32a4ed..ee2cb3d 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListener.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListener.java
@@ -17,7 +17,6 @@
 package org.onosproject.yangutils.parser.impl.listeners;
 
 import java.net.URI;
-
 import org.onosproject.yangutils.datamodel.YangModule;
 import org.onosproject.yangutils.datamodel.YangNameSpace;
 import org.onosproject.yangutils.datamodel.utils.Parsable;
@@ -30,6 +29,7 @@
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
+import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
 
 /*
@@ -71,10 +71,10 @@
      * (namespace), perform validations and update the data model tree.
      *
      * @param listener Listener's object
-     * @param ctx context object of the grammar rule
+     * @param ctx      context object of the grammar rule
      */
     public static void processNamespaceEntry(TreeWalkListener listener,
-            GeneratedYangParser.NamespaceStatementContext ctx) {
+                                             GeneratedYangParser.NamespaceStatementContext ctx) {
 
         // Check for stack to be non empty.
         checkStackIsNotEmpty(listener, MISSING_HOLDER, NAMESPACE_DATA, ctx.string().getText(), ENTRY);
@@ -92,7 +92,7 @@
             case MODULE_DATA: {
                 YangModule module = (YangModule) tmpNode;
                 YangNameSpace uri = new YangNameSpace();
-                uri.setUri(ctx.string().getText());
+                uri.setUri(removeQuotesAndHandleConcat(ctx.string().getText()));
                 module.setNameSpace(uri);
                 break;
             }
diff --git a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListenerTest.java b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListenerTest.java
index 0f35705..c10046f 100644
--- a/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListenerTest.java
+++ b/plugin/src/test/java/org/onosproject/yangutils/parser/impl/listeners/NamespaceListenerTest.java
@@ -16,14 +16,13 @@
 
 package org.onosproject.yangutils.parser.impl.listeners;
 
+import java.io.IOException;
 import org.junit.Test;
 import org.onosproject.yangutils.datamodel.YangModule;
 import org.onosproject.yangutils.datamodel.YangNode;
 import org.onosproject.yangutils.parser.exceptions.ParserException;
 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
 
-import java.io.IOException;
-
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
 
@@ -52,7 +51,7 @@
         YangNode node = manager.getDataModel("src/test/resources/NamespaceInDoubleQuotes.yang");
 
         // Checks for the version value in data model tree.
-        assertThat(((YangModule) node).getNameSpace().getUri(), is("\"urn:ietf:params:xml:ns:yang:ietf-ospf\""));
+        assertThat(((YangModule) node).getNameSpace().getUri(), is("urn:ietf:params:xml:ns:yang:ietf-ospf"));
     }
 
     /**
@@ -84,4 +83,4 @@
 
         YangNode node = manager.getDataModel("src/test/resources/NamespaceNoEntryTest.yang");
     }
-}
\ No newline at end of file
+}
diff --git a/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/SchemaNodeTest.java b/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/SchemaNodeTest.java
new file mode 100644
index 0000000..df41c3d
--- /dev/null
+++ b/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/SchemaNodeTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2016-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.
+ * 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.yangutils.plugin.manager;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangLeaf;
+import org.onosproject.yangutils.datamodel.YangLeavesHolder;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangSchemaNodeContextInfo;
+import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
+import org.onosproject.yangutils.utils.io.YangPluginConfig;
+import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
+
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
+
+/**
+ * Test cases for testing YANG schema node.
+ */
+public class SchemaNodeTest {
+
+    private final YangUtilManager utilManager = new YangUtilManager();
+
+    /**
+     * Checks method to get schema node from map.
+     *
+     * @throws MojoExecutionException
+     */
+    @Test
+    public void processSchemaNodeMap() throws IOException, ParserException, MojoExecutionException {
+
+        String searchDir = "src/test/resources/schemaMap";
+        utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+        utilManager.parseYangFileInfoSet();
+        utilManager.createYangNodeSet();
+        utilManager.resolveDependenciesUsingLinker();
+        YangPluginConfig yangPluginConfig = new YangPluginConfig();
+        yangPluginConfig.setCodeGenDir("target/schemaMap/");
+        utilManager.translateToJava(yangPluginConfig);
+
+        Iterator<YangNode> yangNodeIterator = utilManager.getYangNodeSet().iterator();
+        YangNode rootNode = yangNodeIterator.next();
+
+        assertThat(rootNode.getYsnContextInfoMap(), is(notNullValue()));
+        Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap = rootNode.getYsnContextInfoMap();
+        YangSchemaNodeIdentifier yangSchemaNodeIdentifier = new YangSchemaNodeIdentifier();
+        yangSchemaNodeIdentifier.setName("testcontainer");
+        yangSchemaNodeIdentifier.setNamespace("http://huawei.com");
+        assertThat(schemaMap.get(yangSchemaNodeIdentifier), is(notNullValue()));
+        YangSchemaNodeContextInfo yangSchemaNodeContextInfo = schemaMap.get(yangSchemaNodeIdentifier);
+        assertThat(yangSchemaNodeContextInfo.getSchemaNode(), is(rootNode.getChild()));
+
+        assertThat(rootNode.getChild().getYsnContextInfoMap(), is(notNullValue()));
+        Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap2 = rootNode.getChild()
+                .getYsnContextInfoMap();
+        yangSchemaNodeIdentifier.setName("testleaf");
+        yangSchemaNodeIdentifier.setNamespace("http://huawei.com");
+        assertThat(schemaMap2.get(yangSchemaNodeIdentifier), is(notNullValue()));
+
+        yangSchemaNodeIdentifier.setName("pretzel");
+        yangSchemaNodeIdentifier.setNamespace("http://huawei.com");
+        assertThat(schemaMap2.get(yangSchemaNodeIdentifier), is(notNullValue()));
+
+        assertThat(rootNode.getChild().getChild().getYsnContextInfoMap(), is(notNullValue()));
+        Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap3 = rootNode.getChild().getChild()
+                .getYsnContextInfoMap();
+        yangSchemaNodeIdentifier.setName("pretzel");
+        yangSchemaNodeIdentifier.setNamespace("http://huawei.com");
+        assertThat(schemaMap3.get(yangSchemaNodeIdentifier), is(notNullValue()));
+        YangSchemaNodeContextInfo yangSchemaNodeContextInfo3 = schemaMap3.get(yangSchemaNodeIdentifier);
+
+        assertThat(rootNode.getChild().getChild().getChild().getYsnContextInfoMap(), is(notNullValue()));
+        Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap4 = rootNode.getChild().getChild().getChild()
+                .getYsnContextInfoMap();
+        yangSchemaNodeIdentifier.setName("pretzel");
+        yangSchemaNodeIdentifier.setNamespace("http://huawei.com");
+        assertThat(schemaMap4.get(yangSchemaNodeIdentifier), is(notNullValue()));
+
+        YangSchemaNodeContextInfo yangSchemaNodeContextInfo2 = schemaMap4.get(yangSchemaNodeIdentifier);
+        List<YangLeaf> yangListOfLeaf = ((YangLeavesHolder) rootNode.getChild().getChild().getChild()).getListOfLeaf();
+        YangLeaf yangLeaf = yangListOfLeaf.get(0);
+        assertThat(yangSchemaNodeContextInfo2.getSchemaNode(), is(yangLeaf));
+
+        assertThat(yangSchemaNodeContextInfo3.getSchemaNode(), is(yangLeaf));
+        assertThat(yangSchemaNodeContextInfo3.getContextSwitchedNode(), is(rootNode.getChild().getChild().getChild()));
+
+        deleteDirectory("target/schemaMap/");
+    }
+}
diff --git a/plugin/src/test/resources/schemaMap/SchemaMap.yang b/plugin/src/test/resources/schemaMap/SchemaMap.yang
new file mode 100644
index 0000000..b8b5c48
--- /dev/null
+++ b/plugin/src/test/resources/schemaMap/SchemaMap.yang
@@ -0,0 +1,17 @@
+module Testmodule {
+    yang-version 1;
+    namespace http://huawei.com;
+    prefix Ant;
+    container testcontainer {
+        leaf testleaf {
+            type "uint16";
+        }
+        choice snack {
+            case sports-arena {
+                leaf pretzel {
+                    type empty;
+                }
+             }
+        }
+    }
+}