Make ListenerAdapter serialize JSON directly
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / formatters / 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.common.formatters;
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 public 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     public static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory() {
29         @Override
30         public XMLNotificationFormatter getFormatter(final String xpathFilter) throws XPathExpressionException {
31             return new XMLNotificationFormatter(xpathFilter);
32         }
33
34         @Override
35         public XMLNotificationFormatter getFormatter() {
36             return INSTANCE;
37         }
38     };
39
40     public XMLNotificationFormatter() {
41     }
42
43     public XMLNotificationFormatter(String xpathFilter) throws XPathExpressionException {
44         super(xpathFilter);
45     }
46
47     @Override
48     String createText(EffectiveModelContext schemaContext, DOMNotification input, Instant now,
49                       boolean leafNodesOnly, boolean skipData)
50             throws IOException {
51         final StringWriter writer = new StringWriter();
52         try {
53             final XMLStreamWriter xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(writer);
54             xmlStreamWriter.setDefaultNamespace(NOTIFICATION_NAMESPACE);
55
56             xmlStreamWriter.writeStartElement(NOTIFICATION_NAMESPACE, NOTIFICATION_ELEMENT);
57             xmlStreamWriter.writeDefaultNamespace(NOTIFICATION_NAMESPACE);
58
59             xmlStreamWriter.writeStartElement("eventTime");
60             xmlStreamWriter.writeCharacters(toRFC3339(now));
61             xmlStreamWriter.writeEndElement();
62
63             final NormalizedNodeStreamWriter nnStreamWriter =
64                     XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext, input.getType());
65
66             final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter);
67             nnWriter.write(input.getBody());
68             nnWriter.flush();
69
70             xmlStreamWriter.writeEndElement();
71             xmlStreamWriter.writeEndDocument();
72             xmlStreamWriter.flush();
73
74             nnWriter.close();
75
76             return writer.toString();
77         } catch (XMLStreamException e) {
78             throw new IOException("Failed to write notification content", e);
79         }
80     }
81 }