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() {}
45 public static Document checkIsMessageOk(final Document response) throws DocumentedException {
46 XmlElement element = XmlElement.fromDomDocument(response);
47 Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
48 element = element.getOnlyChildElement();
49 if (element.getName().equals(XmlNetconfConstants.OK)) {
52 LOG.warn("Can not load last configuration. Operation failed.");
53 throw new IllegalStateException("Can not load last configuration. Operation failed: "
54 + XmlUtil.toString(response));
57 @SuppressWarnings("checkstyle:IllegalCatch")
58 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
59 final SchemaPath schemaPath, final SchemaContext context)
60 throws IOException, XMLStreamException {
61 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
63 NormalizedNodeStreamWriter normalizedNodeStreamWriter =
64 XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
65 NormalizedNodeWriter normalizedNodeWriter =
66 NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
68 normalizedNodeWriter.write(normalized);
69 normalizedNodeWriter.flush();
75 } catch (final Exception e) {
76 LOG.warn("Unable to close resource properly", e);
81 public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
82 final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
83 if (query.isEmpty()) {
88 final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
90 try (NormalizedNodeStreamWriter writer =
91 XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
92 final Iterator<PathArgument> it = query.getPathArguments().iterator();
93 final PathArgument first = it.next();
94 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,