Changed model versions to dependencies
[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 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;
32
33 import java.io.IOException;
34 import java.net.InetSocketAddress;
35
36 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
37
38     private AuthenticationHandler authHandler;
39     private HashedWheelTimer timer;
40     private NetconfClientSessionNegotiatorFactory negotatorFactory;
41
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);
48     }
49
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);
56     }
57
58     public Future<NetconfClientSession> createClient(InetSocketAddress address,
59             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
60         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
61
62             @Override
63             public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
64                 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
65             }
66
67         });
68     }
69
70     private static final class NetconfSshClientInitializer extends AbstractChannelInitializer {
71
72         private final NetconfHandlerFactory handlerFactory;
73         private final AuthenticationHandler authenticationHandler;
74         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
75         private final NetconfClientSessionListener sessionListener;
76
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;
84         }
85
86         @Override
87         public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
88             try {
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);
94             }
95         }
96
97         @Override
98         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
99             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
100                 @Override
101                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
102                     return sessionListener;
103                 }
104             }, ch, promise));
105
106         }
107     }
108
109     private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
110
111         public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
112             super(msgFactory);
113         }
114
115         @Override
116         public ChannelHandler[] getEncoders() {
117             return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
118         }
119
120         @Override
121         public ChannelHandler[] getDecoders() {
122             return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };
123         }
124     }
125 }