Unmask NotificationListenerAdapter 99/96199/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 15 May 2021 13:16:45 +0000 (15:16 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 15 May 2021 13:34:35 +0000 (15:34 +0200)
Use .orElseThrow() to acquire a nice instance, without needing to issue
multiple .get()s.

JIRA: NETCONF-773
Change-Id: Ic499f6eee7f19cba1796e62cad47892b64a28b4c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/SubscribeToStreamUtil.java

index 1f8f2fdf59b88916ef61a053175562d01aa97020..553a59fe73c395e35f784b75ca937c73c8e0b001 100644 (file)
@@ -129,36 +129,32 @@ abstract class SubscribeToStreamUtil {
         if (Strings.isNullOrEmpty(streamName)) {
             throw new RestconfDocumentedException("Stream name is empty.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
-        final Optional<NotificationListenerAdapter> notificationListenerAdapter =
-                ListenersBroker.getInstance().getNotificationListenerFor(streamName);
 
-        if (notificationListenerAdapter.isEmpty()) {
-            throw new RestconfDocumentedException(String.format(
-                    "Stream with name %s was not found.", streamName),
-                    ErrorType.PROTOCOL,
-                    ErrorTag.UNKNOWN_ELEMENT);
-        }
+        final NotificationListenerAdapter notificationListenerAdapter = ListenersBroker.getInstance()
+            .getNotificationListenerFor(streamName)
+            .orElseThrow(() -> new RestconfDocumentedException(
+                String.format("Stream with name %s was not found.", streamName),
+                ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT));
 
         final DOMTransactionChain transactionChain = handlersHolder.getTransactionChainHandler().get();
         final DOMDataTreeReadWriteTransaction writeTransaction = transactionChain.newReadWriteTransaction();
         final EffectiveModelContext schemaContext = handlersHolder.getSchemaHandler().get();
 
         final URI uri = prepareUriByStreamName(uriInfo, streamName);
-        registerToListenNotification(
-                notificationListenerAdapter.get(), handlersHolder.getNotificationServiceHandler());
-        notificationListenerAdapter.get().setQueryParams(
+        registerToListenNotification(notificationListenerAdapter, handlersHolder.getNotificationServiceHandler());
+        notificationListenerAdapter.setQueryParams(
                 notificationQueryParams.getStart(),
                 notificationQueryParams.getStop().orElse(null),
                 notificationQueryParams.getFilter().orElse(null),
                 false, notificationQueryParams.isSkipNotificationData());
-        notificationListenerAdapter.get().setCloseVars(
+        notificationListenerAdapter.setCloseVars(
                 handlersHolder.getTransactionChainHandler(), handlersHolder.getSchemaHandler());
         final MapEntryNode mapToStreams = RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
-                    notificationListenerAdapter.get().getSchemaPath().lastNodeIdentifier(),
+                    notificationListenerAdapter.getSchemaPath().lastNodeIdentifier(),
                     schemaContext.getNotifications(), notificationQueryParams.getStart(),
-                    notificationListenerAdapter.get().getOutputType(), uri);
+                    notificationListenerAdapter.getOutputType(), uri);
         writeDataToDS(schemaContext,
-            notificationListenerAdapter.get().getSchemaPath().lastNodeIdentifier().getLocalName(), writeTransaction,
+            notificationListenerAdapter.getSchemaPath().lastNodeIdentifier().getLocalName(), writeTransaction,
             mapToStreams);
         submitData(writeTransaction);
         transactionChain.close();