Skip to main content

asterust_junos/
device.rs

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