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.stream.Collectors;
18 import javax.xml.stream.XMLOutputFactory;
19 import javax.xml.stream.XMLStreamException;
20 import javax.xml.stream.XMLStreamWriter;
21 import javax.xml.transform.dom.DOMResult;
22 import javax.xml.transform.dom.DOMSource;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.netconf.api.DocumentedException;
25 import org.opendaylight.netconf.api.xml.XmlElement;
26 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
27 import org.opendaylight.netconf.api.xml.XmlUtil;
28 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
29 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
30 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
31 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.Revision;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
40 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
42 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
44 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
45 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.w3c.dom.Document;
52 import org.xml.sax.SAXException;
54 public final class NetconfUtil {
56 * Shim interface to handle differences around namespace handling between various XMLStreamWriter implementations.
59 * <li>OpenJDK DOM writer (com.sun.xml.internal.stream.writers.XMLDOMWriterImpl) throws
60 * UnsupportedOperationException from its setNamespaceContext() method</li>
61 * <li>Woodstox DOM writer (com.ctc.wstx.dom.WstxDOMWrappingWriter) works with namespace context, but treats
62 * setPrefix() calls as hints -- which are not discoverable.</li>
66 * Due to this we perform a quick test for behavior and decide the appropriate strategy.
69 private interface NamespaceSetter {
70 void initializeNamespace(XMLStreamWriter writer) throws XMLStreamException;
72 static NamespaceSetter forFactory(final XMLOutputFactory xmlFactory) {
73 final String netconfNamespace = NETCONF_QNAME.getNamespace().toString();
74 final AnyXmlNamespaceContext namespaceContext = new AnyXmlNamespaceContext(ImmutableMap.of(
75 "op", netconfNamespace));
78 final XMLStreamWriter testWriter = xmlFactory.createXMLStreamWriter(new DOMResult(
79 XmlUtil.newDocument()));
80 testWriter.setNamespaceContext(namespaceContext);
81 } catch (final UnsupportedOperationException e) {
82 // This happens with JDK's DOM writer, which we may be using
83 LOG.warn("Unable to set namespace context, falling back to setPrefix()", e);
84 return writer -> writer.setPrefix("op", netconfNamespace);
85 } catch (XMLStreamException e) {
86 throw new ExceptionInInitializerError(e);
89 // Success, we can use setNamespaceContext()
90 return writer -> writer.setNamespaceContext(namespaceContext);
94 private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
96 // FIXME: document what exactly this QName means, as it is not referring to a tangible node nor the ietf-module.
97 // FIXME: what is this contract saying?
98 // - is it saying all data is going to be interpreted with this root?
99 // - is this saying we are following a specific interface contract (i.e. do we have schema mounts?)
100 // - is it also inferring some abilities w.r.t. RFC8342?
101 public static final QName NETCONF_QNAME = QName.create(QNameModule.create(SchemaContext.NAME.getNamespace(),
102 Revision.of("2011-06-01")), "netconf").intern();
103 // FIXME: is this the device-bound revision?
104 public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data").intern();
106 public static final XMLOutputFactory XML_FACTORY;
109 final XMLOutputFactory f = XMLOutputFactory.newFactory();
110 // FIXME: not repairing namespaces is probably common, this should be availabe as common XML constant.
111 f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
115 private static final NamespaceSetter XML_NAMESPACE_SETTER = NamespaceSetter.forFactory(XML_FACTORY);
117 private NetconfUtil() {
121 public static Document checkIsMessageOk(final Document response) throws DocumentedException {
122 final XmlElement docElement = XmlElement.fromDomDocument(response);
123 // FIXME: we should throw DocumentedException here
124 checkState(XmlNetconfConstants.RPC_REPLY_KEY.equals(docElement.getName()));
125 final XmlElement element = docElement.getOnlyChildElement();
126 if (XmlNetconfConstants.OK.equals(element.getName())) {
130 LOG.warn("Can not load last configuration. Operation failed.");
131 // FIXME: we should be throwing a DocumentedException here
132 throw new IllegalStateException("Can not load last configuration. Operation failed: "
133 + XmlUtil.toString(response));
136 @SuppressWarnings("checkstyle:IllegalCatch")
137 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
138 final SchemaPath schemaPath, final EffectiveModelContext context)
139 throws IOException, XMLStreamException {
140 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
142 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
143 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
144 NormalizedNodeWriter normalizedNodeWriter =
145 NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
147 normalizedNodeWriter.write(normalized);
148 normalizedNodeWriter.flush();
151 if (writer != null) {
154 } catch (final Exception e) {
155 LOG.warn("Unable to close resource properly", e);
160 @SuppressWarnings("checkstyle:IllegalCatch")
161 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
162 final @Nullable NormalizedMetadata metadata,
163 final DOMResult result, final SchemaPath schemaPath,
164 final EffectiveModelContext context) throws IOException, XMLStreamException {
165 if (metadata == null) {
166 writeNormalizedNode(normalized, result, schemaPath, context);
170 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
171 XML_NAMESPACE_SETTER.initializeNamespace(writer);
173 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
174 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
175 NormalizedMetadataWriter normalizedNodeWriter =
176 NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
178 normalizedNodeWriter.write(normalized, metadata);
179 normalizedNodeWriter.flush();
182 if (writer != null) {
185 } catch (final Exception e) {
186 LOG.warn("Unable to close resource properly", e);
192 * Writing subtree filter specified by {@link YangInstanceIdentifier} into {@link DOMResult}.
194 * @param query path to the root node
195 * @param result DOM result holder
196 * @param schemaPath schema path of the parent node
197 * @param context mountpoint schema context
198 * @throws IOException failed to write filter into {@link NormalizedNodeStreamWriter}
199 * @throws XMLStreamException failed to serialize filter into XML document
201 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
202 final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
203 if (query.isEmpty()) {
208 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
210 try (NormalizedNodeStreamWriter writer =
211 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
212 final Iterator<PathArgument> it = query.getPathArguments().iterator();
213 final PathArgument first = it.next();
214 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
223 * Writing subtree filter specified by parent {@link YangInstanceIdentifier} and specific fields
224 * into {@link DOMResult}. Field paths are relative to parent query path.
226 * @param query path to the root node
227 * @param result DOM result holder
228 * @param schemaPath schema path of the parent node
229 * @param context mountpoint schema context
230 * @param fields list of specific fields for which the filter should be created
231 * @throws IOException failed to write filter into {@link NormalizedNodeStreamWriter}
232 * @throws XMLStreamException failed to serialize filter into XML document
233 * @throws NullPointerException if any argument is null
235 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
236 final SchemaPath schemaPath, final EffectiveModelContext context,
237 final List<YangInstanceIdentifier> fields) throws IOException, XMLStreamException {
238 if (query.isEmpty() || fields.isEmpty()) {
242 final List<YangInstanceIdentifier> aggregatedFields = aggregateFields(fields);
243 final PathNode rootNode = constructPathArgumentTree(query, aggregatedFields);
245 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
247 try (NormalizedNodeStreamWriter writer = XMLStreamNormalizedNodeStreamWriter.create(
248 xmlWriter, context, schemaPath)) {
249 final PathArgument first = rootNode.element();
250 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType())
251 .streamToWriter(writer, first, rootNode);
258 private static List<YangInstanceIdentifier> aggregateFields(final List<YangInstanceIdentifier> fields) {
259 return fields.stream()
260 .filter(field -> fields.stream()
261 .filter(fieldYiid -> !field.equals(fieldYiid))
262 .noneMatch(fieldYiid -> fieldYiid.contains(field)))
263 .collect(Collectors.toList());
266 private static PathNode constructPathArgumentTree(final YangInstanceIdentifier query,
267 final List<YangInstanceIdentifier> fields) {
268 final Iterator<PathArgument> queryIterator = query.getPathArguments().iterator();
269 final PathNode rootTreeNode = new PathNode(queryIterator.next());
271 PathNode queryTreeNode = rootTreeNode;
272 while (queryIterator.hasNext()) {
273 queryTreeNode = queryTreeNode.ensureChild(queryIterator.next());
276 for (final YangInstanceIdentifier field : fields) {
277 PathNode actualFieldTreeNode = queryTreeNode;
278 for (final PathArgument fieldPathArg : field.getPathArguments()) {
279 actualFieldTreeNode = actualFieldTreeNode.ensureChild(fieldPathArg);
285 public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mountContext,
286 final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
287 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
288 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
289 final XmlCodecFactory codecs = XmlCodecFactory.create(mountContext);
291 // FIXME: we probably need to propagate MountPointContext here and not just the child nodes
292 final ContainerSchemaNode dataRead = new NodeContainerProxy(NETCONF_DATA_QNAME,
293 mountContext.getEffectiveModelContext().getChildNodes());
294 try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, codecs, dataRead)) {
295 xmlParserStream.traverse(value);
301 // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
302 public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final EffectiveModelContext schemaContext,
303 final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
304 return transformDOMSourceToNormalizedNode(new EmptyMountPointContext(schemaContext), value);