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 com.google.common.base.Optional;
12 import io.netty.channel.ChannelHandler;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.socket.SocketChannel;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.Promise;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.api.NetconfSession;
20 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
21 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
22 import org.opendaylight.controller.netconf.util.handler.ssh.SshHandler;
23 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
24 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
25 import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
26 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
27 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
28 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
29 import org.opendaylight.protocol.framework.ReconnectStrategy;
30 import org.opendaylight.protocol.framework.SessionListener;
31 import org.opendaylight.protocol.framework.SessionListenerFactory;
33 import java.io.IOException;
34 import java.net.InetSocketAddress;
36 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
38 private AuthenticationHandler authHandler;
39 private HashedWheelTimer timer;
40 private NetconfClientSessionNegotiatorFactory negotatorFactory;
42 public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
43 EventLoopGroup workerGroup, long connectionTimeoutMillis) {
44 super(bossGroup, workerGroup, connectionTimeoutMillis);
45 this.authHandler = authHandler;
46 this.timer = new HashedWheelTimer();
47 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.<String>absent(), connectionTimeoutMillis);
50 public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
51 EventLoopGroup workerGroup, String additionalHeader, long socketTimeoutMillis) {
52 super(bossGroup, workerGroup, additionalHeader, socketTimeoutMillis);
53 this.authHandler = authHandler;
54 this.timer = new HashedWheelTimer();
55 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader), socketTimeoutMillis);
58 public Future<NetconfClientSession> createClient(InetSocketAddress address,
59 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
60 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
63 public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
64 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
70 private static final class NetconfSshClientInitializer extends AbstractChannelInitializer {
72 private final NetconfHandlerFactory handlerFactory;
73 private final AuthenticationHandler authenticationHandler;
74 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
75 private final NetconfClientSessionListener sessionListener;
77 public NetconfSshClientInitializer(AuthenticationHandler authHandler,
78 NetconfClientSessionNegotiatorFactory negotiatorFactory,
79 final NetconfClientSessionListener sessionListener) {
80 this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory());
81 this.authenticationHandler = authHandler;
82 this.negotiatorFactory = negotiatorFactory;
83 this.sessionListener = sessionListener;
87 public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
89 Invoker invoker = Invoker.subsystem("netconf");
90 ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
91 super.initialize(ch,promise);
92 } catch (IOException e) {
93 throw new RuntimeException(e);
98 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
99 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
101 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
102 return sessionListener;
109 private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
111 public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
116 public ChannelHandler[] getEncoders() {
117 return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
121 public ChannelHandler[] getDecoders() {
122 return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };