NETCONF-125 connection timeout and between timeout are fixed
[netconf.git] / netconf / netconf-client / src / main / java / org / opendaylight / netconf / client / SshClientChannelInitializer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.client;
9
10 import io.netty.channel.Channel;
11 import io.netty.util.concurrent.Promise;
12 import org.apache.sshd.client.SshClient;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
15 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
16 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
17
18 final class SshClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
19
20     private final AuthenticationHandler authenticationHandler;
21     private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
22     private final NetconfClientSessionListener sessionListener;
23     private final SshClient sshClient;
24
25     SshClientChannelInitializer(final AuthenticationHandler authHandler,
26             final NetconfClientSessionNegotiatorFactory negotiatorFactory,
27             final NetconfClientSessionListener sessionListener, @Nullable final SshClient sshClient) {
28         this.authenticationHandler = authHandler;
29         this.negotiatorFactory = negotiatorFactory;
30         this.sessionListener = sessionListener;
31         this.sshClient = sshClient;
32     }
33
34     SshClientChannelInitializer(final AuthenticationHandler authHandler,
35             final NetconfClientSessionNegotiatorFactory negotiatorFactory,
36             final NetconfClientSessionListener sessionListener) {
37         this(authHandler, negotiatorFactory, sessionListener, null);
38     }
39
40     @Override
41     public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) {
42         // ssh handler has to be the first handler in pipeline
43         ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(authenticationHandler, promise, sshClient));
44         super.initialize(ch, promise);
45     }
46
47     @Override
48     protected void initializeSessionNegotiator(final Channel ch,
49                                                final Promise<NetconfClientSession> promise) {
50         ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
51                 negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
52         ch.config().setConnectTimeoutMillis((int)negotiatorFactory.getConnectionTimeoutMillis());
53     }
54 }