2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.client;
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;
17 import java.io.IOException;
18 import java.net.InetSocketAddress;
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;
28 import com.google.common.base.Optional;
30 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
32 private final AuthenticationHandler authHandler;
33 private final HashedWheelTimer timer;
34 private final NetconfClientSessionNegotiatorFactory negotatorFactory;
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);
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);
53 public Future<NetconfClientSession> createClient(InetSocketAddress address,
54 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
55 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
58 public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
59 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
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);
71 return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy,
72 new PipelineInitializer<NetconfClientSession>() {
74 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
75 init.initialize(ch, promise);
80 private static final class NetconfSshClientInitializer extends AbstractChannelInitializer<NetconfClientSession> {
82 private final AuthenticationHandler authenticationHandler;
83 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
84 private final NetconfClientSessionListener sessionListener;
86 public NetconfSshClientInitializer(AuthenticationHandler authHandler,
87 NetconfClientSessionNegotiatorFactory negotiatorFactory,
88 final NetconfClientSessionListener sessionListener) {
89 this.authenticationHandler = authHandler;
90 this.negotiatorFactory = negotiatorFactory;
91 this.sessionListener = sessionListener;
95 public void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
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);
106 protected void initializeAfterDecoder(SocketChannel ch, Promise<NetconfClientSession> promise) {
107 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory<NetconfClientSessionListener>() {
109 public NetconfClientSessionListener getSessionListener() {
110 return sessionListener;