Rework NETCONF interfaces
[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.protocol.framework.ReconnectStrategy;
25 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
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     @Override
66     public Future<Void> createReconnectingClient(final InetSocketAddress address,
67             final NetconfClientSessionListener listener,
68             final ReconnectStrategyFactory connectStrategyFactory, final ReconnectStrategy reestablishStrategy) {
69         final NetconfSshClientInitializer init = new NetconfSshClientInitializer(authHandler, negotatorFactory, listener);
70
71         return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy,
72                 new PipelineInitializer<NetconfClientSession>() {
73             @Override
74             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
75                 init.initialize(ch, promise);
76             }
77         });
78     }
79
80     private static final class NetconfSshClientInitializer extends AbstractChannelInitializer<NetconfClientSession> {
81
82         private final AuthenticationHandler authenticationHandler;
83         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
84         private final NetconfClientSessionListener sessionListener;
85
86         public NetconfSshClientInitializer(AuthenticationHandler authHandler,
87                 NetconfClientSessionNegotiatorFactory negotiatorFactory,
88                 final NetconfClientSessionListener sessionListener) {
89             this.authenticationHandler = authHandler;
90             this.negotiatorFactory = negotiatorFactory;
91             this.sessionListener = sessionListener;
92         }
93
94         @Override
95         public void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
96             try {
97                 Invoker invoker = Invoker.subsystem("netconf");
98                 ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
99                 super.initialize(ch,promise);
100             } catch (IOException e) {
101                 throw new RuntimeException(e);
102             }
103         }
104
105         @Override
106         protected void initializeAfterDecoder(SocketChannel ch, Promise<NetconfClientSession> promise) {
107             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory<NetconfClientSessionListener>() {
108                 @Override
109                 public NetconfClientSessionListener getSessionListener() {
110                     return sessionListener;
111                 }
112             }, ch, promise));
113
114         }
115     }
116 }