Skip to main content

asterust_junos/
device.rs

1//! Bundles one parsed Junos configuration with every supported view as a field.
2
3use crate::class_of_service::{class_of_service, ClassOfService};
4use crate::firewall::{firewall, Firewall};
5use crate::interfaces::{interfaces, Interface};
6use crate::policy_options::{policy_options, PolicyOptions};
7use crate::protocols::{protocols, Protocols};
8use crate::routing_instances::{routing_instances, RoutingInstance};
9use crate::routing_options::{routing_options, RoutingOptions};
10use crate::system::{system, System};
11use asterust::config::{juniper, Config};
12
13/// Holds a parsed Junos `show configuration` (brace format) and every supported view of it, read once when the device
14/// is built.
15///
16/// The fields mirror the configuration hierarchy: `protocols { bgp { ... } }` is `device.protocols.bgp`.
17///
18/// # Examples
19///
20/// ```
21/// use asterust_junos::JunosDevice;
22///
23/// let device = JunosDevice::parse("system {\n    host-name pe1;\n}\n");
24/// assert_eq!(device.system.unwrap().host_name.as_deref(), Some("pe1"));
25/// assert!(device.protocols.bgp.is_none());
26/// ```
27#[derive(Clone, Debug)]
28pub struct JunosDevice {
29    config: Config,
30    pub class_of_service: Option<ClassOfService>,
31    pub interfaces: Vec<Interface>,
32    pub protocols: Protocols,
33    pub routing_instances: Vec<RoutingInstance>,
34    pub routing_options: Option<RoutingOptions>,
35    pub policy_options: Option<PolicyOptions>,
36    pub firewall: Option<Firewall>,
37    pub system: Option<System>,
38}
39
40impl JunosDevice {
41    /// Parses raw brace-format `show configuration` text.
42    pub fn parse(config_text: &str) -> Self {
43        Self::from_config(juniper::parse_braces(config_text))
44    }
45
46    /// Wraps an already-parsed [`Config`].
47    pub fn from_config(config: Config) -> Self {
48        Self {
49            class_of_service: class_of_service(&config),
50            interfaces: interfaces(&config),
51            protocols: protocols(&config),
52            routing_instances: routing_instances(&config),
53            routing_options: routing_options(&config),
54            policy_options: policy_options(&config),
55            firewall: firewall(&config),
56            system: system(&config),
57            config,
58        }
59    }
60
61    /// Returns the underlying generic config tree.
62    pub fn config(&self) -> &Config {
63        &self.config
64    }
65}
66
67impl asterust::device::Device for JunosDevice {
68    fn parse(config_text: &str) -> Self {
69        Self::parse(config_text)
70    }
71
72    fn from_config(config: Config) -> Self {
73        Self::from_config(config)
74    }
75
76    fn config(&self) -> &Config {
77        Self::config(self)
78    }
79}