Clean up callhome TLS implementation 22/102322/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 9 Sep 2022 08:59:41 +0000 (10:59 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 12 Sep 2022 15:27:05 +0000 (15:27 +0000)
We have fields for single-use listeners and we are using
GenericFutureListener with suppressions. Clean that up.

Change-Id: I59908f032a422a15ef67ee48f18e90ec5d2695e1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/tls/NetconfCallHomeTlsServer.java
netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/tls/TlsAuthChannelInitializer.java

index 8b61eed7f26753ddb63429fe72dfe02c111e4b7b..b92321226035736c650df6a1092f348c87a3b368 100644 (file)
@@ -12,12 +12,11 @@ import static java.util.Objects.requireNonNull;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
 import io.netty.channel.ChannelOption;
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.handler.ssl.SslHandler;
-import io.netty.util.concurrent.Future;
-import io.netty.util.concurrent.GenericFutureListener;
 import java.net.InetSocketAddress;
 import java.security.PublicKey;
 import java.security.cert.Certificate;
@@ -67,45 +66,38 @@ public final class NetconfCallHomeTlsServer {
             .childOption(ChannelOption.SO_KEEPALIVE, true)
             .childOption(ChannelOption.SO_BACKLOG, maxConnections)
             .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout)
-            .childHandler(new TlsAuthChannelInitializer(sslHandlerFactory, handshakeListener))
-            .bind();
-        bindFuture.addListener(bindListener);
-    }
-
-    GenericFutureListener<Future<Channel>> handshakeListener = new GenericFutureListener<>() {
-        @Override
-        public void operationComplete(final Future<Channel> future) throws Exception {
-            if (future.isSuccess()) {
-                LOG.debug("SSL handshake completed successfully, accepting connection...");
-                final Channel channel = future.get();
-                // If the ssl handshake was successful it is expected that session contains peer certificate(s)
-                final Certificate cert = channel.pipeline().get(SslHandler.class).engine().getSession()
-                    .getPeerCertificates()[0];
-                final PublicKey publicKey = cert.getPublicKey();
-                final Optional<String> deviceId = allowedDevicesMonitor.findDeviceIdByPublicKey(publicKey);
-                if (deviceId.isEmpty()) {
-                    LOG.error("Unable to identify connected device by provided certificate");
-                    channel.close();
+            .childHandler(new TlsAuthChannelInitializer(sslHandlerFactory, future -> {
+                if (future.isSuccess()) {
+                    LOG.debug("SSL handshake completed successfully, accepting connection...");
+                    final Channel channel = future.get();
+                    // If the ssl handshake was successful it is expected that session contains peer certificate(s)
+                    final Certificate cert = channel.pipeline().get(SslHandler.class).engine().getSession()
+                        .getPeerCertificates()[0];
+                    final PublicKey publicKey = cert.getPublicKey();
+                    final Optional<String> deviceId = allowedDevicesMonitor.findDeviceIdByPublicKey(publicKey);
+                    if (deviceId.isEmpty()) {
+                        LOG.error("Unable to identify connected device by provided certificate");
+                        channel.close();
+                    } else {
+                        final CallHomeTlsSessionContext tlsSessionContext = new CallHomeTlsSessionContext(
+                            deviceId.orElseThrow(), channel, sslHandlerFactory, subsystemListener);
+                        tlsSessionContext.openNetconfChannel(channel);
+                    }
                 } else {
-                    final CallHomeTlsSessionContext tlsSessionContext = new CallHomeTlsSessionContext(deviceId.get(),
-                        channel, sslHandlerFactory, subsystemListener);
-                    tlsSessionContext.openNetconfChannel(channel);
+                    LOG.debug("SSL handshake failed, rejecting connection...");
+                    future.get().close();
                 }
+            }))
+            .bind();
+        bindFuture.addListener((ChannelFutureListener) future -> {
+            if (future.isSuccess()) {
+                LOG.debug("Call-Home TLS server bind completed");
             } else {
-                LOG.debug("SSL handshake failed, rejecting connection...");
-                future.get().close();
+                LOG.error("Call-Home TLS server bind failed", future.cause());
             }
-        }
-    };
-
-    GenericFutureListener<ChannelFuture> bindListener = future -> {
-        if (future.isSuccess()) {
-            LOG.debug("Call-Home TLS server bind completed");
-        } else {
-            LOG.error("Call-Home TLS server bind failed", future.cause());
-        }
-        cf = future.channel().closeFuture().addListener(f -> stop());
-    };
+            cf = future.channel().closeFuture().addListener(f -> stop());
+        });
+    }
 
     public void stop() {
         LOG.debug("Stopping the Call-Home TLS server...");
index a413174509d11965e6e9b94a45ac7329fe13dcfa..ab9e95bbd50465fc47de3748690b81e817a5cfab 100644 (file)
@@ -10,23 +10,20 @@ package org.opendaylight.netconf.callhome.protocol.tls;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelInitializer;
 import io.netty.handler.ssl.SslHandler;
-import io.netty.util.concurrent.GenericFutureListener;
+import io.netty.util.concurrent.FutureListener;
 import org.opendaylight.netconf.client.SslHandlerFactory;
 
-@SuppressWarnings("rawtypes")
-final class TlsAuthChannelInitializer extends ChannelInitializer {
-
+final class TlsAuthChannelInitializer extends ChannelInitializer<Channel> {
     private static final String SSL_HANDLER_CHANNEL_NAME = "sslHandler";
 
     private final SslHandlerFactory sslHandlerFactory;
-    private final GenericFutureListener listener;
+    private final FutureListener<Channel> listener;
 
-    TlsAuthChannelInitializer(final SslHandlerFactory sslHandlerFactory, final GenericFutureListener listener) {
+    TlsAuthChannelInitializer(final SslHandlerFactory sslHandlerFactory, final FutureListener<Channel> listener) {
         this.sslHandlerFactory = sslHandlerFactory;
         this.listener = listener;
     }
 
-    @SuppressWarnings("unchecked")
     @Override
     public void initChannel(final Channel ch) {
         final SslHandler sslHandler = sslHandlerFactory.createSslHandler();