Use XMLStreamNormalizedNodeStreamWriter from yang-data-codec-xml
[honeycomb.git] / infra / notification / impl / src / main / java / io / fd / honeycomb / notification / impl / TranslationUtil.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.notification.impl;
18
19 import java.io.IOException;
20 import javax.xml.stream.XMLOutputFactory;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamWriter;
23 import javax.xml.transform.dom.DOMResult;
24 import org.opendaylight.controller.config.util.xml.XmlUtil;
25 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
26 import org.opendaylight.netconf.notifications.NetconfNotification;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
31 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39 public final class TranslationUtil {
40
41     public TranslationUtil() {}
42
43     private static final Logger LOG = LoggerFactory.getLogger(TranslationUtil.class);
44     private static final XMLOutputFactory XML_FACTORY;
45
46     static {
47         XML_FACTORY = XMLOutputFactory.newFactory();
48         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
49     }
50
51     /**
52      * Transform {@link org.opendaylight.mdsal.dom.api.DOMNotification} into an XML based {@link NetconfNotification}.
53      */
54     public static NetconfNotification notificationToXml(final DOMNotification domNotification, final SchemaContext ctx) {
55
56         LOG.trace("Transforming notification: {} into XML", domNotification.getType());
57
58         final SchemaPath type = domNotification.getType();
59         final QName notificationQName = type.getLastComponent();
60         final DOMResult result = prepareDomResultForRpcRequest(notificationQName);
61
62         try {
63             writeNormalizedRpc(domNotification, result, type, ctx);
64         } catch (final XMLStreamException | IOException | IllegalStateException e) {
65             LOG.warn("Unable to transform notification: {} into XML", domNotification.getType(), e);
66             throw new IllegalArgumentException("Unable to serialize " + type, e);
67         }
68
69         final Document node = result.getNode().getOwnerDocument();
70         return new NetconfNotification(node);
71     }
72
73     private static DOMResult prepareDomResultForRpcRequest(final QName notificationQName) {
74         final Document document = XmlUtil.newDocument();
75         final Element notificationElement =
76             document.createElementNS(notificationQName.getNamespace().toString(), notificationQName.getLocalName());
77         document.appendChild(notificationElement);
78         return new DOMResult(notificationElement);
79     }
80
81     private static void writeNormalizedRpc(final DOMNotification normalized, final DOMResult result,
82                                            final SchemaPath schemaPath, final SchemaContext baseNetconfCtx)
83         throws IOException, XMLStreamException {
84         final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
85         try {
86             try (final NormalizedNodeStreamWriter normalizedNodeStreamWriter =
87                      XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath)) {
88                 try (final NormalizedNodeWriter normalizedNodeWriter =
89                          NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)) {
90                     for (DataContainerChild<?, ?> dataContainerChild : normalized.getBody().getValue()) {
91                         normalizedNodeWriter.write(dataContainerChild);
92                     }
93                     normalizedNodeWriter.flush();
94                 }
95             }
96         } finally {
97             try {
98                 writer.close();
99             } catch (final Exception e) {
100                 LOG.warn("Unable to close resource properly. Ignoring", e);
101             }
102         }
103     }
104 }