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 com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.util.Iterator;
13 import javax.xml.stream.XMLOutputFactory;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.stream.XMLStreamWriter;
16 import javax.xml.transform.dom.DOMResult;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.netconf.api.DocumentedException;
19 import org.opendaylight.netconf.api.xml.XmlElement;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
23 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
29 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
36 public final class NetconfUtil {
38 private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
39 public static final XMLOutputFactory XML_FACTORY;
42 XML_FACTORY = XMLOutputFactory.newFactory();
43 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
46 private NetconfUtil() {}
48 public static Document checkIsMessageOk(final Document response) throws DocumentedException {
49 XmlElement element = XmlElement.fromDomDocument(response);
50 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
51 element = element.getOnlyChildElement();
52 if (element.getName().equals(XmlNetconfConstants.OK)) {
55 LOG.warn("Can not load last configuration. Operation failed.");
56 throw new IllegalStateException("Can not load last configuration. Operation failed: "
57 + XmlUtil.toString(response));
60 @SuppressWarnings("checkstyle:IllegalCatch")
61 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
62 final SchemaPath schemaPath, final SchemaContext context)
63 throws IOException, XMLStreamException {
64 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
66 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
67 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
68 NormalizedNodeWriter normalizedNodeWriter =
69 NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
71 normalizedNodeWriter.write(normalized);
72 normalizedNodeWriter.flush();
78 } catch (final Exception e) {
79 LOG.warn("Unable to close resource properly", e);
84 @SuppressWarnings("checkstyle:IllegalCatch")
85 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
86 final @Nullable NormalizedMetadata metadata,
87 final DOMResult result, final SchemaPath schemaPath,
88 final SchemaContext context) throws IOException, XMLStreamException {
89 if (metadata == null) {
90 writeNormalizedNode(normalized, result, schemaPath, context);
94 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
96 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
97 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
98 NormalizedMetadataWriter normalizedNodeWriter =
99 NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
101 normalizedNodeWriter.write(normalized, metadata);
102 normalizedNodeWriter.flush();
105 if (writer != null) {
108 } catch (final Exception e) {
109 LOG.warn("Unable to close resource properly", e);
114 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
115 final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
116 if (query.isEmpty()) {
121 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
123 try (NormalizedNodeStreamWriter writer =
124 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
125 final Iterator<PathArgument> it = query.getPathArguments().iterator();
126 final PathArgument first = it.next();
127 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,