49381fdb34067e1e251859a82b93fcd80234fa54
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / formatters / NotificationFormatter.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.common.formatters;
9
10 import java.io.IOException;
11 import java.time.Instant;
12 import javax.xml.stream.XMLOutputFactory;
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.stream.XMLStreamWriter;
15 import javax.xml.transform.dom.DOMResult;
16 import javax.xml.xpath.XPathExpressionException;
17 import org.opendaylight.mdsal.dom.api.DOMEvent;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
22 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 public abstract class NotificationFormatter extends EventFormatter<DOMNotification> {
28     protected static final XMLOutputFactory XML_OUTPUT_FACTORY;
29
30     static {
31         XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
32         XML_OUTPUT_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
33     }
34
35     NotificationFormatter() {
36
37     }
38
39     public NotificationFormatter(final String xpathFilter) throws XPathExpressionException {
40         super(xpathFilter);
41     }
42
43     @Override
44     void fillDocument(Document doc, EffectiveModelContext schemaContext, DOMNotification input) throws IOException {
45         final Element notificationElement = doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0",
46                 "notification");
47         final Element eventTimeElement = doc.createElement("eventTime");
48         if (input instanceof DOMEvent) {
49             eventTimeElement.setTextContent(toRFC3339(((DOMEvent) input).getEventInstant()));
50         } else {
51             eventTimeElement.setTextContent(toRFC3339(Instant.now()));
52         }
53         notificationElement.appendChild(eventTimeElement);
54
55         final Element notificationEventElement = doc.createElementNS(
56                 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "create-notification-stream");
57         final Element dataElement = doc.createElement("notification");
58         final DOMResult result = new DOMResult(dataElement);
59         try {
60             final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
61             try {
62                 writeNotificationBody(XMLStreamNormalizedNodeStreamWriter.create(writer, schemaContext,
63                         input.getType()), input.getBody());
64             } finally {
65                 writer.close();
66             }
67         } catch (final XMLStreamException e) {
68             throw new IOException("Failed to write notification content", e);
69         }
70         notificationElement.appendChild(notificationEventElement);
71         doc.appendChild(notificationElement);
72     }
73
74     static void writeNotificationBody(final NormalizedNodeStreamWriter writer, final ContainerNode body)
75             throws IOException {
76         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(writer)) {
77             nodeWriter.write(body);
78         }
79     }
80 }