Ext service integration support for Netconf SSH client
[netconf.git] / protocol / netconf-client / src / main / java / org / opendaylight / netconf / client / conf / NetconfClientConfigurationBuilder.java
1 /*
2  * Copyright (c) 2014 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.conf;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
26
27 /**
28  * Builder for {@link NetconfClientConfiguration}.
29  */
30 public class NetconfClientConfigurationBuilder {
31
32     public static final int DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000;
33     public static final NetconfClientConfiguration.NetconfClientProtocol DEFAULT_CLIENT_PROTOCOL =
34         NetconfClientConfiguration.NetconfClientProtocol.TCP;
35
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;
47     private String name;
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;
53
54     protected NetconfClientConfigurationBuilder() {
55     }
56
57     public static NetconfClientConfigurationBuilder create() {
58         return new NetconfClientConfigurationBuilder();
59     }
60
61     /**
62      * Set remote address.
63      *
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)}
69      */
70     @Deprecated
71     @SuppressWarnings("checkstyle:hiddenField")
72     public NetconfClientConfigurationBuilder withAddress(final InetSocketAddress address) {
73         this.address = address;
74         return this;
75     }
76
77     /**
78      * Set connection timeout value in milliseconds.
79      *
80      * @param connectionTimeoutMillis value
81      * @return current builder instance
82      */
83     @SuppressWarnings("checkstyle:hiddenField")
84     public NetconfClientConfigurationBuilder withConnectionTimeoutMillis(final long connectionTimeoutMillis) {
85         this.connectionTimeoutMillis = connectionTimeoutMillis;
86         return this;
87     }
88
89     /**
90      * Set client protocol.
91      *
92      * @param clientProtocol client protocol
93      * @return current builder instance
94      */
95     @SuppressWarnings("checkstyle:hiddenField")
96     public NetconfClientConfigurationBuilder withProtocol(
97         final NetconfClientConfiguration.NetconfClientProtocol clientProtocol) {
98         this.clientProtocol = clientProtocol;
99         return this;
100     }
101
102     /**
103      * Set additional header for Hello message.
104      *
105      * @param additionalHeader additional header
106      * @return current builder instance
107      */
108     @SuppressWarnings("checkstyle:hiddenField")
109     public NetconfClientConfigurationBuilder withAdditionalHeader(
110         final NetconfHelloMessageAdditionalHeader additionalHeader) {
111         this.additionalHeader = additionalHeader;
112         return this;
113     }
114
115     /**
116      * Set NETCONF session client listener.
117      *
118      * @param sessionListener session listener
119      * @return current builder instance
120      */
121     @SuppressWarnings("checkstyle:hiddenField")
122     public NetconfClientConfigurationBuilder withSessionListener(final NetconfClientSessionListener sessionListener) {
123         this.sessionListener = sessionListener;
124         return this;
125     }
126
127     /**
128      * Set authentication handler. Used for SSH authentication.
129      *
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)}
135      */
136     @Deprecated
137     @SuppressWarnings("checkstyle:hiddenField")
138     public NetconfClientConfigurationBuilder withAuthHandler(final AuthenticationHandler authHandler) {
139         this.authHandler = authHandler;
140         return this;
141     }
142
143     /**
144      * Set SSL Handler factory.
145      *
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)}
151      */
152     @Deprecated
153     @SuppressWarnings("checkstyle:hiddenField")
154     public NetconfClientConfigurationBuilder withSslHandlerFactory(final SslHandlerFactory sslHandlerFactory) {
155         this.sslHandlerFactory = sslHandlerFactory;
156         return this;
157     }
158
159     /**
160      * Set SSH client instance for use as SSH transport overlay.
161      *
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)}
167      */
168     @Deprecated
169     @SuppressWarnings("checkstyle:hiddenField")
170     public NetconfClientConfigurationBuilder withSshClient(final NetconfSshClient sshClient) {
171         this.sshClient = sshClient;
172         return this;
173     }
174
175     /**
176      * Set client name.
177      *
178      * @param name value
179      * @return current builder instance
180      */
181     @SuppressWarnings("checkstyle:hiddenField")
182     public NetconfClientConfigurationBuilder withName(final String name) {
183         this.name = name;
184         return this;
185     }
186
187     /**
188      * Set capabilities for Hello message.
189      *
190      * @param odlHelloCapabilities capabilities
191      * @return current builder instance
192      */
193     @SuppressWarnings("checkstyle:hiddenField")
194     public NetconfClientConfigurationBuilder withOdlHelloCapabilities(final List<Uri> odlHelloCapabilities) {
195         this.odlHelloCapabilities = odlHelloCapabilities;
196         return this;
197     }
198
199     /**
200      * Set max size of incoming data chink in bytes. Positive value is required.
201      *
202      * @param maximumIncomingChunkSize value
203      * @return current builder instance
204      * @throws IllegalArgumentException if value zero or less
205      */
206     @SuppressWarnings("checkstyle:hiddenField")
207     public NetconfClientConfigurationBuilder withMaximumIncomingChunkSize(
208         final @NonNegative int maximumIncomingChunkSize) {
209         checkArgument(maximumIncomingChunkSize > 0);
210         this.maximumIncomingChunkSize = maximumIncomingChunkSize;
211         return this;
212     }
213
214     /**
215      * Set TCP client transport parameters.
216      *
217      * @param tcpParameters parameters
218      * @return current builder instance
219      */
220     @SuppressWarnings("checkstyle:hiddenField")
221     public NetconfClientConfigurationBuilder withTcpParameters(final TcpClientGrouping tcpParameters) {
222         this.tcpParameters = tcpParameters;
223         return this;
224     }
225
226     /**
227      * Set TLS client transport parameters.
228      *
229      * @param tlsParameters parameters
230      * @return current builder instance
231      */
232     @SuppressWarnings("checkstyle:hiddenField")
233     public NetconfClientConfigurationBuilder withTlsParameters(final TlsClientGrouping tlsParameters) {
234         this.tlsParameters = tlsParameters;
235         return this;
236     }
237
238     /**
239      * Set SslHandlerFactory for TLS transport.
240      *
241      * @param transportSslHandlerFactory ssl handler factory
242      * @return current builder instance
243      */
244     @SuppressWarnings("checkstyle:hiddenField")
245     public NetconfClientConfigurationBuilder withTransportSslHandlerFactory(
246             final org.opendaylight.netconf.transport.tls.SslHandlerFactory transportSslHandlerFactory) {
247         this.transportSslHandlerFactory = transportSslHandlerFactory;
248         return this;
249     }
250
251     /**
252      * Set SSH client transport parameters.
253      *
254      * @param sshParameters SSH parameters
255      * @return current builder instance
256      */
257     @SuppressWarnings("checkstyle:hiddenField")
258     public NetconfClientConfigurationBuilder withSshParameters(final SshClientGrouping sshParameters) {
259         this.sshParameters = sshParameters;
260         return this;
261     }
262
263     /**
264      * Set SSH Client Factory Manager configurator.
265      *
266      * @param sshConfigurator configurator
267      * @return current builder instance
268      */
269     @SuppressWarnings("checkstyle:hiddenField")
270     public NetconfClientConfigurationBuilder withSshConfigurator(
271             final ClientFactoryManagerConfigurator sshConfigurator) {
272         this.sshConfigurator = sshConfigurator;
273         return this;
274     }
275
276     final InetSocketAddress getAddress() {
277         return address;
278     }
279
280     final long getConnectionTimeoutMillis() {
281         return connectionTimeoutMillis;
282     }
283
284     final NetconfHelloMessageAdditionalHeader getAdditionalHeader() {
285         return additionalHeader;
286     }
287
288     final NetconfClientSessionListener getSessionListener() {
289         return sessionListener;
290     }
291
292     final AuthenticationHandler getAuthHandler() {
293         return authHandler;
294     }
295
296     final NetconfClientConfiguration.NetconfClientProtocol getProtocol() {
297         return clientProtocol;
298     }
299
300     final SslHandlerFactory getSslHandlerFactory() {
301         return sslHandlerFactory;
302     }
303
304     public NetconfSshClient getSshClient() {
305         return sshClient;
306     }
307
308     final List<Uri> getOdlHelloCapabilities() {
309         return odlHelloCapabilities;
310     }
311
312     final @NonNegative int getMaximumIncomingChunkSize() {
313         return maximumIncomingChunkSize;
314     }
315
316     final String getName() {
317         return name;
318     }
319
320     /**
321      * Builds configuration based on parameters provided.
322      *
323      * @return immutable configuration instance
324      */
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)
331             // new configuration
332             : new NetconfClientConfiguration(clientProtocol, tcpParameters, tlsParameters, transportSslHandlerFactory,
333                 sshParameters, sshConfigurator, sessionListener, odlHelloCapabilities, connectionTimeoutMillis,
334                 maximumIncomingChunkSize, additionalHeader, name);
335     }
336 }