Add option to provide custom SshClient for netconf-client
[netconf.git] / netconf / netconf-client / src / main / java / org / opendaylight / netconf / client / NetconfClientDispatcherImpl.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 package org.opendaylight.netconf.client;
9
10 import io.netty.channel.EventLoopGroup;
11 import io.netty.util.Timer;
12 import io.netty.util.concurrent.Future;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import java.util.Set;
16 import org.apache.sshd.client.SshClient;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
19 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
20 import org.opendaylight.netconf.nettyutil.AbstractNetconfDispatcher;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class NetconfClientDispatcherImpl
26         extends AbstractNetconfDispatcher<NetconfClientSession, NetconfClientSessionListener>
27         implements NetconfClientDispatcher {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NetconfClientDispatcherImpl.class);
30
31     private final Timer timer;
32     private final SshClient sshClient;
33
34     public NetconfClientDispatcherImpl(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup,
35                                        final Timer timer, @Nullable final SshClient sshClient) {
36         super(bossGroup, workerGroup);
37         this.timer = timer;
38         this.sshClient = sshClient;
39     }
40
41     public NetconfClientDispatcherImpl(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup,
42             final Timer timer) {
43         this(bossGroup, workerGroup, timer, null);
44     }
45
46     protected Timer getTimer() {
47         return timer;
48     }
49
50     @Override
51     public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
52         switch (clientConfiguration.getProtocol()) {
53             case TCP:
54                 return createTcpClient(clientConfiguration);
55             case SSH:
56                 return createSshClient(clientConfiguration);
57             case TLS:
58                 return createTlsClient(clientConfiguration);
59             default:
60                 throw new IllegalArgumentException("Unknown client protocol " + clientConfiguration.getProtocol());
61         }
62     }
63
64     @Override
65     public Future<Void> createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
66         switch (clientConfiguration.getProtocol()) {
67             case TCP:
68                 return createReconnectingTcpClient(clientConfiguration);
69             case SSH:
70                 return createReconnectingSshClient(clientConfiguration);
71             case TLS:
72                 return createReconnectingTlsClient(clientConfiguration);
73             default:
74                 throw new IllegalArgumentException("Unknown client protocol " + clientConfiguration.getProtocol());
75         }
76     }
77
78     private Future<NetconfClientSession> createTcpClient(final NetconfClientConfiguration currentConfiguration) {
79         LOG.debug("Creating TCP client with configuration: {}", currentConfiguration);
80         return super.createClient(currentConfiguration.getAddress(), currentConfiguration.getReconnectStrategy(),
81             (ch, promise) -> new TcpClientChannelInitializer(getNegotiatorFactory(currentConfiguration),
82                         currentConfiguration
83                         .getSessionListener()).initialize(ch, promise));
84     }
85
86     private Future<Void> createReconnectingTcpClient(
87             final NetconfReconnectingClientConfiguration currentConfiguration) {
88         LOG.debug("Creating reconnecting TCP client with configuration: {}", currentConfiguration);
89         final TcpClientChannelInitializer init =
90                 new TcpClientChannelInitializer(getNegotiatorFactory(currentConfiguration),
91                 currentConfiguration.getSessionListener());
92
93         return super.createReconnectingClient(currentConfiguration.getAddress(), currentConfiguration
94                 .getConnectStrategyFactory(),
95                 currentConfiguration.getReconnectStrategy(), init::initialize);
96     }
97
98     private Future<NetconfClientSession> createSshClient(final NetconfClientConfiguration currentConfiguration) {
99         LOG.debug("Creating SSH client with configuration: {}", currentConfiguration);
100         return super.createClient(currentConfiguration.getAddress(), currentConfiguration.getReconnectStrategy(),
101             (ch, sessionPromise) -> new SshClientChannelInitializer(currentConfiguration.getAuthHandler(),
102                         getNegotiatorFactory(currentConfiguration), currentConfiguration.getSessionListener(),
103                     sshClient).initialize(ch, sessionPromise));
104     }
105
106     private Future<Void> createReconnectingSshClient(
107             final NetconfReconnectingClientConfiguration currentConfiguration) {
108         LOG.debug("Creating reconnecting SSH client with configuration: {}", currentConfiguration);
109         final SshClientChannelInitializer init = new SshClientChannelInitializer(currentConfiguration.getAuthHandler(),
110                 getNegotiatorFactory(currentConfiguration), currentConfiguration.getSessionListener(), sshClient);
111
112         return super.createReconnectingClient(currentConfiguration.getAddress(), currentConfiguration
113                 .getConnectStrategyFactory(), currentConfiguration.getReconnectStrategy(),
114                 init::initialize);
115     }
116
117     private Future<NetconfClientSession> createTlsClient(final NetconfClientConfiguration currentConfiguration) {
118         LOG.debug("Creating TLS client with configuration: {}", currentConfiguration);
119         return super.createClient(currentConfiguration.getAddress(), currentConfiguration.getReconnectStrategy(),
120             (ch, sessionPromise) -> new TlsClientChannelInitializer(currentConfiguration.getSslHandlerFactory(),
121                     getNegotiatorFactory(currentConfiguration), currentConfiguration.getSessionListener())
122                     .initialize(ch, sessionPromise));
123     }
124
125     private Future<Void> createReconnectingTlsClient(
126             final NetconfReconnectingClientConfiguration currentConfiguration) {
127         LOG.debug("Creating reconnecting TLS client with configuration: {}", currentConfiguration);
128         final TlsClientChannelInitializer init = new TlsClientChannelInitializer(
129                 currentConfiguration.getSslHandlerFactory(), getNegotiatorFactory(currentConfiguration),
130                 currentConfiguration.getSessionListener());
131
132         return super.createReconnectingClient(currentConfiguration.getAddress(), currentConfiguration
133                 .getConnectStrategyFactory(), currentConfiguration.getReconnectStrategy(),
134                 init::initialize);
135     }
136
137     protected NetconfClientSessionNegotiatorFactory getNegotiatorFactory(final NetconfClientConfiguration cfg) {
138         final List<Uri> odlHelloCapabilities = cfg.getOdlHelloCapabilities();
139         if (odlHelloCapabilities == null || odlHelloCapabilities.isEmpty()) {
140             return new NetconfClientSessionNegotiatorFactory(timer, cfg.getAdditionalHeader(),
141                     cfg.getConnectionTimeoutMillis());
142         } else {
143             // LinkedHashSet since perhaps the device cares about order of hello message capabilities.
144             // This allows user control of the order while complying with the existing interface.
145             final Set<String> stringCapabilities = new LinkedHashSet<>();
146             for (final Uri uri : odlHelloCapabilities) {
147                 stringCapabilities.add(uri.getValue());
148             }
149             return new NetconfClientSessionNegotiatorFactory(timer, cfg.getAdditionalHeader(),
150                     cfg.getConnectionTimeoutMillis(), stringCapabilities);
151         }
152     }
153 }