Implement subtree filtering using fields
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / NetconfUtil.java
index 5184d675e24db6c1415ddfee93d303b0502a5e7b..eb7fb247e3151fd3c372133f45a879c6c9f84059 100644 (file)
@@ -13,6 +13,8 @@ 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.stream.Collectors;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
@@ -186,6 +188,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()) {
@@ -207,6 +219,69 @@ public final class NetconfUtil {
         }
     }
 
+    /**
+     * 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 writer = XMLStreamNormalizedNodeStreamWriter.create(
+                    xmlWriter, context, schemaPath)) {
+                final PathArgument first = rootNode.element();
+                StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
+                        .streamToWriter(writer, first, rootNode);
+            }
+        } finally {
+            xmlWriter.close();
+        }
+    }
+
+    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());
+    }
+
+    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 mountContext,
             final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();