Skip to main content

asterust_junos/system/
access_line.rs

1//! `access-line`
2//! `system { access-line { ... } }`.
3//!
4//! # See also
5//!
6//! - [access-line (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/access-line-edit-system.html)
7//! - [attributes (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/attributes-edit-system-access-line.html)
8//! - [dsl (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dsl-edit-system-access-line.html)
9//! - [pon (Access-Line Rate Adjustment)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pon-edit-system-access-line.html)
10
11use crate::{active_children, arg_of, child_block};
12use asterust::config::ConfigLine;
13
14/// The adjustments of one access line type, such as `adsl2-plus` or `type 145`.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct AccessLineAdjustment {
18    /// The statement as written, e.g. `gfast` or `type 145`.
19    pub line_type: String,
20    pub overhead_adjust: Option<String>,
21    pub overhead_bytes: Option<String>,
22    pub total_adjust: Option<String>,
23}
24
25/// The `system access-line` rate adjustment settings.
26#[derive(Clone, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct AccessLine {
29    /// `dsl` or `pon`: the TLVs kept when both are received.
30    pub preference: Option<String>,
31    pub dsl: Vec<AccessLineAdjustment>,
32    pub pon: Vec<AccessLineAdjustment>,
33}
34
35fn adjustments(family: Option<&ConfigLine>) -> Vec<AccessLineAdjustment> {
36    family
37        .map(|family| {
38            active_children(family)
39                .map(|line| AccessLineAdjustment {
40                    line_type: line.text.clone(),
41                    overhead_adjust: arg_of(line, "overhead-adjust").map(str::to_string),
42                    overhead_bytes: arg_of(line, "overhead-bytes").map(str::to_string),
43                    total_adjust: arg_of(line, "total-adjust").map(str::to_string),
44                })
45                .collect()
46        })
47        .unwrap_or_default()
48}
49
50pub(super) fn parse_access_line(access_line: &ConfigLine) -> AccessLine {
51    AccessLine {
52        preference: child_block(access_line, "attributes")
53            .and_then(|attributes| arg_of(attributes, "preference"))
54            .map(str::to_string),
55        dsl: adjustments(child_block(access_line, "dsl")),
56        pon: adjustments(child_block(access_line, "pon")),
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use crate::system::system;
63    use asterust::config::juniper;
64
65    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
66    #[test]
67    fn extracts_the_access_line_adjustments() {
68        let access_line = system(&juniper::parse_braces(
69            r#"
70system {
71    access-line {
72        attributes {
73            preference dsl;
74        }
75        dsl {
76            adsl2-plus {
77                overhead-bytes 10;
78                total-adjust 95;
79            }
80            gfast {
81                overhead-adjust 90;
82            }
83            type 145 {
84                overhead-bytes 4;
85            }
86        }
87        pon {
88            gpon {
89                overhead-adjust 85;
90                total-adjust 98;
91            }
92        }
93    }
94}
95"#,
96        ))
97        .unwrap()
98        .access_line
99        .unwrap();
100        assert_eq!(access_line.preference.as_deref(), Some("dsl"));
101        assert_eq!(access_line.dsl.len(), 3);
102        assert_eq!(access_line.dsl[0].line_type, "adsl2-plus");
103        assert_eq!(access_line.dsl[0].overhead_bytes.as_deref(), Some("10"));
104        assert_eq!(access_line.dsl[0].total_adjust.as_deref(), Some("95"));
105        assert_eq!(access_line.dsl[0].overhead_adjust, None);
106        assert_eq!(access_line.dsl[1].overhead_adjust.as_deref(), Some("90"));
107        assert_eq!(access_line.dsl[2].line_type, "type 145");
108        assert_eq!(access_line.pon[0].line_type, "gpon");
109        assert_eq!(access_line.pon[0].overhead_adjust.as_deref(), Some("85"));
110    }
111
112    #[test]
113    fn no_access_line_is_none() {
114        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
115        assert!(system.access_line.is_none());
116    }
117}