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.rcf8528.data.util.EmptyMountPointContext;
26 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
27 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
28 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.common.Revision;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
37 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
39 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
42 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.w3c.dom.Document;
48 import org.xml.sax.SAXException;
50 public final class NetconfUtil {
51 private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
53 // FIXME: document what exactly this QName means, as it is not referring to a tangible node nor the ietf-module.
54 // FIXME: what is this contract saying?
55 // - is it saying all data is going to be interpreted with this root?
56 // - is this saying we are following a specific interface contract (i.e. do we have schema mounts?)
57 // - is it also inferring some abilities w.r.t. RFC8342?
58 public static final QName NETCONF_QNAME = QName.create(QNameModule.create(SchemaContext.NAME.getNamespace(),
59 Revision.of("2011-06-01")), "netconf").intern();
60 // FIXME: is this the device-bound revision?
61 public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data").intern();
62 public static final XMLOutputFactory XML_FACTORY;
65 final XMLOutputFactory f = XMLOutputFactory.newFactory();
66 // FIXME: not repairing namespaces is probably common, this should be availabe as common XML constant.
67 f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
71 private NetconfUtil() {
75 public static Document checkIsMessageOk(final Document response) throws DocumentedException {
76 final XmlElement docElement = XmlElement.fromDomDocument(response);
77 // FIXME: we should throw DocumentedException here
78 checkState(XmlNetconfConstants.RPC_REPLY_KEY.equals(docElement.getName()));
79 final XmlElement element = docElement.getOnlyChildElement();
80 if (XmlNetconfConstants.OK.equals(element.getName())) {
84 LOG.warn("Can not load last configuration. Operation failed.");
85 // FIXME: we should be throwing a DocumentedException here
86 throw new IllegalStateException("Can not load last configuration. Operation failed: "
87 + XmlUtil.toString(response));
90 @SuppressWarnings("checkstyle:IllegalCatch")
91 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
92 final SchemaPath schemaPath, final SchemaContext context)
93 throws IOException, XMLStreamException {
94 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
96 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
97 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
98 NormalizedNodeWriter normalizedNodeWriter =
99 NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
101 normalizedNodeWriter.write(normalized);
102 normalizedNodeWriter.flush();
105 if (writer != null) {
108 } catch (final Exception e) {
109 LOG.warn("Unable to close resource properly", e);
114 @SuppressWarnings("checkstyle:IllegalCatch")
115 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
116 final @Nullable NormalizedMetadata metadata,
117 final DOMResult result, final SchemaPath schemaPath,
118 final SchemaContext context) throws IOException, XMLStreamException {
119 if (metadata == null) {
120 writeNormalizedNode(normalized, result, schemaPath, context);
124 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
126 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
127 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
128 NormalizedMetadataWriter normalizedNodeWriter =
129 NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
131 normalizedNodeWriter.write(normalized, metadata);
132 normalizedNodeWriter.flush();
135 if (writer != null) {
138 } catch (final Exception e) {
139 LOG.warn("Unable to close resource properly", e);
144 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
145 final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
146 if (query.isEmpty()) {
151 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
153 try (NormalizedNodeStreamWriter writer =
154 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
155 final Iterator<PathArgument> it = query.getPathArguments().iterator();
156 final PathArgument first = it.next();
157 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
165 public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final MountPointContext mountContext,
166 final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
167 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
168 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
169 final XmlCodecFactory codecs = XmlCodecFactory.create(mountContext);
171 // FIXME: we probably need to propagate MountPointContext here and not just the child nodes
172 final ContainerSchemaNode dataRead = new NodeContainerProxy(NETCONF_DATA_QNAME,
173 mountContext.getSchemaContext().getChildNodes());
174 try (XmlParserStream xmlParserStream = XmlParserStream.create(writer, codecs, dataRead)) {
175 xmlParserStream.traverse(value);
181 // FIXME: document this interface contract. Does it support RFC8528/RFC8542? How?
182 public static NormalizedNodeResult transformDOMSourceToNormalizedNode(final SchemaContext schemaContext,
183 final DOMSource value) throws XMLStreamException, URISyntaxException, IOException, SAXException {
184 return transformDOMSourceToNormalizedNode(new EmptyMountPointContext(schemaContext), value);