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.opendaylight.netconf.api.DocumentedException;
18 import org.opendaylight.netconf.api.xml.XmlElement;
19 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.netconf.api.xml.XmlUtil;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
26 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
33 public final class NetconfUtil {
35 private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
36 public static final XMLOutputFactory XML_FACTORY;
39 XML_FACTORY = XMLOutputFactory.newFactory();
40 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
43 private NetconfUtil() {
47 public static Document checkIsMessageOk(final Document response) throws DocumentedException {
48 XmlElement element = XmlElement.fromDomDocument(response);
49 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
50 element = element.getOnlyChildElement();
51 if (element.getName().equals(XmlNetconfConstants.OK)) {
54 LOG.warn("Can not load last configuration. Operation failed.");
55 throw new IllegalStateException("Can not load last configuration. Operation failed: "
56 + XmlUtil.toString(response));
59 @SuppressWarnings("checkstyle:IllegalCatch")
60 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
61 final SchemaPath schemaPath, final SchemaContext context)
62 throws IOException, XMLStreamException {
63 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
65 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
66 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
67 NormalizedNodeWriter normalizedNodeWriter =
68 NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
70 normalizedNodeWriter.write(normalized);
71 normalizedNodeWriter.flush();
77 } catch (final Exception e) {
78 LOG.warn("Unable to close resource properly", e);
83 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
84 final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
85 if (query.isEmpty()) {
90 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
92 try (NormalizedNodeStreamWriter writer =
93 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
94 final Iterator<PathArgument> it = query.getPathArguments().iterator();
95 final PathArgument first = it.next();
96 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,