{% 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) %}
{% set indent = "\t" %}
{% if comm %}
{%	if comm.std %}
{%		if replace_peer_as %}
{{ indent }}bgp_community.delete([{{ write_community(comm.std, "1..64511") }}]);
{%			for asn_32, asn_16 in asn3216_map.items() %}
{{ indent }}# {{ asn_16 }} is the 16bit_mapped_asn for {{ asn_32 }}
{{ indent }}bgp_community.delete([{{ write_community(comm.std, asn_16) }}]);
{%			endfor %}
{%		elif replace_dyn_val %}
{{ indent }}bgp_community.delete([{{ write_community(comm.std, replace_dyn_val=replace_dyn_val) }}]);
{%		else %}
{{ indent }}bgp_community.delete([{{ write_community(comm.std) }}]);
{%		endif %}
{%	endif %}
{%	if comm.lrg %}
{%		if replace_peer_as %}
{{ indent }}bgp_large_community.delete([{{ write_community(comm.lrg, "1..64511") }}]);
{{ indent }}bgp_large_community.delete([{{ write_community(comm.lrg, "131072..4199999999") }}]);
{%		elif replace_dyn_val %}
{{ indent }}bgp_large_community.delete([{{ write_community(comm.lrg, replace_dyn_val=replace_dyn_val) }}]);
{%		else %}
{{ indent }}bgp_large_community.delete([{{ write_community(comm.lrg) }}]);
{%		endif %}
{%	endif %}
{%	if comm.ext %}
{%		if replace_peer_as %}
{{ indent }}bgp_ext_community.delete([{{ write_community(comm.ext, "1..64511") }}]);
{%			if comm.ext.split(":")[1]|int <= 65535 %}
{{ indent }}bgp_ext_community.delete([{{ write_community(comm.ext, "131072..4199999999") }}]);
{%			endif %}
{%		elif replace_dyn_val %}
{{ indent }}bgp_ext_community.delete([{{ write_community(comm.ext, replace_dyn_val=replace_dyn_val) }}]);
{%		else %}
{{ indent }}bgp_ext_community.delete([{{ write_community(comm.ext) }}]);
{%		endif %}
{%	endif %}
{% endif %}
{% endmacro %}

{% macro add_communities(comm, tabs=2, ignore_std=False, replace_dyn_val_for_std=False) %}
{% set indent = "" %}
{% if comm %}
{%	if comm.std and not ignore_std %}
{{ indent }}bgp_community.add({{ write_community(comm.std, replace_dyn_val=replace_dyn_val_for_std) }});
{%          set indent = "\t" * tabs %}
{%	endif %}
{%	if comm.lrg %}
{{ indent }}bgp_large_community.add({{ write_community(comm.lrg) }});
{%          set indent = "\t" * tabs %}
{% 	endif %}
{%	if comm.ext %}
{{ indent }}bgp_ext_community.add({{ write_community(comm.ext) }});
{%          set indent = "\t" * tabs %}
{% 	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 }}
{%			if asn3216_map %}
{{ indent }}} else {
{{ indent }}	mapped_16bit_asn = get_16bit_mapped_asn(peer_as);
{{ indent }}	if mapped_16bit_asn > 0 then {
{{ indent }}		if {{ write_community(comm.std, replace_peer_as="mapped_16bit_asn") }} ~ bgp_community then
{{ indent }}			{{ if_match }}
{{ indent }}	}
{%			endif %}
{{ 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 %}
