Clean up XML interactions
[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.DATA_CHANGED_NOTIFICATION_ELEMENT;
11 import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.SAL_REMOTE_NAMESPACE;
12
13 import java.io.IOException;
14 import java.io.StringWriter;
15 import java.time.Instant;
16 import java.util.Collection;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.xpath.XPathExpressionException;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21
22 public final class XMLDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
23     private static final XMLDataTreeCandidateFormatter INSTANCE = new XMLDataTreeCandidateFormatter();
24
25     static final DataTreeCandidateFormatterFactory FACTORY =
26         new DataTreeCandidateFormatterFactory() {
27             @Override
28             public XMLDataTreeCandidateFormatter getFormatter(final String xpathFilter)
29                     throws XPathExpressionException {
30                 return new XMLDataTreeCandidateFormatter(xpathFilter);
31             }
32
33             @Override
34             public XMLDataTreeCandidateFormatter getFormatter() {
35                 return INSTANCE;
36             }
37         };
38
39     private XMLDataTreeCandidateFormatter() {
40
41     }
42
43     private XMLDataTreeCandidateFormatter(final String xpathFilter) throws XPathExpressionException {
44         super(xpathFilter);
45     }
46
47     @Override
48     String createText(final EffectiveModelContext schemaContext, final Collection<DataTreeCandidate> input,
49                       final Instant now, final boolean leafNodesOnly, final boolean skipData) throws Exception {
50         final var writer = new StringWriter();
51
52         try {
53             final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
54
55             xmlStreamWriter.setDefaultNamespace(SAL_REMOTE_NAMESPACE);
56             xmlStreamWriter.writeStartElement(SAL_REMOTE_NAMESPACE, DATA_CHANGED_NOTIFICATION_ELEMENT);
57
58             final var serializer = new XmlDataTreeCandidateSerializer(schemaContext, xmlStreamWriter);
59             for (var candidate : input) {
60                 serializer.serialize(candidate, leafNodesOnly, skipData);
61             }
62
63             // data-changed-notification
64             xmlStreamWriter.writeEndElement();
65
66             // notification
67             xmlStreamWriter.writeEndElement();
68             xmlStreamWriter.close();
69         } catch (XMLStreamException e) {
70             throw new IOException("Failed to write notification content", e);
71         }
72
73         return writer.toString();
74     }
75 }