Skip to main content

asterust_junos/
device.rs

1//! [`JunosDevice`]: one parsed JunOS config 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/// A parsed JunOS `show configuration` (brace format).
15#[derive(Clone, Debug)]
16pub struct JunosDevice {
17    config: Config,
18}
19
20impl JunosDevice {
21    /// Parse raw brace-format `show configuration` text.
22    pub fn parse(config_text: &str) -> Self {
23        Self::from_config(juniper::parse_braces(config_text))
24    }
25
26    /// Wrap an already-parsed [`Config`].
27    pub fn from_config(config: Config) -> Self {
28        Self { config }
29    }
30
31    /// The underlying generic config tree.
32    pub fn config(&self) -> &Config {
33        &self.config
34    }
35
36    pub fn interfaces(&self) -> Vec<Interface> {
37        interfaces(&self.config)
38    }
39
40    pub fn ospf(&self) -> Option<Ospf> {
41        ospf(&self.config)
42    }
43
44    pub fn bgp(&self) -> Option<Bgp> {
45        bgp(&self.config)
46    }
47
48    pub fn isis(&self) -> Option<Isis> {
49        isis(&self.config)
50    }
51
52    pub fn routing_instances(&self) -> Vec<RoutingInstance> {
53        routing_instances(&self.config)
54    }
55
56    pub fn routing_options(&self) -> Option<RoutingOptions> {
57        routing_options(&self.config)
58    }
59
60    pub fn policy_options(&self) -> Option<PolicyOptions> {
61        policy_options(&self.config)
62    }
63
64    pub fn firewall(&self) -> Option<Firewall> {
65        firewall(&self.config)
66    }
67
68    pub fn system(&self) -> Option<System> {
69        system(&self.config)
70    }
71}
72
73impl asterust::device::Device for JunosDevice {
74    fn parse(config_text: &str) -> Self {
75        Self::parse(config_text)
76    }
77
78    fn from_config(config: Config) -> Self {
79        Self::from_config(config)
80    }
81
82    fn config(&self) -> &Config {
83        Self::config(self)
84    }
85}