Merge "Bug 1234: Force to use legacy OF plugin if it is installed."
[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.Channel;
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.authentication.AuthenticationHandler;
15 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
16 import org.opendaylight.protocol.framework.SessionListenerFactory;
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
24     public SshClientChannelInitializer(final AuthenticationHandler authHandler,
25                                        final NetconfClientSessionNegotiatorFactory negotiatorFactory,
26                                        final NetconfClientSessionListener sessionListener) {
27         this.authenticationHandler = authHandler;
28         this.negotiatorFactory = negotiatorFactory;
29         this.sessionListener = sessionListener;
30     }
31
32     @Override
33     public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) {
34         try {
35             ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(authenticationHandler));
36             super.initialize(ch,promise);
37         } catch (final IOException e) {
38             throw new RuntimeException(e);
39         }
40     }
41
42     @Override
43     protected void initializeSessionNegotiator(final Channel ch,
44                                                final Promise<NetconfClientSession> promise) {
45         ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER,  AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
46                 negotiatorFactory.getSessionNegotiator(new SessionListenerFactory<NetconfClientSessionListener>() {
47                     @Override
48                     public NetconfClientSessionListener getSessionListener() {
49                         return sessionListener;
50                     }
51                 }, ch, promise));
52     }
53 }