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