Eliminate NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / EventFormatter.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.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.io.IOException;
14 import java.io.Writer;
15 import java.time.Instant;
16 import java.time.OffsetDateTime;
17 import java.time.ZoneId;
18 import java.time.format.DateTimeFormatter;
19 import javax.xml.XMLConstants;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.stream.XMLOutputFactory;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import javax.xml.xpath.XPath;
26 import javax.xml.xpath.XPathConstants;
27 import javax.xml.xpath.XPathExpression;
28 import javax.xml.xpath.XPathExpressionException;
29 import javax.xml.xpath.XPathFactory;
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.opendaylight.netconf.api.NamespaceURN;
33 import org.opendaylight.yangtools.concepts.Immutable;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40
41 public abstract class EventFormatter<T> implements Immutable {
42     private static final XPathFactory XPF = XPathFactory.newInstance();
43
44     // FIXME: NETCONF-369: XPath operates without namespace context, therefore we need an namespace-unaware builder.
45     //        Once it is fixed we can use UntrustedXML instead.
46     private static final @NonNull DocumentBuilderFactory DBF;
47
48     static {
49         final DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
50         f.setCoalescing(true);
51         f.setExpandEntityReferences(false);
52         f.setIgnoringElementContentWhitespace(true);
53         f.setIgnoringComments(true);
54         f.setXIncludeAware(false);
55         try {
56             f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
57             f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
58             f.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
59             f.setFeature("http://xml.org/sax/features/external-general-entities", false);
60             f.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
61         } catch (final ParserConfigurationException e) {
62             throw new ExceptionInInitializerError(e);
63         }
64         DBF = f;
65     }
66
67     protected static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
68
69     private final TextParameters textParams;
70     private final XPathExpression filter;
71
72     protected EventFormatter(final TextParameters textParams)  {
73         this.textParams = requireNonNull(textParams);
74         filter = null;
75     }
76
77     protected EventFormatter(final TextParameters params, final String xpathFilter) throws XPathExpressionException {
78         textParams = requireNonNull(params);
79
80         final XPath xpath;
81         synchronized (XPF) {
82             xpath = XPF.newXPath();
83         }
84         // FIXME: NETCONF-369: we need to bind the namespace context here and for that we need the SchemaContext
85         filter = xpath.compile(xpathFilter);
86     }
87
88     @VisibleForTesting
89     public final @Nullable String eventData(final EffectiveModelContext schemaContext, final T input,
90             final Instant now) throws Exception {
91         return filterMatches(schemaContext, input, now) ? createText(textParams, schemaContext, input, now) : null;
92     }
93
94     /**
95      * Export the provided input into the provided document so we can verify whether a filter matches the content.
96      *
97      * @param doc the document to fill
98      * @param schemaContext context to use for the export
99      * @param input data to export
100      * @throws IOException if any IOException occurs during export to the document
101      */
102     protected abstract void fillDocument(Document doc, EffectiveModelContext schemaContext, T input) throws IOException;
103
104     /**
105      * Format the input data into string representation of the data provided.
106      *
107      * @param params output text parameters
108      * @param schemaContext context to use for the export
109      * @param input input data
110      * @param now time the event happened
111      * @return String representation of the formatted data
112      * @throws Exception if the underlying formatters fail to export the data to the requested format
113      */
114     protected abstract String createText(TextParameters params, EffectiveModelContext schemaContext, T input,
115         Instant now) throws Exception;
116
117     private boolean filterMatches(final EffectiveModelContext schemaContext, final T input, final Instant now)
118             throws IOException {
119         if (filter == null) {
120             return true;
121         }
122
123         final Document doc;
124         try {
125             doc = DBF.newDocumentBuilder().newDocument();
126         } catch (final ParserConfigurationException e) {
127             throw new IOException("Failed to create a new document", e);
128         }
129         fillDocument(doc, schemaContext, input);
130
131         final Boolean eval;
132         try {
133             eval = (Boolean) filter.evaluate(doc, XPathConstants.BOOLEAN);
134         } catch (final XPathExpressionException e) {
135             throw new IllegalStateException("Failed to evaluate expression " + filter, e);
136         }
137
138         return eval;
139     }
140
141     /**
142      * Formats data specified by RFC3339.
143      *
144      * @param now time stamp
145      * @return Data specified by RFC3339.
146      */
147     protected static final String toRFC3339(final Instant now) {
148         return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OffsetDateTime.ofInstant(now, ZoneId.systemDefault()));
149     }
150
151     protected static final @NonNull Element createNotificationElement(final Document doc, final Instant now) {
152         final var notificationElement = doc.createElementNS(NamespaceURN.NOTIFICATION, "notification");
153         final var eventTimeElement = doc.createElement("eventTime");
154         eventTimeElement.setTextContent(toRFC3339(now));
155         notificationElement.appendChild(eventTimeElement);
156         return notificationElement;
157     }
158
159     protected static final @NonNull XMLStreamWriter createStreamWriterWithNotification(final Writer writer,
160             final Instant now) throws XMLStreamException {
161         final var xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(writer);
162         xmlStreamWriter.setDefaultNamespace(NamespaceURN.NOTIFICATION);
163
164         xmlStreamWriter.writeStartElement(NamespaceURN.NOTIFICATION, "notification");
165         xmlStreamWriter.writeDefaultNamespace(NamespaceURN.NOTIFICATION);
166
167         xmlStreamWriter.writeStartElement("eventTime");
168         xmlStreamWriter.writeCharacters(toRFC3339(now));
169         xmlStreamWriter.writeEndElement();
170         return xmlStreamWriter;
171     }
172
173     protected static final void writeBody(final NormalizedNodeStreamWriter writer, final NormalizedNode body)
174             throws IOException {
175         try (var nodeWriter = NormalizedNodeWriter.forStreamWriter(writer)) {
176             nodeWriter.write(body);
177         }
178     }
179 }