Skip to main content

asterust_junos/protocols/
traceoptions.rs

1//! `traceoptions`
2//! `protocols { <protocol> { traceoptions { file <name> [size <size>] [files <n>] [world-readable]; flag <flag> [<modifier>] [disable]; } } }`.
3//!
4//! The statement has the same shape in every protocol; only the protocol-specific flags differ.
5//!
6//! # See also
7//!
8//! - [traceoptions (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-isis.html)
9
10use crate::{active_children, has_leaf, named_children, split_name};
11use asterust::config::ConfigLine;
12
13/// A `traceoptions` block. Values are kept as written.
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct Traceoptions {
17    /// The trace file name, without quotes.
18    pub file: Option<String>,
19    /// Maximum size of each trace file, e.g. `10m`.
20    pub size: Option<String>,
21    /// Maximum number of trace files.
22    pub files: Option<String>,
23    /// `Some(true)` for `world-readable`, `Some(false)` for `no-world-readable`.
24    pub world_readable: Option<bool>,
25    pub no_remote_trace: bool,
26    pub flags: Vec<TraceFlag>,
27}
28
29/// A `flag <name> [<modifier>...] [disable]` of a [`Traceoptions`] block.
30#[derive(Clone, Debug, PartialEq, Eq)]
31#[non_exhaustive]
32pub struct TraceFlag {
33    pub name: String,
34    /// `detail`, `receive` and `send`, in the order written.
35    pub modifiers: Vec<String>,
36    pub disable: bool,
37}
38
39/// The first active `traceoptions` block of `parent`.
40pub(crate) fn parse_traceoptions(parent: &ConfigLine) -> Option<Traceoptions> {
41    let trace = active_children(parent).find(|child| child.text == "traceoptions")?;
42    // `file` is read raw, since its name may be quoted and be followed by options.
43    let file = active_children(trace).find_map(|child| child.arg("file"));
44    let (name, options) = file.map_or((None, Vec::new()), |arg| {
45        let (name, rest) = split_name(arg);
46        (Some(name.to_string()), rest.split_whitespace().collect())
47    });
48    let option = |keyword: &str| {
49        let position = options.iter().position(|word| *word == keyword)?;
50        options.get(position + 1).map(|word| word.to_string())
51    };
52    let world_readable = if options.contains(&"world-readable") {
53        Some(true)
54    } else if options.contains(&"no-world-readable") {
55        Some(false)
56    } else {
57        None
58    };
59    Some(Traceoptions {
60        file: name,
61        size: option("size"),
62        files: option("files"),
63        world_readable,
64        no_remote_trace: has_leaf(trace, "no-remote-trace"),
65        flags: named_children(trace, "flag")
66            .map(|(arg, node)| {
67                let mut words = arg
68                    .split_whitespace()
69                    .chain(active_children(node).flat_map(|child| child.text.split_whitespace()));
70                let name = words.next().unwrap_or_default().to_string();
71                let rest: Vec<&str> = words.collect();
72                TraceFlag {
73                    name,
74                    disable: rest.contains(&"disable"),
75                    modifiers: rest
76                        .into_iter()
77                        .filter(|word| matches!(*word, "detail" | "receive" | "send"))
78                        .map(str::to_string)
79                        .collect(),
80                }
81            })
82            .collect(),
83    })
84}
85
86#[cfg(test)]
87mod tests {
88    use crate::protocols::isis::isis;
89    use asterust::config::juniper;
90
91    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
92    const CONFIG: &str = r#"
93protocols {
94    isis {
95        traceoptions {
96            file "isis-log" size 10m files 5 world-readable;
97            flag hello detail;
98            flag lsp receive detail;
99            flag spf disable;
100            flag error;
101        }
102    }
103}
104"#;
105
106    #[test]
107    fn reads_the_file_options() {
108        let trace = isis(&juniper::parse_braces(CONFIG)).unwrap().traceoptions.unwrap();
109        assert_eq!(trace.file.as_deref(), Some("isis-log"));
110        assert_eq!(trace.size.as_deref(), Some("10m"));
111        assert_eq!(trace.files.as_deref(), Some("5"));
112        assert_eq!(trace.world_readable, Some(true));
113        assert!(!trace.no_remote_trace);
114    }
115
116    #[test]
117    fn reads_the_flags_with_their_modifiers() {
118        let trace = isis(&juniper::parse_braces(CONFIG)).unwrap().traceoptions.unwrap();
119        let flags: Vec<_> = trace
120            .flags
121            .iter()
122            .map(|flag| (flag.name.as_str(), flag.modifiers.clone(), flag.disable))
123            .collect();
124        assert_eq!(
125            flags,
126            [
127                ("hello", vec!["detail".to_string()], false),
128                ("lsp", vec!["receive".to_string(), "detail".to_string()], false),
129                ("spf", vec![], true),
130                ("error", vec![], false),
131            ]
132        );
133    }
134
135    #[test]
136    fn reads_an_unquoted_file_and_no_world_readable() {
137        let config = juniper::parse_braces(
138            "protocols {\n    isis {\n        traceoptions {\n            file isis-log no-world-readable;\n            no-remote-trace;\n        }\n    }\n}\n",
139        );
140        let trace = isis(&config).unwrap().traceoptions.unwrap();
141        assert_eq!(trace.file.as_deref(), Some("isis-log"));
142        assert_eq!(trace.size, None);
143        assert_eq!(trace.world_readable, Some(false));
144        assert!(trace.no_remote_trace);
145        assert!(trace.flags.is_empty());
146    }
147
148    #[test]
149    fn is_none_when_absent() {
150        let config = juniper::parse_braces("protocols {\n    isis {\n        interface ge-0/0/0.0;\n    }\n}\n");
151        assert_eq!(isis(&config).unwrap().traceoptions, None);
152    }
153}