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;
}
}
+ /**
+ * 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()) {
}
}
+ /**
+ * 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();