Modernize notification extraction 78/103978/4
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 10 Jan 2023 23:49:18 +0000 (00:49 +0100)
committerRobert Varga <nite@hq.sk>
Mon, 30 Jan 2023 15:00:15 +0000 (15:00 +0000)
Extract-transform-verify arguments before talking to anyone, making it
obvious the definitions do not actually leak.

Change-Id: I46c079a08d72bb9f9847a7766bc3025a932ae7b4
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

index ab8bc3d4dd0fea990f61acd50919c10aa31018c6..e2800435e03c17c16b1f3ae9854eac701e77c0a1 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableSet;
-import java.util.Collection;
 import java.util.Set;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.Nullable;
@@ -49,6 +48,7 @@ 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;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -178,20 +178,19 @@ final class CreateStreamUtil {
             .orElseThrow(() -> new RestconfDocumentedException("Mount point schema not available",
                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED))
             .getGlobalContext();
-        final Collection<? extends NotificationDefinition> notifications = mountModelContext.getNotifications();
-        if (notifications.isEmpty()) {
+        final Set<Absolute> notificationPaths = mountModelContext.getModuleStatements().values().stream()
+            .flatMap(module -> module.streamEffectiveSubstatements(NotificationEffectiveStatement.class))
+            .map(notification -> Absolute.of(notification.argument()))
+            .collect(Collectors.toUnmodifiableSet());
+        if (notificationPaths.isEmpty()) {
             throw new RestconfDocumentedException("Device does not support notification", ErrorType.APPLICATION,
                 ErrorTag.OPERATION_FAILED);
         }
 
-        final Set<Absolute> absolutes = notifications.stream()
-            .map(notificationDefinition -> Absolute.of(notificationDefinition.getQName()))
-            .collect(Collectors.toUnmodifiableSet());
-
         final DeviceNotificationListenerAdaptor notificationListenerAdapter = ListenersBroker.getInstance()
             .registerDeviceNotificationListener(deviceName, prepareOutputType(data), mountModelContext,
                 mountPointService, mountPoint.getIdentifier());
-        notificationListenerAdapter.listen(mountNotifService, absolutes);
+        notificationListenerAdapter.listen(mountNotifService, notificationPaths);
 
         // building of output
         return new DefaultDOMRpcResult(Builders.containerBuilder()