%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%

%% ==========================================================================
%% ----------------------------------------------------------------------------
%% RabbitMQ Stream Plugin
%%
%% See https://www.rabbitmq.com/stream.html for details
%% ----------------------------------------------------------------------------

%  {rabbitmq_stream,
%   [%% Network Configuration - the format is generally the same as for the broker

%% Listen only on localhost (ipv4 & ipv6) on a specific port.
%% {tcp_listeners, [{"127.0.0.1", 5552},
%%                  {"::1",       5552}]},

{mapping, "stream.listeners.tcp", "rabbitmq_stream.tcp_listeners",[
    {datatype, {enum, [none]}}
]}.

{mapping, "stream.listeners.tcp.$name", "rabbitmq_stream.tcp_listeners",[
    {datatype, [integer, ip]}
]}.

{translation, "rabbitmq_stream.tcp_listeners",
fun(Conf) ->
    case cuttlefish:conf_get("stream.listeners.tcp", Conf, undefined) of
        none -> [];
        _ ->
            Settings = cuttlefish_variable:filter_by_prefix("stream.listeners.tcp", Conf),
            [ V || {_, V} <- Settings ]
    end
end}.

{mapping, "stream.tcp_listen_options", "rabbitmq_stream.tcp_listen_options", [
    {datatype, {enum, [none]}}]}.

{translation, "rabbitmq_stream.tcp_listen_options",
fun(Conf) ->
    case cuttlefish:conf_get("stream.tcp_listen_options", Conf, undefined) of
        none -> [];
        _    -> cuttlefish:invalid("Invalid stream.tcp_listen_options")
    end
end}.

{mapping, "stream.tcp_listen_options.backlog", "rabbitmq_stream.tcp_listen_options.backlog", [
    {datatype, integer}
]}.

{mapping, "stream.tcp_listen_options.nodelay", "rabbitmq_stream.tcp_listen_options.nodelay", [
    {datatype, {enum, [true, false]}}
]}.

{mapping, "stream.tcp_listen_options.buffer", "rabbitmq_stream.tcp_listen_options.buffer",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.delay_send", "rabbitmq_stream.tcp_listen_options.delay_send",
    [{datatype, {enum, [true, false]}}]}.

{mapping, "stream.tcp_listen_options.dontroute", "rabbitmq_stream.tcp_listen_options.dontroute",
    [{datatype, {enum, [true, false]}}]}.

{mapping, "stream.tcp_listen_options.exit_on_close", "rabbitmq_stream.tcp_listen_options.exit_on_close",
    [{datatype, {enum, [true, false]}}]}.

{mapping, "stream.tcp_listen_options.fd", "rabbitmq_stream.tcp_listen_options.fd",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.high_msgq_watermark", "rabbitmq_stream.tcp_listen_options.high_msgq_watermark",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.high_watermark", "rabbitmq_stream.tcp_listen_options.high_watermark",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.keepalive", "rabbitmq_stream.tcp_listen_options.keepalive",
    [{datatype, {enum, [true, false]}}]}.

{mapping, "stream.tcp_listen_options.low_msgq_watermark", "rabbitmq_stream.tcp_listen_options.low_msgq_watermark",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.low_watermark", "rabbitmq_stream.tcp_listen_options.low_watermark",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.port", "rabbitmq_stream.tcp_listen_options.port",
    [{datatype, integer}, {validators, ["port"]}]}.

{mapping, "stream.tcp_listen_options.priority", "rabbitmq_stream.tcp_listen_options.priority",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.recbuf", "rabbitmq_stream.tcp_listen_options.recbuf",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.send_timeout", "rabbitmq_stream.tcp_listen_options.send_timeout",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.send_timeout_close", "rabbitmq_stream.tcp_listen_options.send_timeout_close",
    [{datatype, {enum, [true, false]}}]}.

{mapping, "stream.tcp_listen_options.sndbuf", "rabbitmq_stream.tcp_listen_options.sndbuf",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.tos", "rabbitmq_stream.tcp_listen_options.tos",
    [{datatype, integer}]}.

{mapping, "stream.tcp_listen_options.linger.on", "rabbitmq_stream.tcp_listen_options.linger",
    [{datatype, {enum, [true, false]}}]}.

{mapping, "stream.tcp_listen_options.linger.timeout", "rabbitmq_stream.tcp_listen_options.linger",
    [{datatype, integer}, {validators, ["non_negative_integer"]}]}.

{translation, "rabbitmq_stream.tcp_listen_options.linger",
fun(Conf) ->
    LingerOn = cuttlefish:conf_get("stream.tcp_listen_options.linger.on", Conf, false),
    LingerTimeout = cuttlefish:conf_get("stream.tcp_listen_options.linger.timeout", Conf, 0),
    {LingerOn, LingerTimeout}
end}.

%% Number of Erlang processes that will accept connections for the TCP listener
%%
%% {num_tcp_acceptors, 10},

{mapping, "stream.num_acceptors.tcp", "rabbitmq_stream.num_tcp_acceptors", [
    {datatype, integer}
]}.


%%
%% TLS
%%

{mapping, "stream.listeners.ssl", "rabbitmq_stream.ssl_listeners",[
    {datatype, {enum, [none]}}
]}.

{mapping, "stream.listeners.ssl.$name", "rabbitmq_stream.ssl_listeners",[
    {datatype, [integer, ip]}
]}.

{translation, "rabbitmq_stream.ssl_listeners",
fun(Conf) ->
    case cuttlefish:conf_get("stream.listeners.ssl", Conf, undefined) of
        none -> [];
        _ ->
            Settings = cuttlefish_variable:filter_by_prefix("stream.listeners.ssl", Conf),
            [ V || {_, V} <- Settings ]
    end
end}.

%% Number of Erlang processes that will accept connections for the SSL listeners.
%%
%% {num_ssl_acceptors, 10},

{mapping, "stream.num_acceptors.ssl", "rabbitmq_stream.num_ssl_acceptors", [
    {datatype, integer}
]}.

%% Additional TLS options

%% Extract a name from the client's certificate when using TLS.
%%
%% Defaults to true.

{mapping, "stream.ssl_cert_login", "rabbitmq_stream.ssl_cert_login",
    [{datatype, {enum, [true, false]}}]}.

{mapping, "stream.initial_credits", "rabbitmq_stream.initial_credits", [
    {datatype, integer}
]}.

{mapping, "stream.credits_required_for_unblocking", "rabbitmq_stream.credits_required_for_unblocking", [
    {datatype, integer}
]}.

{mapping, "stream.frame_max", "rabbitmq_stream.frame_max", [
    {datatype, integer}
]}.

{mapping, "stream.heartbeat", "rabbitmq_stream.heartbeat", [
    {datatype, integer}
]}.

{mapping, "stream.advertised_host", "rabbitmq_stream.advertised_host", [
    {datatype, string}
]}.

{translation, "rabbitmq_stream.advertised_host",
fun(Conf) ->
    list_to_binary(cuttlefish:conf_get("stream.advertised_host", Conf))
end}.

{mapping, "stream.advertised_tls_host", "rabbitmq_stream.advertised_tls_host", [
    {datatype, string}
]}.

{translation, "rabbitmq_stream.advertised_tls_host",
fun(Conf) ->
    list_to_binary(cuttlefish:conf_get("stream.advertised_tls_host", Conf))
end}.

{mapping, "stream.advertised_port", "rabbitmq_stream.advertised_port", [
    {datatype, integer}
]}.

{mapping, "stream.advertised_tls_port", "rabbitmq_stream.advertised_tls_port", [
    {datatype, integer}
]}.
