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 com.google.common.annotations.VisibleForTesting;
11 import java.io.IOException;
12 import java.io.StringWriter;
13 import java.time.Instant;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.xpath.XPathExpressionException;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.restconf.server.spi.TextParameters;
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 {
24 static final XMLNotificationFormatter EMPTY = new XMLNotificationFormatter(TextParameters.EMPTY);
25 static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory(EMPTY) {
27 public XMLNotificationFormatter newFormatter(final TextParameters textParams) {
28 return new XMLNotificationFormatter(textParams);
32 public XMLNotificationFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
33 throws XPathExpressionException {
34 return new XMLNotificationFormatter(textParams, xpathFilter);
38 XMLNotificationFormatter(final TextParameters textParams) {
42 XMLNotificationFormatter(final TextParameters textParams, final String xpathFilter)
43 throws XPathExpressionException {
44 super(textParams, xpathFilter);
48 protected String createText(final TextParameters params, final EffectiveModelContext schemaContext,
49 final DOMNotification input, final Instant now) throws IOException {
50 final var writer = new StringWriter();
53 final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
54 try (var nnWriter = NormalizedNodeWriter.forStreamWriter(XMLStreamNormalizedNodeStreamWriter.create(
55 xmlStreamWriter, schemaContext, input.getType()))) {
56 nnWriter.write(input.getBody());
59 xmlStreamWriter.writeEndElement();
60 xmlStreamWriter.writeEndDocument();
61 xmlStreamWriter.flush();
63 } catch (XMLStreamException e) {
64 throw new IOException("Failed to write notification content", e);
67 return writer.toString();