Unify variable naming
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / NetconfUtil.java
index 3ad497cc98712aff24807616ce3e5a70aa2e19f3..59ad61c8fe11c5bf29e944d0d130790c25c7b63a 100644 (file)
@@ -9,11 +9,11 @@ package org.opendaylight.netconf.util;
 
 import static com.google.common.base.Preconditions.checkState;
 
-import com.google.common.collect.ImmutableMap;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.stream.Collectors;
 import javax.xml.stream.XMLOutputFactory;
@@ -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;
@@ -72,8 +73,7 @@ public final class NetconfUtil {
 
         static NamespaceSetter forFactory(final XMLOutputFactory xmlFactory) {
             final String netconfNamespace = NETCONF_QNAME.getNamespace().toString();
-            final AnyXmlNamespaceContext namespaceContext = new AnyXmlNamespaceContext(ImmutableMap.of(
-                "op", netconfNamespace));
+            final AnyXmlNamespaceContext namespaceContext = new AnyXmlNamespaceContext(Map.of("op", netconfNamespace));
 
             try {
                 final XMLStreamWriter testWriter = xmlFactory.createXMLStreamWriter(new DOMResult(
@@ -81,7 +81,7 @@ public final class NetconfUtil {
                 testWriter.setNamespaceContext(namespaceContext);
             } catch (final UnsupportedOperationException e) {
                 // This happens with JDK's DOM writer, which we may be using
-                LOG.warn("Unable to set namespace context, falling back to setPrefix()", e);
+                LOG.debug("Unable to set namespace context, falling back to setPrefix()", e);
                 return writer -> writer.setPrefix("op", netconfNamespace);
             } catch (XMLStreamException e) {
                 throw new ExceptionInInitializerError(e);
@@ -134,58 +134,112 @@ public final class NetconfUtil {
                 + XmlUtil.toString(response));
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
+    /**
+     * Write {@code normalized} data into {@link DOMResult}.
+     *
+     * @param normalized data 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
+     */
     public static void writeNormalizedNode(final NormalizedNode normalized, final DOMResult result,
-                                           final SchemaPath schemaPath, final EffectiveModelContext context)
-            throws IOException, XMLStreamException {
-        final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
-        try (
-             NormalizedNodeStreamWriter normalizedNodeStreamWriter =
-                     XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
-             NormalizedNodeWriter normalizedNodeWriter =
-                     NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
-        ) {
-            normalizedNodeWriter.write(normalized);
-            normalizedNodeWriter.flush();
+            final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
+        final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+        try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+             var writer = NormalizedNodeWriter.forStreamWriter(streamWriter)) {
+            writer.write(normalized);
+            writer.flush();
         } finally {
-            try {
-                if (writer != null) {
-                    writer.close();
-                }
-            } catch (final Exception e) {
-                LOG.warn("Unable to close resource properly", e);
-            }
+            xmlWriter.close();
         }
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
-    public static void writeNormalizedNode(final NormalizedNode normalized,
-                                           final @Nullable NormalizedMetadata metadata,
-                                           final DOMResult result, final SchemaPath schemaPath,
-                                           final EffectiveModelContext context) throws IOException, XMLStreamException {
+    /**
+     * Write {@code normalized} data along with corresponding {@code metadata} into {@link DOMResult}.
+     *
+     * @param normalized data to be written
+     * @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
+     */
+    public static void writeNormalizedNode(final NormalizedNode normalized, final @Nullable NormalizedMetadata metadata,
+            final DOMResult result, final SchemaPath schemaPath, final EffectiveModelContext context)
+                throws IOException, XMLStreamException {
         if (metadata == null) {
             writeNormalizedNode(normalized, result, schemaPath, context);
             return;
         }
 
-        final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
-        XML_NAMESPACE_SETTER.initializeNamespace(writer);
-        try (
-             NormalizedNodeStreamWriter normalizedNodeStreamWriter =
-                     XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
-                NormalizedMetadataWriter normalizedNodeWriter =
-                     NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
-        ) {
-            normalizedNodeWriter.write(normalized, metadata);
-            normalizedNodeWriter.flush();
+        final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+        XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter);
+        try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+             var writer = NormalizedMetadataWriter.forStreamWriter(streamWriter)) {
+            writer.write(normalized, metadata);
+            writer.flush();
         } finally {
-            try {
-                if (writer != null) {
-                    writer.close();
-                }
-            } catch (final Exception e) {
-                LOG.warn("Unable to close resource properly", e);
-            }
+            xmlWriter.close();
+        }
+    }
+
+    /**
+     * 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
+     */
+    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 (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+             var writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
+            final Iterator<PathArgument> it = query.getPathArguments().iterator();
+            final PathArgument first = it.next();
+            StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
+        } finally {
+            xmlWriter.close();
+        }
+    }
+
+    /**
+     * 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
+     */
+    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 (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+             var writer = new EmptyListXmlMetadataWriter(streamWriter, xmlWriter,
+                 streamWriter.getExtensions().getInstance(StreamWriterMetadataExtension.class), metadata)) {
+            final Iterator<PathArgument> it = query.getPathArguments().iterator();
+            final PathArgument first = it.next();
+            StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
+        } finally {
+            xmlWriter.close();
         }
     }
 
@@ -207,15 +261,11 @@ public final class NetconfUtil {
         }
 
         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
-        try {
-            try (NormalizedNodeStreamWriter streamWriter =
-                    XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
-                 EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
-                final Iterator<PathArgument> it = query.getPathArguments().iterator();
-                final PathArgument first = it.next();
-                StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
-                    it);
-            }
+        try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+             var writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
+            final Iterator<PathArgument> it = query.getPathArguments().iterator();
+            final PathArgument first = it.next();
+            StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
         } finally {
             xmlWriter.close();
         }
@@ -241,14 +291,14 @@ public final class NetconfUtil {
             // No query at all
             return;
         }
+
         final List<YangInstanceIdentifier> aggregatedFields = aggregateFields(fields);
         final PathNode rootNode = constructPathArgumentTree(query, aggregatedFields);
 
         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
         try {
-            try (NormalizedNodeStreamWriter streamWriter =
-                    XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
-                 EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
+            try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
+                 var writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
                 final PathArgument first = rootNode.element();
                 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
                         .streamToWriter(writer, first, rootNode);
@@ -330,7 +380,7 @@ public final class NetconfUtil {
      *
      * @param query  path to parent element
      * @param fields subpaths relative to parent path that identify specific fields
-     * @return created {@link TreeNode} structure
+     * @return created {@link PathNode} structure
      */
     private static PathNode constructPathArgumentTree(final YangInstanceIdentifier query,
             final List<YangInstanceIdentifier> fields) {