Remove dependency on controller config-util
[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.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.xml.XmlElement;
18 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
23 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29
30 public final class NetconfUtil {
31
32     private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
33     public static final XMLOutputFactory XML_FACTORY;
34
35     static {
36         XML_FACTORY = XMLOutputFactory.newFactory();
37         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
38     }
39
40     private NetconfUtil() {}
41
42     public static Document checkIsMessageOk(final Document response) throws DocumentedException {
43         XmlElement element = XmlElement.fromDomDocument(response);
44         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
45         element = element.getOnlyChildElement();
46         if (element.getName().equals(XmlNetconfConstants.OK)) {
47             return response;
48         }
49         LOG.warn("Can not load last configuration. Operation failed.");
50         throw new IllegalStateException("Can not load last configuration. Operation failed: "
51                 + XmlUtil.toString(response));
52     }
53
54     @SuppressWarnings("checkstyle:IllegalCatch")
55     public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
56                                            final SchemaPath schemaPath, final SchemaContext context)
57             throws IOException, XMLStreamException {
58         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
59         try (
60              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
61                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
62              NormalizedNodeWriter normalizedNodeWriter =
63                      NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
64         ) {
65             normalizedNodeWriter.write(normalized);
66             normalizedNodeWriter.flush();
67         } finally {
68             try {
69                 if (writer != null) {
70                     writer.close();
71                 }
72             } catch (final Exception e) {
73                 LOG.warn("Unable to close resource properly", e);
74             }
75         }
76     }
77 }