Merge "Re-Enable the pax-exam execution in Eclipse"
[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 java.io.IOException;
12 import java.net.InetSocketAddress;
13
14 import javax.net.ssl.SSLContext;
15
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;
33
34 import com.google.common.base.Optional;
35
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;
42
43 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
44
45     private AuthenticationHandler authHandler;
46     private HashedWheelTimer timer;
47     private NetconfClientSessionNegotiatorFactory negotatorFactory;
48
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);
55     }
56
57     @Override
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                 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);
99             }
100         }
101
102         @Override
103         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
104             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
105                 @Override
106                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
107                     return sessionListener;
108                 }
109             }, ch, promise));
110
111         }
112     }
113
114     private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
115
116         public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
117             super(msgFactory);
118         }
119
120         @Override
121         public ChannelHandler[] getEncoders() {
122             return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
123         }
124
125         @Override
126         public ChannelHandler[] getDecoders() {
127             return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };
128         }
129     }
130 }