d47c86d10af5c76f179824d3372309f5c583b2cb
[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 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;
32
33 public final class NetconfUtil {
34
35     private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
36     public static final XMLOutputFactory XML_FACTORY;
37
38     static {
39         XML_FACTORY = XMLOutputFactory.newFactory();
40         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
41     }
42
43     private NetconfUtil() {
44
45     }
46
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)) {
52             return response;
53         }
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));
57     }
58
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);
64         try (
65              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
66                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
67              NormalizedNodeWriter normalizedNodeWriter =
68                      NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
69         ) {
70             normalizedNodeWriter.write(normalized);
71             normalizedNodeWriter.flush();
72         } finally {
73             try {
74                 if (writer != null) {
75                     writer.close();
76                 }
77             } catch (final Exception e) {
78                 LOG.warn("Unable to close resource properly", e);
79             }
80         }
81     }
82
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()) {
86             // No query at all
87             return;
88         }
89
90         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
91         try {
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,
97                     it);
98             }
99         } finally {
100             xmlWriter.close();
101         }
102     }
103 }