- remove TLS/SSL support from netconf server and client
[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 import java.io.IOException;
18 import java.net.InetSocketAddress;
19 import org.opendaylight.controller.netconf.api.NetconfMessage;
20 import org.opendaylight.controller.netconf.api.NetconfSession;
21 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
22 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
23 import org.opendaylight.controller.netconf.util.handler.ssh.SshHandler;
24 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
25 import org.opendaylight.controller.netconf.util.handler.ssh.client.Invoker;
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 public class NetconfSshClientDispatcher extends NetconfClientDispatcher {
35
36     private AuthenticationHandler authHandler;
37     private HashedWheelTimer timer;
38     private NetconfClientSessionNegotiatorFactory negotatorFactory;
39
40     public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup,
41             EventLoopGroup workerGroup) {
42         super(bossGroup, workerGroup);
43         this.authHandler = authHandler;
44         this.timer = new HashedWheelTimer();
45         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer);
46     }
47
48     public Future<NetconfClientSession> createClient(InetSocketAddress address,
49             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
50         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
51
52             @Override
53             public void initializeChannel(SocketChannel arg0, Promise<NetconfClientSession> arg1) {
54                 new NetconfSshClientInitializer(authHandler, negotatorFactory, sessionListener).initialize(arg0, arg1);
55             }
56
57         });
58     }
59
60     private static final class NetconfSshClientInitializer extends AbstractChannelInitializer {
61
62         private final NetconfHandlerFactory handlerFactory;
63         private final AuthenticationHandler authenticationHandler;
64         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
65         private final NetconfClientSessionListener sessionListener;
66
67         public NetconfSshClientInitializer(AuthenticationHandler authHandler,
68                 NetconfClientSessionNegotiatorFactory negotiatorFactory,
69                 final NetconfClientSessionListener sessionListener) {
70             this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory());
71             this.authenticationHandler = authHandler;
72             this.negotiatorFactory = negotiatorFactory;
73             this.sessionListener = sessionListener;
74         }
75
76         @Override
77         public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
78             try {
79                 Invoker invoker = Invoker.subsystem("netconf");
80                 ch.pipeline().addFirst(new SshHandler(authenticationHandler, invoker));
81                 super.initialize(ch,promise);
82             } catch (IOException e) {
83                 throw new RuntimeException(e);
84             }
85         }
86
87         @Override
88         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
89             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
90                 @Override
91                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
92                     return sessionListener;
93                 }
94             }, ch, promise));
95
96         }
97     }
98
99     private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
100
101         public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
102             super(msgFactory);
103         }
104
105         @Override
106         public ChannelHandler[] getEncoders() {
107             return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
108         }
109
110         @Override
111         public ChannelHandler[] getDecoders() {
112             return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };
113         }
114     }
115 }