Refactor Additional header for netconf hello message.
[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.util.AbstractChannelInitializer;
21 import org.opendaylight.controller.netconf.util.handler.ssh.SshHandler;
22 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
23 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
24 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
25 import org.opendaylight.protocol.framework.ReconnectStrategy;
26 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
27 import org.opendaylight.protocol.framework.SessionListenerFactory;
28
29 import com.google.common.base.Optional;
30
31 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
32
33     private final AuthenticationHandler authHandler;
34     private final HashedWheelTimer timer;
35     private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
36
37     public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
38             EventLoopGroup workerGroup, long connectionTimeoutMillis) {
39         super(bossGroup, workerGroup, connectionTimeoutMillis);
40         this.authHandler = authHandler;
41         this.timer = new HashedWheelTimer();
42         this.negotiatorFactory = new NetconfClientSessionNegotiatorFactory(timer,
43                 Optional.<NetconfHelloMessageAdditionalHeader> absent(), connectionTimeoutMillis);
44     }
45
46     public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
47             EventLoopGroup workerGroup, NetconfHelloMessageAdditionalHeader additionalHeader, long socketTimeoutMillis) {
48         super(bossGroup, workerGroup, additionalHeader, socketTimeoutMillis);
49         this.authHandler = authHandler;
50         this.timer = new HashedWheelTimer();
51         this.negotiatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader),
52                 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, negotiatorFactory, sessionListener).initialize(arg0, arg1);
63             }
64
65         });
66     }
67
68     @Override
69     public Future<Void> createReconnectingClient(final InetSocketAddress address,
70             final NetconfClientSessionListener listener,
71             final ReconnectStrategyFactory connectStrategyFactory, final ReconnectStrategy reestablishStrategy) {
72         final NetconfSshClientInitializer init = new NetconfSshClientInitializer(authHandler, negotiatorFactory, listener);
73
74         return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy,
75                 new PipelineInitializer<NetconfClientSession>() {
76             @Override
77             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
78                 init.initialize(ch, promise);
79             }
80         });
81     }
82
83     private static final class NetconfSshClientInitializer extends AbstractChannelInitializer<NetconfClientSession> {
84
85         private final AuthenticationHandler authenticationHandler;
86         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
87         private final NetconfClientSessionListener sessionListener;
88
89         public NetconfSshClientInitializer(AuthenticationHandler authHandler,
90                 NetconfClientSessionNegotiatorFactory negotiatorFactory,
91                 final NetconfClientSessionListener sessionListener) {
92             this.authenticationHandler = authHandler;
93             this.negotiatorFactory = negotiatorFactory;
94             this.sessionListener = sessionListener;
95         }
96
97         @Override
98         public void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
99             try {
100                 Invoker invoker = Invoker.subsystem("netconf");
101                 ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
102                 super.initialize(ch,promise);
103             } catch (IOException e) {
104                 throw new RuntimeException(e);
105             }
106         }
107
108         @Override
109         protected void initializeSessionNegotiator(SocketChannel ch,
110                 Promise<NetconfClientSession> promise) {
111             ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER,  AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
112                     negotiatorFactory.getSessionNegotiator(new SessionListenerFactory<NetconfClientSessionListener>() {
113                 @Override
114                 public NetconfClientSessionListener getSessionListener() {
115                     return sessionListener;
116                 }
117             }, ch, promise));
118         }
119     }
120 }