Clean up XML interactions
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / XMLNotificationFormatter.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 java.io.IOException;
11 import java.io.StringWriter;
12 import java.time.Instant;
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.xpath.XPathExpressionException;
15 import org.opendaylight.mdsal.dom.api.DOMNotification;
16 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
17 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19
20 final class XMLNotificationFormatter extends NotificationFormatter {
21     private static final XMLNotificationFormatter INSTANCE = new XMLNotificationFormatter();
22
23     static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory() {
24         @Override
25         public XMLNotificationFormatter getFormatter(final String xpathFilter) throws XPathExpressionException {
26             return new XMLNotificationFormatter(xpathFilter);
27         }
28
29         @Override
30         public XMLNotificationFormatter getFormatter() {
31             return INSTANCE;
32         }
33     };
34
35     XMLNotificationFormatter() {
36
37     }
38
39     XMLNotificationFormatter(final String xpathFilter) throws XPathExpressionException {
40         super(xpathFilter);
41     }
42
43     @Override
44     String createText(final EffectiveModelContext schemaContext, final DOMNotification input, final Instant now,
45                       final boolean leafNodesOnly, final boolean skipData) throws IOException {
46         final var writer = new StringWriter();
47
48         try {
49             final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
50             final var nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext,
51                 input.getType());
52
53             try (var nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter)) {
54                 nnWriter.write(input.getBody());
55                 nnWriter.flush();
56
57                 xmlStreamWriter.writeEndElement();
58                 xmlStreamWriter.writeEndDocument();
59                 xmlStreamWriter.flush();
60             }
61         } catch (XMLStreamException e) {
62             throw new IOException("Failed to write notification content", e);
63         }
64
65         return writer.toString();
66     }
67 }