X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fcallhome-protocol%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fcallhome%2Fprotocol%2FCallHomeSessionContext.java;h=f517d8a64c9aded40634f46dbc1467071dbe2a1a;hb=a201b000f7d777bd7b53748c3f13487fbb398599;hp=8d844c7c8e56eeabe4b4f5e3054399e47b75a8d5;hpb=a4435c189593770de81621c9cd6de261633030ca;p=netconf.git diff --git a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java index 8d844c7c8e..f517d8a64c 100644 --- a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java +++ b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java @@ -5,11 +5,11 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netconf.callhome.protocol; -import com.google.common.base.Preconditions; -import com.google.common.base.Throwables; +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; + import io.netty.channel.EventLoopGroup; import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.Promise; @@ -19,15 +19,15 @@ import java.net.SocketAddress; import java.security.PublicKey; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import javax.annotation.Nullable; -import javax.annotation.concurrent.GuardedBy; -import org.apache.sshd.ClientChannel; -import org.apache.sshd.ClientSession; +import org.apache.sshd.client.channel.ClientChannel; import org.apache.sshd.client.future.AuthFuture; import org.apache.sshd.client.future.OpenFuture; +import org.apache.sshd.client.session.ClientSession; import org.apache.sshd.client.session.ClientSessionImpl; -import org.apache.sshd.common.Session; 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; import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory; @@ -48,23 +48,23 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { private volatile MinaSshNettyChannel nettyChannel = null; private volatile boolean activated; - private InetSocketAddress remoteAddress; - private PublicKey serverKey; + private final InetSocketAddress remoteAddress; + private final PublicKey serverKey; - CallHomeSessionContext(ClientSession sshSession, CallHomeAuthorization authorization, SocketAddress remoteAddress, - Factory factory) { - this.authorization = Preconditions.checkNotNull(authorization, "authorization"); - Preconditions.checkArgument(this.authorization.isServerAllowed(), "Server was not allowed."); - Preconditions.checkArgument(sshSession instanceof ClientSessionImpl, + CallHomeSessionContext(final ClientSession sshSession, final CallHomeAuthorization authorization, + final SocketAddress remoteAddress, final Factory factory) { + this.authorization = requireNonNull(authorization, "authorization"); + checkArgument(this.authorization.isServerAllowed(), "Server was not allowed."); + checkArgument(sshSession instanceof ClientSessionImpl, "sshSession must implement ClientSessionImpl"); - this.factory = Preconditions.checkNotNull(factory, "factory"); + this.factory = requireNonNull(factory, "factory"); this.sshSession = (ClientSessionImpl) sshSession; this.sshSession.setAttribute(SESSION_KEY, this); this.remoteAddress = (InetSocketAddress) this.sshSession.getIoSession().getRemoteAddress(); this.serverKey = this.sshSession.getKex().getServerKey(); } - static CallHomeSessionContext getFrom(ClientSession sshSession) { + static CallHomeSessionContext getFrom(final ClientSession sshSession) { return sshSession.getAttribute(SESSION_KEY); } @@ -80,14 +80,13 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { netconfChannel.setStreaming(ClientChannel.Streaming.Async); netconfChannel.open().addListener(newSshFutureListener(netconfChannel)); } catch (IOException e) { - throw Throwables.propagate(e); + throw new IllegalStateException(e); } } SshFutureListener newSshFutureListener(final ClientChannel netconfChannel) { - return future -> - { - if (future.isOpened()) { + return future -> { + if (future.isOpened()) { netconfChannelOpened(netconfChannel); } else { channelOpenFailed(future.getException()); @@ -95,20 +94,21 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { }; } - private void channelOpenFailed(Throwable e) { - LOG.error("Unable to open netconf subsystem, disconnecting.", e); + private void channelOpenFailed(final Throwable throwable) { + LOG.error("Unable to open netconf subsystem, disconnecting.", throwable); sshSession.close(false); } - private void netconfChannelOpened(ClientChannel netconfChannel) { + private void netconfChannelOpened(final ClientChannel netconfChannel) { nettyChannel = newMinaSshNettyChannel(netconfChannel); - factory.getChannelOpenListener().onNetconfSubsystemOpened(CallHomeSessionContext.this, - listener -> doActivate(listener)); + factory.getChannelOpenListener().onNetconfSubsystemOpened( + CallHomeSessionContext.this, this::doActivate); } - @GuardedBy("this") - private synchronized Promise doActivate(NetconfClientSessionListener listener) { - if(activated) { + // FIXME: this does not look right + @Holding("this") + private synchronized Promise doActivate(final NetconfClientSessionListener listener) { + if (activated) { return newSessionPromise().setFailure(new IllegalStateException("Session already activated.")); } activated = true; @@ -119,11 +119,11 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { return activationPromise; } - protected MinaSshNettyChannel newMinaSshNettyChannel(ClientChannel netconfChannel) { + protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) { return new MinaSshNettyChannel(this, sshSession, netconfChannel); } - private Promise newSessionPromise() { + private static Promise newSessionPromise() { return GlobalEventExecutor.INSTANCE.newPromise(); } @@ -158,18 +158,18 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { private final CallHomeNetconfSubsystemListener subsystemListener; private final ConcurrentMap sessions = new ConcurrentHashMap<>(); - Factory(EventLoopGroup nettyGroup, NetconfClientSessionNegotiatorFactory negotiatorFactory, - CallHomeNetconfSubsystemListener subsystemListener) { - this.nettyGroup = Preconditions.checkNotNull(nettyGroup, "nettyGroup"); - this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory, "negotiatorFactory"); - this.subsystemListener = Preconditions.checkNotNull(subsystemListener); + Factory(final EventLoopGroup nettyGroup, final NetconfClientSessionNegotiatorFactory negotiatorFactory, + final CallHomeNetconfSubsystemListener subsystemListener) { + this.nettyGroup = requireNonNull(nettyGroup, "nettyGroup"); + this.negotiatorFactory = requireNonNull(negotiatorFactory, "negotiatorFactory"); + this.subsystemListener = requireNonNull(subsystemListener); } - void remove(CallHomeSessionContext session) { + void remove(final CallHomeSessionContext session) { sessions.remove(session.getSessionName(), session); } - ReverseSshChannelInitializer getChannelInitializer(NetconfClientSessionListener listener) { + ReverseSshChannelInitializer getChannelInitializer(final NetconfClientSessionListener listener) { return ReverseSshChannelInitializer.create(negotiatorFactory, listener); } @@ -177,10 +177,10 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { return this.subsystemListener; } - @Nullable - CallHomeSessionContext createIfNotExists(ClientSession sshSession, CallHomeAuthorization authorization, - SocketAddress remoteAddress) { - CallHomeSessionContext session = new CallHomeSessionContext(sshSession, authorization, remoteAddress, this); + @Nullable CallHomeSessionContext createIfNotExists(final ClientSession sshSession, + final CallHomeAuthorization authorization, final SocketAddress remoteAddress) { + CallHomeSessionContext session = new CallHomeSessionContext(sshSession, authorization, + remoteAddress, this); CallHomeSessionContext preexisting = sessions.putIfAbsent(session.getSessionName(), session); // If preexisting is null - session does not exist, so we can safely create new one, otherwise we return // null and incoming connection will be rejected. @@ -190,7 +190,5 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { EventLoopGroup getNettyGroup() { return nettyGroup; } - } - }