Revert "Convert anyxml nodes lazily"
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / NetconfUtil.java
index 9b47c58e571e0b10d3df3c6482edd2f8ac4409e1..635c507ad40cdfcba6211a4b3aaef86580564d07 100644 (file)
@@ -8,11 +8,27 @@
 package org.opendaylight.netconf.util;
 
 import com.google.common.base.Preconditions;
-import org.opendaylight.controller.config.util.xml.DocumentedException;
-import org.opendaylight.controller.config.util.xml.XmlElement;
-import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
+import java.io.IOException;
+import java.util.Iterator;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.dom.DOMResult;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.netconf.api.DocumentedException;
+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.util.NormalizedMetadataWriter;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+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.api.schema.stream.NormalizedNodeWriter;
+import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -20,12 +36,18 @@ import org.w3c.dom.Document;
 public final class NetconfUtil {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
+    public static final XMLOutputFactory XML_FACTORY;
+
+    static {
+        XML_FACTORY = XMLOutputFactory.newFactory();
+        XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
+    }
 
     private NetconfUtil() {}
 
-    public static Document checkIsMessageOk(Document response) throws DocumentedException {
+    public static Document checkIsMessageOk(final Document response) throws DocumentedException {
         XmlElement element = XmlElement.fromDomDocument(response);
-        Preconditions.checkState(element.getName().equals(XmlMappingConstants.RPC_REPLY_KEY));
+        Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
         element = element.getOnlyChildElement();
         if (element.getName().equals(XmlNetconfConstants.OK)) {
             return response;
@@ -34,4 +56,79 @@ public final class NetconfUtil {
         throw new IllegalStateException("Can not load last configuration. Operation failed: "
                 + XmlUtil.toString(response));
     }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
+                                           final SchemaPath schemaPath, final SchemaContext 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();
+        } finally {
+            try {
+                if (writer != null) {
+                    writer.close();
+                }
+            } catch (final Exception e) {
+                LOG.warn("Unable to close resource properly", e);
+            }
+        }
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
+                                           final @Nullable NormalizedMetadata metadata,
+                                           final DOMResult result, final SchemaPath schemaPath,
+                                           final SchemaContext context) throws IOException, XMLStreamException {
+        if (metadata == null) {
+            writeNormalizedNode(normalized, result, schemaPath, context);
+            return;
+        }
+
+        final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
+        try (
+             NormalizedNodeStreamWriter normalizedNodeStreamWriter =
+                     XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
+                NormalizedMetadataWriter normalizedNodeWriter =
+                     NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
+        ) {
+            normalizedNodeWriter.write(normalized, metadata);
+            normalizedNodeWriter.flush();
+        } finally {
+            try {
+                if (writer != null) {
+                    writer.close();
+                }
+            } catch (final Exception e) {
+                LOG.warn("Unable to close resource properly", e);
+            }
+        }
+    }
+
+    public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
+            final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
+        if (query.isEmpty()) {
+            // No query at all
+            return;
+        }
+
+        final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
+        try {
+            try (NormalizedNodeStreamWriter writer =
+                    XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
+                final Iterator<PathArgument> it = query.getPathArguments().iterator();
+                final PathArgument first = it.next();
+                StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
+                    it);
+            }
+        } finally {
+            xmlWriter.close();
+        }
+    }
 }