Move streams support classes
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.stream.XMLStreamWriter;
15 import javax.xml.xpath.XPathExpressionException;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
19 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21
22 final class XMLNotificationFormatter extends NotificationFormatter {
23     private static final XMLNotificationFormatter INSTANCE = new XMLNotificationFormatter();
24
25     static final String NOTIFICATION_NAMESPACE = "urn:ietf:params:xml:ns:netconf:notification:1.0";
26     static final String NOTIFICATION_ELEMENT = "notification";
27
28     static final String DATA_CHANGED_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote";
29     static final String DATA_CHANGED_NOTIFICATION_ELEMENT = "data-changed-notification";
30
31     static final String DATA_CHANGE_EVENT_ELEMENT = "data-change-event";
32
33     static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory() {
34         @Override
35         public XMLNotificationFormatter getFormatter(final String xpathFilter) throws XPathExpressionException {
36             return new XMLNotificationFormatter(xpathFilter);
37         }
38
39         @Override
40         public XMLNotificationFormatter getFormatter() {
41             return INSTANCE;
42         }
43     };
44
45     XMLNotificationFormatter() {
46
47     }
48
49     XMLNotificationFormatter(final String xpathFilter) throws XPathExpressionException {
50         super(xpathFilter);
51     }
52
53     @Override
54     String createText(final EffectiveModelContext schemaContext, final DOMNotification input, final Instant now,
55                       final boolean leafNodesOnly, final boolean skipData)
56             throws IOException {
57         final StringWriter writer = new StringWriter();
58         try {
59             final XMLStreamWriter xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(writer);
60             xmlStreamWriter.setDefaultNamespace(NOTIFICATION_NAMESPACE);
61
62             xmlStreamWriter.writeStartElement(NOTIFICATION_NAMESPACE, NOTIFICATION_ELEMENT);
63             xmlStreamWriter.writeDefaultNamespace(NOTIFICATION_NAMESPACE);
64
65             xmlStreamWriter.writeStartElement("eventTime");
66             xmlStreamWriter.writeCharacters(toRFC3339(now));
67             xmlStreamWriter.writeEndElement();
68
69             final NormalizedNodeStreamWriter nnStreamWriter =
70                     XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext, input.getType());
71
72             final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter);
73             nnWriter.write(input.getBody());
74             nnWriter.flush();
75
76             xmlStreamWriter.writeEndElement();
77             xmlStreamWriter.writeEndDocument();
78             xmlStreamWriter.flush();
79
80             nnWriter.close();
81
82             return writer.toString();
83         } catch (XMLStreamException e) {
84             throw new IOException("Failed to write notification content", e);
85         }
86     }
87 }