From: Peter Suna Date: Tue, 10 Jan 2023 09:55:13 +0000 (+0100) Subject: Fix XML parser condition in XmlPatchBodyReader X-Git-Tag: v5.0.1~2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=73795b7e0ceef10455970623ffadf3c8c25820a7;p=netconf.git Fix XML parser condition in XmlPatchBodyReader Remove redundant if condition which rejects to parse LeafListSchemaNode or LeafSchemaNode value from XML. JIRA: NETCONF-937 Change-Id: Ib001582b8476f4508bab7d746296cb7ea1b1619e Signed-off-by: Peter Suna Signed-off-by: Ivan Hrasko Signed-off-by: Robert Varga --- diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java index 0059023a61..2a566a8a49 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java @@ -19,7 +19,6 @@ import java.util.Locale; import javax.ws.rs.Consumes; import javax.ws.rs.WebApplicationException; import javax.ws.rs.ext.Provider; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLStreamException; import javax.xml.transform.dom.DOMSource; import org.eclipse.jdt.annotation.NonNull; @@ -37,15 +36,11 @@ import org.opendaylight.yangtools.yang.common.ErrorTag; import org.opendaylight.yangtools.yang.common.ErrorType; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult; import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree; -import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; @@ -85,13 +80,12 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader { } private static PatchContext parse(final InstanceIdentifierContext pathContext, final Document doc) - throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException { + throws XMLStreamException, IOException, SAXException, URISyntaxException { final List resultCollection = new ArrayList<>(); final String patchId = doc.getElementsByTagName("patch-id").item(0).getFirstChild().getNodeValue(); final NodeList editNodes = doc.getElementsByTagName("edit"); for (int i = 0; i < editNodes.getLength(); i++) { - DataSchemaNode schemaNode = (DataSchemaNode) pathContext.getSchemaNode(); final Element element = (Element) editNodes.item(i); final String operation = element.getElementsByTagName("operation").item(0).getFirstChild().getNodeValue(); final PatchEditOperation oper = PatchEditOperation.valueOf(operation.toUpperCase(Locale.ROOT)); @@ -117,7 +111,6 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader { final var lookup = DataSchemaContextTree.from(pathContext.getSchemaContext()) .enterPath(targetII).orElseThrow(); - schemaNode = lookup.node().getDataSchemaNode(); final var stack = lookup.stack(); inference = stack.toInference(); if (!stack.isEmpty()) { @@ -140,23 +133,15 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader { } if (oper.isWithValue()) { - final NormalizedNode parsed; - if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode) { - final NormalizedNodeResult resultHolder = new NormalizedNodeResult(); - final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder); - final XmlParserStream xmlParser = XmlParserStream.create(writer, inference); - xmlParser.traverse(new DOMSource(firstValueElement)); - parsed = resultHolder.getResult(); - } else { - parsed = null; - } - + final NormalizedNodeResult resultHolder = new NormalizedNodeResult(); + final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder); + final XmlParserStream xmlParser = XmlParserStream.create(writer, inference); + xmlParser.traverse(new DOMSource(firstValueElement)); // for lists allow to manipulate with list items through their parent if (targetII.getLastPathArgument() instanceof NodeIdentifierWithPredicates) { targetII = targetII.getParent(); } - - resultCollection.add(new PatchEntity(editId, oper, targetII, parsed)); + resultCollection.add(new PatchEntity(editId, oper, targetII, resultHolder.getResult())); } else { resultCollection.add(new PatchEntity(editId, oper, targetII)); } diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java index 664ae5e79a..87485e60a4 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java @@ -168,6 +168,9 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest { final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream); checkPatchContext(returnValue); + final var data = returnValue.getData().get(0).getNode(); + assertEquals(LEAF_NAME_QNAME, data.getIdentifier().getNodeType()); + assertEquals(ImmutableNodes.leafNode(LEAF_NAME_QNAME, "my-leaf20"), data); } /** @@ -254,7 +257,7 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest { final var expectedData = Builders.leafSetBuilder() .withNodeIdentifier(new NodeIdentifier(LEAF_SET_QNAME)) .withChild(Builders.leafSetEntryBuilder() - .withNodeIdentifier(new NodeWithValue(LEAF_SET_QNAME, "data1")) + .withNodeIdentifier(new NodeWithValue<>(LEAF_SET_QNAME, "data1")) .withValue("data1") .build()) .build(); diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java index c9b33b2062..b791ed626d 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java @@ -15,7 +15,6 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import javax.ws.rs.core.MediaType; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest; @@ -176,9 +175,7 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest { /** * Test of Yang Patch on the leaf set node element. - * TODO: Remove ignore when NETCONF-937 will be resolved */ - @Ignore @Test public void modulePatchTargetLeafSetNodeTest() throws Exception { mockBodyReader("", xmlToPatchBodyReader, false); @@ -199,7 +196,7 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest { final var expectedData = Builders.leafSetBuilder() .withNodeIdentifier(new NodeIdentifier(LEAF_SET_QNAME)) .withChild(Builders.leafSetEntryBuilder() - .withNodeIdentifier(new NodeWithValue(LEAF_SET_QNAME, "data1")) + .withNodeIdentifier(new NodeWithValue<>(LEAF_SET_QNAME, "data1")) .withValue("data1") .build()) .build(); @@ -280,4 +277,31 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest { assertEquals(CHOICE_CONT_QNAME, data.getIdentifier().getNodeType()); assertEquals(expectedData, data); } + + /** + * Test reading simple leaf value. + */ + @Test + public void modulePatchSimpleLeafValueTest() throws Exception { + mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false); + final var inputStream = new ByteArrayInputStream(""" + + test-patch + this is test patch + + edit1 + replace + /my-list2=my-leaf20/name + + my-leaf20 + + + + """.getBytes(StandardCharsets.UTF_8)); + final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream); + checkPatchContext(returnValue); + final var data = returnValue.getData().get(0).getNode(); + assertEquals(LEAF_NAME_QNAME, data.getIdentifier().getNodeType()); + assertEquals(ImmutableNodes.leafNode(LEAF_NAME_QNAME, "my-leaf20"), data); + } } diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/AbstractBodyReaderTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/AbstractBodyReaderTest.java index 876dbfa002..ba0e597b95 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/AbstractBodyReaderTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/AbstractBodyReaderTest.java @@ -52,6 +52,8 @@ public abstract class AbstractBodyReaderTest { protected static final QName LIST_LEAF2_QNAME = QName.create("list:ns", "leaf2").intern(); protected static final QName CHOICE_CONT_QNAME = QName.create("choice:ns", "case-cont1").intern(); protected static final QName CASE_LEAF1_QNAME = QName.create("choice:ns", "case-leaf1").intern(); + protected static final QName LEAF_NAME_QNAME = QName.create("instance:identifier:patch:module", + "2015-11-21", "name").intern(); protected final MediaType mediaType; protected final DatabindProvider databindProvider;