Use random UUIDs for stream names
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / AbstractNotificationListenerAdaptor.java
index acca7d2fc1cba46a2b9d701cee04ffa4be962858..34511f1c73331398b72acb6c1170958da92a023f 100644 (file)
@@ -8,13 +8,11 @@
 package org.opendaylight.restconf.nb.rfc8040.streams;
 
 import java.time.Instant;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMEvent;
 import org.opendaylight.mdsal.dom.api.DOMNotification;
 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
 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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -23,11 +21,9 @@ import org.slf4j.LoggerFactory;
  * Abstract base class for functionality shared between {@link NotificationListenerAdapter} and
  * {@link DeviceNotificationListenerAdaptor}.
  */
-abstract class AbstractNotificationListenerAdaptor extends AbstractCommonSubscriber<DOMNotification>
+abstract class AbstractNotificationListenerAdaptor extends AbstractStream<DOMNotification>
         implements DOMNotificationListener {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationListenerAdaptor.class);
-    private static final NotificationFormatterFactory JSON_FORMATTER_FACTORY =
-        JSONNotificationFormatter.createFactory(JSONCodecFactorySupplier.RFC7951);
 
     AbstractNotificationListenerAdaptor(final String streamName, final NotificationOutputType outputType,
             final ListenersBroker listenersBroker) {
@@ -36,7 +32,7 @@ abstract class AbstractNotificationListenerAdaptor extends AbstractCommonSubscri
 
     private static NotificationFormatterFactory getFormatterFactory(final NotificationOutputType outputType) {
         return switch (outputType) {
-            case JSON -> JSON_FORMATTER_FACTORY;
+            case JSON -> JSONNotificationFormatter.FACTORY;
             case XML -> XMLNotificationFormatter.FACTORY;
         };
     }
@@ -45,14 +41,16 @@ abstract class AbstractNotificationListenerAdaptor extends AbstractCommonSubscri
     @SuppressWarnings("checkstyle:IllegalCatch")
     public final void onNotification(final DOMNotification notification) {
         final var eventInstant = notification instanceof DOMEvent domEvent ? domEvent.getEventInstant() : Instant.now();
-        final Optional<String> maybeOutput;
+        final String data;
         try {
-            maybeOutput = formatter().eventData(effectiveModel(), notification, eventInstant);
+            data = formatter().eventData(effectiveModel(), notification, eventInstant);
         } catch (Exception e) {
             LOG.error("Failed to process notification {}", notification, e);
             return;
         }
-        maybeOutput.ifPresent(this::post);
+        if (data != null) {
+            post(data);
+        }
     }
 
     abstract @NonNull EffectiveModelContext effectiveModel();