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