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