{% macro write_prefix_list_entry(entry) %}
{{	entry.prefix }}/{{ entry.length -}}
{%-	if not entry.exact -%}
{{ 		"{" }}
{%-		if entry.ge -%}
{{ 			entry.ge }}
{%-		else -%}
{{ 			entry.length }}
{%-		endif -%}
{{ 		"," }}
{%-		if entry.le -%}
{{ 			entry.le }}
{%-		else -%}
{{ 			entry.max_length }}
{%-		endif -%}
{{ 		"}" }}
{%-	endif %}
{% endmacro %}

{% macro write_prefix_list(prefix_list, group=False) %}
{% if prefix_list %}
{%	for entry in prefix_list if entry.prefix is current_ipver %}
{%		if not group %}{{ "\t\t" }}# {{ entry.comment }}{{ "\n" }}{% endif %}
{%		if not group %}{{ "\t\t" }}{% else %}{{ "\t" }}{% endif %}
{{-		write_prefix_list_entry(entry) -}}
{%		if not loop.last %}
{%		 	if group %}
{%				if loop.index % 4 == 0 %}{{ ",\n" -}}{% else %}{{ ", " -}}{% endif %}
{%		 	else %}
{{-				",\n" }}
{%			endif %}
{%		endif %}
{% 	endfor %}
{% endif %}
{% endmacro %}

{% macro write_community(comm, replace_peer_as=False, replace_dyn_val=False) %}
{{- "(" -}}
{% for part in comm.split(":") %}
{%-	if part == "peer_as" and replace_peer_as %}{{ replace_peer_as }}
{%-	elif part == "dyn_val" and replace_dyn_val %}{{ replace_dyn_val }}
{%-	else %}{{ part }}
{%-	endif %}
{%-	if not loop.last %}, {% endif %}
{% endfor %}
{{- ")" -}}
{% endmacro %}

{% macro del_communities(comm, replace_peer_as=False, replace_dyn_val=False) %}
{% if comm %}
{%	if comm.std %}
{%		if replace_peer_as %}
        bgp_community.delete([{{ write_community(comm.std, "1..64511") }}]);
{%		elif replace_dyn_val %}
        bgp_community.delete([{{ write_community(comm.std, replace_dyn_val=replace_dyn_val) }}]);
{%		else %}
        bgp_community.delete([{{ write_community(comm.std) }}]);
{%		endif %}
{%	endif %}
{%	if comm.lrg %}
{%		if replace_peer_as %}
        bgp_large_community.delete([{{ write_community(comm.lrg, "1..64511") }}]);
        bgp_large_community.delete([{{ write_community(comm.lrg, "131072..4199999999") }}]);
{%		elif replace_dyn_val %}
        bgp_large_community.delete([{{ write_community(comm.lrg, replace_dyn_val=replace_dyn_val) }}]);
{%		else %}
        bgp_large_community.delete([{{ write_community(comm.lrg) }}]);
{%		endif %}
{%	endif %}
{%	if comm.ext %}
{%		if replace_peer_as %}
        bgp_ext_community.delete([{{ write_community(comm.ext, "1..64511") }}]);
        bgp_ext_community.delete([{{ write_community(comm.ext, "131072..4199999999") }}]);
{%		elif replace_dyn_val %}
        bgp_ext_community.delete([{{ write_community(comm.ext, replace_dyn_val=replace_dyn_val) }}]);
{%		else %}
        bgp_ext_community.delete([{{ write_community(comm.ext) }}]);
{%		endif %}
{%	endif %}
{% endif %}
{% endmacro %}

{% macro add_communities(comm, tabs=2) %}
{% set indent = "\t" * tabs %}
{% if comm %}
{%	if comm.std %}
{{ indent }}bgp_community.add({{ write_community(comm.std) }});
{%	endif %}
{%	if comm.lrg %}
{{ indent }}bgp_large_community.add({{ write_community(comm.lrg) }});
{% 	endif %}
{%	if comm.ext %}
{{ indent }}bgp_ext_community.add({{ write_community(comm.ext) }});
{% 	endif %}
{% endif %}
{% endmacro %}

{% macro match_communities(comm, if_match, replace_dyn_val=False, tabs=1) %}
{% set indent = "\t" * tabs %}

{% if comm %}
{%	if comm.std %}
{%		if "peer_as" in comm.std %}
{{ indent }}if peer_as <= 65535 then {
{{ indent }}	if {{ write_community(comm.std) }} ~ bgp_community then
{{ indent }}		{{ if_match }}
{{ indent }}}
{%		else %}
{{ indent }}if {{ write_community(comm.std, replace_dyn_val=replace_dyn_val) }} ~ bgp_community then
{{ indent }}	{{ if_match }}
{%		endif %}
{%	endif %}
{%	if comm.lrg %}
{{ indent }}if {{ write_community(comm.lrg, replace_dyn_val=replace_dyn_val) }} ~ bgp_large_community then
{{ indent }}	{{ if_match }}
{% 	endif %}
{%	if comm.ext %}
{{ indent }}if {{ write_community(comm.ext, replace_dyn_val=replace_dyn_val) }} ~ bgp_ext_community then
{{ indent }}	{{ if_match }}
{% 	endif %}
{% endif %}
{% endmacro %}

{% macro match_rtt_communities(rtt_thresholds, comm, rtt_var_name, op, if_match, tabs=1) %}

{%	for threshold_val in rtt_thresholds %}
{{ "\t" * tabs }}# {{ threshold_val }} ms
{{- match_communities(comm, "{ if " ~ rtt_var_name ~ " " ~ op ~ " " ~ threshold_val ~ " then\n\t\t\t" ~ if_match ~ " }", replace_dyn_val=threshold_val, tabs=tabs) }}
{%	endfor %}
{% endmacro %}
