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;
18 import java.io.IOException;
19 import java.net.InetSocketAddress;
21 import org.opendaylight.controller.netconf.api.NetconfMessage;
22 import org.opendaylight.controller.netconf.api.NetconfSession;
23 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
24 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
25 import org.opendaylight.controller.netconf.util.handler.ssh.SshHandler;
26 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
27 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
28 import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
29 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
30 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
31 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
32 import org.opendaylight.protocol.framework.ReconnectStrategy;
33 import org.opendaylight.protocol.framework.SessionListener;
34 import org.opendaylight.protocol.framework.SessionListenerFactory;
36 import com.google.common.base.Optional;
38 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
40 private AuthenticationHandler authHandler;
41 private HashedWheelTimer timer;
42 private NetconfClientSessionNegotiatorFactory negotatorFactory;
44 public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
45 EventLoopGroup workerGroup) {
46 super(bossGroup, workerGroup);
47 this.authHandler = authHandler;
48 this.timer = new HashedWheelTimer();
49 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.<String>absent());
52 public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
53 EventLoopGroup workerGroup, String additionalHeader) {
54 super(bossGroup, workerGroup, additionalHeader);
55 this.authHandler = authHandler;
56 this.timer = new HashedWheelTimer();
57 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader));
60 public Future<NetconfClientSession> createClient(InetSocketAddress address,
61 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
62 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
65 public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
66 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
72 private static final class NetconfSshClientInitializer extends AbstractChannelInitializer {
74 private final NetconfHandlerFactory handlerFactory;
75 private final AuthenticationHandler authenticationHandler;
76 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
77 private final NetconfClientSessionListener sessionListener;
79 public NetconfSshClientInitializer(AuthenticationHandler authHandler,
80 NetconfClientSessionNegotiatorFactory negotiatorFactory,
81 final NetconfClientSessionListener sessionListener) {
82 this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory());
83 this.authenticationHandler = authHandler;
84 this.negotiatorFactory = negotiatorFactory;
85 this.sessionListener = sessionListener;
89 public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
91 Invoker invoker = Invoker.subsystem("netconf");
92 ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
93 super.initialize(ch,promise);
94 } catch (IOException e) {
95 throw new RuntimeException(e);
100 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
101 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
103 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
104 return sessionListener;
111 private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
113 public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
118 public ChannelHandler[] getEncoders() {
119 return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
123 public ChannelHandler[] getDecoders() {
124 return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };