asterust_junos/protocols/
traceoptions.rs1use crate::{active_children, arg_of, has_leaf, named_children, split_name, unquote};
11use asterust::config::ConfigLine;
12
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct Traceoptions {
17 pub file: Option<String>,
19 pub size: Option<String>,
21 pub files: Option<String>,
23 pub match_regex: Option<String>,
25 pub world_readable: Option<bool>,
27 pub no_remote_trace: bool,
28 pub level: Option<String>,
30 pub flags: Vec<TraceFlag>,
31}
32
33#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct TraceFlag {
37 pub name: String,
38 pub modifiers: Vec<String>,
40 pub disable: bool,
41}
42
43pub(crate) fn parse_traceoptions(parent: &ConfigLine) -> Option<Traceoptions> {
45 parse_named_traceoptions(parent, "traceoptions")
46}
47
48pub(crate) fn parse_named_traceoptions(parent: &ConfigLine, keyword: &str) -> Option<Traceoptions> {
51 let trace = active_children(parent).find(|child| child.text == keyword)?;
52 let file = active_children(trace).find(|child| child.arg("file").is_some());
54 let (name, options) = file.map_or((None, Vec::new()), |line| {
55 let (name, rest) = split_name(line.arg("file").unwrap_or_default());
56 let options = rest
57 .split_whitespace()
58 .chain(active_children(line).flat_map(|child| child.text.split_whitespace()))
59 .collect();
60 (Some(name.to_string()), options)
61 });
62 let option = |keyword: &str| {
63 let position = options.iter().position(|word| *word == keyword)?;
64 options.get(position + 1).map(|word| word.to_string())
65 };
66 let world_readable = if options.contains(&"world-readable") {
67 Some(true)
68 } else if options.contains(&"no-world-readable") {
69 Some(false)
70 } else {
71 None
72 };
73 Some(Traceoptions {
74 file: name,
75 size: option("size"),
76 files: option("files"),
77 match_regex: option("match").map(|regex| unquote(®ex).to_string()),
78 world_readable,
79 no_remote_trace: has_leaf(trace, "no-remote-trace"),
80 level: arg_of(trace, "level").map(str::to_string),
81 flags: named_children(trace, "flag")
82 .map(|(arg, node)| {
83 let mut words = arg
84 .split_whitespace()
85 .chain(active_children(node).flat_map(|child| child.text.split_whitespace()));
86 let name = words.next().unwrap_or_default().to_string();
87 let rest: Vec<&str> = words.collect();
88 TraceFlag {
89 name,
90 disable: rest.contains(&"disable"),
91 modifiers: rest
92 .into_iter()
93 .filter(|word| matches!(*word, "detail" | "receive" | "send"))
94 .map(str::to_string)
95 .collect(),
96 }
97 })
98 .collect(),
99 })
100}
101
102#[cfg(test)]
103mod tests {
104 use crate::protocols::isis::isis;
105 use asterust::config::juniper;
106
107 const CONFIG: &str = r#"
109protocols {
110 isis {
111 traceoptions {
112 file "isis-log" size 10m files 5 world-readable;
113 flag hello detail;
114 flag lsp receive detail;
115 flag spf disable;
116 flag error;
117 }
118 }
119}
120"#;
121
122 #[test]
123 fn reads_the_file_options() {
124 let trace = isis(&juniper::parse_braces(CONFIG)).unwrap().traceoptions.unwrap();
125 assert_eq!(trace.file.as_deref(), Some("isis-log"));
126 assert_eq!(trace.size.as_deref(), Some("10m"));
127 assert_eq!(trace.files.as_deref(), Some("5"));
128 assert_eq!(trace.world_readable, Some(true));
129 assert!(!trace.no_remote_trace);
130 }
131
132 #[test]
133 fn reads_the_flags_with_their_modifiers() {
134 let trace = isis(&juniper::parse_braces(CONFIG)).unwrap().traceoptions.unwrap();
135 let flags: Vec<_> = trace
136 .flags
137 .iter()
138 .map(|flag| (flag.name.as_str(), flag.modifiers.clone(), flag.disable))
139 .collect();
140 assert_eq!(
141 flags,
142 [
143 ("hello", vec!["detail".to_string()], false),
144 ("lsp", vec!["receive".to_string(), "detail".to_string()], false),
145 ("spf", vec![], true),
146 ("error", vec![], false),
147 ]
148 );
149 }
150
151 #[test]
152 fn reads_an_unquoted_file_and_no_world_readable() {
153 let config = juniper::parse_braces(
154 "protocols {\n isis {\n traceoptions {\n file isis-log no-world-readable;\n no-remote-trace;\n }\n }\n}\n",
155 );
156 let trace = isis(&config).unwrap().traceoptions.unwrap();
157 assert_eq!(trace.file.as_deref(), Some("isis-log"));
158 assert_eq!(trace.size, None);
159 assert_eq!(trace.world_readable, Some(false));
160 assert!(trace.no_remote_trace);
161 assert!(trace.flags.is_empty());
162 }
163
164 #[test]
165 fn is_none_when_absent() {
166 let config = juniper::parse_braces("protocols {\n isis {\n interface ge-0/0/0.0;\n }\n}\n");
167 assert_eq!(isis(&config).unwrap().traceoptions, None);
168 }
169}