Skip to main content

asterust_junos/system/
fib_streaming.rs

1//! `fib-streaming`
2//! `system { fib-streaming { prefix-list { table <name> { family { ... } } } } }`.
3//!
4//! # See also
5//!
6//! - [prefix-list (Junos Telemetry Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/prefix-list-edit-system-fib-streaming.html)
7
8use crate::{args_of, child_block, list_values, named_children};
9use asterust::config::ConfigLine;
10
11/// A `table <name>` of the streamed prefix list.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct FibStreamingTable {
15    pub name: String,
16    pub inet_prefixes: Vec<String>,
17    pub inet6_prefixes: Vec<String>,
18}
19
20/// The `system fib-streaming` prefixes streamed to a telemetry collector.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct FibStreaming {
24    pub tables: Vec<FibStreamingTable>,
25}
26
27pub(super) fn parse_fib_streaming(streaming: &ConfigLine) -> FibStreaming {
28    let prefixes = |family: &ConfigLine, name: &str| -> Vec<String> {
29        child_block(family, name)
30            .map(|block| args_of(block, "prefix").into_iter().flat_map(list_values).collect())
31            .unwrap_or_default()
32    };
33    FibStreaming {
34        tables: child_block(streaming, "prefix-list")
35            .map(|list| {
36                named_children(list, "table")
37                    .map(|(name, table)| {
38                        let family = child_block(table, "family");
39                        FibStreamingTable {
40                            name: name.to_string(),
41                            inet_prefixes: family.map(|f| prefixes(f, "inet")).unwrap_or_default(),
42                            inet6_prefixes: family.map(|f| prefixes(f, "inet6")).unwrap_or_default(),
43                        }
44                    })
45                    .collect()
46            })
47            .unwrap_or_default(),
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use crate::system::system;
54    use asterust::config::juniper;
55
56    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
57    #[test]
58    fn extracts_the_streamed_prefixes() {
59        let streaming = system(&juniper::parse_braces(
60            r#"
61system {
62    fib-streaming {
63        prefix-list {
64            table core {
65                family {
66                    inet {
67                        prefix [ 192.0.2.0/24 198.51.100.0/24 ];
68                    }
69                    inet6 {
70                        prefix 2001:db8::/32;
71                    }
72                }
73            }
74        }
75    }
76}
77"#,
78        ))
79        .unwrap()
80        .fib_streaming
81        .unwrap();
82        assert_eq!(streaming.tables.len(), 1);
83        assert_eq!(streaming.tables[0].name, "core");
84        assert_eq!(streaming.tables[0].inet_prefixes, ["192.0.2.0/24", "198.51.100.0/24"]);
85        assert_eq!(streaming.tables[0].inet6_prefixes, ["2001:db8::/32"]);
86    }
87
88    #[test]
89    fn no_fib_streaming_is_none() {
90        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
91        assert!(system.fib_streaming.is_none());
92    }
93}