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.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;
29 import com.google.common.base.Optional;
31 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
33 private final AuthenticationHandler authHandler;
34 private final HashedWheelTimer timer;
35 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
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);
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),
56 public Future<NetconfClientSession> createClient(InetSocketAddress address,
57 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
58 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
61 public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
62 new NetconfSshClientInitializer(authHandler, negotiatorFactory, sessionListener).initialize(arg0, arg1);
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);
74 return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy,
75 new PipelineInitializer<NetconfClientSession>() {
77 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
78 init.initialize(ch, promise);
83 private static final class NetconfSshClientInitializer extends AbstractChannelInitializer<NetconfClientSession> {
85 private final AuthenticationHandler authenticationHandler;
86 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
87 private final NetconfClientSessionListener sessionListener;
89 public NetconfSshClientInitializer(AuthenticationHandler authHandler,
90 NetconfClientSessionNegotiatorFactory negotiatorFactory,
91 final NetconfClientSessionListener sessionListener) {
92 this.authenticationHandler = authHandler;
93 this.negotiatorFactory = negotiatorFactory;
94 this.sessionListener = sessionListener;
98 public void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
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);
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>() {
114 public NetconfClientSessionListener getSessionListener() {
115 return sessionListener;