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 java.io.IOException;
12 import java.net.InetSocketAddress;
14 import javax.net.ssl.SSLContext;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.NetconfSession;
18 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
19 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
20 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
21 import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
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.FramingMechanism;
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 import com.google.common.base.Optional;
36 import io.netty.channel.ChannelHandler;
37 import io.netty.channel.EventLoopGroup;
38 import io.netty.channel.socket.SocketChannel;
39 import io.netty.util.HashedWheelTimer;
40 import io.netty.util.concurrent.Future;
41 import io.netty.util.concurrent.Promise;
43 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
45 private AuthenticationHandler authHandler;
46 private HashedWheelTimer timer;
47 private NetconfClientSessionNegotiatorFactory negotatorFactory;
49 public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
50 EventLoopGroup workerGroup) {
51 super(Optional.<SSLContext> absent(), bossGroup, workerGroup);
52 this.authHandler = authHandler;
53 this.timer = new HashedWheelTimer();
54 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer);
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 ch.pipeline().addLast("aggregator", new NetconfMessageAggregator(FramingMechanism.EOM));
92 ch.pipeline().addLast(handlerFactory.getDecoders());
93 initializeAfterDecoder(ch, promise);
94 ch.pipeline().addLast("frameEncoder",
95 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
96 ch.pipeline().addLast(handlerFactory.getEncoders());
97 } catch (IOException e) {
98 throw new RuntimeException(e);
103 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
104 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
106 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
107 return sessionListener;
114 private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
116 public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
121 public ChannelHandler[] getEncoders() {
122 return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
126 public ChannelHandler[] getDecoders() {
127 return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };