2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.client.conf;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import java.net.InetSocketAddress;
13 import java.util.List;
14 import org.checkerframework.checker.index.qual.NonNegative;
15 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
16 import org.opendaylight.netconf.client.NetconfClientSessionListener;
17 import org.opendaylight.netconf.client.SslHandlerFactory;
18 import org.opendaylight.netconf.nettyutil.NetconfSessionNegotiator;
19 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
20 import org.opendaylight.netconf.nettyutil.handler.ssh.client.NetconfSshClient;
21 import org.opendaylight.netconf.transport.ssh.ClientFactoryManagerConfigurator;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev230417.SshClientGrouping;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev230417.TcpClientGrouping;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tls.client.rev230417.TlsClientGrouping;
28 * Builder for {@link NetconfClientConfiguration}.
30 public class NetconfClientConfigurationBuilder {
32 public static final int DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000;
33 public static final NetconfClientConfiguration.NetconfClientProtocol DEFAULT_CLIENT_PROTOCOL =
34 NetconfClientConfiguration.NetconfClientProtocol.TCP;
36 private InetSocketAddress address;
37 private long connectionTimeoutMillis = DEFAULT_CONNECTION_TIMEOUT_MILLIS;
38 private NetconfHelloMessageAdditionalHeader additionalHeader;
39 private NetconfClientSessionListener sessionListener;
40 private AuthenticationHandler authHandler;
41 private NetconfClientConfiguration.NetconfClientProtocol clientProtocol = DEFAULT_CLIENT_PROTOCOL;
42 private SslHandlerFactory sslHandlerFactory;
43 private NetconfSshClient sshClient;
44 private List<Uri> odlHelloCapabilities;
45 private @NonNegative int maximumIncomingChunkSize =
46 NetconfSessionNegotiator.DEFAULT_MAXIMUM_INCOMING_CHUNK_SIZE;
48 private TcpClientGrouping tcpParameters;
49 private TlsClientGrouping tlsParameters;
50 private org.opendaylight.netconf.transport.tls.SslHandlerFactory transportSslHandlerFactory;
51 private SshClientGrouping sshParameters;
52 private ClientFactoryManagerConfigurator sshConfigurator;
54 protected NetconfClientConfigurationBuilder() {
57 public static NetconfClientConfigurationBuilder create() {
58 return new NetconfClientConfigurationBuilder();
64 * @param address remote address
65 * @return current builder instance
66 * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
67 * ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
68 * expects remote address defined via TCP transport configuration {@link #withTcpParameters(TcpClientGrouping)}
71 @SuppressWarnings("checkstyle:hiddenField")
72 public NetconfClientConfigurationBuilder withAddress(final InetSocketAddress address) {
73 this.address = address;
78 * Set connection timeout value in milliseconds.
80 * @param connectionTimeoutMillis value
81 * @return current builder instance
83 @SuppressWarnings("checkstyle:hiddenField")
84 public NetconfClientConfigurationBuilder withConnectionTimeoutMillis(final long connectionTimeoutMillis) {
85 this.connectionTimeoutMillis = connectionTimeoutMillis;
90 * Set client protocol.
92 * @param clientProtocol client protocol
93 * @return current builder instance
95 @SuppressWarnings("checkstyle:hiddenField")
96 public NetconfClientConfigurationBuilder withProtocol(
97 final NetconfClientConfiguration.NetconfClientProtocol clientProtocol) {
98 this.clientProtocol = clientProtocol;
103 * Set additional header for Hello message.
105 * @param additionalHeader additional header
106 * @return current builder instance
108 @SuppressWarnings("checkstyle:hiddenField")
109 public NetconfClientConfigurationBuilder withAdditionalHeader(
110 final NetconfHelloMessageAdditionalHeader additionalHeader) {
111 this.additionalHeader = additionalHeader;
116 * Set NETCONF session client listener.
118 * @param sessionListener session listener
119 * @return current builder instance
121 @SuppressWarnings("checkstyle:hiddenField")
122 public NetconfClientConfigurationBuilder withSessionListener(final NetconfClientSessionListener sessionListener) {
123 this.sessionListener = sessionListener;
128 * Set authentication handler. Used for SSH authentication.
130 * @param authHandler authentication handler
131 * @return current builder instance
132 * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
133 * ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
134 * expects SSH transport overlay configuration via {@link #withSshParameters(SshClientGrouping)}
137 @SuppressWarnings("checkstyle:hiddenField")
138 public NetconfClientConfigurationBuilder withAuthHandler(final AuthenticationHandler authHandler) {
139 this.authHandler = authHandler;
144 * Set SSL Handler factory.
146 * @param sslHandlerFactory ssh handler instance
147 * @return current builder instance
148 * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
149 * ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
150 * expects TLS transport overlay configuration via {@link #withTlsParameters(TlsClientGrouping)}
153 @SuppressWarnings("checkstyle:hiddenField")
154 public NetconfClientConfigurationBuilder withSslHandlerFactory(final SslHandlerFactory sslHandlerFactory) {
155 this.sslHandlerFactory = sslHandlerFactory;
160 * Set SSH client instance for use as SSH transport overlay.
162 * @param sshClient ssh client instance
163 * @return current builder instance
164 * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
165 * ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
166 * expects SSH transport overlay configuration via {@link #withSshParameters(SshClientGrouping)}
169 @SuppressWarnings("checkstyle:hiddenField")
170 public NetconfClientConfigurationBuilder withSshClient(final NetconfSshClient sshClient) {
171 this.sshClient = sshClient;
179 * @return current builder instance
181 @SuppressWarnings("checkstyle:hiddenField")
182 public NetconfClientConfigurationBuilder withName(final String name) {
188 * Set capabilities for Hello message.
190 * @param odlHelloCapabilities capabilities
191 * @return current builder instance
193 @SuppressWarnings("checkstyle:hiddenField")
194 public NetconfClientConfigurationBuilder withOdlHelloCapabilities(final List<Uri> odlHelloCapabilities) {
195 this.odlHelloCapabilities = odlHelloCapabilities;
200 * Set max size of incoming data chink in bytes. Positive value is required.
202 * @param maximumIncomingChunkSize value
203 * @return current builder instance
204 * @throws IllegalArgumentException if value zero or less
206 @SuppressWarnings("checkstyle:hiddenField")
207 public NetconfClientConfigurationBuilder withMaximumIncomingChunkSize(
208 final @NonNegative int maximumIncomingChunkSize) {
209 checkArgument(maximumIncomingChunkSize > 0);
210 this.maximumIncomingChunkSize = maximumIncomingChunkSize;
215 * Set TCP client transport parameters.
217 * @param tcpParameters parameters
218 * @return current builder instance
220 @SuppressWarnings("checkstyle:hiddenField")
221 public NetconfClientConfigurationBuilder withTcpParameters(final TcpClientGrouping tcpParameters) {
222 this.tcpParameters = tcpParameters;
227 * Set TLS client transport parameters.
229 * @param tlsParameters parameters
230 * @return current builder instance
232 @SuppressWarnings("checkstyle:hiddenField")
233 public NetconfClientConfigurationBuilder withTlsParameters(final TlsClientGrouping tlsParameters) {
234 this.tlsParameters = tlsParameters;
239 * Set SslHandlerFactory for TLS transport.
241 * @param transportSslHandlerFactory ssl handler factory
242 * @return current builder instance
244 @SuppressWarnings("checkstyle:hiddenField")
245 public NetconfClientConfigurationBuilder withTransportSslHandlerFactory(
246 final org.opendaylight.netconf.transport.tls.SslHandlerFactory transportSslHandlerFactory) {
247 this.transportSslHandlerFactory = transportSslHandlerFactory;
252 * Set SSH client transport parameters.
254 * @param sshParameters SSH parameters
255 * @return current builder instance
257 @SuppressWarnings("checkstyle:hiddenField")
258 public NetconfClientConfigurationBuilder withSshParameters(final SshClientGrouping sshParameters) {
259 this.sshParameters = sshParameters;
264 * Set SSH Client Factory Manager configurator.
266 * @param sshConfigurator configurator
267 * @return current builder instance
269 @SuppressWarnings("checkstyle:hiddenField")
270 public NetconfClientConfigurationBuilder withSshConfigurator(
271 final ClientFactoryManagerConfigurator sshConfigurator) {
272 this.sshConfigurator = sshConfigurator;
276 final InetSocketAddress getAddress() {
280 final long getConnectionTimeoutMillis() {
281 return connectionTimeoutMillis;
284 final NetconfHelloMessageAdditionalHeader getAdditionalHeader() {
285 return additionalHeader;
288 final NetconfClientSessionListener getSessionListener() {
289 return sessionListener;
292 final AuthenticationHandler getAuthHandler() {
296 final NetconfClientConfiguration.NetconfClientProtocol getProtocol() {
297 return clientProtocol;
300 final SslHandlerFactory getSslHandlerFactory() {
301 return sslHandlerFactory;
304 public NetconfSshClient getSshClient() {
308 final List<Uri> getOdlHelloCapabilities() {
309 return odlHelloCapabilities;
312 final @NonNegative int getMaximumIncomingChunkSize() {
313 return maximumIncomingChunkSize;
316 final String getName() {
321 * Builds configuration based on parameters provided.
323 * @return immutable configuration instance
325 public NetconfClientConfiguration build() {
326 return tcpParameters == null
327 // legacy configuration
328 ? new NetconfClientConfiguration(clientProtocol, address, connectionTimeoutMillis, additionalHeader,
329 sessionListener, authHandler, sslHandlerFactory, sshClient, odlHelloCapabilities,
330 maximumIncomingChunkSize, name)
332 : new NetconfClientConfiguration(clientProtocol, tcpParameters, tlsParameters, transportSslHandlerFactory,
333 sshParameters, sshConfigurator, sessionListener, odlHelloCapabilities, connectionTimeoutMillis,
334 maximumIncomingChunkSize, additionalHeader, name);