Merge "Refactor netconf clustered topology tests"
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / NetconfUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.util;
9
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;
30
31 public final class NetconfUtil {
32
33     private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
34     public static final XMLOutputFactory XML_FACTORY;
35
36     static {
37         XML_FACTORY = XMLOutputFactory.newFactory();
38         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
39     }
40
41     private NetconfUtil() {}
42
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)) {
48             return response;
49         }
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));
53     }
54
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
57                                            final SchemaPath schemaPath, final SchemaContext context)
58             throws IOException, XMLStreamException {
59         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
60         try (
61              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
62                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
63              NormalizedNodeWriter normalizedNodeWriter =
64                      NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
65         ) {
66             normalizedNodeWriter.write(normalized);
67             normalizedNodeWriter.flush();
68         } finally {
69             try {
70                 if (writer != null) {
71                     writer.close();
72                 }
73             } catch (final Exception e) {
74                 LOG.warn("Unable to close resource properly", e);
75             }
76         }
77     }
78 }