Do not acquire DOMSchemaService twice 75/103975/4
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 10 Jan 2023 23:27:13 +0000 (00:27 +0100)
committerRobert Varga <nite@hq.sk>
Mon, 30 Jan 2023 15:00:05 +0000 (15:00 +0000)
Properly sanitize input, exactly once. Prevents a 500 when there is no
DOMSchemaService exposed.

Change-Id: I304ba58efe82530186cbb5a28e0b9a79b138ff3e
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 e8c228ebfed8fd967fe55b780693799352128ac8..274d9660ddac9e7c84fc285b241c3b7e6eda2b5b 100644 (file)
@@ -165,22 +165,24 @@ final class CreateStreamUtil {
 
         final String deviceName = extractDeviceName(value);
         final NotificationOutputType outputType = prepareOutputType(data);
-        EffectiveModelContext effectiveModelContext = mountPoint.getService(DOMSchemaService.class).get()
+
+        // FIXME: what is the relationship to the unused refSchemaCtx?
+        final EffectiveModelContext mountModelContext = mountPoint.getService(DOMSchemaService.class)
+            .orElseThrow(() -> new RestconfDocumentedException("Mount point schema not available",
+                ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED))
             .getGlobalContext();
-        Collection<? extends NotificationDefinition> notificationDefinitions = mountPoint.getService(
-                DOMSchemaService.class).get().getGlobalContext()
-            .getNotifications();
-        if (notificationDefinitions == null || notificationDefinitions.isEmpty()) {
+        final Collection<? extends NotificationDefinition> notifications = mountModelContext.getNotifications();
+        if (notifications.isEmpty()) {
             throw new RestconfDocumentedException("Device does not support notification", ErrorType.APPLICATION,
                 ErrorTag.OPERATION_FAILED);
         }
 
-        Set<Absolute> absolutes = notificationDefinitions.stream()
+        final Set<Absolute> absolutes = notifications.stream()
             .map(notificationDefinition -> Absolute.of(notificationDefinition.getQName()))
             .collect(Collectors.toUnmodifiableSet());
 
         final DeviceNotificationListenerAdaptor notificationListenerAdapter = ListenersBroker.getInstance()
-            .registerDeviceNotificationListener(deviceName, outputType, effectiveModelContext, mountPointService,
+            .registerDeviceNotificationListener(deviceName, outputType, mountModelContext, mountPointService,
                 mountPoint.getIdentifier());
         notificationListenerAdapter.listen(mountPoint.getService(DOMNotificationService.class).get(), absolutes);