[ONOS-5102] Value validation in YANG utils for YMS + schema node map changes
Change-Id: I9aafe857e50e06e57d0ab254dbe44c365052b65d
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;
+ }
+ }
+ }
+ }
+}