Skip to main content

asterust_junos/system/
configuration_database.rs

1//! `configuration-database`
2//! `system { configuration-database { ... } }`.
3//!
4//! # See also
5//!
6//! - [configuration-database](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/configuration-database-edit-system.html)
7//! - [configuration-database (Enhanced Subscriber Management)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/configuration-database-edit-system-enhanced-universal-edge.html)
8//! - [ephemeral](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ephemeral-edit-system-configuration-database.html)
9//! - [instance (Ephemeral Database)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/instance-edit-system-configuration-database-ephemeral.html)
10//! - [resize-ephemeral-default](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resize-ephemeral-default-edit-system-configuration-database-ephemeral.html)
11//! - [resize (Ephemeral Database)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resize-edit-system-configuration-database-ephemeral-instance.html)
12//! - [max-db-size](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/max-db-size-edit-system-configuration-database.html)
13
14use crate::{arg_of, child_block, first_word, has_leaf, named_children};
15use asterust::config::ConfigLine;
16
17/// The `database-size-diff` and `database-size-on-disk` thresholds of a `resize` block, as written
18/// (sizes in MB).
19#[derive(Clone, Debug, Default, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct DatabaseResize {
22    pub database_size_diff: Option<String>,
23    pub database_size_on_disk: Option<String>,
24}
25
26/// An `instance <name>` of the ephemeral configuration database.
27#[derive(Clone, Debug, PartialEq, Eq)]
28#[non_exhaustive]
29pub struct EphemeralInstance {
30    pub name: String,
31    pub cyclic_version: Option<String>,
32    pub resize: Option<DatabaseResize>,
33}
34
35/// The `ephemeral` block of the configuration database.
36#[derive(Clone, Debug, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct EphemeralDatabase {
39    pub allow_commit_synchronize_with_gres: bool,
40    /// `asynchronous` or `synchronous`.
41    pub commit_synchronize_model: Option<String>,
42    pub cyclic_version_for_ephemeral_default: Option<String>,
43    pub delete_ephemeral_default: bool,
44    pub ignore_ephemeral_default: bool,
45    /// In the order written, which is the priority order.
46    pub instances: Vec<EphemeralInstance>,
47    pub resize_ephemeral_default: Option<DatabaseResize>,
48}
49
50/// A `process <name>` of `virtual-memory-mapping`.
51#[derive(Clone, Debug, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct VirtualMemoryProcess {
54    pub name: String,
55    /// KB.
56    pub fixed_size: Option<String>,
57    /// KB.
58    pub page_pooling_size: Option<String>,
59}
60
61/// The `system configuration-database` settings.
62#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct ConfigurationDatabase {
65    pub ephemeral: Option<EphemeralDatabase>,
66    pub extend_size: bool,
67    pub max_db_size: Option<String>,
68    pub optimize_db_size: bool,
69    pub resize: Option<DatabaseResize>,
70    pub virtual_memory_mapping: Vec<VirtualMemoryProcess>,
71}
72
73fn resize_values(resize: &ConfigLine) -> DatabaseResize {
74    DatabaseResize {
75        database_size_diff: arg_of(resize, "database-size-diff").map(str::to_string),
76        database_size_on_disk: arg_of(resize, "database-size-on-disk").map(str::to_string),
77    }
78}
79
80fn parse_resize(parent: &ConfigLine) -> Option<DatabaseResize> {
81    child_block(parent, "resize").map(resize_values)
82}
83
84fn parse_ephemeral(ephemeral: &ConfigLine) -> EphemeralDatabase {
85    EphemeralDatabase {
86        allow_commit_synchronize_with_gres: has_leaf(ephemeral, "allow-commit-synchronize-with-gres"),
87        commit_synchronize_model: arg_of(ephemeral, "commit-synchronize-model").map(str::to_string),
88        cyclic_version_for_ephemeral_default: arg_of(ephemeral, "cyclic-version-for-ephemeral-default")
89            .map(str::to_string),
90        delete_ephemeral_default: has_leaf(ephemeral, "delete-ephemeral-default"),
91        ignore_ephemeral_default: has_leaf(ephemeral, "ignore-ephemeral-default"),
92        instances: named_children(ephemeral, "instance")
93            .map(|(name, instance)| EphemeralInstance {
94                name: name.to_string(),
95                cyclic_version: arg_of(instance, "cyclic-version").map(str::to_string),
96                resize: parse_resize(instance),
97            })
98            .collect(),
99        resize_ephemeral_default: child_block(ephemeral, "resize-ephemeral-default").map(resize_values),
100    }
101}
102
103pub(super) fn parse_configuration_database(database: &ConfigLine) -> ConfigurationDatabase {
104    ConfigurationDatabase {
105        ephemeral: child_block(database, "ephemeral").map(parse_ephemeral),
106        extend_size: has_leaf(database, "extend-size"),
107        max_db_size: arg_of(database, "max-db-size").map(str::to_string),
108        optimize_db_size: has_leaf(database, "optimize-db-size"),
109        resize: parse_resize(database),
110        virtual_memory_mapping: child_block(database, "virtual-memory-mapping")
111            .map(|mapping| {
112                named_children(mapping, "process")
113                    .map(|(name, process)| VirtualMemoryProcess {
114                        name: name.to_string(),
115                        fixed_size: arg_of(process, "fixed-size").and_then(first_word).map(str::to_string),
116                        page_pooling_size: arg_of(process, "page-pooling-size")
117                            .and_then(first_word)
118                            .map(str::to_string),
119                    })
120                    .collect()
121            })
122            .unwrap_or_default(),
123    }
124}
125
126#[cfg(test)]
127mod tests {
128    use crate::system::system;
129    use asterust::config::juniper;
130
131    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
132    #[test]
133    fn extracts_the_configuration_database() {
134        let database = system(&juniper::parse_braces(
135            r#"
136system {
137    configuration-database {
138        extend-size;
139        max-db-size 1024;
140        optimize-db-size;
141        resize {
142            database-size-diff 100;
143            database-size-on-disk 200;
144        }
145        virtual-memory-mapping {
146            process rpd {
147                fixed-size 512 KB;
148                page-pooling-size 64;
149            }
150        }
151        ephemeral {
152            allow-commit-synchronize-with-gres;
153            commit-synchronize-model asynchronous;
154            cyclic-version-for-ephemeral-default 4;
155            delete-ephemeral-default;
156            ignore-ephemeral-default;
157            instance high {
158                cyclic-version 8;
159                resize {
160                    database-size-diff 50;
161                    database-size-on-disk 80;
162                }
163            }
164            instance low;
165            resize-ephemeral-default {
166                database-size-diff 10;
167            }
168        }
169    }
170}
171"#,
172        ))
173        .unwrap()
174        .configuration_database
175        .unwrap();
176        assert!(database.extend_size);
177        assert!(database.optimize_db_size);
178        assert_eq!(database.max_db_size.as_deref(), Some("1024"));
179        let resize = database.resize.unwrap();
180        assert_eq!(resize.database_size_diff.as_deref(), Some("100"));
181        assert_eq!(resize.database_size_on_disk.as_deref(), Some("200"));
182        assert_eq!(database.virtual_memory_mapping[0].name, "rpd");
183        assert_eq!(database.virtual_memory_mapping[0].fixed_size.as_deref(), Some("512"));
184        assert_eq!(
185            database.virtual_memory_mapping[0].page_pooling_size.as_deref(),
186            Some("64")
187        );
188
189        let ephemeral = database.ephemeral.unwrap();
190        assert!(ephemeral.allow_commit_synchronize_with_gres);
191        assert_eq!(ephemeral.commit_synchronize_model.as_deref(), Some("asynchronous"));
192        assert_eq!(ephemeral.cyclic_version_for_ephemeral_default.as_deref(), Some("4"));
193        assert!(ephemeral.delete_ephemeral_default);
194        assert!(ephemeral.ignore_ephemeral_default);
195        let names: Vec<_> = ephemeral.instances.iter().map(|i| i.name.as_str()).collect();
196        assert_eq!(names, ["high", "low"]);
197        assert_eq!(ephemeral.instances[0].cyclic_version.as_deref(), Some("8"));
198        assert_eq!(
199            ephemeral.instances[0]
200                .resize
201                .as_ref()
202                .unwrap()
203                .database_size_on_disk
204                .as_deref(),
205            Some("80")
206        );
207        assert!(ephemeral.instances[1].resize.is_none());
208        assert_eq!(
209            ephemeral
210                .resize_ephemeral_default
211                .unwrap()
212                .database_size_diff
213                .as_deref(),
214            Some("10")
215        );
216    }
217
218    #[test]
219    fn no_configuration_database_is_none() {
220        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
221        assert!(system.configuration_database.is_none());
222    }
223}