pub mod generic;
pub mod junos;
use crate::capability::Capabilities;
use crate::facts::Facts;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CloseSequence {
Standard,
DiscardThenClose,
}
pub trait VendorProfile: Send + Sync {
fn name(&self) -> &str;
fn wrap_config(&self, config: &str) -> String;
fn unwrap_config(&self, response: &str) -> String;
fn normalize_capability(&self, uri: &str) -> Option<String>;
fn close_sequence(&self) -> CloseSequence;
fn facts_rpc(&self) -> Option<&str> {
None
}
fn parse_facts(&self, _response: &str) -> Facts {
Facts::default()
}
fn post_facts_hook(&mut self, _facts: &Facts, _raw_response: &str) {}
fn requires_open_configuration(&self) -> bool {
false
}
}
pub fn detect_vendor(capabilities: &Capabilities) -> Box<dyn VendorProfile> {
if let Some(v) = junos::JunosVendor::detect(capabilities) {
return v;
}
Box::new(generic::GenericVendor)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::capability::Capabilities;
use std::collections::HashSet;
#[test]
fn test_detect_vendor_junos() {
let mut uris = HashSet::new();
uris.insert("urn:ietf:params:netconf:base:1.0".to_string());
uris.insert("http://xml.juniper.net/netconf/junos/1.0".to_string());
let caps = Capabilities::new(uris, Some(1));
let vendor = detect_vendor(&caps);
assert_eq!(vendor.name(), "junos");
}
#[test]
fn test_detect_vendor_unknown_falls_back_to_generic() {
let mut uris = HashSet::new();
uris.insert("urn:ietf:params:netconf:base:1.0".to_string());
let caps = Capabilities::new(uris, Some(1));
let vendor = detect_vendor(&caps);
assert_eq!(vendor.name(), "generic");
}
}