Fixed reading whole list/leaf-list using GET/GET-CONFIG RPC
[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.rcf8528.data.util.EmptyMountPointContext;
30 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
31 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
32 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
42 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
43 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
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.ContainerSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.w3c.dom.Document;
54 import org.w3c.dom.Element;
55 import org.xml.sax.SAXException;
56
57 public final class NetconfUtil {
58     /**
59      * Shim interface to handle differences around namespace handling between various XMLStreamWriter implementations.
60      * Specifically:
61      * <ul>
62      *   <li>OpenJDK DOM writer (com.sun.xml.internal.stream.writers.XMLDOMWriterImpl) throws
63      *       UnsupportedOperationException from its setNamespaceContext() method</li>
64      *   <li>Woodstox DOM writer (com.ctc.wstx.dom.WstxDOMWrappingWriter) works with namespace context, but treats
65      *       setPrefix() calls as hints -- which are not discoverable.</li>
66      * </ul>
67      *
68      * <p>
69      * Due to this we perform a quick test for behavior and decide the appropriate strategy.
70      */
71     @FunctionalInterface
72     private interface NamespaceSetter {
73         void initializeNamespace(XMLStreamWriter writer) throws XMLStreamException;
74
75         static NamespaceSetter forFactory(final XMLOutputFactory xmlFactory) {
76             final String netconfNamespace = NETCONF_QNAME.getNamespace().toString();
77             final AnyXmlNamespaceContext namespaceContext = new AnyXmlNamespaceContext(ImmutableMap.of(
78                 "op", netconfNamespace));
79
80             try {
81                 final XMLStreamWriter testWriter = xmlFactory.createXMLStreamWriter(new DOMResult(
82                     XmlUtil.newDocument()));
83                 testWriter.setNamespaceContext(namespaceContext);
84             } catch (final UnsupportedOperationException e) {
85                 // This happens with JDK's DOM writer, which we may be using
86                 LOG.warn("Unable to set namespace context, falling back to setPrefix()", e);
87                 return writer -> writer.setPrefix("op", netconfNamespace);
88             } catch (XMLStreamException e) {
89                 throw new ExceptionInInitializerError(e);
90             }
91
92             // Success, we can use setNamespaceContext()
93             return writer -> writer.setNamespaceContext(namespaceContext);
94         }
95     }
96
97     private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
98
99     // FIXME: document what exactly this QName means, as it is not referring to a tangible node nor the ietf-module.
100     // FIXME: what is this contract saying?
101     //        - is it saying all data is going to be interpreted with this root?
102     //        - is this saying we are following a specific interface contract (i.e. do we have schema mounts?)
103     //        - is it also inferring some abilities w.r.t. RFC8342?
104     public static final QName NETCONF_QNAME = QName.create(QNameModule.create(SchemaContext.NAME.getNamespace(),
105         Revision.of("2011-06-01")), "netconf").intern();
106     // FIXME: is this the device-bound revision?
107     public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data").intern();
108
109     public static final XMLOutputFactory XML_FACTORY;
110
111     static {
112         final XMLOutputFactory f = XMLOutputFactory.newFactory();
113         // FIXME: not repairing namespaces is probably common, this should be availabe as common XML constant.
114         f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
115         XML_FACTORY = f;
116     }
117
118     private static final NamespaceSetter XML_NAMESPACE_SETTER = NamespaceSetter.forFactory(XML_FACTORY);
119
120     private NetconfUtil() {
121         // No-op
122     }
123
124     public static Document checkIsMessageOk(final Document response) throws DocumentedException {
125         final XmlElement docElement = XmlElement.fromDomDocument(response);
126         // FIXME: we should throw DocumentedException here
127         checkState(XmlNetconfConstants.RPC_REPLY_KEY.equals(docElement.getName()));
128         final XmlElement element = docElement.getOnlyChildElement();
129         if (XmlNetconfConstants.OK.equals(element.getName())) {
130             return response;
131         }
132
133         LOG.warn("Can not load last configuration. Operation failed.");
134         // FIXME: we should be throwing a DocumentedException here
135         throw new IllegalStateException("Can not load last configuration. Operation failed: "
136                 + XmlUtil.toString(response));
137     }
138
139     @SuppressWarnings("checkstyle:IllegalCatch")
140     public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
141                                            final SchemaPath schemaPath, final EffectiveModelContext context)
142             throws IOException, XMLStreamException {
143         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
144         try (
145              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
146                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
147              NormalizedNodeWriter normalizedNodeWriter =
148                      NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
149         ) {
150             normalizedNodeWriter.write(normalized);
151             normalizedNodeWriter.flush();
152         } finally {
153             try {
154                 if (writer != null) {
155                     writer.close();
156                 }
157             } catch (final Exception e) {
158                 LOG.warn("Unable to close resource properly", e);
159             }
160         }
161     }
162
163     @SuppressWarnings("checkstyle:IllegalCatch")
164     public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
165                                            final @Nullable NormalizedMetadata metadata,
166                                            final DOMResult result, final SchemaPath schemaPath,
167                                            final EffectiveModelContext context) throws IOException, XMLStreamException {
168         if (metadata == null) {
169             writeNormalizedNode(normalized, result, schemaPath, context);
170             return;
171         }
172
173         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
174         XML_NAMESPACE_SETTER.initializeNamespace(writer);
175         try (
176              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
177                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
178                 NormalizedMetadataWriter normalizedNodeWriter =
179                      NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
180         ) {
181             normalizedNodeWriter.write(normalized, metadata);
182             normalizedNodeWriter.flush();
183         } finally {
184             try {
185                 if (writer != null) {
186                     writer.close();
187                 }
188             } catch (final Exception e) {
189                 LOG.warn("Unable to close resource properly", e);
190             }
191         }
192     }
193
194     /**
195      * Writing subtree filter specified by {@link YangInstanceIdentifier} into {@link DOMResult}.
196      *
197      * @param query      path to the root node
198      * @param result     DOM result holder
199      * @param schemaPath schema path of the parent node
200      * @param context    mountpoint schema context
201      * @throws IOException        failed to write filter into {@link NormalizedNodeStreamWriter}
202      * @throws XMLStreamException failed to serialize filter into XML document
203      */
204     public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
205             final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
206         if (query.isEmpty()) {
207             // No query at all
208             return;
209         }
210
211         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
212         try {
213             try (NormalizedNodeStreamWriter streamWriter =
214                     XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
215                  EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
216                 final Iterator<PathArgument> it = query.getPathArguments().iterator();
217                 final PathArgument first = it.next();
218                 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
219                     it);
220             }
221         } finally {
222             xmlWriter.close();
223         }
224     }
225
226     /**
227      * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
228      * into {@link DOMResult}. Field paths are relative to parent query path.
229      *
230      * @param query      path to the root node
231      * @param result     DOM result holder
232      * @param schemaPath schema path of the parent node
233      * @param context    mountpoint schema context
234      * @param fields     list of specific fields for which the filter should be created
235      * @throws IOException        failed to write filter into {@link NormalizedNodeStreamWriter}
236      * @throws XMLStreamException failed to serialize filter into XML document
237      * @throws NullPointerException if any argument is null
238      */
239     public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
240                                    final SchemaPath schemaPath, final EffectiveModelContext context,
241                                    final List<YangInstanceIdentifier> fields) throws IOException, XMLStreamException {
242         if (query.isEmpty() || fields.isEmpty()) {
243             // No query at all
244             return;
245         }
246         final List<YangInstanceIdentifier> aggregatedFields = aggregateFields(fields);
247         final PathNode rootNode = constructPathArgumentTree(query, aggregatedFields);
248
249         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
250         try {
251             try (NormalizedNodeStreamWriter streamWriter =
252                     XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
253                  EmptyListXmlWriter writer = new EmptyListXmlWriter(streamWriter, xmlWriter)) {
254                 final PathArgument first = rootNode.element();
255                 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
256                         .streamToWriter(writer, first, rootNode);
257             }
258         } finally {
259             xmlWriter.close();
260         }
261     }
262
263     /**
264      * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
265      * into {@link Element}. Field paths are relative to parent query path. Filter is created without following
266      * {@link EffectiveModelContext}.
267      *
268      * @param query         path to the root node
269      * @param fields        list of specific fields for which the filter should be created
270      * @param filterElement XML filter element to which the created filter will be written
271      */
272     public static void writeSchemalessFilter(final YangInstanceIdentifier query,
273                                              final List<YangInstanceIdentifier> fields, final Element filterElement) {
274         pathArgumentTreeToXmlStructure(constructPathArgumentTree(query, aggregateFields(fields)), filterElement);
275     }
276
277     private static void pathArgumentTreeToXmlStructure(final PathNode pathArgumentTree, final Element data) {
278         final PathArgument pathArg = pathArgumentTree.element();
279
280         final QName nodeType = pathArg.getNodeType();
281         final String elementNamespace = nodeType.getNamespace().toString();
282
283         if (data.getElementsByTagNameNS(elementNamespace, nodeType.getLocalName()).getLength() != 0) {
284             // element has already been written as list key
285             return;
286         }
287
288         final Element childElement = data.getOwnerDocument().createElementNS(elementNamespace, nodeType.getLocalName());
289         data.appendChild(childElement);
290         if (pathArg instanceof NodeIdentifierWithPredicates) {
291             appendListKeyNodes(childElement, (NodeIdentifierWithPredicates) pathArg);
292         }
293         for (final PathNode childrenNode : pathArgumentTree.children()) {
294             pathArgumentTreeToXmlStructure(childrenNode, childElement);
295         }
296     }
297
298     /**
299      * Appending list key elements to parent element.
300      *
301      * @param parentElement parent XML element to which children elements are appended
302      * @param listEntryId   list entry identifier
303      */
304     public static void appendListKeyNodes(final Element parentElement, final NodeIdentifierWithPredicates listEntryId) {
305         for (Entry<QName, Object> key : listEntryId.entrySet()) {
306             final Element keyElement = parentElement.getOwnerDocument().createElementNS(
307                     key.getKey().getNamespace().toString(), key.getKey().getLocalName());
308             keyElement.setTextContent(key.getValue().toString());
309             parentElement.appendChild(keyElement);
310         }
311     }
312
313     /**
314      * Aggregation of the fields paths based on parenthesis. Only parent/enclosing {@link YangInstanceIdentifier}
315      * are kept. For example, paths '/x/y/z', '/x/y', and '/x' are aggregated into single field path: '/x'
316      *
317      * @param fields paths of fields
318      * @return filtered {@link List} of paths
319      */
320     private static List<YangInstanceIdentifier> aggregateFields(final List<YangInstanceIdentifier> fields) {
321         return fields.stream()
322                 .filter(field -> fields.stream()
323                         .filter(fieldYiid -> !field.equals(fieldYiid))
324                         .noneMatch(fieldYiid -> fieldYiid.contains(field)))
325                 .collect(Collectors.toList());
326     }
327
328     /**
329      * Construct a tree based on the parent {@link YangInstanceIdentifier} and provided list of fields. The goal of this
330      * procedure is the elimination of the redundancy that is introduced by potentially overlapping parts of the fields
331      * paths.
332      *
333      * @param query  path to parent element
334      * @param fields subpaths relative to parent path that identify specific fields
335      * @return created {@link TreeNode} structure
336      */
337     private static PathNode constructPathArgumentTree(final YangInstanceIdentifier query,
338             final List<YangInstanceIdentifier> fields) {
339         final Iterator<PathArgument> queryIterator = query.getPathArguments().iterator();
340         final PathNode rootTreeNode = new PathNode(queryIterator.next());
341
342         PathNode queryTreeNode = rootTreeNode;
343         while (queryIterator.hasNext()) {
344             queryTreeNode = queryTreeNode.ensureChild(queryIterator.next());
345         }
346
347         for (final YangInstanceIdentifier field : fields) {
348             PathNode actualFieldTreeNode = queryTreeNode;
349             for (final PathArgument fieldPathArg : field.getPathArguments()) {
350                 actualFieldTreeNode = actualFieldTreeNode.ensureChild(fieldPathArg);
351             }
352         }
353         return rootTreeNode;
354     }
355
356     public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mountContext,
357             final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
358         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
359         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
360         final XmlCodecFactory codecs = XmlCodecFactory.create(mountContext);
361
362         // FIXME: we probably need to propagate MountPointContext here and not just the child nodes
363         final ContainerSchemaNode dataRead = new NodeContainerProxy(NETCONF_DATA_QNAME,
364             mountContext.getEffectiveModelContext().getChildNodes());
365         try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, codecs, dataRead)) {
366             xmlParserStream.traverse(value);
367         }
368         return resultHolder;
369     }
370
371
372     // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
373     public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final EffectiveModelContext schemaContext,
374             final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
375         return transformDOMSourceToNormalizedNode(new EmptyMountPointContext(schemaContext), value);
376     }
377 }