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 java.io.IOException;
13 import java.net.URISyntaxException;
14 import java.util.Iterator;
15 import javax.xml.stream.XMLOutputFactory;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.stream.XMLStreamWriter;
18 import javax.xml.transform.dom.DOMResult;
19 import javax.xml.transform.dom.DOMSource;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.netconf.api.DocumentedException;
22 import org.opendaylight.netconf.api.xml.XmlElement;
23 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
24 import org.opendaylight.netconf.api.xml.XmlUtil;
25 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
26 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.Revision;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
35 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
37 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
40 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.w3c.dom.Document;
46 import org.xml.sax.SAXException;
48 public final class NetconfUtil {
49 private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
51 // FIXME: document what exactly this QName means, as it is not referring to a tangible node nor the ietf-module.
52 // FIXME: what is this contract saying?
53 // - is it saying all data is going to be interpreted with this root?
54 // - is this saying we are following a specific interface contract (i.e. do we have schema mounts?)
55 // - is it also inferring some abilities w.r.t. RFC8342?
56 public static final QName NETCONF_QNAME = QName.create(QNameModule.create(SchemaContext.NAME.getNamespace(),
57 Revision.of("2011-06-01")), "netconf").intern();
58 // FIXME: is this the device-bound revision?
59 public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data").intern();
60 public static final XMLOutputFactory XML_FACTORY;
63 final XMLOutputFactory f = XMLOutputFactory.newFactory();
64 // FIXME: not repairing namespaces is probably common, this should be availabe as common XML constant.
65 f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
69 private NetconfUtil() {
73 public static Document checkIsMessageOk(final Document response) throws DocumentedException {
74 final XmlElement docElement = XmlElement.fromDomDocument(response);
75 // FIXME: we should throw DocumentedException here
76 checkState(XmlNetconfConstants.RPC_REPLY_KEY.equals(docElement.getName()));
77 final XmlElement element = docElement.getOnlyChildElement();
78 if (XmlNetconfConstants.OK.equals(element.getName())) {
82 LOG.warn("Can not load last configuration. Operation failed.");
83 // FIXME: we should be throwing a DocumentedException here
84 throw new IllegalStateException("Can not load last configuration. Operation failed: "
85 + XmlUtil.toString(response));
88 @SuppressWarnings("checkstyle:IllegalCatch")
89 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
90 final SchemaPath schemaPath, final SchemaContext context)
91 throws IOException, XMLStreamException {
92 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
94 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
95 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
96 NormalizedNodeWriter normalizedNodeWriter =
97 NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
99 normalizedNodeWriter.write(normalized);
100 normalizedNodeWriter.flush();
103 if (writer != null) {
106 } catch (final Exception e) {
107 LOG.warn("Unable to close resource properly", e);
112 @SuppressWarnings("checkstyle:IllegalCatch")
113 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
114 final @Nullable NormalizedMetadata metadata,
115 final DOMResult result, final SchemaPath schemaPath,
116 final SchemaContext context) throws IOException, XMLStreamException {
117 if (metadata == null) {
118 writeNormalizedNode(normalized, result, schemaPath, context);
122 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
124 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
125 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
126 NormalizedMetadataWriter normalizedNodeWriter =
127 NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
129 normalizedNodeWriter.write(normalized, metadata);
130 normalizedNodeWriter.flush();
133 if (writer != null) {
136 } catch (final Exception e) {
137 LOG.warn("Unable to close resource properly", e);
142 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
143 final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
144 if (query.isEmpty()) {
149 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
151 try (NormalizedNodeStreamWriter writer =
152 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
153 final Iterator<PathArgument> it = query.getPathArguments().iterator();
154 final PathArgument first = it.next();
155 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
163 // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
164 public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final SchemaContext schemaContext,
165 final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
166 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
167 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
168 final XmlCodecFactory codecs = XmlCodecFactory.create(schemaContext);
169 final ContainerSchemaNode dataRead = new NodeContainerProxy(NETCONF_DATA_QNAME, schemaContext.getChildNodes());
170 try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, codecs, dataRead)) {
171 xmlParserStream.traverse(value);