{% 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 %}
{{		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 %}
{%		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) %}

{% if comm %}
{%	if comm.std %}
		bgp_community.add({{ write_community(comm.std) }});
{%	endif %}
{%	if comm.lrg %}
		bgp_large_community.add({{ write_community(comm.lrg) }});
{% 	endif %}
{%	if comm.ext %}
		bgp_ext_community.add({{ write_community(comm.ext) }});
{% 	endif %}
{% endif %}
{% endmacro %}

{% macro match_communities(comm, if_match, replace_dyn_val=False) %}

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