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