asterust-junos 0.4.1

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `classifiers`
//! `class-of-service { classifiers { <type> <name> { ... } } }`.
//!
//! # See also
//!
//! - [classifiers](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/classifiers-edit-cos.html)
//! - [forwarding-class (classifiers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forwarding-class-edit-cos-classifiers.html)
//! - [Example: Configure and Apply Custom BA Classifiers](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/cos/topics/example/cos-behavior-aggregate-classifier-configuring-security.html)
//! - [Example: Configuring Forwarding Classes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-forwarding-class-configuring-security.html)

use super::{code_points, typed_children};
use crate::{arg_of, args_of, child_block, named_children};
use asterust::config::ConfigLine;

/// A `loss-priority <level> code-points [ ... ]` statement of a classifier.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ClassifierEntry {
    pub loss_priority: String,
    /// Bit patterns or code-point aliases.
    pub code_points: Vec<String>,
}

/// A `forwarding-class <name>` entry of a classifier.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ClassifierClass {
    pub name: String,
    pub entries: Vec<ClassifierEntry>,
}

/// A `<type> <name>` classifier.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Classifier {
    /// `dscp`, `dscp-ipv6`, `exp`, `ieee-802.1`, `ieee-802.1ad`, `inet-precedence` or `inet6-precedence`.
    pub kind: String,
    pub name: String,
    /// The classifier imported, or `default`.
    pub import: Option<String>,
    pub forwarding_classes: Vec<ClassifierClass>,
}

fn parse_entry(arg: &str) -> ClassifierEntry {
    let (level, points) = arg.split_once("code-points").unwrap_or((arg, ""));
    ClassifierEntry {
        loss_priority: level.trim().to_string(),
        code_points: code_points(points),
    }
}

pub(super) fn parse_classifiers(cos: &ConfigLine) -> Vec<Classifier> {
    let Some(block) = child_block(cos, "classifiers") else {
        return Vec::new();
    };
    typed_children(block)
        .map(|(kind, name, classifier)| Classifier {
            kind: kind.to_string(),
            name: name.to_string(),
            import: arg_of(classifier, "import").map(str::to_string),
            forwarding_classes: named_children(classifier, "forwarding-class")
                .map(|(class, line)| ClassifierClass {
                    name: class.to_string(),
                    entries: args_of(line, "loss-priority").into_iter().map(parse_entry).collect(),
                })
                .collect(),
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::class_of_service::class_of_service;
    use asterust::config::juniper;

    // The `classifiers` of Example: Configure and Apply Custom BA Classifiers and of Example: Configuring Forwarding
    // Classes (Class of Service User Guide for Security Devices), wrapped in `class-of-service`; see the README
    // "Sources".
    const DOC_BA_CLASSIFIER: &str = r#"
class-of-service {
    classifiers {
        dscp ba-classifier {
            import default;
            forwarding-class be-class {
                loss-priority high code-points 000001;
            }
            forwarding-class ef-class {
                loss-priority high code-points 101111;
            }
            forwarding-class af-class {
                loss-priority high code-points 001100;
            }
            forwarding-class nc-class {
                loss-priority high code-points 110001;
            }
        }
    }
}
"#;

    // The guide writes the list with commas.
    const DOC_DSCP_TABLE: &str = r#"
class-of-service {
    classifiers {
        dscp dscp-table {
            forwarding-class ef {
                loss-priority low code-points [101000, 101001];
                loss-priority high code-points [101010, 101011];
            }
            forwarding-class be {
                loss-priority low code-points [000000];
            }
        }
    }
}
"#;

    const DOC_INET_PRECEDENCE: &str = r#"
class-of-service {
    classifiers {
        inet-precedence corp-traffic {
            forwarding-class voice-class {
                loss-priority low code-points 101;
            }
            forwarding-class data-class {
                loss-priority high code-points 000;
            }
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference. See the README "Sources".
    const EXTRA: &str = r#"
class-of-service {
    classifiers {
        exp mpls-in {
            import mpls-base;
            forwarding-class expedited-forwarding {
                loss-priority low code-points [ ef af11 ];
            }
        }
        ieee-802.1 dot1p;
    }
}
"#;

    fn classifiers(text: &str) -> Vec<super::Classifier> {
        class_of_service(&juniper::parse_braces(text)).unwrap().classifiers
    }

    #[test]
    fn reads_the_guide_ba_classifier() {
        let classifiers = classifiers(DOC_BA_CLASSIFIER);
        assert_eq!(classifiers.len(), 1);
        let classifier = &classifiers[0];
        assert_eq!(classifier.kind, "dscp");
        assert_eq!(classifier.name, "ba-classifier");
        assert_eq!(classifier.import.as_deref(), Some("default"));
        assert_eq!(classifier.forwarding_classes.len(), 4);
        assert_eq!(classifier.forwarding_classes[1].name, "ef-class");
        let entry = &classifier.forwarding_classes[1].entries[0];
        assert_eq!(entry.loss_priority, "high");
        assert_eq!(entry.code_points, ["101111"]);
    }

    #[test]
    fn reads_the_guide_code_point_lists() {
        let classifier = &classifiers(DOC_DSCP_TABLE)[0];
        let ef = &classifier.forwarding_classes[0];
        assert_eq!(ef.entries.len(), 2);
        assert_eq!(ef.entries[0].loss_priority, "low");
        assert_eq!(ef.entries[0].code_points, ["101000", "101001"]);
        assert_eq!(ef.entries[1].code_points, ["101010", "101011"]);
        assert_eq!(classifier.forwarding_classes[1].entries[0].code_points, ["000000"]);
        assert_eq!(classifier.import, None);
    }

    #[test]
    fn reads_the_guide_inet_precedence_classifier() {
        let classifier = &classifiers(DOC_INET_PRECEDENCE)[0];
        assert_eq!(classifier.kind, "inet-precedence");
        assert_eq!(classifier.name, "corp-traffic");
        assert_eq!(classifier.forwarding_classes[0].name, "voice-class");
        assert_eq!(classifier.forwarding_classes[0].entries[0].code_points, ["101"]);
        assert_eq!(classifier.forwarding_classes[1].entries[0].loss_priority, "high");
    }

    #[test]
    fn reads_aliases_and_a_classifier_without_entries() {
        let classifiers = classifiers(EXTRA);
        assert_eq!(classifiers.len(), 2);
        assert_eq!(classifiers[0].kind, "exp");
        assert_eq!(classifiers[0].import.as_deref(), Some("mpls-base"));
        assert_eq!(
            classifiers[0].forwarding_classes[0].entries[0].code_points,
            ["ef", "af11"]
        );
        assert_eq!(classifiers[1].kind, "ieee-802.1");
        assert_eq!(classifiers[1].name, "dot1p");
        assert!(classifiers[1].forwarding_classes.is_empty());
    }
}