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.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;
27 public abstract class NotificationFormatter extends EventFormatter<DOMNotification> {
28 protected static final XMLOutputFactory XML_OUTPUT_FACTORY;
31 XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
32 XML_OUTPUT_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
35 NotificationFormatter() {
39 public NotificationFormatter(final String xpathFilter) throws XPathExpressionException {
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",
47 final Element eventTimeElement = doc.createElement("eventTime");
48 if (input instanceof DOMEvent) {
49 eventTimeElement.setTextContent(toRFC3339(((DOMEvent) input).getEventInstant()));
51 eventTimeElement.setTextContent(toRFC3339(Instant.now()));
53 notificationElement.appendChild(eventTimeElement);
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);
60 final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
62 writeNotificationBody(XMLStreamNormalizedNodeStreamWriter.create(writer, schemaContext,
63 input.getType()), input.getBody());
67 } catch (final XMLStreamException e) {
68 throw new IOException("Failed to write notification content", e);
70 notificationElement.appendChild(notificationEventElement);
71 doc.appendChild(notificationElement);
74 static void writeNotificationBody(final NormalizedNodeStreamWriter writer, final ContainerNode body)
76 try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(writer)) {
77 nodeWriter.write(body);