53d9a27c962987a80d0ebc844aa213fbe3ccaf5d
[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.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
14 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
15 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
16 import org.opendaylight.netconf.nettyutil.handler.ssh.client.NetconfSshClient;
17
18 final class SshClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
19     private final AuthenticationHandler authenticationHandler;
20     private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
21     private final NetconfClientSessionListener sessionListener;
22     private final NetconfSshClient sshClient;
23
24     SshClientChannelInitializer(final AuthenticationHandler authHandler,
25             final NetconfClientSessionNegotiatorFactory negotiatorFactory,
26             final NetconfClientSessionListener sessionListener, @Nullable final NetconfSshClient sshClient) {
27         this.authenticationHandler = authHandler;
28         this.negotiatorFactory = negotiatorFactory;
29         this.sessionListener = sessionListener;
30         this.sshClient = sshClient;
31     }
32
33     SshClientChannelInitializer(final AuthenticationHandler authHandler,
34             final NetconfClientSessionNegotiatorFactory negotiatorFactory,
35             final NetconfClientSessionListener sessionListener) {
36         this(authHandler, negotiatorFactory, sessionListener, null);
37     }
38
39     @Override
40     public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) {
41         // ssh handler has to be the first handler in pipeline
42         ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(authenticationHandler, promise, sshClient));
43         super.initialize(ch, promise);
44     }
45
46     @Override
47     protected void initializeSessionNegotiator(final Channel ch,
48                                                final Promise<NetconfClientSession> promise) {
49         ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
50                 negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
51         ch.config().setConnectTimeoutMillis((int)negotiatorFactory.getConnectionTimeoutMillis());
52     }
53 }