netgauze_ipfix_code_generator/xml_parsers/
sub_registries.rs

1// Copyright (C) 2022-present The NetGauze Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12// implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::{
17    xml_parsers::xml_common::*, InformationElementSubRegistry, ReasonCodeNestedRegistry,
18    SubRegistryType, ValueNameDescRegistry, Xref,
19};
20
21use regex::Regex;
22use roxmltree::Node;
23
24const MAX_WORDS_NAME: usize = 10;
25const MAX_CHARS_DISPLAY_NAME: usize = 50;
26
27/// Subregistry Trait with getter functions for common values
28pub trait SubRegistry {
29    fn name(&self) -> &str;
30    fn display_name(&self) -> &str;
31    fn description(&self) -> &str;
32    fn comments(&self) -> &Option<String>;
33    fn parameters(&self) -> &Option<String>;
34    fn xrefs(&self) -> &Vec<Xref>;
35}
36
37impl SubRegistry for ValueNameDescRegistry {
38    fn name(&self) -> &str {
39        &self.name
40    }
41
42    fn display_name(&self) -> &str {
43        &self.display_name
44    }
45
46    fn description(&self) -> &str {
47        &self.description
48    }
49
50    fn comments(&self) -> &Option<String> {
51        &self.comments
52    }
53
54    fn parameters(&self) -> &Option<String> {
55        &self.parameters
56    }
57
58    fn xrefs(&self) -> &Vec<Xref> {
59        &self.xrefs
60    }
61}
62
63impl SubRegistry for ReasonCodeNestedRegistry {
64    fn name(&self) -> &str {
65        &self.name
66    }
67
68    fn display_name(&self) -> &str {
69        &self.display_name
70    }
71
72    fn description(&self) -> &str {
73        &self.description
74    }
75
76    fn comments(&self) -> &Option<String> {
77        &self.comments
78    }
79
80    fn parameters(&self) -> &Option<String> {
81        &self.parameters
82    }
83
84    fn xrefs(&self) -> &Vec<Xref> {
85        &self.xrefs
86    }
87}
88
89/// Wrapper to call the appropriate sub-registry parsing function based on the
90/// registry_type. Returns a `Vec<InformationElementSubRegistry>`
91pub fn parse_subregistry(
92    node: &Node<'_, '_>,
93    registry_type: SubRegistryType,
94) -> (u16, Vec<InformationElementSubRegistry>) {
95    match registry_type {
96        SubRegistryType::ValueNameDescRegistry => {
97            let (ie_id, reg) = parse_val_name_desc_u8_registry(node);
98            let ie_subreg: Vec<InformationElementSubRegistry> = reg
99                .into_iter()
100                .map(InformationElementSubRegistry::ValueNameDescRegistry)
101                .collect();
102            (ie_id, ie_subreg)
103        }
104        SubRegistryType::ReasonCodeNestedRegistry => {
105            let (ie_id, reg) = parse_reason_code_nested_u8_registry_2bit(node);
106            let ie_subreg: Vec<InformationElementSubRegistry> = reg
107                .into_iter()
108                .map(InformationElementSubRegistry::ReasonCodeNestedRegistry)
109                .collect();
110            (ie_id, ie_subreg)
111        }
112    }
113}
114
115/// Parse generic sub-registries with value, name and/or description, and
116/// optionally a comment, and parameter set. Examples:
117/// - [flowEndReason (Value 136)](https://www.iana.org/assignments/ipfix/ipfix.xhtml#ipfix-flow-end-reason)
118/// - [flowSelectorAlgorithm (Value 390)](https://www.iana.org/assignments/ipfix/ipfix.xhtml#ipfix-flowselectoralgorithm)
119pub fn parse_val_name_desc_u8_registry(node: &Node<'_, '_>) -> (u16, Vec<ValueNameDescRegistry>) {
120    let mut ret = Vec::new();
121
122    let children = node
123        .children()
124        .filter(|x| x.tag_name() == (IANA_NAMESPACE, "record").into())
125        .collect::<Vec<_>>();
126
127    let title = get_string_child(node, (IANA_NAMESPACE, "title").into()).unwrap_or_default();
128
129    let ie_id_regex = Regex::new(r"Value (\d+)").unwrap();
130    let ie_id = ie_id_regex
131        .captures(&title)
132        .and_then(|captures| captures.get(1))
133        .and_then(|capture| capture.as_str().parse().ok())
134        .unwrap_or(0);
135
136    for child in &children {
137        let value = get_string_child(child, (IANA_NAMESPACE, "value").into()).map(|x| {
138            if let Some(hex_value) = x.strip_prefix("0x") {
139                u8::from_str_radix(hex_value, 16)
140            } else if let Some(bin_value) = x.strip_prefix("0b") {
141                u8::from_str_radix(bin_value, 2)
142            } else if let Some(bin_value) = x.strip_suffix('b') {
143                u8::from_str_radix(bin_value, 2)
144            } else {
145                x.parse::<u8>()
146            }
147        });
148
149        let name_parsed = get_string_child(child, (IANA_NAMESPACE, "name").into());
150
151        // TODO: also consider unassigned and experimentation values
152        if Some(true)
153            == name_parsed
154                .as_ref()
155                .map(|x| x.as_str() == UNASSIGNED || x.contains(EXPERIMENTATION))
156        {
157            continue;
158        }
159
160        let description_parsed = parse_simple_description_string(child);
161        if Some(true)
162            == description_parsed
163                .as_ref()
164                .map(|x| x.as_str() == UNASSIGNED || x.contains(EXPERIMENTATION))
165        {
166            continue;
167        }
168
169        // Populate name, display_name, and description
170        // - name is always a usable enum variant type name (if not there in the
171        //   registry, take it from the description)
172        // - display_name matches the IANA registry name (apart from when name is
173        //   populated from description field)
174        let mut name: String;
175        let mut display_name: String;
176        let description: String;
177        if let Some(Ok(value)) = value {
178            if value == u8::MAX {
179                // TODO: also consider unassigned and experimentation values
180                continue;
181            }
182
183            if let Some(name_parsed) = name_parsed {
184                display_name = name_parsed.clone();
185
186                (_, name) = xml_string_to_enum_type(&name_parsed);
187                if let Some(desc_parsed) = description_parsed {
188                    description = desc_parsed;
189                } else {
190                    description = name_parsed;
191                }
192            } else if let Some(mut desc_parsed) = description_parsed {
193                description = desc_parsed.clone();
194
195                let desc_words_amount: usize;
196                (desc_words_amount, desc_parsed) = xml_string_to_enum_type(&desc_parsed);
197
198                if desc_words_amount < MAX_WORDS_NAME {
199                    display_name = desc_parsed.clone();
200                    name = desc_parsed;
201                } else {
202                    display_name = format!("Value{value}");
203                    name = format!("Value{value}");
204                }
205
206                if description.len() < MAX_CHARS_DISPLAY_NAME {
207                    display_name = description.clone();
208                }
209            } else {
210                log::info!("Skipping sub-registry: missing both name and description!");
211                continue;
212            }
213
214            // Handle duplicates
215            if name == *RESERVED || name == *PRIVATE {
216                name = format!("{name}{value}");
217            }
218
219            let comments = get_string_child(child, (IANA_NAMESPACE, "comments").into());
220            let parameters = get_string_child(child, (IANA_NAMESPACE, "parameters").into());
221            let xrefs = parse_xref(child);
222
223            ret.push(ValueNameDescRegistry {
224                value,
225                name,
226                display_name,
227                description,
228                comments,
229                parameters,
230                xrefs,
231            });
232        }
233    }
234
235    (ie_id, ret)
236}
237
238/// Parse sub-registries with nested (first 2bit = status) registries for reason
239/// code, such as: [Forwarding Status (Value 89)](https://www.iana.org/assignments/ipfix/ipfix.xml#forwarding-status)
240pub fn parse_reason_code_nested_u8_registry_2bit(
241    node: &Node<'_, '_>,
242) -> (u16, Vec<ReasonCodeNestedRegistry>) {
243    let (ie_id, subreg) = parse_val_name_desc_u8_registry(node);
244
245    let ret: Vec<ReasonCodeNestedRegistry> = subreg
246        .iter()
247        .map(|subreg| {
248            let val_bin_str = format!("{:02b}", subreg.value);
249            let reason_code_pattern = format!(r".*-{val_bin_str}b");
250            let reason_code_reg_pattern = Regex::new(&reason_code_pattern).unwrap();
251            let reason_code_node = find_node_by_regex(node, &reason_code_reg_pattern).unwrap();
252            ReasonCodeNestedRegistry {
253                value: subreg.value << 6,
254                name: SubRegistry::name(subreg).to_string(),
255                display_name: SubRegistry::display_name(subreg).to_string(),
256                description: SubRegistry::description(subreg).to_string(),
257                comments: SubRegistry::comments(subreg).to_owned(),
258                parameters: SubRegistry::parameters(subreg).to_owned(),
259                xrefs: SubRegistry::xrefs(subreg).to_owned(),
260                reason_code_reg: {
261                    let (_, reg) = parse_val_name_desc_u8_registry(&reason_code_node);
262                    let reg: Vec<InformationElementSubRegistry> = reg
263                        .into_iter()
264                        .map(InformationElementSubRegistry::ValueNameDescRegistry)
265                        .collect();
266                    reg
267                },
268            }
269        })
270        .collect();
271
272    (ie_id, ret)
273}