Move streams support classes
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / NotificationListenerAdapter.java
index c7c0963d3ad62ec20bdf24e2539268dfc9941ee5..5f43481862a82f478b325cb7e6802cca4a2ab2bb 100644 (file)
@@ -7,21 +7,11 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.MoreObjects;
 import java.time.Instant;
 import java.util.Optional;
-import javax.xml.xpath.XPathExpressionException;
 import org.opendaylight.mdsal.dom.api.DOMNotification;
 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
-import org.opendaylight.restconf.common.formatters.JSONNotificationFormatter;
-import org.opendaylight.restconf.common.formatters.NotificationFormatter;
-import org.opendaylight.restconf.common.formatters.NotificationFormatterFactory;
-import org.opendaylight.restconf.common.formatters.XMLNotificationFormatter;
 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
@@ -31,19 +21,13 @@ import org.slf4j.LoggerFactory;
 /**
  * {@link NotificationListenerAdapter} is responsible to track events on notifications.
  */
-public class NotificationListenerAdapter extends AbstractCommonSubscriber implements DOMNotificationListener {
+public final class NotificationListenerAdapter extends AbstractCommonSubscriber<Absolute, DOMNotification>
+        implements DOMNotificationListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class);
     private static final NotificationFormatterFactory JSON_FORMATTER_FACTORY = JSONNotificationFormatter.createFactory(
             JSONCodecFactorySupplier.RFC7951);
 
-    private final String streamName;
-    private final Absolute path;
-    private final NotificationOutputType outputType;
-
-    @VisibleForTesting NotificationFormatter formatter;
-
-
     /**
      * Set path of listener and stream name.
      *
@@ -51,19 +35,11 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
      * @param streamName Name of the stream.
      * @param outputType Type of output on notification (JSON or XML).
      */
-    NotificationListenerAdapter(final Absolute path, final String streamName, final String outputType) {
-        setLocalNameOfPath(path.lastNodeIdentifier().getLocalName());
-
-        this.outputType = NotificationOutputType.forName(requireNonNull(outputType)).get();
-        this.path = requireNonNull(path);
-        this.streamName = requireNonNull(streamName);
-        checkArgument(!streamName.isEmpty());
-        this.formatter = getFormatterFactory().getFormatter();
-
-        LOG.debug("output type: {}, {}", outputType, this.outputType);
+    NotificationListenerAdapter(final Absolute path, final String streamName, final NotificationOutputType outputType) {
+        super(path.lastNodeIdentifier(), streamName, path, outputType, getFormatterFactory(outputType));
     }
 
-    private NotificationFormatterFactory getFormatterFactory() {
+    private static NotificationFormatterFactory getFormatterFactory(final NotificationOutputType outputType) {
         switch (outputType) {
             case JSON:
                 return JSON_FORMATTER_FACTORY;
@@ -74,43 +50,17 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
         }
     }
 
-    private NotificationFormatter getFormatter(final String filter) throws XPathExpressionException {
-        NotificationFormatterFactory factory = getFormatterFactory();
-        return filter == null || filter.isEmpty() ? factory.getFormatter() : factory.getFormatter(filter);
-    }
-
-    @Override
-    public void setQueryParams(final Instant start, final Instant stop, final String filter,
-                               final boolean leafNodesOnly, final boolean skipNotificationData) {
-        super.setQueryParams(start, stop, filter, leafNodesOnly, skipNotificationData);
-        try {
-            this.formatter = getFormatter(filter);
-        } catch (XPathExpressionException e) {
-            throw new IllegalArgumentException("Failed to get filter", e);
-        }
-    }
-
-    /**
-     * Get output type of this listener.
-     *
-     * @return The configured output type (JSON or XML).
-     */
-    @Override
-    public String getOutputType() {
-        return this.outputType.getName();
-    }
-
     @Override
     @SuppressWarnings("checkstyle:IllegalCatch")
     public void onNotification(final DOMNotification notification) {
         final Instant now = Instant.now();
-        if (!checkStartStop(now, this)) {
+        if (!checkStartStop(now)) {
             return;
         }
 
         final Optional<String> maybeOutput;
         try {
-            maybeOutput = formatter.eventData(schemaHandler.get(), notification, now, getLeafNodesOnly(),
+            maybeOutput = formatter().eventData(schemaHandler.get(), notification, now, getLeafNodesOnly(),
                     isSkipNotificationData());
         } catch (Exception e) {
             LOG.error("Failed to process notification {}", notification, e);
@@ -121,37 +71,18 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem
         }
     }
 
-    /**
-     * Get stream name of this listener.
-     *
-     * @return The configured stream name.
-     */
-    @Override
-    public String getStreamName() {
-        return this.streamName;
-    }
-
     /**
      * Get schema path of notification.
      *
      * @return The configured schema path that points to observing YANG notification schema node.
      */
     public Absolute getSchemaPath() {
-        return this.path;
+        return path();
     }
 
-    public final synchronized void listen(final DOMNotificationService notificationService) {
+    public synchronized void listen(final DOMNotificationService notificationService) {
         if (!isListening()) {
             setRegistration(notificationService.registerNotificationListener(this, getSchemaPath()));
         }
     }
-
-    @Override
-    public String toString() {
-        return MoreObjects.toStringHelper(this)
-                .add("path", path)
-                .add("stream-name", streamName)
-                .add("output-type", outputType)
-                .toString();
-    }
 }