Skip to main content

asterust_junos/protocols/bgp/
group.rs

1//! `group`
2//! `protocols { bgp { group <name> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [group (Protocols BGP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-edit-protocols-bgp.html)
7
8use super::dynamic_neighbor::{parse_allow, parse_dynamic_neighbor, DynamicNeighbor};
9use super::neighbor::{parse_neighbor, BgpNeighbor};
10use super::peer_options::{parse_peer_options, BgpPeerOptions};
11use crate::{arg_of, args_of, first_word, list_values, named_children};
12use asterust::config::ConfigLine;
13
14/// A `group <name>`.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct BgpGroup {
18    pub name: String,
19    /// `internal` or `external`.
20    pub group_type: Option<String>,
21    pub description: Option<String>,
22    pub peer_as: Option<String>,
23    pub local_as: Option<String>,
24    pub local_address: Option<String>,
25    /// Route-reflector `cluster` id.
26    pub cluster: Option<String>,
27    /// Address families, e.g. `"inet"`, `"inet-vpn"`.
28    pub families: Vec<String>,
29    pub import_policies: Vec<String>,
30    pub export_policies: Vec<String>,
31    pub neighbors: Vec<BgpNeighbor>,
32    /// Prefixes from which the group accepts sessions, written `allow [ ... ];`.
33    pub allow: Vec<String>,
34    /// The name of the `policy-options as-list` that a dynamic peer's AS must match.
35    pub peer_as_list: Option<String>,
36    pub dynamic_neighbors: Vec<DynamicNeighbor>,
37    pub options: BgpPeerOptions,
38}
39
40pub(super) fn parse_group(name: &str, group: &ConfigLine) -> BgpGroup {
41    let policies =
42        |keyword: &str| -> Vec<String> { args_of(group, keyword).into_iter().flat_map(list_values).collect() };
43    BgpGroup {
44        name: name.to_string(),
45        group_type: arg_of(group, "type").map(str::to_string),
46        description: arg_of(group, "description").map(str::to_string),
47        peer_as: arg_of(group, "peer-as").and_then(first_word).map(str::to_string),
48        local_as: arg_of(group, "local-as").and_then(first_word).map(str::to_string),
49        local_address: arg_of(group, "local-address").map(str::to_string),
50        cluster: arg_of(group, "cluster").map(str::to_string),
51        families: named_children(group, "family").map(|(f, _)| f.to_string()).collect(),
52        import_policies: policies("import"),
53        export_policies: policies("export"),
54        neighbors: named_children(group, "neighbor")
55            .map(|(address, neighbor)| parse_neighbor(address, neighbor))
56            .collect(),
57        allow: parse_allow(group),
58        peer_as_list: arg_of(group, "peer-as-list").map(str::to_string),
59        dynamic_neighbors: named_children(group, "dynamic-neighbor")
60            .map(|(name, block)| parse_dynamic_neighbor(name, block))
61            .collect(),
62        options: parse_peer_options(group),
63    }
64}