Bump MRI upstreams
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / NetconfUtil.java
index 5184d675e24db6c1415ddfee93d303b0502a5e7b..3ad497cc98712aff24807616ce3e5a70aa2e19f3 100644 (file)
@@ -13,6 +13,9 @@ 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.Entry;
+import java.util.stream.Collectors;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
@@ -23,30 +26,30 @@ 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.rcf8528.data.util.EmptyMountPointContext;
 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
+import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 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.data.codec.xml.XmlCodecFactory;
 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.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 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;
+import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
 public final class NetconfUtil {
@@ -132,7 +135,7 @@ public final class NetconfUtil {
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
+    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);
@@ -156,7 +159,7 @@ public final class NetconfUtil {
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
+    public static void writeNormalizedNode(final NormalizedNode normalized,
                                            final @Nullable NormalizedMetadata metadata,
                                            final DOMResult result, final SchemaPath schemaPath,
                                            final EffectiveModelContext context) throws IOException, XMLStreamException {
@@ -186,6 +189,16 @@ public final class NetconfUtil {
         }
     }
 
+    /**
+     * Writing subtree filter 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        failed to write filter into {@link NormalizedNodeStreamWriter}
+     * @throws XMLStreamException failed to serialize filter into XML document
+     */
     public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
             final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
         if (query.isEmpty()) {
@@ -195,8 +208,9 @@ public final class NetconfUtil {
 
         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
         try {
-            try (NormalizedNodeStreamWriter writer =
-                    XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
+            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,
@@ -207,22 +221,146 @@ public final class NetconfUtil {
         }
     }
 
-    public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mountContext,
+    /**
+     * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
+     * into {@link DOMResult}. Field paths are relative to parent query path.
+     *
+     * @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
+     * @param fields     list of specific fields for which the filter should be created
+     * @throws IOException        failed to write filter into {@link NormalizedNodeStreamWriter}
+     * @throws XMLStreamException failed to serialize filter into XML document
+     * @throws NullPointerException if any argument is null
+     */
+    public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
+                                   final SchemaPath schemaPath, final EffectiveModelContext context,
+                                   final List<YangInstanceIdentifier> fields) throws IOException, XMLStreamException {
+        if (query.isEmpty() || fields.isEmpty()) {
+            // 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)) {
+                final PathArgument first = rootNode.element();
+                StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
+                        .streamToWriter(writer, first, rootNode);
+            }
+        } finally {
+            xmlWriter.close();
+        }
+    }
+
+    /**
+     * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
+     * into {@link Element}. Field paths are relative to parent query path. Filter is created without following
+     * {@link EffectiveModelContext}.
+     *
+     * @param query         path to the root node
+     * @param fields        list of specific fields for which the filter should be created
+     * @param filterElement XML filter element to which the created filter will be written
+     */
+    public static void writeSchemalessFilter(final YangInstanceIdentifier query,
+                                             final List<YangInstanceIdentifier> fields, final Element filterElement) {
+        pathArgumentTreeToXmlStructure(constructPathArgumentTree(query, aggregateFields(fields)), filterElement);
+    }
+
+    private static void pathArgumentTreeToXmlStructure(final PathNode pathArgumentTree, final Element data) {
+        final PathArgument pathArg = pathArgumentTree.element();
+
+        final QName nodeType = pathArg.getNodeType();
+        final String elementNamespace = nodeType.getNamespace().toString();
+
+        if (data.getElementsByTagNameNS(elementNamespace, nodeType.getLocalName()).getLength() != 0) {
+            // element has already been written as list key
+            return;
+        }
+
+        final Element childElement = data.getOwnerDocument().createElementNS(elementNamespace, nodeType.getLocalName());
+        data.appendChild(childElement);
+        if (pathArg instanceof NodeIdentifierWithPredicates) {
+            appendListKeyNodes(childElement, (NodeIdentifierWithPredicates) pathArg);
+        }
+        for (final PathNode childrenNode : pathArgumentTree.children()) {
+            pathArgumentTreeToXmlStructure(childrenNode, childElement);
+        }
+    }
+
+    /**
+     * Appending list key elements to parent element.
+     *
+     * @param parentElement parent XML element to which children elements are appended
+     * @param listEntryId   list entry identifier
+     */
+    public static void appendListKeyNodes(final Element parentElement, final NodeIdentifierWithPredicates listEntryId) {
+        for (Entry<QName, Object> key : listEntryId.entrySet()) {
+            final Element keyElement = parentElement.getOwnerDocument().createElementNS(
+                    key.getKey().getNamespace().toString(), key.getKey().getLocalName());
+            keyElement.setTextContent(key.getValue().toString());
+            parentElement.appendChild(keyElement);
+        }
+    }
+
+    /**
+     * Aggregation of the fields paths based on parenthesis. Only parent/enclosing {@link YangInstanceIdentifier}
+     * are kept. For example, paths '/x/y/z', '/x/y', and '/x' are aggregated into single field path: '/x'
+     *
+     * @param fields paths of fields
+     * @return filtered {@link List} of paths
+     */
+    private static List<YangInstanceIdentifier> aggregateFields(final List<YangInstanceIdentifier> fields) {
+        return fields.stream()
+                .filter(field -> fields.stream()
+                        .filter(fieldYiid -> !field.equals(fieldYiid))
+                        .noneMatch(fieldYiid -> fieldYiid.contains(field)))
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Construct a tree based on the parent {@link YangInstanceIdentifier} and provided list of fields. The goal of this
+     * procedure is the elimination of the redundancy that is introduced by potentially overlapping parts of the fields
+     * paths.
+     *
+     * @param query  path to parent element
+     * @param fields subpaths relative to parent path that identify specific fields
+     * @return created {@link TreeNode} structure
+     */
+    private static PathNode constructPathArgumentTree(final YangInstanceIdentifier query,
+            final List<YangInstanceIdentifier> fields) {
+        final Iterator<PathArgument> queryIterator = query.getPathArguments().iterator();
+        final PathNode rootTreeNode = new PathNode(queryIterator.next());
+
+        PathNode queryTreeNode = rootTreeNode;
+        while (queryIterator.hasNext()) {
+            queryTreeNode = queryTreeNode.ensureChild(queryIterator.next());
+        }
+
+        for (final YangInstanceIdentifier field : fields) {
+            PathNode actualFieldTreeNode = queryTreeNode;
+            for (final PathArgument fieldPathArg : field.getPathArguments()) {
+                actualFieldTreeNode = actualFieldTreeNode.ensureChild(fieldPathArg);
+            }
+        }
+        return rootTreeNode;
+    }
+
+    public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mount,
             final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
-        final XmlCodecFactory codecs = XmlCodecFactory.create(mountContext);
-
-        // FIXME: we probably need to propagate MountPointContext here and not just the child nodes
-        final ContainerSchemaNode dataRead = new NodeContainerProxy(NETCONF_DATA_QNAME,
-            mountContext.getEffectiveModelContext().getChildNodes());
-        try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, codecs, dataRead)) {
+        try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, new ProxyMountPointContext(mount))) {
             xmlParserStream.traverse(value);
         }
         return resultHolder;
     }
 
-
     // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
     public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final EffectiveModelContext schemaContext,
             final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {