Integrate AbstractCommonSubscriber more tightly
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / ListenerAdapter.java
index ddc573b95cc5fbcff8123de89f142a2ba826ab50..fe82e96ddfe67ec725ca2bbdec8ae7acfbaff44f 100644 (file)
@@ -7,22 +7,16 @@
  */
 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.Collection;
 import java.util.Optional;
 import java.util.stream.Collectors;
-import javax.xml.xpath.XPathExpressionException;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
-import org.opendaylight.restconf.common.formatters.DataTreeCandidateFormatter;
 import org.opendaylight.restconf.common.formatters.DataTreeCandidateFormatterFactory;
 import org.opendaylight.restconf.common.formatters.JSONDataTreeCandidateFormatter;
 import org.opendaylight.restconf.common.formatters.XMLDataTreeCandidateFormatter;
@@ -36,18 +30,12 @@ import org.slf4j.LoggerFactory;
 /**
  * {@link ListenerAdapter} is responsible to track events, which occurred by changing data in data source.
  */
-public class ListenerAdapter extends AbstractCommonSubscriber implements ClusteredDOMDataTreeChangeListener {
+public class ListenerAdapter extends AbstractCommonSubscriber<YangInstanceIdentifier, Collection<DataTreeCandidate>>
+        implements ClusteredDOMDataTreeChangeListener {
     private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapter.class);
-    private static final String PATH = "path";
     private static final DataTreeCandidateFormatterFactory JSON_FORMATTER_FACTORY =
             JSONDataTreeCandidateFormatter.createFactory(JSONCodecFactorySupplier.RFC7951);
 
-    private final YangInstanceIdentifier path;
-    private final String streamName;
-    private final NotificationOutputType outputType;
-
-    @VisibleForTesting DataTreeCandidateFormatter formatter;
-
     /**
      * Creates new {@link ListenerAdapter} listener specified by path and stream name and register for subscribing.
      *
@@ -55,19 +43,13 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster
      * @param streamName The name of the stream.
      * @param outputType Type of output on notification (JSON, XML).
      */
-    ListenerAdapter(final YangInstanceIdentifier path, final String streamName,
+    @VisibleForTesting
+    public ListenerAdapter(final YangInstanceIdentifier path, final String streamName,
             final NotificationOutputType outputType) {
-        setLocalNameOfPath(path.getLastPathArgument().getNodeType().getLocalName());
-
-        this.outputType = requireNonNull(outputType);
-        this.path = requireNonNull(path);
-        this.streamName = requireNonNull(streamName);
-        checkArgument(!streamName.isEmpty());
-
-        formatter = getFormatterFactory().getFormatter();
+        super(path.getLastPathArgument().getNodeType(), streamName, path, outputType, getFormatterFactory(outputType));
     }
 
-    private DataTreeCandidateFormatterFactory getFormatterFactory() {
+    private static DataTreeCandidateFormatterFactory getFormatterFactory(final NotificationOutputType outputType) {
         switch (outputType) {
             case JSON:
                 return JSON_FORMATTER_FACTORY;
@@ -78,12 +60,6 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster
         }
     }
 
-    @Override
-    final void setFilter(final String filter) throws XPathExpressionException {
-        final DataTreeCandidateFormatterFactory factory = getFormatterFactory();
-        formatter = filter == null || filter.isEmpty() ? factory.getFormatter() : factory.getFormatter(filter);
-    }
-
     @Override
     public void onInitialData() {
         // No-op
@@ -99,7 +75,7 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster
 
         final Optional<String> maybeData;
         try {
-            maybeData = formatter.eventData(schemaHandler.get(), dataTreeCandidates, now, getLeafNodesOnly(),
+            maybeData = formatter().eventData(schemaHandler.get(), dataTreeCandidates, now, getLeafNodesOnly(),
                     isSkipNotificationData());
         } catch (final Exception e) {
             LOG.error("Failed to process notification {}",
@@ -112,28 +88,13 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster
         }
     }
 
-    /**
-     * Gets the name of the stream.
-     *
-     * @return The name of the stream.
-     */
-    @Override
-    public String getStreamName() {
-        return streamName;
-    }
-
-    @Override
-    public String getOutputType() {
-        return outputType.getName();
-    }
-
     /**
      * Get path pointed to data in data store.
      *
      * @return Path pointed to data in data store.
      */
     public YangInstanceIdentifier getPath() {
-        return path;
+        return path();
     }
 
     /**
@@ -154,13 +115,4 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster
                 new DOMDataTreeIdentifier(datastore, getPath()), this));
         }
     }
-
-    @Override
-    public String toString() {
-        return MoreObjects.toStringHelper(this)
-                .add(PATH, path)
-                .add("stream-name", streamName)
-                .add("output-type", outputType)
-                .toString();
-    }
 }