asterust-junos 0.5.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
//! `virtual-channels` and `virtual-channel-groups`
//! `class-of-service { virtual-channels { <name>; } virtual-channel-groups { <group> { <channel> { ... } } } }`.
//!
//! The group a unit uses is `virtual-channel-group` in [`CosSettings`](super::CosSettings).
//!
//! # See also
//!
//! - [virtual-channels](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/class-of-service-edit-virtual-channel.html)
//! - [virtual-channel-groups](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/class-of-service-edit-virtual-channel-groups.html)
//! - [Example: Configuring Virtual Channels](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-virtual-channel-security-configuring.html)

use super::scheduler::{rate_of, SchedulerRate};
use crate::{active_children, arg_of, child_block, has_leaf};
use asterust::config::ConfigLine;

/// A virtual channel of a virtual channel group.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct VirtualChannel {
    pub name: String,
    pub scheduler_map: Option<String>,
    pub shaping_rate: Option<SchedulerRate>,
    /// The channel that carries traffic no filter sends elsewhere.
    pub default: bool,
}

/// A `virtual-channel-groups` entry.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct VirtualChannelGroup {
    pub name: String,
    pub channels: Vec<VirtualChannel>,
}

fn parse_channel(channel: &ConfigLine) -> Option<VirtualChannel> {
    Some(VirtualChannel {
        name: channel.text.split_whitespace().next()?.to_string(),
        scheduler_map: arg_of(channel, "scheduler-map").map(str::to_string),
        shaping_rate: rate_of(channel, "shaping-rate"),
        default: has_leaf(channel, "default"),
    })
}

pub(super) fn parse_virtual_channels(cos: &ConfigLine) -> Vec<String> {
    let Some(block) = child_block(cos, "virtual-channels") else {
        return Vec::new();
    };
    active_children(block)
        .filter_map(|channel| channel.text.split_whitespace().next())
        .map(str::to_string)
        .collect()
}

pub(super) fn parse_virtual_channel_groups(cos: &ConfigLine) -> Vec<VirtualChannelGroup> {
    let Some(block) = child_block(cos, "virtual-channel-groups") else {
        return Vec::new();
    };
    active_children(block)
        .filter_map(|group| {
            Some(VirtualChannelGroup {
                name: group.text.split_whitespace().next()?.to_string(),
                channels: active_children(group).filter_map(parse_channel).collect(),
            })
        })
        .collect()
}

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

    // The `class-of-service` blocks of Example: Configuring Virtual Channels (Class of Service User Guide for Security
    // Devices), shortened; see the README "Sources".
    const DOC_VIRTUAL_CHANNELS: &str = r#"
class-of-service {
    virtual-channels {
        branch1-vc;
        branch2-vc;
        default-vc;
    }
    virtual-channel-groups {
        wan-vc-group {
            branch1-vc {
                scheduler-map bestscheduler;
                shaping-rate 1500000;
            }
            branch2-vc {
                scheduler-map bestscheduler;
                shaping-rate 1500000;
            }
            default-vc {
                scheduler-map bestscheduler;
                default;
            }
        }
    }
    interfaces {
        t3-1/0/0 {
            unit 0 {
                virtual-channel-group wan-vc-group;
            }
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference. See the README "Sources".
    const EXTRA: &str = r#"
class-of-service {
    virtual-channel-groups {
        pct {
            fast {
                shaping-rate percent 40;
            }
        }
        empty;
    }
}
"#;

    #[test]
    fn reads_the_guide_virtual_channels_and_groups() {
        let cos = class_of_service(&juniper::parse_braces(DOC_VIRTUAL_CHANNELS)).unwrap();
        assert_eq!(cos.virtual_channels, ["branch1-vc", "branch2-vc", "default-vc"]);
        assert_eq!(cos.virtual_channel_groups.len(), 1);
        let group = &cos.virtual_channel_groups[0];
        assert_eq!(group.name, "wan-vc-group");
        assert_eq!(group.channels.len(), 3);
        assert_eq!(group.channels[0].name, "branch1-vc");
        assert_eq!(group.channels[0].scheduler_map.as_deref(), Some("bestscheduler"));
        assert_eq!(
            group.channels[0].shaping_rate.as_ref().unwrap().value.as_deref(),
            Some("1500000")
        );
        assert!(!group.channels[0].default);
        assert!(group.channels[2].default);
        assert_eq!(group.channels[2].shaping_rate, None);
    }

    #[test]
    fn reads_the_group_a_unit_uses() {
        let cos = class_of_service(&juniper::parse_braces(DOC_VIRTUAL_CHANNELS)).unwrap();
        let unit = &cos.interfaces[0].units[0];
        assert_eq!(unit.settings.virtual_channel_group.as_deref(), Some("wan-vc-group"));
    }

    #[test]
    fn reads_percent_rates_and_empty_groups() {
        let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
        assert!(cos.virtual_channels.is_empty());
        let rate = cos.virtual_channel_groups[0].channels[0].shaping_rate.as_ref().unwrap();
        assert_eq!(rate.mode.as_deref(), Some("percent"));
        assert_eq!(rate.value.as_deref(), Some("40"));
        assert_eq!(cos.virtual_channel_groups[1].name, "empty");
        assert!(cos.virtual_channel_groups[1].channels.is_empty());
    }
}