Skip to main content

asterust_junos/routing_instances/
mod.rs

1//! `routing-instances`
2//! `routing-instances { ... }`.
3//!
4//! # See also
5//!
6//! - [`[edit routing-instances]` hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/topic-map/hierarchy-edit-routing-instances.html)
7//! - [instance-type](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/instance-type-edit-routing-instances-vp.html)
8//! - [route-distinguisher](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/route-distinguisher-edit-routing-instances-vp.html)
9//! - [vrf-target](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vrf-target-edit-routing-instances-vp.html)
10//! - [vrf-import](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vrf-import-edit-routing-instances-vp.html)
11//! - [vrf-export](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vrf-export-edit-routing-instances-vp.html)
12
13use crate::protocols::bgp::{parse_srv6, BgpSrv6};
14use crate::{active_children, arg_of, args_of, child_block, list_values, top_level};
15use asterust::config::{Config, ConfigLine};
16
17/// A routing instance. Of the protocol configuration nested inside the instance only `bgp_srv6` is extracted.
18#[derive(Clone, Debug, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct RoutingInstance {
21    pub name: String,
22    /// e.g. `"vrf"`, `"virtual-router"`.
23    pub instance_type: Option<String>,
24    pub description: Option<String>,
25    pub route_distinguisher: Option<String>,
26    pub interfaces: Vec<String>,
27    /// From `vrf-target <target>` (both directions) or `vrf-target { import ...; }`.
28    pub vrf_target_import: Option<String>,
29    pub vrf_target_export: Option<String>,
30    /// Policies from `vrf-import` / `vrf-export`.
31    pub vrf_import_policies: Vec<String>,
32    pub vrf_export_policies: Vec<String>,
33    /// `protocols bgp source-packet-routing srv6`, the SRv6 service SIDs of the VRF.
34    pub bgp_srv6: Option<BgpSrv6>,
35}
36
37impl RoutingInstance {
38    /// Whether this is a VRF (`instance-type vrf`).
39    pub fn is_vrf(&self) -> bool {
40        self.instance_type.as_deref() == Some("vrf")
41    }
42}
43
44/// Returns every instance under the top-level `routing-instances` block.
45///
46/// # Examples
47///
48/// ```
49/// use asterust::config::juniper;
50/// use asterust_junos::routing_instances::routing_instances;
51///
52/// let config = juniper::parse_braces("routing-instances {\n    CUSTOMER-A {\n        instance-type vrf;\n    }\n}\n");
53/// let instances = routing_instances(&config);
54/// assert_eq!(instances[0].name, "CUSTOMER-A");
55/// assert!(instances[0].is_vrf());
56/// ```
57pub fn routing_instances(config: &Config) -> Vec<RoutingInstance> {
58    let Some(root) = top_level(config, "routing-instances") else {
59        return Vec::new();
60    };
61    // Instance names have no spaces; this skips `apply-groups` and similar statements.
62    active_children(root)
63        .filter(|c| !c.text.contains(' '))
64        .map(parse_instance)
65        .collect()
66}
67
68fn parse_instance(line: &ConfigLine) -> RoutingInstance {
69    let target_both = arg_of(line, "vrf-target");
70    let target_block = child_block(line, "vrf-target");
71    let target = |direction: &str| -> Option<String> {
72        target_both
73            .or_else(|| target_block.and_then(|b| arg_of(b, direction)))
74            .map(str::to_string)
75    };
76    let policies =
77        |keyword: &str| -> Vec<String> { args_of(line, keyword).into_iter().flat_map(list_values).collect() };
78
79    RoutingInstance {
80        name: line.text.clone(),
81        instance_type: arg_of(line, "instance-type").map(str::to_string),
82        description: arg_of(line, "description").map(str::to_string),
83        route_distinguisher: arg_of(line, "route-distinguisher").map(str::to_string),
84        interfaces: args_of(line, "interface").into_iter().map(str::to_string).collect(),
85        vrf_target_import: target("import"),
86        vrf_target_export: target("export"),
87        vrf_import_policies: policies("vrf-import"),
88        vrf_export_policies: policies("vrf-export"),
89        bgp_srv6: child_block(line, "protocols")
90            .and_then(|protocols| child_block(protocols, "bgp"))
91            .and_then(parse_srv6),
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use asterust::config::juniper;
99
100    // Written from JunOS hierarchy knowledge, not adapted from documentation; see the README "Sources".
101    const CONFIG: &str = r#"
102routing-instances {
103    CUSTOMER-A {
104        instance-type vrf;
105        interface ge-0/0/1.100;
106        route-distinguisher 65000:1;
107        vrf-target target:65000:1;
108    }
109    CUSTOMER-B {
110        description "Customer B";
111        instance-type vrf;
112        interface ge-0/0/2.0;
113        interface ge-0/0/3.0;
114        route-distinguisher 65000:2;
115        vrf-import CUSTB-IN;
116        vrf-export [ CUSTB-OUT TAG-ROUTES ];
117    }
118    CUSTOMER-C {
119        instance-type vrf;
120        route-distinguisher 65000:3;
121        vrf-target {
122            import target:65000:3;
123            export target:65000:33;
124        }
125    }
126    MGMT {
127        instance-type virtual-router;
128    }
129}
130"#;
131
132    #[test]
133    fn vrf_target_in_both_directions() {
134        let instances = routing_instances(&juniper::parse_braces(CONFIG));
135        assert_eq!(instances.len(), 4);
136
137        let a = &instances[0];
138        assert_eq!(a.name, "CUSTOMER-A");
139        assert!(a.is_vrf());
140        assert_eq!(a.route_distinguisher.as_deref(), Some("65000:1"));
141        assert_eq!(a.interfaces, ["ge-0/0/1.100"]);
142        assert_eq!(a.vrf_target_import.as_deref(), Some("target:65000:1"));
143        assert_eq!(a.vrf_target_export.as_deref(), Some("target:65000:1"));
144    }
145
146    #[test]
147    fn policies_target_block_and_non_vrf_instance() {
148        let instances = routing_instances(&juniper::parse_braces(CONFIG));
149
150        let b = &instances[1];
151        assert_eq!(b.description.as_deref(), Some("Customer B"));
152        assert_eq!(b.interfaces, ["ge-0/0/2.0", "ge-0/0/3.0"]);
153        assert_eq!(b.vrf_import_policies, ["CUSTB-IN"]);
154        assert_eq!(b.vrf_export_policies, ["CUSTB-OUT", "TAG-ROUTES"]);
155        assert_eq!(b.vrf_target_import, None);
156
157        let c = &instances[2];
158        assert_eq!(c.vrf_target_import.as_deref(), Some("target:65000:3"));
159        assert_eq!(c.vrf_target_export.as_deref(), Some("target:65000:33"));
160
161        assert!(!instances[3].is_vrf());
162    }
163}