Do not instantiate NormalizedNodes for filter
[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     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)) {
50             return response;
51         }
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));
55     }
56
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);
62         try (
63              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
64                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
65              NormalizedNodeWriter normalizedNodeWriter =
66                      NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
67         ) {
68             normalizedNodeWriter.write(normalized);
69             normalizedNodeWriter.flush();
70         } finally {
71             try {
72                 if (writer != null) {
73                     writer.close();
74                 }
75             } catch (final Exception e) {
76                 LOG.warn("Unable to close resource properly", e);
77             }
78         }
79     }
80
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()) {
84             // No query at all
85             return;
86         }
87
88         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
89         try {
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,
95                     it);
96             }
97         } finally {
98             xmlWriter.close();
99         }
100     }
101 }