Revert "Convert anyxml nodes lazily"
[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.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.netconf.api.DocumentedException;
19 import org.opendaylight.netconf.api.xml.XmlElement;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
23 import org.opendaylight.yangtools.rfc7952.data.util.NormalizedMetadataWriter;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
29 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
35
36 public final class NetconfUtil {
37
38     private static final Logger LOG = LoggerFactory.getLogger(NetconfUtil.class);
39     public static final XMLOutputFactory XML_FACTORY;
40
41     static {
42         XML_FACTORY = XMLOutputFactory.newFactory();
43         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
44     }
45
46     private NetconfUtil() {}
47
48     public static Document checkIsMessageOk(final Document response) throws DocumentedException {
49         XmlElement element = XmlElement.fromDomDocument(response);
50         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
51         element = element.getOnlyChildElement();
52         if (element.getName().equals(XmlNetconfConstants.OK)) {
53             return response;
54         }
55         LOG.warn("Can not load last configuration. Operation failed.");
56         throw new IllegalStateException("Can not load last configuration. Operation failed: "
57                 + XmlUtil.toString(response));
58     }
59
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result,
62                                            final SchemaPath schemaPath, final SchemaContext context)
63             throws IOException, XMLStreamException {
64         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
65         try (
66              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
67                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
68              NormalizedNodeWriter normalizedNodeWriter =
69                      NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)
70         ) {
71             normalizedNodeWriter.write(normalized);
72             normalizedNodeWriter.flush();
73         } finally {
74             try {
75                 if (writer != null) {
76                     writer.close();
77                 }
78             } catch (final Exception e) {
79                 LOG.warn("Unable to close resource properly", e);
80             }
81         }
82     }
83
84     @SuppressWarnings("checkstyle:IllegalCatch")
85     public static void writeNormalizedNode(final NormalizedNode<?, ?> normalized,
86                                            final @Nullable NormalizedMetadata metadata,
87                                            final DOMResult result, final SchemaPath schemaPath,
88                                            final SchemaContext context) throws IOException, XMLStreamException {
89         if (metadata == null) {
90             writeNormalizedNode(normalized, result, schemaPath, context);
91             return;
92         }
93
94         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
95         try (
96              NormalizedNodeStreamWriter normalizedNodeStreamWriter =
97                      XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
98                 NormalizedMetadataWriter normalizedNodeWriter =
99                      NormalizedMetadataWriter.forStreamWriter(normalizedNodeStreamWriter)
100         ) {
101             normalizedNodeWriter.write(normalized, metadata);
102             normalizedNodeWriter.flush();
103         } finally {
104             try {
105                 if (writer != null) {
106                     writer.close();
107                 }
108             } catch (final Exception e) {
109                 LOG.warn("Unable to close resource properly", e);
110             }
111         }
112     }
113
114     public static void writeFilter(final YangInstanceIdentifier query, final DOMResult result,
115             final SchemaPath schemaPath, final SchemaContext context) throws IOException, XMLStreamException {
116         if (query.isEmpty()) {
117             // No query at all
118             return;
119         }
120
121         final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
122         try {
123             try (NormalizedNodeStreamWriter writer =
124                     XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath)) {
125                 final Iterator<PathArgument> it = query.getPathArguments().iterator();
126                 final PathArgument first = it.next();
127                 StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first,
128                     it);
129             }
130         } finally {
131             xmlWriter.close();
132         }
133     }
134 }