ee07b3949d6dd2f040da183a46dde263abe6d9af
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfSshClientDispatcher.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.client;
10
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
18 import java.io.IOException;
19 import java.net.InetSocketAddress;
20
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;
35
36 import com.google.common.base.Optional;
37
38 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
39
40     private AuthenticationHandler authHandler;
41     private HashedWheelTimer timer;
42     private NetconfClientSessionNegotiatorFactory negotatorFactory;
43
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());
50     }
51
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));
58     }
59
60     public Future<NetconfClientSession> createClient(InetSocketAddress address,
61             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
62         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
63
64             @Override
65             public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
66                 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
67             }
68
69         });
70     }
71
72     private static final class NetconfSshClientInitializer extends AbstractChannelInitializer {
73
74         private final NetconfHandlerFactory handlerFactory;
75         private final AuthenticationHandler authenticationHandler;
76         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
77         private final NetconfClientSessionListener sessionListener;
78
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;
86         }
87
88         @Override
89         public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
90             try {
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);
96             }
97         }
98
99         @Override
100         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
101             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
102                 @Override
103                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
104                     return sessionListener;
105                 }
106             }, ch, promise));
107
108         }
109     }
110
111     private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
112
113         public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
114             super(msgFactory);
115         }
116
117         @Override
118         public ChannelHandler[] getEncoders() {
119             return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
120         }
121
122         @Override
123         public ChannelHandler[] getDecoders() {
124             return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };
125         }
126     }
127 }