ce368e7cdf33ab3d42b479190e26a6526e40168d
[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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev230417.SshClientGrouping;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev230417.TcpClientGrouping;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tls.client.rev230417.TlsClientGrouping;
25
26 /**
27  * Builder for {@link NetconfClientConfiguration}.
28  */
29 public class NetconfClientConfigurationBuilder {
30
31     public static final int DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000;
32     public static final NetconfClientConfiguration.NetconfClientProtocol DEFAULT_CLIENT_PROTOCOL =
33         NetconfClientConfiguration.NetconfClientProtocol.TCP;
34
35     private InetSocketAddress address;
36     private long connectionTimeoutMillis = DEFAULT_CONNECTION_TIMEOUT_MILLIS;
37     private NetconfHelloMessageAdditionalHeader additionalHeader;
38     private NetconfClientSessionListener sessionListener;
39     private AuthenticationHandler authHandler;
40     private NetconfClientConfiguration.NetconfClientProtocol clientProtocol = DEFAULT_CLIENT_PROTOCOL;
41     private SslHandlerFactory sslHandlerFactory;
42     private NetconfSshClient sshClient;
43     private List<Uri> odlHelloCapabilities;
44     private @NonNegative int maximumIncomingChunkSize =
45         NetconfSessionNegotiator.DEFAULT_MAXIMUM_INCOMING_CHUNK_SIZE;
46     private String name;
47     private TcpClientGrouping tcpParameters;
48     private TlsClientGrouping tlsParameters;
49     private org.opendaylight.netconf.transport.tls.SslHandlerFactory transportSslHandlerFactory;
50     private SshClientGrouping sshParameters;
51
52     protected NetconfClientConfigurationBuilder() {
53     }
54
55     public static NetconfClientConfigurationBuilder create() {
56         return new NetconfClientConfigurationBuilder();
57     }
58
59     /**
60      * Set remote address.
61      *
62      * @param address remote address
63      * @return current builder instance
64      * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
65      *     ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
66      *     expects remote address defined via TCP transport configuration {@link #withTcpParameters(TcpClientGrouping)}
67      */
68     @Deprecated
69     @SuppressWarnings("checkstyle:hiddenField")
70     public NetconfClientConfigurationBuilder withAddress(final InetSocketAddress address) {
71         this.address = address;
72         return this;
73     }
74
75     /**
76      * Set connection timeout value in milliseconds.
77      *
78      * @param connectionTimeoutMillis value
79      * @return current builder instance
80      */
81     @SuppressWarnings("checkstyle:hiddenField")
82     public NetconfClientConfigurationBuilder withConnectionTimeoutMillis(final long connectionTimeoutMillis) {
83         this.connectionTimeoutMillis = connectionTimeoutMillis;
84         return this;
85     }
86
87     /**
88      * Set client protocol.
89      *
90      * @param clientProtocol client protocol
91      * @return current builder instance
92      */
93     @SuppressWarnings("checkstyle:hiddenField")
94     public NetconfClientConfigurationBuilder withProtocol(
95         final NetconfClientConfiguration.NetconfClientProtocol clientProtocol) {
96         this.clientProtocol = clientProtocol;
97         return this;
98     }
99
100     /**
101      * Set additional header for Hello message.
102      *
103      * @param additionalHeader additional header
104      * @return current builder instance
105      */
106     @SuppressWarnings("checkstyle:hiddenField")
107     public NetconfClientConfigurationBuilder withAdditionalHeader(
108         final NetconfHelloMessageAdditionalHeader additionalHeader) {
109         this.additionalHeader = additionalHeader;
110         return this;
111     }
112
113     /**
114      * Set NETCONF session client listener.
115      *
116      * @param sessionListener session listener
117      * @return current builder instance
118      */
119     @SuppressWarnings("checkstyle:hiddenField")
120     public NetconfClientConfigurationBuilder withSessionListener(final NetconfClientSessionListener sessionListener) {
121         this.sessionListener = sessionListener;
122         return this;
123     }
124
125     /**
126      * Set authentication handler. Used for SSH authentication.
127      *
128      * @param authHandler authentication handler
129      * @return current builder instance
130      * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
131      *     ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
132      *     expects SSH transport overlay configuration via {@link #withSshParameters(SshClientGrouping)}
133      */
134     @Deprecated
135     @SuppressWarnings("checkstyle:hiddenField")
136     public NetconfClientConfigurationBuilder withAuthHandler(final AuthenticationHandler authHandler) {
137         this.authHandler = authHandler;
138         return this;
139     }
140
141     /**
142      * Set SSL Handler factory.
143      *
144      * @param sslHandlerFactory ssh handler instance
145      * @return current builder instance
146      * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
147      *     ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
148      *     expects TLS transport overlay configuration via {@link #withTlsParameters(TlsClientGrouping)}
149      */
150     @Deprecated
151     @SuppressWarnings("checkstyle:hiddenField")
152     public NetconfClientConfigurationBuilder withSslHandlerFactory(final SslHandlerFactory sslHandlerFactory) {
153         this.sslHandlerFactory = sslHandlerFactory;
154         return this;
155     }
156
157     /**
158      * Set SSH client instance for use as SSH transport overlay.
159      *
160      * @param sshClient ssh client instance
161      * @return current builder instance
162      * @deprecated due to design change. Only used with {@link org.opendaylight.netconf.client.NetconfClientDispatcher},
163      *     ignored when building configuration for {@link org.opendaylight.netconf.client.NetconfClientFactory} which
164      *     expects SSH transport overlay configuration via {@link #withSshParameters(SshClientGrouping)}
165      */
166     @Deprecated
167     @SuppressWarnings("checkstyle:hiddenField")
168     public NetconfClientConfigurationBuilder withSshClient(final NetconfSshClient sshClient) {
169         this.sshClient = sshClient;
170         return this;
171     }
172
173     /**
174      * Set client name.
175      *
176      * @param name value
177      * @return current builder instance
178      */
179     @SuppressWarnings("checkstyle:hiddenField")
180     public NetconfClientConfigurationBuilder withName(final String name) {
181         this.name = name;
182         return this;
183     }
184
185     /**
186      * Set capabilities for Hello message.
187      *
188      * @param odlHelloCapabilities capabilities
189      * @return current builder instance
190      */
191     @SuppressWarnings("checkstyle:hiddenField")
192     public NetconfClientConfigurationBuilder withOdlHelloCapabilities(final List<Uri> odlHelloCapabilities) {
193         this.odlHelloCapabilities = odlHelloCapabilities;
194         return this;
195     }
196
197     /**
198      * Set max size of incoming data chink in bytes. Positive value is required.
199      *
200      * @param maximumIncomingChunkSize value
201      * @return current builder instance
202      * @throws IllegalArgumentException if value zero or less
203      */
204     @SuppressWarnings("checkstyle:hiddenField")
205     public NetconfClientConfigurationBuilder withMaximumIncomingChunkSize(
206         final @NonNegative int maximumIncomingChunkSize) {
207         checkArgument(maximumIncomingChunkSize > 0);
208         this.maximumIncomingChunkSize = maximumIncomingChunkSize;
209         return this;
210     }
211
212     /**
213      * Set TCP client transport parameters.
214      *
215      * @param tcpParameters parameters
216      * @return current builder instance
217      */
218     @SuppressWarnings("checkstyle:hiddenField")
219     public NetconfClientConfigurationBuilder withTcpParameters(final TcpClientGrouping tcpParameters) {
220         this.tcpParameters = tcpParameters;
221         return this;
222     }
223
224     /**
225      * Set TLS client transport parameters.
226      *
227      * @param tlsParameters parameters
228      * @return current builder instance
229      */
230     @SuppressWarnings("checkstyle:hiddenField")
231     public NetconfClientConfigurationBuilder withTlsParameters(final TlsClientGrouping tlsParameters) {
232         this.tlsParameters = tlsParameters;
233         return this;
234     }
235
236     /**
237      * Set SslHandlerFactory for TLS transport.
238      *
239      * @param transportSslHandlerFactory ssl handler factory
240      * @return current builder instance
241      */
242     @SuppressWarnings("checkstyle:hiddenField")
243     public NetconfClientConfigurationBuilder withTransportSslHandlerFactory(
244             final org.opendaylight.netconf.transport.tls.SslHandlerFactory transportSslHandlerFactory) {
245         this.transportSslHandlerFactory = transportSslHandlerFactory;
246         return this;
247     }
248
249     /**
250      * Set SSH client transport parameters.
251      *
252      * @param sshParameters SSH parameters
253      * @return current builder instance
254      */
255     @SuppressWarnings("checkstyle:hiddenField")
256     public NetconfClientConfigurationBuilder withSshParameters(final SshClientGrouping sshParameters) {
257         this.sshParameters = sshParameters;
258         return this;
259     }
260
261     final InetSocketAddress getAddress() {
262         return address;
263     }
264
265     final long getConnectionTimeoutMillis() {
266         return connectionTimeoutMillis;
267     }
268
269     final NetconfHelloMessageAdditionalHeader getAdditionalHeader() {
270         return additionalHeader;
271     }
272
273     final NetconfClientSessionListener getSessionListener() {
274         return sessionListener;
275     }
276
277     final AuthenticationHandler getAuthHandler() {
278         return authHandler;
279     }
280
281     final NetconfClientConfiguration.NetconfClientProtocol getProtocol() {
282         return clientProtocol;
283     }
284
285     final SslHandlerFactory getSslHandlerFactory() {
286         return sslHandlerFactory;
287     }
288
289     public NetconfSshClient getSshClient() {
290         return sshClient;
291     }
292
293     final List<Uri> getOdlHelloCapabilities() {
294         return odlHelloCapabilities;
295     }
296
297     final @NonNegative int getMaximumIncomingChunkSize() {
298         return maximumIncomingChunkSize;
299     }
300
301     final String getName() {
302         return name;
303     }
304
305     /**
306      * Builds configuration based on parameters provided.
307      *
308      * @return immutable configuration instance
309      */
310     public NetconfClientConfiguration build() {
311         return tcpParameters == null
312             // legacy configuration
313             ? new NetconfClientConfiguration(clientProtocol, address, connectionTimeoutMillis, additionalHeader,
314                 sessionListener, authHandler, sslHandlerFactory, sshClient, odlHelloCapabilities,
315                 maximumIncomingChunkSize, name)
316             // new configuration
317             : new NetconfClientConfiguration(clientProtocol, tcpParameters, tlsParameters, transportSslHandlerFactory,
318                 sshParameters, sessionListener, odlHelloCapabilities, connectionTimeoutMillis,
319                 maximumIncomingChunkSize, additionalHeader, name);
320     }
321 }