Skip to main content

asterust_junos/system/
resiliency.rs

1//! `resiliency`
2//! `system { resiliency { ... } }`.
3//!
4//! # See also
5//!
6//! - [resiliency](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resiliency-edit-system.html)
7
8use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
9use crate::{active_children, arg_of, child_block, has_leaf, named_children, toggle};
10use asterust::config::ConfigLine;
11
12/// A `file <name>` or `fwding-file <name>` of `resiliency store`.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct ResiliencyFile {
16    pub name: String,
17    pub files: Option<String>,
18    pub size: Option<String>,
19    /// `Some(true)` for `world-readable`, `Some(false)` for `no-world-readable`.
20    pub world_readable: Option<bool>,
21}
22
23/// The `store` block of `system resiliency`.
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct ResiliencyStore {
27    pub database: bool,
28    pub files: Vec<ResiliencyFile>,
29    pub forwarding_files: Vec<ResiliencyFile>,
30}
31
32/// The `system resiliency` settings of the on-box exception collector.
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct Resiliency {
36    /// The exception kinds collected: `forwarding`, `os` and `routing`.
37    pub exceptions: Vec<String>,
38    pub store: Option<ResiliencyStore>,
39    pub traceoptions: Option<Traceoptions>,
40}
41
42fn files(store: &ConfigLine, keyword: &str) -> Vec<ResiliencyFile> {
43    named_children(store, keyword)
44        .map(|(name, file)| ResiliencyFile {
45            name: name.to_string(),
46            files: arg_of(file, "files").map(str::to_string),
47            size: arg_of(file, "size").map(str::to_string),
48            world_readable: toggle(file, "world-readable"),
49        })
50        .collect()
51}
52
53pub(super) fn parse_resiliency(resiliency: &ConfigLine) -> Resiliency {
54    Resiliency {
55        exceptions: child_block(resiliency, "exceptions")
56            .map(|exceptions| {
57                active_children(exceptions)
58                    .map(|exception| exception.text.clone())
59                    .collect()
60            })
61            .unwrap_or_default(),
62        store: child_block(resiliency, "store").map(|store| ResiliencyStore {
63            database: has_leaf(store, "database"),
64            files: files(store, "file"),
65            forwarding_files: files(store, "fwding-file"),
66        }),
67        traceoptions: parse_traceoptions(resiliency),
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use crate::system::system;
74    use asterust::config::juniper;
75
76    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
77    #[test]
78    fn extracts_the_resiliency_settings() {
79        let resiliency = system(&juniper::parse_braces(
80            r#"
81system {
82    resiliency {
83        exceptions {
84            forwarding;
85            routing;
86        }
87        store {
88            database;
89            file exceptions.log {
90                files 5;
91                size 10m;
92                world-readable;
93            }
94            fwding-file fwd.log {
95                files 3;
96                no-world-readable;
97            }
98        }
99        traceoptions {
100            file resiliency.log;
101            flag all;
102        }
103    }
104}
105"#,
106        ))
107        .unwrap()
108        .resiliency
109        .unwrap();
110        assert_eq!(resiliency.exceptions, ["forwarding", "routing"]);
111        let store = resiliency.store.unwrap();
112        assert!(store.database);
113        assert_eq!(store.files[0].name, "exceptions.log");
114        assert_eq!(store.files[0].files.as_deref(), Some("5"));
115        assert_eq!(store.files[0].size.as_deref(), Some("10m"));
116        assert_eq!(store.files[0].world_readable, Some(true));
117        assert_eq!(store.forwarding_files[0].name, "fwd.log");
118        assert_eq!(store.forwarding_files[0].world_readable, Some(false));
119        assert_eq!(store.forwarding_files[0].size, None);
120        assert_eq!(resiliency.traceoptions.unwrap().file.as_deref(), Some("resiliency.log"));
121    }
122
123    #[test]
124    fn no_resiliency_is_none() {
125        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
126        assert!(system.resiliency.is_none());
127    }
128}