{%- macro write_custom_config_lines(client, af, section) -%}
{% 	 if client.cfg.custom_options and
		"openbgpd" in client.cfg.custom_options and
		af in client.cfg.custom_options["openbgpd"] and
		section in client.cfg.custom_options["openbgpd"][af] and
		client.cfg.custom_options["openbgpd"][af][section].get("config_lines") %}
# Custom options for AF {{ af }} follow
{%     for custom_config_line in client.cfg.custom_options["openbgpd"][af][section]["config_lines"] %}
{{ custom_config_line }}
{%     endfor %}
{%   endif %}
{%- endmacro -%}

# ---------------------------------------------------------
# MEMBERS

{{ "pre-clients"|include_local_file -}}

group "clients" {
{% for client in clients|sort(attribute="id") if client.ip is current_ipver %}

{%	if "rtt" in client and client["rtt"] %}
	# RTT: {{ client["rtt"] }} ms (normalized value: {{ client["rtt"]|get_normalized_rtt }})
{%	endif %}
	neighbor {{ client.ip }} {
		{{ "client"|include_local_file -}}
		remote-as {{ client.asn }}

{% if cfg.path_hiding %}
{%		if "7.5"|target_version_ge and "7.5"|target_version_le and client.cfg.add_path %}
		# This is needed to avoid the bgpd error
		#	"neighbors with add-path send cannot use 'rde evaluate all'"
		# It overrides the global 'rde evaluate all' setting for
		# the neighbors for which ADD-PATH is configured.
		rde evaluate default
{%		else %}
		rde evaluate all
{%		endif %}
{% endif %}

{%	if client.description %}
		descr "{{ client.description|replace("\"", "")|truncate(31, true, "...", 0) }}"
{%	endif %}
{%	if client.cfg.passive %}
		passive
{%	endif %}
{%	if client.cfg.multihop %}
		multihop {{ client.cfg.multihop }}
{%	endif %}
{%	if client.password %}
		tcp md5sig password "{{ client.password }}"
{%	endif %}
{%	if client.cfg.gtsm %}
		ttl-security yes
{%	else %}
		ttl-security no
{%	endif %}
{%	if client.cfg.filtering.max_prefix.action == "shutdown" or
	   client.cfg.filtering.max_prefix.action == "restart" %}
{%      	if client.ip|ipaddr_ver == 4 and client.cfg.filtering.max_prefix.limit_ipv4 %}
{%              	set max_pref_limit = client.cfg.filtering.max_prefix.limit_ipv4 %}
{%      	elif client.ip|ipaddr_ver == 6 and client.cfg.filtering.max_prefix.limit_ipv6 %}
{%              	set max_pref_limit = client.cfg.filtering.max_prefix.limit_ipv6 %}
{%      	else %}
{%              	set max_pref_limit = 0 %}
{%      	endif %}
{%      	if max_pref_limit > 0 %}
{%			if client.cfg.filtering.max_prefix.action == "restart" %}
		max-prefix {{ max_pref_limit }} restart {{ client.cfg.filtering.max_prefix.restart_after }}
{%			else %}
		max-prefix {{ max_pref_limit }}
{%			endif %}
{%		endif %}
{%	endif %}
{%	if client.cfg.prepend_rs_as %}
		transparent-as no
{%	else %}
		transparent-as yes
{%	endif %}
		enforce neighbor-as no

{% if client.cfg.filtering.roles.enabled %}
{%    if "7.8"|target_version_ge %}
		role {{ client.cfg.filtering.roles.get("local_role")|default("rs") }}
{%        if client.cfg.filtering.roles.strict_mode %}
		announce policy enforce
{%        else %}
		announce policy yes
{%        endif %}
{%    elif "7.5"|target_version_ge %}
{%        if client.cfg.filtering.roles.strict_mode %}
		announce policy {{ client.cfg.filtering.roles.get("local_role")|default("rs") }} enforce
{%        else %}
		announce policy {{ client.cfg.filtering.roles.get("local_role")|default("rs") }}
{%        endif %}
{%    endif %}
{% endif %}

		announce as-4byte yes
{%	if client.ip|ipaddr_ver == 4 %}
		announce IPv6 none
		announce IPv4 unicast
{%	else %}
		announce IPv6 unicast
		announce IPv4 none
{%	endif %}
{%	if "7.5"|target_version_ge and client.cfg.add_path %}
		announce add-path send all
{%	endif %}

		set nexthop no-modify

		{{- write_custom_config_lines(client, "ipv" ~ client.ip|ipaddr_ver, "client")|indent("		") }}
		{{- write_custom_config_lines(client, "any", "client")|indent("		") }}
	}
{% endfor %}
}

{{ "post-clients"|include_local_file -}}

