2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.server.mdsal.streams.notif;
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.restconf.server.spi.EventFormatter;
18 import org.opendaylight.restconf.server.spi.TextParameters;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateNotificationStream;
20 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.w3c.dom.Document;
24 abstract class NotificationFormatter extends EventFormatter<DOMNotification> {
25 private static final String CREATE_NOTIFICATION_STREAM_ELEMENT = CreateNotificationStream.QNAME.getLocalName();
26 private static final String CREATE_NOTIFICATION_STREAM_NS =
27 CreateNotificationStream.QNAME.getNamespace().toString();
29 NotificationFormatter(final TextParameters textParams) {
33 NotificationFormatter(final TextParameters textParams, final String xpathFilter) throws XPathExpressionException {
34 super(textParams, xpathFilter);
38 protected final void fillDocument(final Document doc, final EffectiveModelContext schemaContext,
39 final DOMNotification input) throws IOException {
40 final var notificationElement = createNotificationElement(doc,
41 input instanceof DOMEvent domEvent ? domEvent.getEventInstant() : Instant.now());
42 // FIXME: what is this really?!
43 final var notificationEventElement = doc.createElementNS(CREATE_NOTIFICATION_STREAM_NS,
44 CREATE_NOTIFICATION_STREAM_ELEMENT);
45 final var dataElement = doc.createElement("notification");
47 final var writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(new DOMResult(dataElement));
49 writeBody(XMLStreamNormalizedNodeStreamWriter.create(writer, schemaContext, input.getType()),
54 } catch (final XMLStreamException e) {
55 throw new IOException("Failed to write notification content", e);
57 notificationElement.appendChild(notificationEventElement);
58 doc.appendChild(notificationElement);