Skip to main content

asterust_junos/
device.rs

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