asterust_junos/protocols/bgp/family/
prefix_limit.rs1use crate::{statement_words, value_after};
10use asterust::config::ConfigLine;
11
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct PrefixLimit {
16 pub maximum: Option<String>,
17 pub teardown: bool,
19 pub teardown_percentage: Option<String>,
20 pub idle_timeout: Option<String>,
22 pub drop_excess: bool,
23 pub drop_excess_percentage: Option<String>,
24 pub hide_excess: bool,
25 pub hide_excess_percentage: Option<String>,
26}
27
28pub(super) fn parse_prefix_limit(node: &ConfigLine, keyword: &str) -> Option<PrefixLimit> {
30 let words = statement_words(node, keyword)?;
31 let is_number = |word: &str| word.chars().all(|c| c.is_ascii_digit());
32 let percentage = |name: &str| value_after(&words, name).filter(|value| is_number(value));
33 let bare_maximum = words
34 .first()
35 .filter(|word| is_number(word))
36 .map(|word| word.to_string());
37 Some(PrefixLimit {
38 maximum: value_after(&words, "maximum").or(bare_maximum),
39 teardown: words.contains(&"teardown"),
40 teardown_percentage: percentage("teardown"),
41 idle_timeout: value_after(&words, "idle-timeout"),
42 drop_excess: words.contains(&"drop-excess"),
43 drop_excess_percentage: percentage("drop-excess"),
44 hide_excess: words.contains(&"hide-excess"),
45 hide_excess_percentage: percentage("hide-excess"),
46 })
47}
48
49#[cfg(test)]
50mod tests {
51 use crate::protocols::bgp::bgp;
52 use asterust::config::juniper;
53
54 const CONFIG: &str = r#"
56protocols {
57 bgp {
58 group EBGP {
59 family inet {
60 unicast {
61 prefix-limit {
62 maximum 1000;
63 teardown 80 idle-timeout 10;
64 }
65 accepted-prefix-limit {
66 maximum 500;
67 hide-excess 90;
68 }
69 }
70 multicast {
71 prefix-limit {
72 maximum 10;
73 teardown idle-timeout forever;
74 }
75 accepted-prefix-limit {
76 maximum 20;
77 drop-excess;
78 }
79 }
80 }
81 family iso-vpn {
82 unicast {
83 prefix-limit 250;
84 }
85 }
86 }
87 }
88}
89"#;
90
91 #[test]
92 fn reads_teardown_and_idle_timeout() {
93 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
94 let limit = bgp.groups[0].options.address_families[0].prefix_limit.as_ref().unwrap();
95 assert_eq!(limit.maximum.as_deref(), Some("1000"));
96 assert!(limit.teardown);
97 assert_eq!(limit.teardown_percentage.as_deref(), Some("80"));
98 assert_eq!(limit.idle_timeout.as_deref(), Some("10"));
99 assert!(!limit.drop_excess);
100 }
101
102 #[test]
103 fn reads_the_excess_actions_of_the_accepted_limit() {
104 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
105 let families = &bgp.groups[0].options.address_families;
106
107 let hide = families[0].accepted_prefix_limit.as_ref().unwrap();
108 assert!(hide.hide_excess);
109 assert_eq!(hide.hide_excess_percentage.as_deref(), Some("90"));
110 assert!(!hide.teardown);
111
112 let drop = families[1].accepted_prefix_limit.as_ref().unwrap();
113 assert!(drop.drop_excess);
114 assert_eq!(drop.drop_excess_percentage, None);
115 }
116
117 #[test]
118 fn reads_a_bare_teardown_with_forever() {
119 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
120 let limit = bgp.groups[0].options.address_families[1].prefix_limit.as_ref().unwrap();
121 assert!(limit.teardown);
122 assert_eq!(limit.teardown_percentage, None);
123 assert_eq!(limit.idle_timeout.as_deref(), Some("forever"));
124 }
125
126 #[test]
127 fn reads_the_older_single_number_form() {
128 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
129 let limit = bgp.groups[0].options.address_families[2].prefix_limit.as_ref().unwrap();
130 assert_eq!(limit.maximum.as_deref(), Some("250"));
131 }
132}