Reduce exception guard
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / XMLDataTreeCandidateFormatter.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.streams.listeners;
9
10 import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.XML_OUTPUT_FACTORY;
11 import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.DATA_CHANGED_NAMESPACE;
12 import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.DATA_CHANGED_NOTIFICATION_ELEMENT;
13 import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.NOTIFICATION_ELEMENT;
14 import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.NOTIFICATION_NAMESPACE;
15
16 import java.io.IOException;
17 import java.io.StringWriter;
18 import java.time.Instant;
19 import java.util.Collection;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.stream.XMLStreamWriter;
22 import javax.xml.xpath.XPathExpressionException;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25
26 public final class XMLDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
27     private static final XMLDataTreeCandidateFormatter INSTANCE = new XMLDataTreeCandidateFormatter();
28
29     static final DataTreeCandidateFormatterFactory FACTORY =
30         new DataTreeCandidateFormatterFactory() {
31             @Override
32             public XMLDataTreeCandidateFormatter getFormatter(final String xpathFilter)
33                     throws XPathExpressionException {
34                 return new XMLDataTreeCandidateFormatter(xpathFilter);
35             }
36
37             @Override
38             public XMLDataTreeCandidateFormatter getFormatter() {
39                 return INSTANCE;
40             }
41         };
42
43     private XMLDataTreeCandidateFormatter() {
44
45     }
46
47     private XMLDataTreeCandidateFormatter(final String xpathFilter) throws XPathExpressionException {
48         super(xpathFilter);
49     }
50
51     @Override
52     String createText(final EffectiveModelContext schemaContext, final Collection<DataTreeCandidate> input,
53                       final Instant now, final boolean leafNodesOnly, final boolean skipData) throws Exception {
54         StringWriter writer = new StringWriter();
55
56         final XMLStreamWriter xmlStreamWriter;
57         try {
58             xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(writer);
59             xmlStreamWriter.setDefaultNamespace(NOTIFICATION_NAMESPACE);
60
61             xmlStreamWriter.writeStartElement(NOTIFICATION_NAMESPACE, NOTIFICATION_ELEMENT);
62             xmlStreamWriter.writeDefaultNamespace(NOTIFICATION_NAMESPACE);
63
64             xmlStreamWriter.writeStartElement("eventTime");
65             xmlStreamWriter.writeCharacters(toRFC3339(now));
66             xmlStreamWriter.writeEndElement();
67
68             xmlStreamWriter.setDefaultNamespace(DATA_CHANGED_NAMESPACE);
69             xmlStreamWriter.writeStartElement(DATA_CHANGED_NAMESPACE, DATA_CHANGED_NOTIFICATION_ELEMENT);
70
71             final XmlDataTreeCandidateSerializer serializer =
72                     new XmlDataTreeCandidateSerializer(schemaContext, xmlStreamWriter);
73
74             for (final DataTreeCandidate candidate : input) {
75                 serializer.serialize(candidate, leafNodesOnly, skipData);
76             }
77
78             // data-changed-notification
79             xmlStreamWriter.writeEndElement();
80
81             // notification
82             xmlStreamWriter.writeEndElement();
83             xmlStreamWriter.close();
84         } catch (XMLStreamException e) {
85             throw new IOException("Failed to write notification content", e);
86         }
87
88
89         return writer.toString();
90     }
91 }