Fix findbugs violations in netconf
[netconf.git] / netconf / netconf-client / src / main / java / org / opendaylight / netconf / client / TcpClientChannelInitializer.java
index 7558758efebecccbfe82990905860ed1e236faa8..5f25800b4b0c595137485c78fc0bc095c1114e82 100644 (file)
@@ -17,7 +17,6 @@ import io.netty.util.concurrent.GenericFutureListener;
 import io.netty.util.concurrent.Promise;
 import java.net.SocketAddress;
 import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
-import org.opendaylight.protocol.framework.SessionListenerFactory;
 
 class TcpClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
 
@@ -42,29 +41,24 @@ class TcpClientChannelInitializer extends AbstractChannelInitializer<NetconfClie
             GenericFutureListener<Future<NetconfClientSession>> negotiationFutureListener;
 
             @Override
-            public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress,
+            public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress,
+                                final SocketAddress localAddress,
                                 final ChannelPromise channelPromise) throws Exception {
                 connectPromise = channelPromise;
                 ChannelPromise tcpConnectFuture = new DefaultChannelPromise(ch);
 
-                negotiationFutureListener = new GenericFutureListener<Future<NetconfClientSession>>() {
-                    @Override
-                    public void operationComplete(final Future<NetconfClientSession> future) throws Exception {
-                        if (future.isSuccess()) {
-                            connectPromise.setSuccess();
-                        }
+                negotiationFutureListener = future -> {
+                    if (future.isSuccess()) {
+                        channelPromise.setSuccess();
                     }
                 };
 
-                tcpConnectFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
-                    @Override
-                    public void operationComplete(final Future<? super Void> future) throws Exception {
-                        if(future.isSuccess()) {
-                            //complete connection promise with netconf negotiation future
-                            negotiationFuture.addListener(negotiationFutureListener);
-                        } else {
-                            connectPromise.setFailure(future.cause());
-                        }
+                tcpConnectFuture.addListener(future -> {
+                    if (future.isSuccess()) {
+                        //complete connection promise with netconf negotiation future
+                        negotiationFuture.addListener(negotiationFutureListener);
+                    } else {
+                        channelPromise.setFailure(future.cause());
                     }
                 });
                 ctx.connect(remoteAddress, localAddress, tcpConnectFuture);
@@ -72,14 +66,19 @@ class TcpClientChannelInitializer extends AbstractChannelInitializer<NetconfClie
 
             @Override
             public void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
-                // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
-                if(connectPromise.isSuccess()) {
+                if (connectPromise == null) {
+                    return;
+                }
+
+                // If we have already succeeded and the session was dropped after, we need to fire inactive to notify
+                // reconnect logic
+                if (connectPromise.isSuccess()) {
                     ctx.fireChannelInactive();
                 }
 
                 //If connection promise is not already set, it means negotiation failed
                 //we must set connection promise to failure
-                if(!connectPromise.isDone()) {
+                if (!connectPromise.isDone()) {
                     connectPromise.setFailure(new IllegalStateException("Negotiation failed"));
                 }
 
@@ -98,11 +97,6 @@ class TcpClientChannelInitializer extends AbstractChannelInitializer<NetconfClie
     @Override
     protected void initializeSessionNegotiator(final Channel ch, final Promise<NetconfClientSession> promise) {
         ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
-                negotiatorFactory.getSessionNegotiator(new SessionListenerFactory<NetconfClientSessionListener>() {
-                    @Override
-                    public NetconfClientSessionListener getSessionListener() {
-                        return sessionListener;
-                    }
-                }, ch, promise));
+                negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
     }
 }