45a3a36190c62512a071ce4cc39935a5cbd41acc
[netconf.git] / transport / transport-http / src / main / java / org / opendaylight / netconf / transport / http / HTTPClient.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech s.r.o. 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.transport.http;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.handler.codec.http.FullHttpRequest;
15 import io.netty.handler.codec.http.FullHttpResponse;
16 import org.opendaylight.netconf.transport.api.TransportChannelListener;
17 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
18 import org.opendaylight.netconf.transport.tcp.TCPClient;
19 import org.opendaylight.netconf.transport.tls.TLSClient;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.http.client.rev240208.HttpClientGrouping;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.http.client.rev240208.HttpClientStackGrouping;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.http.client.rev240208.http.client.stack.grouping.transport.Tcp;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.http.client.rev240208.http.client.stack.grouping.transport.Tls;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.client.rev240208.TcpClientGrouping;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tls.client.rev240208.TlsClientGrouping;
26
27 /**
28  * A {@link HTTPTransportStack} acting as a client.
29  */
30 public final class HTTPClient extends HTTPTransportStack {
31
32     private final RequestDispatcher dispatcher;
33
34     private HTTPClient(final TransportChannelListener listener, final HttpChannelInitializer channelInitializer,
35             final RequestDispatcher dispatcher) {
36         super(listener, channelInitializer);
37         this.dispatcher = dispatcher;
38     }
39
40     /**
41      * Invokes the HTTP request over established connection.
42      *
43      * @param request the full http request object
44      * @return a future providing full http response or cause in case of error
45      */
46     public ListenableFuture<FullHttpResponse> invoke(final FullHttpRequest request) {
47         return dispatcher.dispatch(requireNonNull(request));
48     }
49
50     /**
51      * Attempt to establish a {@link HTTPClient} by connecting to a remote address.
52      *
53      * @param listener {@link TransportChannelListener} to notify when the session is established
54      * @param bootstrap Client {@link Bootstrap} to use for the underlying Netty channel
55      * @param connectParams Connection parameters
56      * @param http2 indicates HTTP/2 protocol to be used
57      * @return A future
58      * @throws UnsupportedConfigurationException when {@code connectParams} contains an unsupported options
59      * @throws NullPointerException if any argument is {@code null}
60      */
61     public static ListenableFuture<HTTPClient> connect(final TransportChannelListener listener,
62             final Bootstrap bootstrap, final HttpClientStackGrouping connectParams, final boolean http2)
63             throws UnsupportedConfigurationException {
64         final HttpClientGrouping httpParams;
65         final TcpClientGrouping tcpParams;
66         final TlsClientGrouping tlsParams;
67         final var transport = requireNonNull(connectParams).getTransport();
68         if (transport instanceof Tcp tcp) {
69             httpParams = tcp.getTcp().getHttpClientParameters();
70             tcpParams = tcp.getTcp().nonnullTcpClientParameters();
71             tlsParams = null;
72         } else if (transport instanceof Tls tls) {
73             httpParams = tls.getTls().getHttpClientParameters();
74             tcpParams = tls.getTls().nonnullTcpClientParameters();
75             tlsParams = tls.getTls().nonnullTlsClientParameters();
76         } else {
77             throw new UnsupportedConfigurationException("Unsupported transport: " + transport);
78         }
79         final var dispatcher = http2 ? new ClientHttp2RequestDispatcher() : new ClientHttp1RequestDispatcher();
80         final var client = new HTTPClient(listener, new ClientChannelInitializer(httpParams, dispatcher, http2),
81             dispatcher);
82         final var underlay = tlsParams == null
83             ? TCPClient.connect(client.asListener(), bootstrap, tcpParams)
84             : TLSClient.connect(client.asListener(), bootstrap, tcpParams, new HttpSslHandlerFactory(tlsParams, http2));
85         return transformUnderlay(client, underlay);
86     }
87 }