Expose streams with all supported encodings
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / NotificationFormatter.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;
9
10 import java.io.IOException;
11 import java.time.Instant;
12 import javax.xml.stream.XMLStreamException;
13 import javax.xml.transform.dom.DOMResult;
14 import javax.xml.xpath.XPathExpressionException;
15 import org.opendaylight.mdsal.dom.api.DOMEvent;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateNotificationStream;
18 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.w3c.dom.Document;
21
22 abstract class NotificationFormatter extends EventFormatter<DOMNotification> {
23     private static final String CREATE_NOTIFICATION_STREAM_ELEMENT = CreateNotificationStream.QNAME.getLocalName();
24     private static final String CREATE_NOTIFICATION_STREAM_NS =
25         CreateNotificationStream.QNAME.getNamespace().toString();
26
27     NotificationFormatter(final TextParameters textParams) {
28         super(textParams);
29     }
30
31     NotificationFormatter(final TextParameters textParams, final String xpathFilter) throws XPathExpressionException {
32         super(textParams, xpathFilter);
33     }
34
35     @Override
36     final void fillDocument(final Document doc, final EffectiveModelContext schemaContext, final DOMNotification input)
37             throws IOException {
38         final var notificationElement = createNotificationElement(doc,
39             input instanceof DOMEvent domEvent ? domEvent.getEventInstant() : Instant.now());
40         // FIXME: what is this really?!
41         final var notificationEventElement = doc.createElementNS(CREATE_NOTIFICATION_STREAM_NS,
42             CREATE_NOTIFICATION_STREAM_ELEMENT);
43         final var dataElement = doc.createElement("notification");
44         try {
45             final var writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(new DOMResult(dataElement));
46             try {
47                 writeBody(XMLStreamNormalizedNodeStreamWriter.create(writer, schemaContext, input.getType()),
48                     input.getBody());
49             } finally {
50                 writer.close();
51             }
52         } catch (final XMLStreamException e) {
53             throw new IOException("Failed to write notification content", e);
54         }
55         notificationElement.appendChild(notificationEventElement);
56         doc.appendChild(notificationElement);
57     }
58 }