asterust-junos 0.5.12

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `fib-streaming`
//! `system { fib-streaming { prefix-list { table <name> { family { ... } } } } }`.
//!
//! # See also
//!
//! - [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)

use crate::{args_of, child_block, list_values, named_children};
use asterust::config::ConfigLine;

/// A `table <name>` of the streamed prefix list.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FibStreamingTable {
    pub name: String,
    pub inet_prefixes: Vec<String>,
    pub inet6_prefixes: Vec<String>,
}

/// The `system fib-streaming` prefixes streamed to a telemetry collector.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FibStreaming {
    pub tables: Vec<FibStreamingTable>,
}

pub(super) fn parse_fib_streaming(streaming: &ConfigLine) -> FibStreaming {
    let prefixes = |family: &ConfigLine, name: &str| -> Vec<String> {
        child_block(family, name)
            .map(|block| args_of(block, "prefix").into_iter().flat_map(list_values).collect())
            .unwrap_or_default()
    };
    FibStreaming {
        tables: child_block(streaming, "prefix-list")
            .map(|list| {
                named_children(list, "table")
                    .map(|(name, table)| {
                        let family = child_block(table, "family");
                        FibStreamingTable {
                            name: name.to_string(),
                            inet_prefixes: family.map(|f| prefixes(f, "inet")).unwrap_or_default(),
                            inet6_prefixes: family.map(|f| prefixes(f, "inet6")).unwrap_or_default(),
                        }
                    })
                    .collect()
            })
            .unwrap_or_default(),
    }
}

#[cfg(test)]
mod tests {
    use crate::system::system;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_the_streamed_prefixes() {
        let streaming = system(&juniper::parse_braces(
            r#"
system {
    fib-streaming {
        prefix-list {
            table core {
                family {
                    inet {
                        prefix [ 192.0.2.0/24 198.51.100.0/24 ];
                    }
                    inet6 {
                        prefix 2001:db8::/32;
                    }
                }
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .fib_streaming
        .unwrap();
        assert_eq!(streaming.tables.len(), 1);
        assert_eq!(streaming.tables[0].name, "core");
        assert_eq!(streaming.tables[0].inet_prefixes, ["192.0.2.0/24", "198.51.100.0/24"]);
        assert_eq!(streaming.tables[0].inet6_prefixes, ["2001:db8::/32"]);
    }

    #[test]
    fn no_fib_streaming_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.fib_streaming.is_none());
    }
}