X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fclient%2FNetconfClientDispatcher.java;h=c7c723cb27b552fa51b7a7a287f66306649f35ca;hp=6fc4da026f38acc3add7538f3c172e2c6ee01a0f;hb=1ded3b4311dc761d39c132f9f38fcceb761ea997;hpb=18a090d8e17e83debe6748b4db865435aeab88c8 diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java index 6fc4da026f..c7c723cb27 100644 --- a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java @@ -9,34 +9,44 @@ package org.opendaylight.controller.netconf.client; import com.google.common.base.Optional; -import com.google.common.base.Preconditions; import io.netty.channel.EventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.util.HashedWheelTimer; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Promise; -import org.opendaylight.controller.netconf.api.NetconfMessage; -import org.opendaylight.controller.netconf.api.NetconfSession; -import org.opendaylight.controller.netconf.api.NetconfTerminationReason; -import org.opendaylight.controller.netconf.util.AbstractSslChannelInitializer; +import org.opendaylight.controller.netconf.util.AbstractChannelInitializer; +import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader; import org.opendaylight.protocol.framework.AbstractDispatcher; import org.opendaylight.protocol.framework.ReconnectStrategy; -import org.opendaylight.protocol.framework.SessionListener; +import org.opendaylight.protocol.framework.ReconnectStrategyFactory; import org.opendaylight.protocol.framework.SessionListenerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; +import java.io.Closeable; import java.net.InetSocketAddress; -public class NetconfClientDispatcher extends AbstractDispatcher { +public class NetconfClientDispatcher extends AbstractDispatcher implements Closeable { - private final Optional maybeContext; - private final NetconfClientSessionNegotiatorFactory negotatorFactory; + private static final Logger logger = LoggerFactory.getLogger(NetconfClientDispatcher.class); - public NetconfClientDispatcher(final Optional maybeContext, EventLoopGroup bossGroup, EventLoopGroup workerGroup) { + private final NetconfClientSessionNegotiatorFactory negotiatorFactory; + private final HashedWheelTimer timer; + + public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, + long clientConnectionTimeoutMillis) { + super(bossGroup, workerGroup); + timer = new HashedWheelTimer(); + this.negotiatorFactory = new NetconfClientSessionNegotiatorFactory(timer, + Optional. absent(), clientConnectionTimeoutMillis); + } + + public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, + NetconfHelloMessageAdditionalHeader additionalHeader, long connectionTimeoutMillis) { super(bossGroup, workerGroup); - this.maybeContext = Preconditions.checkNotNull(maybeContext); - this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(new HashedWheelTimer()); + timer = new HashedWheelTimer(); + this.negotiatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader), + connectionTimeoutMillis); } public Future createClient(InetSocketAddress address, @@ -50,37 +60,60 @@ public class NetconfClientDispatcher extends AbstractDispatcher promise) { - new ClientSslChannelInitializer(maybeContext, negotatorFactory, sessionListener).initialize(ch, promise); + new ClientChannelInitializer(negotiatorFactory, sessionListener).initialize(ch, promise); } }); } - private static class ClientSslChannelInitializer extends AbstractSslChannelInitializer { + public Future createReconnectingClient(final InetSocketAddress address, + final NetconfClientSessionListener listener, + final ReconnectStrategyFactory connectStrategyFactory, final ReconnectStrategy reestablishStrategy) { + final ClientChannelInitializer init = new ClientChannelInitializer(negotiatorFactory, listener); + + return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy, + new PipelineInitializer() { + @Override + public void initializeChannel(final SocketChannel ch, final Promise promise) { + init.initialize(ch, promise); + } + }); + } + + private static final class ClientChannelInitializer extends AbstractChannelInitializer { private final NetconfClientSessionNegotiatorFactory negotiatorFactory; private final NetconfClientSessionListener sessionListener; - private ClientSslChannelInitializer(Optional maybeContext, - NetconfClientSessionNegotiatorFactory negotiatorFactory, NetconfClientSessionListener sessionListener) { - super(maybeContext); + private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory, + NetconfClientSessionListener sessionListener) { this.negotiatorFactory = negotiatorFactory; this.sessionListener = sessionListener; } @Override - protected void initializeAfterDecoder(SocketChannel ch, Promise promise) { - ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() { - @Override - public SessionListener getSessionListener() { - return sessionListener; - } - }, ch, promise)); + public void initialize(SocketChannel ch, Promise promise) { + super.initialize(ch,promise); } @Override - protected void initSslEngine(SSLEngine sslEngine) { - sslEngine.setUseClientMode(true); + protected void initializeSessionNegotiator(SocketChannel ch, Promise promise) { + ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR, + negotiatorFactory.getSessionNegotiator( + new SessionListenerFactory() { + @Override + public NetconfClientSessionListener getSessionListener() { + return sessionListener; + } + }, ch, promise)); } } + @Override + public void close() { + try { + timer.stop(); + } catch (Exception e) { + logger.debug("Ignoring exception while closing {}", timer, e); + } + } }