1e1a04e62b9dfcf64dcca199cd982ed00f01e11b
[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, final boolean changedLeafNodesOnly)
46             throws IOException {
47         final var writer = new StringWriter();
48
49         try {
50             final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
51             final var nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext,
52                 input.getType());
53
54             try (var nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter)) {
55                 nnWriter.write(input.getBody());
56                 nnWriter.flush();
57
58                 xmlStreamWriter.writeEndElement();
59                 xmlStreamWriter.writeEndDocument();
60                 xmlStreamWriter.flush();
61             }
62         } catch (XMLStreamException e) {
63             throw new IOException("Failed to write notification content", e);
64         }
65
66         return writer.toString();
67     }
68 }