Fix delete operation for leaf nodes
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / NetconfUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.util;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.stream.Collectors;
19 import javax.xml.stream.XMLOutputFactory;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.stream.XMLStreamWriter;
22 import javax.xml.transform.dom.DOMResult;
23 import javax.xml.transform.dom.DOMSource;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.netconf.api.DocumentedException;
26 import org.opendaylight.netconf.api.xml.XmlElement;
27 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
28 import org.opendaylight.netconf.api.xml.XmlUtil;
29 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
30 import org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension;
31 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
32 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
33 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.QNameModule;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
43 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
44 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
46 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
47 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.w3c.dom.Document;
53 import org.w3c.dom.Element;
54 import org.xml.sax.SAXException;
55
56 public final class NetconfUtil {
57     /**
58      * Shim interface to handle differences around namespace handling between various XMLStreamWriter implementations.
59      * Specifically:
60      * <ul>
61      *   <li>OpenJDK DOM writer (com.sun.xml.internal.stream.writers.XMLDOMWriterImpl) throws
62      *       UnsupportedOperationException from its setNamespaceContext() method</li>
63      *   <li>Woodstox DOM writer (com.ctc.wstx.dom.WstxDOMWrappingWriter) works with namespace context, but treats
64      *       setPrefix() calls as hints -- which are not discoverable.</li>
65      * </ul>
66      *
67      * <p>
68      * Due to this we perform a quick test for behavior and decide the appropriate strategy.
69      */
70     @FunctionalInterface
71     private interface NamespaceSetter {
72         void initializeNamespace(XMLStreamWriter writer) throws XMLStreamException;
73
74         static NamespaceSetter forFactory(final XMLOutputFactory xmlFactory) {
75             final String netconfNamespace = NETCONF_QNAME.getNamespace().toString();
76             final AnyXmlNamespaceContext namespaceContext = new AnyXmlNamespaceContext(ImmutableMap.of(
77                 "op", netconfNamespace));
78
79             try {
80                 final XMLStreamWriter testWriter = xmlFactory.createXMLStreamWriter(new DOMResult(
81                     XmlUtil.newDocument()));
82                 testWriter.setNamespaceContext(namespaceContext);
83             } catch (final UnsupportedOperationException e) {
84                 // This happens with JDK's DOM writer, which we may be using
85                 LOG.warn("Unable to set namespace context, falling back to setPrefix()", e);
86                 return writer -> writer.setPrefix("op", netconfNamespace);
87             } catch (XMLStreamException e) {
88                 throw new ExceptionInInitializerError(e);
89             }
90
91             // Success, we can use setNamespaceContext()
92             return writer -> writer.setNamespaceContext(namespaceContext);
93         }
94     }
95
96     private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
97
98     // FIXME: document what exactly this QName means, as it is not referring to a tangible node nor the ietf-module.
99     // FIXME: what is this contract saying?
100     //        - is it saying all data is going to be interpreted with this root?
101     //        - is this saying we are following a specific interface contract (i.e. do we have schema mounts?)
102     //        - is it also inferring some abilities w.r.t. RFC8342?
103     public static final QName NETCONF_QNAME = QName.create(QNameModule.create(SchemaContext.NAME.getNamespace(),
104         Revision.of("2011-06-01")), "netconf").intern();
105     // FIXME: is this the device-bound revision?
106     public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data").intern();
107
108     public static final XMLOutputFactory XML_FACTORY;
109
110     static {
111         final XMLOutputFactory f = XMLOutputFactory.newFactory();
112         // FIXME: not repairing namespaces is probably common, this should be availabe as common XML constant.
113         f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
114         XML_FACTORY = f;
115     }
116
117     private static final NamespaceSetter XML_NAMESPACE_SETTER = NamespaceSetter.forFactory(XML_FACTORY);
118
119     private NetconfUtil() {
120         // No-op
121     }
122
123     public static Document checkIsMessageOk(final Document response) throws DocumentedException {
124         final XmlElement docElement = XmlElement.fromDomDocument(response);
125         // FIXME: we should throw DocumentedException here
126         checkState(XmlNetconfConstants.RPC_REPLY_KEY.equals(docElement.getName()));
127         final XmlElement element = docElement.getOnlyChildElement();
128         if (XmlNetconfConstants.OK.equals(element.getName())) {
129             return response;
130         }
131
132         LOG.warn("Can not load last configuration. Operation failed.");
133         // FIXME: we should be throwing a DocumentedException here
134         throw new IllegalStateException("Can not load last configuration. Operation failed: "
135                 + XmlUtil.toString(response));
136     }
137
138     /**
139      * Write {@code normalized} data into {@link DOMResult}.
140      *
141      * @param normalized data to be written
142      * @param result     DOM result holder
143      * @param schemaPath schema path of the parent node
144      * @param context    mountpoint schema context
145      * @throws IOException        when failed to write data into {@link NormalizedNodeStreamWriter}
146      * @throws XMLStreamException when failed to serialize data into XML document
147      */
148     @SuppressWarnings("checkstyle:IllegalCatch")
149     public static void writeNormalizedNode(final NormalizedNode normalized, final DOMResult result,
150                                            final SchemaPath schemaPath, final EffectiveModelContext context)
151             throws IOException, XMLStreamException {
152         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
153         try (
154              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
155                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
156              NormalizedNodeWriter normalizedNodeWriter =
157                      NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
158         ) {
159             normalizedNodeWriter.write(normalized);
160             normalizedNodeWriter.flush();
161         } finally {
162             try {
163                 if (writer != null) {
164                     writer.close();
165                 }
166             } catch (final Exception e) {
167                 LOG.warn("Unable to close resource properly", e);
168             }
169         }
170     }
171
172     /**
173      * Write {@code normalized} data along with corresponding {@code metadata} into {@link DOMResult}.
174      *
175      * @param normalized data to be written
176      * @param metadata   metadata to be written
177      * @param result     DOM result holder
178      * @param schemaPath schema path of the parent node
179      * @param context    mountpoint schema context
180      * @throws IOException        when failed to write data into {@link NormalizedNodeStreamWriter}
181      * @throws XMLStreamException when failed to serialize data into XML document
182      */
183     @SuppressWarnings("checkstyle:IllegalCatch")
184     public static void writeNormalizedNode(final NormalizedNode normalized,
185                                            final @Nullable NormalizedMetadata metadata,
186                                            final DOMResult result, final SchemaPath schemaPath,
187                                            final EffectiveModelContext context) throws IOException, XMLStreamException {
188         if (metadata == null) {
189             writeNormalizedNode(normalized, result, schemaPath, context);
190             return;
191         }
192
193         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
194         XML_NAMESPACE_SETTER.initializeNamespace(writer);
195         try (
196              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
197                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
198                 NormalizedMetadataWriter normalizedNodeWriter =
199                      NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
200         ) {
201             normalizedNodeWriter.write(normalized, metadata);
202             normalizedNodeWriter.flush();
203         } finally {
204             try {
205                 if (writer != null) {
206                     writer.close();
207                 }
208             } catch (final Exception e) {
209                 LOG.warn("Unable to close resource properly", e);
210             }
211         }
212     }
213
214     /**
215      * Write data specified by {@link YangInstanceIdentifier} into {@link DOMResult}.
216      *
217      * @param query      path to the root node
218      * @param result     DOM result holder
219      * @param schemaPath schema path of the parent node
220      * @param context    mountpoint schema context
221      * @throws IOException        when failed to write data into {@link NormalizedNodeStreamWriter}
222      * @throws XMLStreamException when failed to serialize data into XML document
223      */
224     @SuppressWarnings("checkstyle:IllegalCatch")
225     public static void writeNormalizedNode(final YangInstanceIdentifier query, final DOMResult result,
226             final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
227         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
228         XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter);
229         try (NormalizedNodeStreamWriter streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
230                 context, schemaPath);
231              EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
232             final Iterator<PathArgument> it = query.getPathArguments().iterator();
233             final PathArgument first = it.next();
234             StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
235         } finally {
236             try {
237                 if (xmlWriter != null) {
238                     xmlWriter.close();
239                 }
240             } catch (final Exception e) {
241                 LOG.warn("Unable to close resource properly", e);
242             }
243         }
244     }
245
246     /**
247      * Write data specified by {@link YangInstanceIdentifier} along with corresponding {@code metadata}
248      * into {@link DOMResult}.
249      *
250      * @param query      path to the root node
251      * @param metadata   metadata to be written
252      * @param result     DOM result holder
253      * @param schemaPath schema path of the parent node
254      * @param context    mountpoint schema context
255      * @throws IOException        when failed to write data into {@link NormalizedNodeStreamWriter}
256      * @throws XMLStreamException when failed to serialize data into XML document
257      */
258     @SuppressWarnings("checkstyle:IllegalCatch")
259     public static void writeNormalizedNode(final YangInstanceIdentifier query,
260             final @Nullable NormalizedMetadata metadata, final DOMResult result, final SchemaPath schemaPath,
261             final EffectiveModelContext context) throws IOException, XMLStreamException {
262         if (metadata == null) {
263             writeNormalizedNode(query, result, schemaPath, context);
264             return;
265         }
266
267         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
268         XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter);
269         try (NormalizedNodeStreamWriter streamWriter = XMLStreamNormalizedNodeStreamWriter
270                 .create(xmlWriter, context, schemaPath);
271              EmptyListXmlMetadataWriter writer = new EmptyListXmlMetadataWriter(streamWriter, xmlWriter, streamWriter
272                      .getExtensions().getInstance(StreamWriterMetadataExtension.class), metadata)) {
273             final Iterator<PathArgument> it = query.getPathArguments().iterator();
274             final PathArgument first = it.next();
275             StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
276         } finally {
277             try {
278                 if (xmlWriter != null) {
279                     xmlWriter.close();
280                 }
281             } catch (final Exception e) {
282                 LOG.warn("Unable to close resource properly", e);
283             }
284         }
285     }
286
287     /**
288      * Writing subtree filter specified by {@link YangInstanceIdentifier} into {@link DOMResult}.
289      *
290      * @param query      path to the root node
291      * @param result     DOM result holder
292      * @param schemaPath schema path of the parent node
293      * @param context    mountpoint schema context
294      * @throws IOException        failed to write filter into {@link NormalizedNodeStreamWriter}
295      * @throws XMLStreamException failed to serialize filter into XML document
296      */
297     public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
298             final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
299         if (query.isEmpty()) {
300             // No query at all
301             return;
302         }
303
304         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
305         try {
306             try (NormalizedNodeStreamWriter streamWriter =
307                     XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
308                  EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
309                 final Iterator<PathArgument> it = query.getPathArguments().iterator();
310                 final PathArgument first = it.next();
311                 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
312                     it);
313             }
314         } finally {
315             xmlWriter.close();
316         }
317     }
318
319     /**
320      * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
321      * into {@link DOMResult}. Field paths are relative to parent query path.
322      *
323      * @param query      path to the root node
324      * @param result     DOM result holder
325      * @param schemaPath schema path of the parent node
326      * @param context    mountpoint schema context
327      * @param fields     list of specific fields for which the filter should be created
328      * @throws IOException        failed to write filter into {@link NormalizedNodeStreamWriter}
329      * @throws XMLStreamException failed to serialize filter into XML document
330      * @throws NullPointerException if any argument is null
331      */
332     public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
333                                    final SchemaPath schemaPath, final EffectiveModelContext context,
334                                    final List<YangInstanceIdentifier> fields) throws IOException, XMLStreamException {
335         if (query.isEmpty() || fields.isEmpty()) {
336             // No query at all
337             return;
338         }
339         final List<YangInstanceIdentifier> aggregatedFields = aggregateFields(fields);
340         final PathNode rootNode = constructPathArgumentTree(query, aggregatedFields);
341
342         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
343         try {
344             try (NormalizedNodeStreamWriter streamWriter =
345                     XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
346                  EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
347                 final PathArgument first = rootNode.element();
348                 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
349                         .streamToWriter(writer, first, rootNode);
350             }
351         } finally {
352             xmlWriter.close();
353         }
354     }
355
356     /**
357      * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
358      * into {@link Element}. Field paths are relative to parent query path. Filter is created without following
359      * {@link EffectiveModelContext}.
360      *
361      * @param query         path to the root node
362      * @param fields        list of specific fields for which the filter should be created
363      * @param filterElement XML filter element to which the created filter will be written
364      */
365     public static void writeSchemalessFilter(final YangInstanceIdentifier query,
366                                              final List<YangInstanceIdentifier> fields, final Element filterElement) {
367         pathArgumentTreeToXmlStructure(constructPathArgumentTree(query, aggregateFields(fields)), filterElement);
368     }
369
370     private static void pathArgumentTreeToXmlStructure(final PathNode pathArgumentTree, final Element data) {
371         final PathArgument pathArg = pathArgumentTree.element();
372
373         final QName nodeType = pathArg.getNodeType();
374         final String elementNamespace = nodeType.getNamespace().toString();
375
376         if (data.getElementsByTagNameNS(elementNamespace, nodeType.getLocalName()).getLength() != 0) {
377             // element has already been written as list key
378             return;
379         }
380
381         final Element childElement = data.getOwnerDocument().createElementNS(elementNamespace, nodeType.getLocalName());
382         data.appendChild(childElement);
383         if (pathArg instanceof NodeIdentifierWithPredicates) {
384             appendListKeyNodes(childElement, (NodeIdentifierWithPredicates) pathArg);
385         }
386         for (final PathNode childrenNode : pathArgumentTree.children()) {
387             pathArgumentTreeToXmlStructure(childrenNode, childElement);
388         }
389     }
390
391     /**
392      * Appending list key elements to parent element.
393      *
394      * @param parentElement parent XML element to which children elements are appended
395      * @param listEntryId   list entry identifier
396      */
397     public static void appendListKeyNodes(final Element parentElement, final NodeIdentifierWithPredicates listEntryId) {
398         for (Entry<QName, Object> key : listEntryId.entrySet()) {
399             final Element keyElement = parentElement.getOwnerDocument().createElementNS(
400                     key.getKey().getNamespace().toString(), key.getKey().getLocalName());
401             keyElement.setTextContent(key.getValue().toString());
402             parentElement.appendChild(keyElement);
403         }
404     }
405
406     /**
407      * Aggregation of the fields paths based on parenthesis. Only parent/enclosing {@link YangInstanceIdentifier}
408      * are kept. For example, paths '/x/y/z', '/x/y', and '/x' are aggregated into single field path: '/x'
409      *
410      * @param fields paths of fields
411      * @return filtered {@link List} of paths
412      */
413     private static List<YangInstanceIdentifier> aggregateFields(final List<YangInstanceIdentifier> fields) {
414         return fields.stream()
415                 .filter(field -> fields.stream()
416                         .filter(fieldYiid -> !field.equals(fieldYiid))
417                         .noneMatch(fieldYiid -> fieldYiid.contains(field)))
418                 .collect(Collectors.toList());
419     }
420
421     /**
422      * Construct a tree based on the parent {@link YangInstanceIdentifier} and provided list of fields. The goal of this
423      * procedure is the elimination of the redundancy that is introduced by potentially overlapping parts of the fields
424      * paths.
425      *
426      * @param query  path to parent element
427      * @param fields subpaths relative to parent path that identify specific fields
428      * @return created {@link PathNode} structure
429      */
430     private static PathNode constructPathArgumentTree(final YangInstanceIdentifier query,
431             final List<YangInstanceIdentifier> fields) {
432         final Iterator<PathArgument> queryIterator = query.getPathArguments().iterator();
433         final PathNode rootTreeNode = new PathNode(queryIterator.next());
434
435         PathNode queryTreeNode = rootTreeNode;
436         while (queryIterator.hasNext()) {
437             queryTreeNode = queryTreeNode.ensureChild(queryIterator.next());
438         }
439
440         for (final YangInstanceIdentifier field : fields) {
441             PathNode actualFieldTreeNode = queryTreeNode;
442             for (final PathArgument fieldPathArg : field.getPathArguments()) {
443                 actualFieldTreeNode = actualFieldTreeNode.ensureChild(fieldPathArg);
444             }
445         }
446         return rootTreeNode;
447     }
448
449     public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mount,
450             final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
451         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
452         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
453         try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, new ProxyMountPointContext(mount))) {
454             xmlParserStream.traverse(value);
455         }
456         return resultHolder;
457     }
458
459     // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
460     public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final EffectiveModelContext schemaContext,
461             final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
462         return transformDOMSourceToNormalizedNode(new EmptyMountPointContext(schemaContext), value);
463     }
464 }