Introduce restconf.server.{api,spi,mdsal}
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / EventFormatter.java
similarity index 82%
rename from restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/EventFormatter.java
rename to restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/EventFormatter.java
index f1645736aca39ed4c43d0441e0aaea97e2254651..872eec8736182ef718ebe6357cdfa066c33b34c7 100644 (file)
@@ -5,10 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.restconf.nb.rfc8040.streams;
+package org.opendaylight.restconf.server.spi;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.io.IOException;
 import java.io.Writer;
 import java.time.Instant;
@@ -63,17 +64,17 @@ public abstract class EventFormatter<T> implements Immutable {
         DBF = f;
     }
 
-    static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
+    protected static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
 
     private final TextParameters textParams;
     private final XPathExpression filter;
 
-    EventFormatter(final TextParameters textParams)  {
+    protected EventFormatter(final TextParameters textParams)  {
         this.textParams = requireNonNull(textParams);
         filter = null;
     }
 
-    EventFormatter(final TextParameters params, final String xpathFilter) throws XPathExpressionException {
+    protected EventFormatter(final TextParameters params, final String xpathFilter) throws XPathExpressionException {
         textParams = requireNonNull(params);
 
         final XPath xpath;
@@ -84,8 +85,9 @@ public abstract class EventFormatter<T> implements Immutable {
         filter = xpath.compile(xpathFilter);
     }
 
-    final @Nullable String eventData(final EffectiveModelContext schemaContext, final T input, final Instant now)
-            throws Exception {
+    @VisibleForTesting
+    public final @Nullable String eventData(final EffectiveModelContext schemaContext, final T input,
+            final Instant now) throws Exception {
         return filterMatches(schemaContext, input, now) ? createText(textParams, schemaContext, input, now) : null;
     }
 
@@ -97,7 +99,7 @@ public abstract class EventFormatter<T> implements Immutable {
      * @param input data to export
      * @throws IOException if any IOException occurs during export to the document
      */
-    abstract void fillDocument(Document doc, EffectiveModelContext schemaContext, T input) throws IOException;
+    protected abstract void fillDocument(Document doc, EffectiveModelContext schemaContext, T input) throws IOException;
 
     /**
      * Format the input data into string representation of the data provided.
@@ -109,8 +111,8 @@ public abstract class EventFormatter<T> implements Immutable {
      * @return String representation of the formatted data
      * @throws Exception if the underlying formatters fail to export the data to the requested format
      */
-    abstract String createText(TextParameters params, EffectiveModelContext schemaContext, T input, Instant now)
-        throws Exception;
+    protected abstract String createText(TextParameters params, EffectiveModelContext schemaContext, T input,
+        Instant now) throws Exception;
 
     private boolean filterMatches(final EffectiveModelContext schemaContext, final T input, final Instant now)
             throws IOException {
@@ -142,11 +144,11 @@ public abstract class EventFormatter<T> implements Immutable {
      * @param now time stamp
      * @return Data specified by RFC3339.
      */
-    static final String toRFC3339(final Instant now) {
+    protected static final String toRFC3339(final Instant now) {
         return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OffsetDateTime.ofInstant(now, ZoneId.systemDefault()));
     }
 
-    static final @NonNull Element createNotificationElement(final Document doc, final Instant now) {
+    protected static final @NonNull Element createNotificationElement(final Document doc, final Instant now) {
         final var notificationElement = doc.createElementNS(NamespaceURN.NOTIFICATION, "notification");
         final var eventTimeElement = doc.createElement("eventTime");
         eventTimeElement.setTextContent(toRFC3339(now));
@@ -154,8 +156,8 @@ public abstract class EventFormatter<T> implements Immutable {
         return notificationElement;
     }
 
-    static final @NonNull XMLStreamWriter createStreamWriterWithNotification(final Writer writer, final Instant now)
-            throws XMLStreamException {
+    protected static final @NonNull XMLStreamWriter createStreamWriterWithNotification(final Writer writer,
+            final Instant now) throws XMLStreamException {
         final var xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(writer);
         xmlStreamWriter.setDefaultNamespace(NamespaceURN.NOTIFICATION);
 
@@ -168,7 +170,8 @@ public abstract class EventFormatter<T> implements Immutable {
         return xmlStreamWriter;
     }
 
-    static final void writeBody(final NormalizedNodeStreamWriter writer, final NormalizedNode body) throws IOException {
+    protected static final void writeBody(final NormalizedNodeStreamWriter writer, final NormalizedNode body)
+            throws IOException {
         try (var nodeWriter = NormalizedNodeWriter.forStreamWriter(writer)) {
             nodeWriter.write(body);
         }