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