Eliminate CallHomeSessionContext.nettyChannel 24/89824/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 29 Apr 2020 17:54:00 +0000 (19:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 15 May 2020 09:21:36 +0000 (11:21 +0200)
Holding a temporary field only for it to be picked up by a callback
does not make sense. Eliminate it and allocate it much later in the
game.

Change-Id: I34b7dbc7244ef1627797c19d5b0d1c046ea46be3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 50e7111dd182ce505d2a01041ff0b1493d440437)

netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java

index 4e7bf41b2b4f6b1df9cccb94d453bc16e3a92de4..047342a6ab94e57cdfe814adb553e625c826271f 100644 (file)
@@ -25,7 +25,6 @@ import org.apache.sshd.client.future.OpenFuture;
 import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.session.Session;
-import org.checkerframework.checker.lock.qual.Holding;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netconf.client.NetconfClientSession;
 import org.opendaylight.netconf.client.NetconfClientSessionListener;
@@ -44,7 +43,6 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext {
     private final CallHomeAuthorization authorization;
     private final Factory factory;
 
-    private volatile MinaSshNettyChannel nettyChannel = null;
     private volatile boolean activated;
 
     private final InetSocketAddress remoteAddress;
@@ -84,7 +82,8 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext {
     SshFutureListener<OpenFuture> newSshFutureListener(final ClientChannel netconfChannel) {
         return future -> {
             if (future.isOpened()) {
-                netconfChannelOpened(netconfChannel);
+                factory.getChannelOpenListener().onNetconfSubsystemOpened(this,
+                    listener -> doActivate(netconfChannel, listener));
             } else {
                 channelOpenFailed(future.getException());
             }
@@ -102,21 +101,16 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext {
         sshSession.close(false);
     }
 
-    private void netconfChannelOpened(final ClientChannel netconfChannel) {
-        nettyChannel = newMinaSshNettyChannel(netconfChannel);
-        factory.getChannelOpenListener().onNetconfSubsystemOpened(
-            CallHomeSessionContext.this, this::doActivate);
-    }
-
-    // FIXME: this does not look right
-    @Holding("this")
-    private synchronized Promise<NetconfClientSession> doActivate(final NetconfClientSessionListener listener) {
+    private synchronized Promise<NetconfClientSession> doActivate(final ClientChannel netconfChannel,
+            final NetconfClientSessionListener listener) {
         if (activated) {
             return newSessionPromise().setFailure(new IllegalStateException("Session already activated."));
         }
+
         activated = true;
         LOG.info("Activating Netconf channel for {} with {}", getRemoteAddress(), listener);
         Promise<NetconfClientSession> activationPromise = newSessionPromise();
+        final MinaSshNettyChannel nettyChannel = newMinaSshNettyChannel(netconfChannel);
         factory.getChannelInitializer(listener).initialize(nettyChannel, activationPromise);
         factory.getNettyGroup().register(nettyChannel).awaitUninterruptibly(500);
         return activationPromise;