Skip to main content

asterust_junos/routing_options/
maximum_paths.rs

1//! `maximum-paths`
2//! `routing-options { maximum-paths <limit> [log-interval <s>] [log-only | threshold <n>]; }`.
3//!
4//! # See also
5//!
6//! - [maximum-paths](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/maximum-paths-edit-routing-options.html)
7
8use crate::{statement_words, value_after};
9use asterust::config::ConfigLine;
10
11/// The limit on the number of routes in the routing table.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct MaximumPaths {
15    pub path_limit: Option<String>,
16    /// Seconds between log messages.
17    pub log_interval: Option<String>,
18    /// Log a message instead of enforcing the limit.
19    pub log_only: bool,
20    /// Percentage of the limit that triggers a warning.
21    pub threshold: Option<String>,
22}
23
24pub(super) fn parse_maximum_paths(root: &ConfigLine) -> Option<MaximumPaths> {
25    let words = statement_words(root, "maximum-paths")?;
26    Some(MaximumPaths {
27        path_limit: words.first().map(|word| word.to_string()),
28        log_interval: value_after(&words, "log-interval"),
29        log_only: words.contains(&"log-only"),
30        threshold: value_after(&words, "threshold"),
31    })
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::routing_options::routing_options;
37    use asterust::config::juniper;
38
39    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
40    #[test]
41    fn extracts_limit_threshold_and_interval() {
42        let options = routing_options(&juniper::parse_braces(
43            "routing-options {\n    maximum-paths 1000 threshold 80 log-interval 60;\n}\n",
44        ))
45        .unwrap();
46        let paths = options.maximum_paths.unwrap();
47        assert_eq!(paths.path_limit.as_deref(), Some("1000"));
48        assert_eq!(paths.threshold.as_deref(), Some("80"));
49        assert_eq!(paths.log_interval.as_deref(), Some("60"));
50        assert!(!paths.log_only);
51    }
52
53    #[test]
54    fn extracts_log_only() {
55        let options = routing_options(&juniper::parse_braces(
56            "routing-options {\n    maximum-paths 500 log-only;\n}\n",
57        ))
58        .unwrap();
59        let paths = options.maximum_paths.unwrap();
60        assert_eq!(paths.path_limit.as_deref(), Some("500"));
61        assert!(paths.log_only);
62        assert_eq!(paths.threshold, None);
63    }
64
65    #[test]
66    fn no_maximum_paths_is_none() {
67        let options = routing_options(&juniper::parse_braces(
68            "routing-options {\n    router-id 192.0.2.1;\n}\n",
69        ))
70        .unwrap();
71        assert!(options.maximum_paths.is_none());
72    }
73}