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.ChannelHandler;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.Promise;
17 import java.io.IOException;
18 import java.net.InetSocketAddress;
19 import org.opendaylight.controller.netconf.api.NetconfMessage;
20 import org.opendaylight.controller.netconf.api.NetconfSession;
21 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
22 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
23 import org.opendaylight.controller.netconf.util.handler.ssh.SshHandler;
24 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
25 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
26 import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
27 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
28 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
29 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
30 import org.opendaylight.protocol.framework.ReconnectStrategy;
31 import org.opendaylight.protocol.framework.SessionListener;
32 import org.opendaylight.protocol.framework.SessionListenerFactory;
34 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
36 private AuthenticationHandler authHandler;
37 private HashedWheelTimer timer;
38 private NetconfClientSessionNegotiatorFactory negotatorFactory;
40 public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
41 EventLoopGroup workerGroup) {
42 super(bossGroup, workerGroup);
43 this.authHandler = authHandler;
44 this.timer = new HashedWheelTimer();
45 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer);
48 public Future<NetconfClientSession> createClient(InetSocketAddress address,
49 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
50 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
53 public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
54 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
60 private static final class NetconfSshClientInitializer extends AbstractChannelInitializer {
62 private final NetconfHandlerFactory handlerFactory;
63 private final AuthenticationHandler authenticationHandler;
64 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
65 private final NetconfClientSessionListener sessionListener;
67 public NetconfSshClientInitializer(AuthenticationHandler authHandler,
68 NetconfClientSessionNegotiatorFactory negotiatorFactory,
69 final NetconfClientSessionListener sessionListener) {
70 this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory());
71 this.authenticationHandler = authHandler;
72 this.negotiatorFactory = negotiatorFactory;
73 this.sessionListener = sessionListener;
77 public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
79 Invoker invoker = Invoker.subsystem("netconf");
80 ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
81 super.initialize(ch,promise);
82 } catch (IOException e) {
83 throw new RuntimeException(e);
88 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
89 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
91 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
92 return sessionListener;
99 private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
101 public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
106 public ChannelHandler[] getEncoders() {
107 return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
111 public ChannelHandler[] getDecoders() {
112 return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };