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 javax.xml.stream.XMLOutputFactory;
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.stream.XMLStreamWriter;
15 import javax.xml.transform.dom.DOMResult;
16 import org.opendaylight.controller.config.util.xml.DocumentedException;
17 import org.opendaylight.controller.config.util.xml.XmlElement;
18 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
19 import org.opendaylight.controller.config.util.xml.XmlUtil;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
24 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
31 public final class NetconfUtil {
33 private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
34 public static final XMLOutputFactory XML_FACTORY;
37 XML_FACTORY = XMLOutputFactory.newFactory();
38 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
41 private NetconfUtil() {}
43 public static Document checkIsMessageOk(Document response) throws DocumentedException {
44 XmlElement element = XmlElement.fromDomDocument(response);
45 Preconditions.checkState(element.getName().equals(XmlMappingConstants.RPC_REPLY_KEY));
46 element = element.getOnlyChildElement();
47 if (element.getName().equals(XmlNetconfConstants.OK)) {
50 LOG.warn("Can not load last configuration. Operation failed.");
51 throw new IllegalStateException("Can not load last configuration. Operation failed: "
52 + XmlUtil.toString(response));
55 public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result, final SchemaPath schemaPath, final SchemaContext context)
56 throws IOException, XMLStreamException {
57 final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
59 final NormalizedNodeStreamWriter normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
60 final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
62 normalizedNodeWriter.write(normalized);
63 normalizedNodeWriter.flush();
69 } catch (final Exception e) {
70 LOG.warn("Unable to close resource properly", e);