Introduce restconf.server.{api,spi,mdsal}
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / mdsal / streams / notif / XMLNotificationFormatter.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.server.mdsal.streams.notif;
9
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;
21
22 final class XMLNotificationFormatter extends NotificationFormatter {
23     @VisibleForTesting
24     static final XMLNotificationFormatter EMPTY = new XMLNotificationFormatter(TextParameters.EMPTY);
25     static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory(EMPTY) {
26         @Override
27         public XMLNotificationFormatter newFormatter(final TextParameters textParams) {
28             return new XMLNotificationFormatter(textParams);
29         }
30
31         @Override
32         public XMLNotificationFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
33                 throws XPathExpressionException {
34             return new XMLNotificationFormatter(textParams, xpathFilter);
35         }
36     };
37
38     XMLNotificationFormatter(final TextParameters textParams) {
39         super(textParams);
40     }
41
42     XMLNotificationFormatter(final TextParameters textParams, final String xpathFilter)
43             throws XPathExpressionException {
44         super(textParams, xpathFilter);
45     }
46
47     @Override
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();
51
52         try {
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());
57                 nnWriter.flush();
58
59                 xmlStreamWriter.writeEndElement();
60                 xmlStreamWriter.writeEndDocument();
61                 xmlStreamWriter.flush();
62             }
63         } catch (XMLStreamException e) {
64             throw new IOException("Failed to write notification content", e);
65         }
66
67         return writer.toString();
68     }
69 }