} else if (schemaNodeContext instanceof DataSchemaNode) {
schemaNode = (DataSchemaNode) schemaNodeContext;
} else {
- throw new IllegalStateException("Unknow SchemaNode");
+ throw new IllegalStateException("Unknown SchemaNode");
}
final String docRootElm = doc.getDocumentElement().getLocalName();
+ final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
InstanceIdentifierContext<? extends SchemaNode> outIIContext;
DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, pathContext.getSchemaContext());
if (isPost() && !isRpc) {
- final Deque<Object> foundSchemaNodes = findPathToSchemaNodeByName(schemaNode, docRootElm);
+ final Deque<Object> foundSchemaNodes = findPathToSchemaNodeByName(schemaNode, docRootElm, docRootNamespace);
if (foundSchemaNodes.isEmpty()) {
throw new IllegalStateException(String.format("Child \"%s\" was not found in parent schema node \"%s\"",
docRootElm, schemaNode.getQName()));
NormalizedNode<?, ?> parsed = null;
- if(schemaNode instanceof ContainerSchemaNode) {
+ if (schemaNode instanceof ContainerSchemaNode) {
parsed = parserFactory.getContainerNodeParser().parse(Collections.singletonList(doc.getDocumentElement()), (ContainerSchemaNode) schemaNode);
} else if(schemaNode instanceof ListSchemaNode) {
final ListSchemaNode casted = (ListSchemaNode) schemaNode;
return new NormalizedNodeContext(outIIContext, parsed);
}
- private static Deque<Object> findPathToSchemaNodeByName(final DataSchemaNode schemaNode, final String elementName) {
+ private static Deque<Object> findPathToSchemaNodeByName(final DataSchemaNode schemaNode, final String elementName,
+ final String namespace) {
final Deque<Object> result = new ArrayDeque<>();
final ArrayList<ChoiceSchemaNode> choiceSchemaNodes = new ArrayList<>();
final Collection<DataSchemaNode> children = ((DataNodeContainer) schemaNode).getChildNodes();
for (final DataSchemaNode child : children) {
if (child instanceof ChoiceSchemaNode) {
choiceSchemaNodes.add((ChoiceSchemaNode) child);
- } else if (child.getQName().getLocalName().equalsIgnoreCase(elementName)) {
+ } else if (child.getQName().getLocalName().equalsIgnoreCase(elementName)
+ && child.getQName().getNamespace().toString().equalsIgnoreCase(namespace)) {
+ // add child to result
result.push(child);
+
+ // find augmentation
if (child.isAugmenting()) {
final AugmentationSchema augment = findCorrespondingAugment(schemaNode, child);
if (augment != null) {
result.push(augment);
}
}
+
+ // return result
return result;
}
}
for (final ChoiceSchemaNode choiceNode : choiceSchemaNodes) {
for (final ChoiceCaseNode caseNode : choiceNode.getCases()) {
- final Deque<Object> resultFromRecursion = findPathToSchemaNodeByName(caseNode, elementName);
+ final Deque<Object> resultFromRecursion = findPathToSchemaNodeByName(caseNode, elementName, namespace);
if (!resultFromRecursion.isEmpty()) {
resultFromRecursion.push(choiceNode);
if (choiceNode.isAugmenting()) {
assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
}
+
+ /**
+ * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
+ * used to distinguish between them to find correct one. Check if container was found not only according to its name
+ * but also by correct namespace used in payload.
+ */
+ @Test
+ public void findFooContainerUsingNamespaceTest() throws Exception {
+ mockBodyReader("", xmlBodyReader, true);
+ final InputStream inputStream = TestXmlBodyReader.class
+ .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
+ final NormalizedNodeContext returnValue = xmlBodyReader
+ .readFrom(null, null, null, mediaType, null, inputStream);
+
+ // check return value
+ checkNormalizedNodeContext(returnValue);
+ // check if container was found both according to its name and namespace
+ assertEquals("Not correct container found, name was ignored",
+ "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
+ assertEquals("Not correct container found, namespace was ignored",
+ "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
+ }
+
+ /**
+ * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
+ * used to distinguish between them to find correct one. Check if container was found not only according to its name
+ * but also by correct namespace used in payload.
+ */
+ @Test
+ public void findBarContainerUsingNamespaceTest() throws Exception {
+ mockBodyReader("", xmlBodyReader, true);
+ final InputStream inputStream = TestXmlBodyReader.class
+ .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
+ final NormalizedNodeContext returnValue = xmlBodyReader
+ .readFrom(null, null, null, mediaType, null, inputStream);
+
+ // check return value
+ checkNormalizedNodeContext(returnValue);
+ // check if container was found both according to its name and namespace
+ assertEquals("Not correct container found, name was ignored",
+ "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
+ assertEquals("Not correct container found, namespace was ignored",
+ "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
+ }
}
package org.opendaylight.controller.sal.rest.impl.test.providers;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
dataNodeIdent));
}
+
+ /**
+ * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
+ * used to distinguish between them to find correct one. Check if container was found not only according to its name
+ * but also by correct namespace used in payload.
+ */
+ @Test
+ public void findFooContainerUsingNamespaceTest() throws Exception {
+ mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
+ final InputStream inputStream = TestXmlBodyReader.class
+ .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
+ final NormalizedNodeContext returnValue = xmlBodyReader
+ .readFrom(null, null, null, mediaType, null, inputStream);
+
+ // check return value
+ checkMountPointNormalizedNodeContext(returnValue);
+ // check if container was found both according to its name and namespace
+ assertEquals("Not correct container found, name was ignored",
+ "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
+ assertEquals("Not correct container found, namespace was ignored",
+ "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
+ }
+
+ /**
+ * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
+ * used to distinguish between them to find correct one. Check if container was found not only according to its name
+ * but also by correct namespace used in payload.
+ */
+ @Test
+ public void findBarContainerUsingNamespaceTest() throws Exception {
+ mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
+ final InputStream inputStream = TestXmlBodyReader.class
+ .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
+ final NormalizedNodeContext returnValue = xmlBodyReader
+ .readFrom(null, null, null, mediaType, null, inputStream);
+
+ // check return value
+ checkMountPointNormalizedNodeContext(returnValue);
+ // check if container was found both according to its name and namespace
+ assertEquals("Not correct container found, name was ignored",
+ "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
+ assertEquals("Not correct container found, namespace was ignored",
+ "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
+ }
}
--- /dev/null
+<!--
+ ~ Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ ~
+ ~ This program and the accompanying materials are made available under the
+ ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+ -->
+
+<foo-bar-container xmlns="bar:module">
+</foo-bar-container>
\ No newline at end of file
--- /dev/null
+<!--
+ ~ Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ ~
+ ~ This program and the accompanying materials are made available under the
+ ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+ -->
+
+<foo-bar-container xmlns="foo:module">
+</foo-bar-container>
\ No newline at end of file
--- /dev/null
+module bar-module {
+ namespace "bar:module";
+
+ prefix "bar-module";
+ revision 2016-09-29 {
+ }
+
+ /* This container has the same name as container in foo-module */
+ container foo-bar-container {
+ }
+}
\ No newline at end of file
--- /dev/null
+module foo-module {
+ namespace "foo:module";
+
+ prefix "foo-module";
+ revision 2016-09-29 {
+ }
+
+ /* This container has the same name as container in bar-module */
+ container foo-bar-container {
+ }
+}
\ No newline at end of file