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.nb.rfc8040.streams.listeners;
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;
22 final class XMLNotificationFormatter extends NotificationFormatter {
23 private static final XMLNotificationFormatter INSTANCE = new XMLNotificationFormatter();
25 static final String NOTIFICATION_NAMESPACE = "urn:ietf:params:xml:ns:netconf:notification:1.0";
26 static final String NOTIFICATION_ELEMENT = "notification";
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";
31 static final String DATA_CHANGE_EVENT_ELEMENT = "data-change-event";
33 static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory() {
35 public XMLNotificationFormatter getFormatter(final String xpathFilter) throws XPathExpressionException {
36 return new XMLNotificationFormatter(xpathFilter);
40 public XMLNotificationFormatter getFormatter() {
45 XMLNotificationFormatter() {
49 XMLNotificationFormatter(final String xpathFilter) throws XPathExpressionException {
54 String createText(final EffectiveModelContext schemaContext, final DOMNotification input, final Instant now,
55 final boolean leafNodesOnly, final boolean skipData)
57 final StringWriter writer = new StringWriter();
59 final XMLStreamWriter xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(writer);
60 xmlStreamWriter.setDefaultNamespace(NOTIFICATION_NAMESPACE);
62 xmlStreamWriter.writeStartElement(NOTIFICATION_NAMESPACE, NOTIFICATION_ELEMENT);
63 xmlStreamWriter.writeDefaultNamespace(NOTIFICATION_NAMESPACE);
65 xmlStreamWriter.writeStartElement("eventTime");
66 xmlStreamWriter.writeCharacters(toRFC3339(now));
67 xmlStreamWriter.writeEndElement();
69 final NormalizedNodeStreamWriter nnStreamWriter =
70 XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext, input.getType());
72 final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter);
73 nnWriter.write(input.getBody());
76 xmlStreamWriter.writeEndElement();
77 xmlStreamWriter.writeEndDocument();
78 xmlStreamWriter.flush();
82 return writer.toString();
83 } catch (XMLStreamException e) {
84 throw new IOException("Failed to write notification content", e);