15db6deab1fd3fe399aa001ce219f2a558b9773f
[netconf.git] /
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.nb.rfc8040.streams;
9
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.xpath.XPathExpressionException;
15 import org.opendaylight.mdsal.dom.api.DOMNotification;
16 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
17 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19
20 final class XMLNotificationFormatter extends NotificationFormatter {
21     private static final XMLNotificationFormatter EMPTY = new XMLNotificationFormatter(TextParameters.EMPTY);
22
23     static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory(EMPTY) {
24         @Override
25         XMLNotificationFormatter newFormatter(final TextParameters textParams) {
26             return new XMLNotificationFormatter(textParams);
27         }
28
29         @Override
30         XMLNotificationFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
31                 throws XPathExpressionException {
32             return new XMLNotificationFormatter(textParams, xpathFilter);
33         }
34     };
35
36     XMLNotificationFormatter(final TextParameters textParams) {
37         super(textParams);
38     }
39
40     XMLNotificationFormatter(final TextParameters textParams, final String xpathFilter)
41             throws XPathExpressionException {
42         super(textParams, xpathFilter);
43     }
44
45     @Override
46     String createText(final TextParameters params, final EffectiveModelContext schemaContext,
47             final DOMNotification input, final Instant now) throws IOException {
48         final var writer = new StringWriter();
49
50         try {
51             final var xmlStreamWriter = NotificationFormatter.createStreamWriterWithNotification(writer, now);
52             final var nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schemaContext,
53                 input.getType());
54
55             try (var nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter)) {
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 }