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