799674487f4de5d936067b51b3f9ae392caee897
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / 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.controller.netconf.client;
9
10 import io.netty.channel.socket.SocketChannel;
11 import io.netty.util.concurrent.Promise;
12 import java.io.IOException;
13 import org.opendaylight.controller.netconf.nettyutil.AbstractChannelInitializer;
14 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.SshHandler;
15 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
16 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.Invoker;
17 import org.opendaylight.protocol.framework.SessionListenerFactory;
18
19 final class SshClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
20
21     private final AuthenticationHandler authenticationHandler;
22     private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
23     private final NetconfClientSessionListener sessionListener;
24
25     public SshClientChannelInitializer(final AuthenticationHandler authHandler,
26                                        final NetconfClientSessionNegotiatorFactory negotiatorFactory,
27                                        final NetconfClientSessionListener sessionListener) {
28         this.authenticationHandler = authHandler;
29         this.negotiatorFactory = negotiatorFactory;
30         this.sessionListener = sessionListener;
31     }
32
33     @Override
34     public void initialize(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
35         try {
36             final Invoker invoker = Invoker.subsystem("netconf");
37             ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
38             super.initialize(ch,promise);
39         } catch (final IOException e) {
40             throw new RuntimeException(e);
41         }
42     }
43
44     @Override
45     protected void initializeSessionNegotiator(final SocketChannel ch,
46                                                final Promise<NetconfClientSession> promise) {
47         ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER,  AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
48                 negotiatorFactory.getSessionNegotiator(new SessionListenerFactory<NetconfClientSessionListener>() {
49                     @Override
50                     public NetconfClientSessionListener getSessionListener() {
51                         return sessionListener;
52                     }
53                 }, ch, promise));
54     }
55 }