2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.util;
10 import static com.google.common.base.Preconditions.checkState;
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;
57 public final class NetconfUtil {
59 * Shim interface to handle differences around namespace handling between various XMLStreamWriter implementations.
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>
69 * Due to this we perform a quick test for behavior and decide the appropriate strategy.
72 private interface NamespaceSetter {
73 void initializeNamespace(XMLStreamWriter writer) throws XMLStreamException;
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));
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);
92 // Success, we can use setNamespaceContext()
93 return writer -> writer.setNamespaceContext(namespaceContext);
97 private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
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();
109 public static final XMLOutputFactory XML_FACTORY;
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);
118 private static final NamespaceSetter XML_NAMESPACE_SETTER = NamespaceSetter.forFactory(XML_FACTORY);
120 private NetconfUtil() {
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())) {
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));
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);
145 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
146 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
147 NormalizedNodeWriter normalizedNodeWriter =
148 NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
150 normalizedNodeWriter.write(normalized);
151 normalizedNodeWriter.flush();
154 if (writer != null) {
157 } catch (final Exception e) {
158 LOG.warn("Unable to close resource properly", e);
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);
173 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
174 XML_NAMESPACE_SETTER.initializeNamespace(writer);
176 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
177 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
178 NormalizedMetadataWriter normalizedNodeWriter =
179 NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
181 normalizedNodeWriter.write(normalized, metadata);
182 normalizedNodeWriter.flush();
185 if (writer != null) {
188 } catch (final Exception e) {
189 LOG.warn("Unable to close resource properly", e);
195 * Writing subtree filter specified by {@link YangInstanceIdentifier} into {@link DOMResult}.
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
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()) {
211 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
213 try (NormalizedNodeStreamWriter writer =
214 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
215 final Iterator<PathArgument> it = query.getPathArguments().iterator();
216 final PathArgument first = it.next();
217 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
226 * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
227 * into {@link DOMResult}. Field paths are relative to parent query path.
229 * @param query path to the root node
230 * @param result DOM result holder
231 * @param schemaPath schema path of the parent node
232 * @param context mountpoint schema context
233 * @param fields list of specific fields for which the filter should be created
234 * @throws IOException failed to write filter into {@link NormalizedNodeStreamWriter}
235 * @throws XMLStreamException failed to serialize filter into XML document
236 * @throws NullPointerException if any argument is null
238 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
239 final SchemaPath schemaPath, final EffectiveModelContext context,
240 final List<YangInstanceIdentifier> fields) throws IOException, XMLStreamException {
241 if (query.isEmpty() || fields.isEmpty()) {
245 final List<YangInstanceIdentifier> aggregatedFields = aggregateFields(fields);
246 final PathNode rootNode = constructPathArgumentTree(query, aggregatedFields);
248 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
250 try (NormalizedNodeStreamWriter writer = XMLStreamNormalizedNodeStreamWriter.create(
251 xmlWriter, context, schemaPath)) {
252 final PathArgument first = rootNode.element();
253 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
254 .streamToWriter(writer, first, rootNode);
262 * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
263 * into {@link Element}. Field paths are relative to parent query path. Filter is created without following
264 * {@link EffectiveModelContext}.
266 * @param query path to the root node
267 * @param fields list of specific fields for which the filter should be created
268 * @param filterElement XML filter element to which the created filter will be written
270 public static void writeSchemalessFilter(final YangInstanceIdentifier query,
271 final List<YangInstanceIdentifier> fields, final Element filterElement) {
272 pathArgumentTreeToXmlStructure(constructPathArgumentTree(query, aggregateFields(fields)), filterElement);
275 private static void pathArgumentTreeToXmlStructure(final PathNode pathArgumentTree, final Element data) {
276 final PathArgument pathArg = pathArgumentTree.element();
278 final QName nodeType = pathArg.getNodeType();
279 final String elementNamespace = nodeType.getNamespace().toString();
281 if (data.getElementsByTagNameNS(elementNamespace, nodeType.getLocalName()).getLength() != 0) {
282 // element has already been written as list key
286 final Element childElement = data.getOwnerDocument().createElementNS(elementNamespace, nodeType.getLocalName());
287 data.appendChild(childElement);
288 if (pathArg instanceof NodeIdentifierWithPredicates) {
289 appendListKeyNodes(childElement, (NodeIdentifierWithPredicates) pathArg);
291 for (final PathNode childrenNode : pathArgumentTree.children()) {
292 pathArgumentTreeToXmlStructure(childrenNode, childElement);
297 * Appending list key elements to parent element.
299 * @param parentElement parent XML element to which children elements are appended
300 * @param listEntryId list entry identifier
302 public static void appendListKeyNodes(final Element parentElement, final NodeIdentifierWithPredicates listEntryId) {
303 for (Entry<QName, Object> key : listEntryId.entrySet()) {
304 final Element keyElement = parentElement.getOwnerDocument().createElementNS(
305 key.getKey().getNamespace().toString(), key.getKey().getLocalName());
306 keyElement.setTextContent(key.getValue().toString());
307 parentElement.appendChild(keyElement);
312 * Aggregation of the fields paths based on parenthesis. Only parent/enclosing {@link YangInstanceIdentifier}
313 * are kept. For example, paths '/x/y/z', '/x/y', and '/x' are aggregated into single field path: '/x'
315 * @param fields paths of fields
316 * @return filtered {@link List} of paths
318 private static List<YangInstanceIdentifier> aggregateFields(final List<YangInstanceIdentifier> fields) {
319 return fields.stream()
320 .filter(field -> fields.stream()
321 .filter(fieldYiid -> !field.equals(fieldYiid))
322 .noneMatch(fieldYiid -> fieldYiid.contains(field)))
323 .collect(Collectors.toList());
327 * Construct a tree based on the parent {@link YangInstanceIdentifier} and provided list of fields. The goal of this
328 * procedure is the elimination of the redundancy that is introduced by potentially overlapping parts of the fields
331 * @param query path to parent element
332 * @param fields subpaths relative to parent path that identify specific fields
333 * @return created {@link TreeNode} structure
335 private static PathNode constructPathArgumentTree(final YangInstanceIdentifier query,
336 final List<YangInstanceIdentifier> fields) {
337 final Iterator<PathArgument> queryIterator = query.getPathArguments().iterator();
338 final PathNode rootTreeNode = new PathNode(queryIterator.next());
340 PathNode queryTreeNode = rootTreeNode;
341 while (queryIterator.hasNext()) {
342 queryTreeNode = queryTreeNode.ensureChild(queryIterator.next());
345 for (final YangInstanceIdentifier field : fields) {
346 PathNode actualFieldTreeNode = queryTreeNode;
347 for (final PathArgument fieldPathArg : field.getPathArguments()) {
348 actualFieldTreeNode = actualFieldTreeNode.ensureChild(fieldPathArg);
354 public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mountContext,
355 final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
356 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
357 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
358 final XmlCodecFactory codecs = XmlCodecFactory.create(mountContext);
360 // FIXME: we probably need to propagate MountPointContext here and not just the child nodes
361 final ContainerSchemaNode dataRead = new NodeContainerProxy(NETCONF_DATA_QNAME,
362 mountContext.getEffectiveModelContext().getChildNodes());
363 try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, codecs, dataRead)) {
364 xmlParserStream.traverse(value);
370 // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
371 public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final EffectiveModelContext schemaContext,
372 final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
373 return transformDOMSourceToNormalizedNode(new EmptyMountPointContext(schemaContext), value);