use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, child_block, has_leaf, named_children};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct JtreeResourceType {
pub resource_type: String,
pub low_watermark: Option<String>,
pub high_watermark: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PortLimit {
pub number: String,
pub limit: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PicLimit {
pub number: String,
pub limit: Option<String>,
pub ports: Vec<PortLimit>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FpcLimit {
pub slot: String,
pub limit: Option<String>,
pub pics: Vec<PicLimit>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SubscriberLimit {
pub client_type: String,
pub chassis_limit: Option<String>,
pub fpcs: Vec<FpcLimit>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ResourceMonitor {
pub free_fw_memory_watermark: Option<String>,
pub free_heap_memory_watermark: Option<String>,
pub free_nh_memory_watermark: Option<String>,
pub high_cos_queue_threshold: Option<String>,
pub high_threshold: Option<String>,
pub no_logging: bool,
pub no_throttle: bool,
pub no_load_throttle: bool,
pub jtree_resource_types: Vec<JtreeResourceType>,
pub subscribers_limit: Vec<SubscriberLimit>,
pub traceoptions: Option<Traceoptions>,
}
fn limit_of(block: &ConfigLine) -> Option<String> {
arg_of(block, "limit").map(str::to_string)
}
fn parse_fpc(slot: &str, fpc: &ConfigLine) -> FpcLimit {
FpcLimit {
slot: slot.to_string(),
limit: limit_of(fpc),
pics: named_children(fpc, "pic")
.map(|(number, pic)| PicLimit {
number: number.to_string(),
limit: limit_of(pic),
ports: named_children(pic, "port")
.map(|(number, port)| PortLimit {
number: number.to_string(),
limit: limit_of(port),
})
.collect(),
})
.collect(),
}
}
pub(super) fn parse_resource_monitor(monitor: &ConfigLine) -> ResourceMonitor {
let field = |keyword: &str| arg_of(monitor, keyword).map(str::to_string);
ResourceMonitor {
free_fw_memory_watermark: field("free-fw-memory-watermark"),
free_heap_memory_watermark: field("free-heap-memory-watermark"),
free_nh_memory_watermark: field("free-nh-memory-watermark"),
high_cos_queue_threshold: field("high-cos-queue-threshold"),
high_threshold: field("high-threshold"),
no_logging: has_leaf(monitor, "no-logging"),
no_throttle: has_leaf(monitor, "no-throttle"),
no_load_throttle: has_leaf(monitor, "no-load-throttle"),
jtree_resource_types: named_children(monitor, "resource-category")
.filter(|(category, _)| *category == "jtree")
.flat_map(|(_, jtree)| named_children(jtree, "resource-type"))
.map(|(resource_type, block)| JtreeResourceType {
resource_type: resource_type.to_string(),
low_watermark: arg_of(block, "low-watermark").map(str::to_string),
high_watermark: arg_of(block, "high-watermark").map(str::to_string),
})
.collect(),
subscribers_limit: child_block(monitor, "subscribers-limit")
.map(|limits| {
named_children(limits, "client-type")
.map(|(client_type, client)| SubscriberLimit {
client_type: client_type.to_string(),
chassis_limit: child_block(client, "chassis").and_then(limit_of),
fpcs: named_children(client, "fpc")
.map(|(slot, fpc)| parse_fpc(slot, fpc))
.collect(),
})
.collect()
})
.unwrap_or_default(),
traceoptions: parse_traceoptions(monitor),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_resource_monitor_settings() {
let monitor = system(&juniper::parse_braces(
r#"
system {
services {
resource-monitor {
free-fw-memory-watermark 15;
free-heap-memory-watermark 20;
free-nh-memory-watermark 25;
high-cos-queue-threshold 80;
high-threshold 90;
no-logging;
no-throttle;
no-load-throttle;
resource-category jtree {
resource-type free-pages {
low-watermark 10;
high-watermark 30;
}
resource-type contiguous-pages;
}
subscribers-limit {
client-type pppoe {
chassis {
limit 8000;
}
fpc 1 {
limit 4000;
pic 0 {
limit 2000;
port 3 {
limit 500;
}
}
}
}
}
traceoptions {
file rsmon.log;
flag all;
}
}
}
}
"#,
))
.unwrap()
.services
.unwrap()
.resource_monitor
.unwrap();
assert_eq!(monitor.free_fw_memory_watermark.as_deref(), Some("15"));
assert_eq!(monitor.free_heap_memory_watermark.as_deref(), Some("20"));
assert_eq!(monitor.free_nh_memory_watermark.as_deref(), Some("25"));
assert_eq!(monitor.high_cos_queue_threshold.as_deref(), Some("80"));
assert_eq!(monitor.high_threshold.as_deref(), Some("90"));
assert!(monitor.no_logging);
assert!(monitor.no_throttle);
assert!(monitor.no_load_throttle);
assert_eq!(monitor.jtree_resource_types.len(), 2);
assert_eq!(monitor.jtree_resource_types[0].resource_type, "free-pages");
assert_eq!(monitor.jtree_resource_types[0].low_watermark.as_deref(), Some("10"));
assert_eq!(monitor.jtree_resource_types[0].high_watermark.as_deref(), Some("30"));
assert_eq!(monitor.jtree_resource_types[1].low_watermark, None);
let pppoe = &monitor.subscribers_limit[0];
assert_eq!(pppoe.client_type, "pppoe");
assert_eq!(pppoe.chassis_limit.as_deref(), Some("8000"));
let fpc = &pppoe.fpcs[0];
assert_eq!(fpc.slot, "1");
assert_eq!(fpc.limit.as_deref(), Some("4000"));
assert_eq!(fpc.pics[0].number, "0");
assert_eq!(fpc.pics[0].limit.as_deref(), Some("2000"));
assert_eq!(fpc.pics[0].ports[0].number, "3");
assert_eq!(fpc.pics[0].ports[0].limit.as_deref(), Some("500"));
assert_eq!(monitor.traceoptions.unwrap().file.as_deref(), Some("rsmon.log"));
}
#[test]
fn bare_resource_monitor_has_no_settings() {
let monitor = system(&juniper::parse_braces(
"system {\n services {\n resource-monitor;\n }\n}\n",
))
.unwrap()
.services
.unwrap()
.resource_monitor
.unwrap();
assert!(!monitor.no_logging);
assert!(monitor.jtree_resource_types.is_empty());
assert!(monitor.subscribers_limit.is_empty());
}
#[test]
fn no_resource_monitor_is_none() {
let services = system(&juniper::parse_braces(
"system {\n services {\n telnet;\n }\n}\n",
))
.unwrap()
.services
.unwrap();
assert!(services.resource_monitor.is_none());
}
}