Improve future result access 39/110339/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Feb 2024 10:47:46 +0000 (11:47 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Feb 2024 11:43:10 +0000 (12:43 +0100)
When the future completes, just look at it cause(), as that will be null
for success. This slightly improve the performance in the case of a
failure.

Change-Id: Ieb1aa02e4aa7b86f35a6b6de8c7129f8cc048385
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiator.java
protocol/netconf-server/src/main/java/org/opendaylight/netconf/server/SendErrorExceptionUtil.java
transport/transport-tcp/src/main/java/org/opendaylight/netconf/transport/tcp/TCPClient.java
transport/transport-tcp/src/main/java/org/opendaylight/netconf/transport/tcp/TCPServer.java

index 8455821ef5aca833b14fff2dfef0e6ac52589f4d..35dab27df79352e683ba6c231141618720b34505 100644 (file)
@@ -37,7 +37,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 
 // Non-final for mocking
 class NetconfClientSessionNegotiator
@@ -103,9 +102,9 @@ class NetconfClientSessionNegotiator
                 new ExiConfirmationInboundHandler(session, startExiMessage));
 
         session.sendMessage(startExiMessage).addListener(channelFuture -> {
-            if (!channelFuture.isSuccess()) {
-                LOG.warn("Failed to send start-exi message {} on session {}", startExiMessage, session,
-                        channelFuture.cause());
+            final var cause = channelFuture.cause();
+            if (cause != null) {
+                LOG.warn("Failed to send start-exi message {} on session {}", startExiMessage, session, cause);
                 channel.pipeline().remove(ExiConfirmationInboundHandler.EXI_CONFIRMED_HANDLER);
             } else {
                 LOG.trace("Start-exi message {} sent to socket on session {}", startExiMessage, session);
@@ -118,9 +117,9 @@ class NetconfClientSessionNegotiator
     }
 
     private static boolean containsExi10Capability(final Document doc) {
-        final NodeList nList = doc.getElementsByTagName(XmlNetconfConstants.CAPABILITY);
-        for (int i = 0; i < nList.getLength(); i++) {
-            if (nList.item(i).getTextContent().contains(EXI_1_0_CAPABILITY_MARKER)) {
+        final var nodeList = doc.getElementsByTagName(XmlNetconfConstants.CAPABILITY);
+        for (int i = 0; i < nodeList.getLength(); i++) {
+            if (nodeList.item(i).getTextContent().contains(EXI_1_0_CAPABILITY_MARKER)) {
                 return true;
             }
         }
index 26defc3d0486de1d8376ad7c4b6ae21a372974b7..683cd69431df79ef2d3b3124d5f28078dffb559c 100644 (file)
@@ -103,8 +103,10 @@ final class SendErrorExceptionUtil {
 
         @Override
         public void operationComplete(final ChannelFuture channelFuture) {
-            checkState(channelFuture.isSuccess(), "Unable to send exception %s", sendErrorException,
-                channelFuture.cause());
+            final var cause = channelFuture.cause();
+            if (cause != null) {
+                throw new IllegalStateException("Unable to send exception " + sendErrorException, cause);
+            }
         }
     }
 }
index 96aad1dade23c3a648b7a104c9aa01fa43958d09..e84b80a8a0cee7192423e2ebaba75c959789246a 100644 (file)
@@ -63,13 +63,14 @@ public final class TCPClient extends TCPTransportStack {
                 socketAddressOf(connectParams.requireRemoteAddress(), connectParams.requireRemotePort()),
                 socketAddressOf(connectParams.getLocalAddress(), connectParams.getLocalPort()))
             .addListener((ChannelFutureListener) future -> {
-                if (future.isSuccess()) {
+                final var cause = future.cause();
+                if (cause == null) {
                     // Order of operations is important here: the stack should be visible before the underlying channel
                     final var stack = new TCPClient(listener);
                     ret.set(stack);
                     stack.addTransportChannel(new TCPTransportChannel(future.channel()));
                 } else {
-                    ret.setException(future.cause());
+                    ret.setException(cause);
                 }
             });
         return ret;
index af3d33918564fa138aab265214cf0f2b05ab0072..d7ad92657f0a3027f30c8314f8264474048b7f93 100644 (file)
@@ -71,11 +71,12 @@ public final class TCPServer extends TCPTransportStack {
             .childHandler(initializer)
             .bind(socketAddressOf(listenParams.requireLocalAddress(), listenParams.requireLocalPort()))
             .addListener((ChannelFutureListener) future -> {
-                if (future.isSuccess()) {
+                final var cause = future.cause();
+                if (cause == null) {
                     stack.setListenChannel(future.channel());
                     ret.set(stack);
                 } else {
-                    ret.setException(future.cause());
+                    ret.setException(cause);
                 }
             });
         return ret;