X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Futil%2FNetconfUtil.java;h=d2ae6c73fdc25bd2973b370b832121815c4ebbe3;hb=14323f32667c16d5206e73bfe34eea48d4e2272f;hp=d50d0d71502c4a8f672ef5bb5de99be552667507;hpb=91ef75da940c025390675ed35e239a5914fdf31f;p=netconf.git diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java index d50d0d7150..d2ae6c73fd 100644 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java @@ -27,6 +27,7 @@ import org.opendaylight.netconf.api.xml.XmlElement; import org.opendaylight.netconf.api.xml.XmlNetconfConstants; import org.opendaylight.netconf.api.xml.XmlUtil; import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata; +import org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension; import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter; import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext; import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext; @@ -210,6 +211,79 @@ public final class NetconfUtil { } } + /** + * Write data specified by {@link YangInstanceIdentifier} into {@link DOMResult}. + * + * @param query path to the root node + * @param result DOM result holder + * @param schemaPath schema path of the parent node + * @param context mountpoint schema context + * @throws IOException when failed to write data into {@link NormalizedNodeStreamWriter} + * @throws XMLStreamException when failed to serialize data into XML document + */ + @SuppressWarnings("checkstyle:IllegalCatch") + public static void writeNormalizedNode(final YangInstanceIdentifier query, final DOMResult result, + final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException { + final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result); + XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter); + try (NormalizedNodeStreamWriter streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, + context, schemaPath); + EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) { + final Iterator it = query.getPathArguments().iterator(); + final PathArgument first = it.next(); + StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it); + } finally { + try { + if (xmlWriter != null) { + xmlWriter.close(); + } + } catch (final Exception e) { + LOG.warn("Unable to close resource properly", e); + } + } + } + + /** + * Write data specified by {@link YangInstanceIdentifier} along with corresponding {@code metadata} + * into {@link DOMResult}. + * + * @param query path to the root node + * @param metadata metadata to be written + * @param result DOM result holder + * @param schemaPath schema path of the parent node + * @param context mountpoint schema context + * @throws IOException when failed to write data into {@link NormalizedNodeStreamWriter} + * @throws XMLStreamException when failed to serialize data into XML document + */ + @SuppressWarnings("checkstyle:IllegalCatch") + public static void writeNormalizedNode(final YangInstanceIdentifier query, + final @Nullable NormalizedMetadata metadata, final DOMResult result, final SchemaPath schemaPath, + final EffectiveModelContext context) throws IOException, XMLStreamException { + if (metadata == null) { + writeNormalizedNode(query, result, schemaPath, context); + return; + } + + final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result); + XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter); + try (NormalizedNodeStreamWriter streamWriter = XMLStreamNormalizedNodeStreamWriter + .create(xmlWriter, context, schemaPath); + EmptyListXmlMetadataWriter writer = new EmptyListXmlMetadataWriter(streamWriter, xmlWriter, streamWriter + .getExtensions().getInstance(StreamWriterMetadataExtension.class), metadata)) { + final Iterator it = query.getPathArguments().iterator(); + final PathArgument first = it.next(); + StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it); + } finally { + try { + if (xmlWriter != null) { + xmlWriter.close(); + } + } catch (final Exception e) { + LOG.warn("Unable to close resource properly", e); + } + } + } + /** * Writing subtree filter specified by {@link YangInstanceIdentifier} into {@link DOMResult}. *