Move createYangNotifiStream() 76/107076/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Jul 2023 10:48:19 +0000 (12:48 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Jul 2023 10:48:19 +0000 (12:48 +0200)
This method has a single caller, which can provide proper context,
allowing use to simplify the task of creating the stream name.

Change-Id: Ib03450988c93913d737e094ac5b67a596d8784c7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/CreateStreamUtil.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java

index 3c96b213ac22af8e42b08f5d4e1602bd141e467b..4715e07c5c573c21d58e5b303e02032300a6d7de 100644 (file)
@@ -23,7 +23,6 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.DeviceNotificationListenerAdaptor;
 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
-import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev221106.SubscribeDeviceNotificationInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev221106.SubscribeDeviceNotificationOutput;
@@ -45,8 +44,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
@@ -254,45 +251,4 @@ final class CreateStreamUtil {
         return data.childByArg(childName) instanceof LeafNode<?> leafNode && leafNode.body() instanceof String str
             ? str : null;
     }
-
-    /**
-     * Create YANG notification stream using notification definition in YANG schema.
-     *
-     * @param notificationDefinition YANG notification definition.
-     * @param refSchemaCtx           Reference to {@link EffectiveModelContext}
-     * @param outputType             Output type (XML or JSON).
-     * @return {@link NotificationListenerAdapter}
-     */
-    static NotificationListenerAdapter createYangNotifiStream(final NotificationDefinition notificationDefinition,
-            final EffectiveModelContext refSchemaCtx, final NotificationOutputType outputType) {
-        final var streamName = parseNotificationStreamName(requireNonNull(notificationDefinition),
-                requireNonNull(refSchemaCtx), requireNonNull(outputType));
-        final var listenersBroker = ListenersBroker.getInstance();
-
-        final var existing = listenersBroker.notificationListenerFor(streamName);
-        return existing != null ? existing
-            : listenersBroker.registerNotificationListener(
-                Absolute.of(notificationDefinition.getQName()), streamName, outputType);
-    }
-
-    private static String parseNotificationStreamName(final NotificationDefinition notificationDefinition,
-            final EffectiveModelContext refSchemaCtx, final NotificationOutputType outputType) {
-        final QName notificationDefinitionQName = notificationDefinition.getQName();
-        final Module module = refSchemaCtx.findModule(
-                notificationDefinitionQName.getModule().getNamespace(),
-                notificationDefinitionQName.getModule().getRevision()).orElse(null);
-        requireNonNull(module, String.format("Module for namespace %s does not exist.",
-                notificationDefinitionQName.getModule().getNamespace()));
-
-        final StringBuilder streamNameBuilder = new StringBuilder();
-        streamNameBuilder.append(RestconfStreamsConstants.NOTIFICATION_STREAM)
-                .append('/')
-                .append(module.getName())
-                .append(':')
-                .append(notificationDefinitionQName.getLocalName());
-        if (outputType != NotificationOutputType.XML) {
-            streamNameBuilder.append('/').append(outputType.getName());
-        }
-        return streamNameBuilder.toString();
-    }
 }
index a51fac23fffceeb0a421471567a370abf467e0c0..b65ff44b2c0ad6ca8d48148134a6a4a6db692d71 100644 (file)
@@ -39,7 +39,6 @@ import org.opendaylight.mdsal.dom.api.DOMActionService;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteOperations;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
@@ -64,7 +63,9 @@ import org.opendaylight.restconf.nb.rfc8040.rests.utils.PlainPatchDataTransactio
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PutDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.ReadDataTransactionUtil;
+import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
+import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter;
 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
@@ -83,9 +84,9 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -184,15 +185,21 @@ public class RestconfDataServiceImpl implements RestconfDataService {
     }
 
     private void createAllYangNotificationStreams(final EffectiveModelContext schemaContext, final UriInfo uriInfo) {
-        final DOMDataTreeWriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
-        for (final NotificationDefinition notificationDefinition : schemaContext.getNotifications()) {
-            writeNotificationStreamToDatastore(schemaContext, uriInfo, transaction,
-                CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContext,
-                    NotificationOutputType.XML));
-            writeNotificationStreamToDatastore(schemaContext, uriInfo, transaction,
-                CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContext,
-                    NotificationOutputType.JSON));
+        final var transaction = dataBroker.newWriteOnlyTransaction();
+
+        for (var module : schemaContext.getModuleStatements().values()) {
+            final var moduleName = module.argument().getLocalName();
+            // Note: this handles only RFC6020 notifications
+            module.streamEffectiveSubstatements(NotificationEffectiveStatement.class).forEach(notification -> {
+                final var notifName = notification.argument();
+
+                writeNotificationStreamToDatastore(schemaContext, uriInfo, transaction,
+                    createYangNotifiStream(moduleName, notifName, NotificationOutputType.XML));
+                writeNotificationStreamToDatastore(schemaContext, uriInfo, transaction,
+                    createYangNotifiStream(moduleName, notifName, NotificationOutputType.JSON));
+            });
         }
+
         try {
             transaction.commit().get();
         } catch (final InterruptedException | ExecutionException e) {
@@ -200,6 +207,27 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         }
     }
 
+    private static NotificationListenerAdapter createYangNotifiStream(final String moduleName, final QName notifName,
+            final NotificationOutputType outputType) {
+        final var streamName = createNotificationStreamName(moduleName, notifName.getLocalName(), outputType);
+        final var listenersBroker = ListenersBroker.getInstance();
+
+        final var existing = listenersBroker.notificationListenerFor(streamName);
+        return existing != null ? existing
+            : listenersBroker.registerNotificationListener(Absolute.of(notifName), streamName, outputType);
+    }
+
+    private static String createNotificationStreamName(final String moduleName, final String notifName,
+            final NotificationOutputType outputType) {
+        final var sb = new StringBuilder()
+            .append(RestconfStreamsConstants.NOTIFICATION_STREAM)
+            .append('/').append(moduleName).append(':').append(notifName);
+        if (outputType != NotificationOutputType.XML) {
+            sb.append('/').append(outputType.getName());
+        }
+        return sb.toString();
+    }
+
     private void writeNotificationStreamToDatastore(final EffectiveModelContext schemaContext,
             final UriInfo uriInfo, final DOMDataTreeWriteOperations tx, final NotificationListenerAdapter listener) {
         final URI uri = streamUtils.prepareUriByStreamName(uriInfo, listener.getStreamName());