router id 192.0.2.101;

log "/var/log/bird.log" all;
log syslog all;
debug protocols all;

protocol device { }

protocol static own_prefixes {
        route {{ data.AS101_good1 }} reject;
	route {{ data.AS101_no_rset }} reject;
	route {{ data.AS101_bad_std_comm }} reject;
	route {{ data.AS101_bad_lrg_comm }} reject;
	route {{ data.AS101_other_s_comm }} reject;
	route {{ data.AS101_other_l_comm }} reject;
	route {{ data.AS101_bad_good_comms }} reject;
	route {{ data.AS102_no_asset }} reject;
	route {{ data.AS101_transitfree_1 }} reject;
	route {{ data.AS101_neverviars_1 }} reject;
	route {{ data.AS101_neverviars_2 }} reject;
	route {{ data.AS101_roa_valid1 }} reject;
	route {{ data.AS101_roa_invalid1 }} reject;
	route {{ data.AS101_roa_badlen }} reject;
	route {{ data.AS101_roa_blackhole }} reject;
	{% if data.AS101_no_ipv6_gl_uni %}
	route {{ data.AS101_no_ipv6_gl_uni }} reject;
	{% endif %}
	route {{ data.AS103_gshut_1 }} reject;
	route {{ data.AS103_gshut_2 }} reject;
	route {{ data.AS101_roa_routeobj_1 }} reject;
	route {{ data.AS101_roa_routeobj_2 }} reject;
	route {{ data.AS101_roa_routeobj_3 }} reject;
	route {{ data.AS101_roa_routeobj_4 }} reject;
	route {{ data.AS104_arin_1 }} reject;
	route {{ data.AS104_nicbr_1 }} reject;
}

function outgoing_common() {
	if net = {{ data.AS102_no_asset }} then {
		# To simulate AS102 as a customer of AS101, so that
		# rs fails the validation of origin ASN.
		bgp_path.delete(101);
		bgp_path.prepend(102);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS101_bad_std_comm }} then {
		bgp_community.add((65530,0));
	}
	if net = {{ data.AS101_bad_lrg_comm }} then {
		bgp_large_community.add((999,65530,0));
	}
	if net = {{ data.AS101_other_s_comm }} then {
		bgp_community.add((888,0));
	}
	if net = {{ data.AS101_other_l_comm }} then {
		bgp_large_community.add((888,0,0));
	}
	if net = {{ data.AS101_bad_good_comms }} then {
		bgp_community.add((65530,1));
		bgp_large_community.add((999,65530,1));
		bgp_community.add((777,0));
		bgp_large_community.add((777,0,0));
	}
	if net = {{ data.AS101_transitfree_1 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(174);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS101_neverviars_1 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(666);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS101_neverviars_2 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(777);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS101_roa_blackhole }} then
		bgp_community.add((65535,666));
	if net = {{ data.AS101_roa_routeobj_4 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(105);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS104_arin_1 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(104);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS104_nicbr_1 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(104);
		bgp_path.prepend(101);
	}
}
filter to_AS1 {
	outgoing_common();
	if net = {{ data.AS103_gshut_1 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(103);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS103_gshut_2 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(103);
		bgp_path.prepend(101);
		bgp_path.prepend(101);
	}
	if proto = "own_prefixes" then accept;
	reject;
}
filter to_AS2 {
	outgoing_common();
	if net = {{ data.AS103_gshut_1 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(103);
		bgp_path.prepend(101);
		bgp_path.prepend(101);
	}
	if net = {{ data.AS103_gshut_2 }} then {
		bgp_path.delete(101);
		bgp_path.prepend(103);
		bgp_path.prepend(101);
	}
	if proto = "own_prefixes" then accept;
	reject;
}
protocol bgp AS1_1 {
	local as 101;
	neighbor {{ data.AS1_1_IPAddress }} as 1;
	import all;
	export filter to_AS1;
	connect delay time 1;
	connect retry time 1;
}
protocol bgp AS1_2 {
	local as 101;
	neighbor {{ data.AS1_2_IPAddress }} as 1;
	import all;
	export filter to_AS1;
	connect delay time 1;
	connect retry time 1;
}protocol bgp AS2 {
	local as 101;
	neighbor {{ data.AS2_1_IPAddress }} as 2;
	import all;
	export filter to_AS2;
	connect delay time 1;
	connect retry time 1;
}
