Netconf Device Notification
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / NotificationListenerAdapter.java
index 5f43481862a82f478b325cb7e6802cca4a2ab2bb..f0fa51af769ebca0d61bda3a2aaf3ddedcb2068b 100644 (file)
@@ -7,26 +7,19 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
 
-import java.time.Instant;
-import java.util.Optional;
-import org.opendaylight.mdsal.dom.api.DOMNotification;
-import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects.ToStringHelper;
 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
 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.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * {@link NotificationListenerAdapter} is responsible to track events on notifications.
  */
-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);
+public final class NotificationListenerAdapter extends AbstractNotificationListenerAdaptor {
+    private final Absolute path;
 
     /**
      * Set path of listener and stream name.
@@ -36,39 +29,13 @@ public final class NotificationListenerAdapter extends AbstractCommonSubscriber<
      * @param outputType Type of output on notification (JSON or XML).
      */
     NotificationListenerAdapter(final Absolute path, final String streamName, final NotificationOutputType outputType) {
-        super(path.lastNodeIdentifier(), streamName, path, outputType, getFormatterFactory(outputType));
-    }
-
-    private static NotificationFormatterFactory getFormatterFactory(final NotificationOutputType outputType) {
-        switch (outputType) {
-            case JSON:
-                return JSON_FORMATTER_FACTORY;
-            case XML:
-                return XMLNotificationFormatter.FACTORY;
-            default:
-                throw new IllegalArgumentException("Unsupported outputType " + outputType);
-        }
+        super(path.lastNodeIdentifier(), streamName, outputType);
+        this.path = requireNonNull(path);
     }
 
     @Override
-    @SuppressWarnings("checkstyle:IllegalCatch")
-    public void onNotification(final DOMNotification notification) {
-        final Instant now = Instant.now();
-        if (!checkStartStop(now)) {
-            return;
-        }
-
-        final Optional<String> maybeOutput;
-        try {
-            maybeOutput = formatter().eventData(schemaHandler.get(), notification, now, getLeafNodesOnly(),
-                    isSkipNotificationData());
-        } catch (Exception e) {
-            LOG.error("Failed to process notification {}", notification, e);
-            return;
-        }
-        if (maybeOutput.isPresent()) {
-            post(maybeOutput.get());
-        }
+    EffectiveModelContext effectiveModel() {
+        return databindProvider.currentContext().modelContext();
     }
 
     /**
@@ -77,7 +44,7 @@ public final class NotificationListenerAdapter extends AbstractCommonSubscriber<
      * @return The configured schema path that points to observing YANG notification schema node.
      */
     public Absolute getSchemaPath() {
-        return path();
+        return path;
     }
 
     public synchronized void listen(final DOMNotificationService notificationService) {
@@ -85,4 +52,9 @@ public final class NotificationListenerAdapter extends AbstractCommonSubscriber<
             setRegistration(notificationService.registerNotificationListener(this, getSchemaPath()));
         }
     }
+
+    @Override
+    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return super.addToStringAttributes(helper.add("path", path));
+    }
 }