Merge "Fix warnings reported in toaster"
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfSshClientDispatcher.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.client;
10
11 import io.netty.channel.EventLoopGroup;
12 import io.netty.channel.socket.SocketChannel;
13 import io.netty.util.HashedWheelTimer;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.Promise;
16
17 import java.io.IOException;
18 import java.net.InetSocketAddress;
19
20 import org.opendaylight.controller.netconf.api.NetconfMessage;
21 import org.opendaylight.controller.netconf.api.NetconfSession;
22 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
23 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
24 import org.opendaylight.controller.netconf.util.handler.ssh.SshHandler;
25 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
26 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
27 import org.opendaylight.protocol.framework.ReconnectStrategy;
28 import org.opendaylight.protocol.framework.SessionListener;
29 import org.opendaylight.protocol.framework.SessionListenerFactory;
30
31 import com.google.common.base.Optional;
32
33 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
34
35     private final AuthenticationHandler authHandler;
36     private final HashedWheelTimer timer;
37     private final NetconfClientSessionNegotiatorFactory negotatorFactory;
38
39     public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
40             EventLoopGroup workerGroup, long connectionTimeoutMillis) {
41         super(bossGroup, workerGroup, connectionTimeoutMillis);
42         this.authHandler = authHandler;
43         this.timer = new HashedWheelTimer();
44         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.<String>absent(), connectionTimeoutMillis);
45     }
46
47     public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
48             EventLoopGroup workerGroup, String additionalHeader, long socketTimeoutMillis) {
49         super(bossGroup, workerGroup, additionalHeader, socketTimeoutMillis);
50         this.authHandler = authHandler;
51         this.timer = new HashedWheelTimer();
52         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader), socketTimeoutMillis);
53     }
54
55     @Override
56     public Future<NetconfClientSession> createClient(InetSocketAddress address,
57             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
58         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
59
60             @Override
61             public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
62                 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
63             }
64
65         });
66     }
67
68     private static final class NetconfSshClientInitializer extends AbstractChannelInitializer {
69
70         private final AuthenticationHandler authenticationHandler;
71         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
72         private final NetconfClientSessionListener sessionListener;
73
74         public NetconfSshClientInitializer(AuthenticationHandler authHandler,
75                 NetconfClientSessionNegotiatorFactory negotiatorFactory,
76                 final NetconfClientSessionListener sessionListener) {
77             this.authenticationHandler = authHandler;
78             this.negotiatorFactory = negotiatorFactory;
79             this.sessionListener = sessionListener;
80         }
81
82         @Override
83         public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
84             try {
85                 Invoker invoker = Invoker.subsystem("netconf");
86                 ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
87                 super.initialize(ch,promise);
88             } catch (IOException e) {
89                 throw new RuntimeException(e);
90             }
91         }
92
93         @Override
94         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
95             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
96                 @Override
97                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
98                     return sessionListener;
99                 }
100             }, ch, promise));
101
102         }
103     }
104 }