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.common.formatters;
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 public 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 public static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory() {
30 public XMLNotificationFormatter getFormatter(final String xpathFilter) throws XPathExpressionException {
31 return new XMLNotificationFormatter(xpathFilter);
35 public XMLNotificationFormatter getFormatter() {
40 public XMLNotificationFormatter() {
43 public XMLNotificationFormatter(String xpathFilter) throws XPathExpressionException {
48 String createText(EffectiveModelContext schemaContext, DOMNotification input, Instant now,
49 boolean leafNodesOnly, boolean skipData)
51 final StringWriter writer = new StringWriter();
53 final XMLStreamWriter xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(writer);
54 xmlStreamWriter.setDefaultNamespace(NOTIFICATION_NAMESPACE);
56 xmlStreamWriter.writeStartElement(NOTIFICATION_NAMESPACE, NOTIFICATION_ELEMENT);
57 xmlStreamWriter.writeDefaultNamespace(NOTIFICATION_NAMESPACE);
59 xmlStreamWriter.writeStartElement("eventTime");
60 xmlStreamWriter.writeCharacters(toRFC3339(now));
61 xmlStreamWriter.writeEndElement();
63 final NormalizedNodeStreamWriter nnStreamWriter =
64 XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext, input.getType());
66 final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter);
67 nnWriter.write(input.getBody());
70 xmlStreamWriter.writeEndElement();
71 xmlStreamWriter.writeEndDocument();
72 xmlStreamWriter.flush();
76 return writer.toString();
77 } catch (XMLStreamException e) {
78 throw new IOException("Failed to write notification content", e);