nmstate 2.2.26

Library for networking management in a declarative manner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// SPDX-License-Identifier: Apache-2.0

use crate::NmstateError;

const NEXT_TOKEN_START_CHARS: [char; 6] = [' ', '=', '!', '|', '"', ':'];

#[derive(Clone, Eq, PartialEq, Debug)]
pub(crate) enum NetworkCaptureToken {
    Path(Vec<String>, usize), // Example: routes.running.destination
    Value(String, usize),     // Example: 13 or "Abc"
    Pipe(usize),              // |
    Replace(usize),           // :=
    Equal(usize),             // ==
    Null(usize),              // Unquoted null or NULL or Null
}

impl Default for NetworkCaptureToken {
    fn default() -> Self {
        Self::Value(String::new(), 0)
    }
}

impl NetworkCaptureToken {
    pub(crate) fn pos(&self) -> usize {
        match self {
            Self::Path(_, p)
            | Self::Value(_, p)
            | Self::Pipe(p)
            | Self::Replace(p)
            | Self::Equal(p)
            | Self::Null(p) => *p,
        }
    }
}

pub(crate) fn parse_str_to_capture_tokens(
    line: &str,
) -> Result<Vec<NetworkCaptureToken>, NmstateError> {
    let mut ret: Vec<NetworkCaptureToken> = Vec::new();
    let line_chars: Vec<char> = line.chars().collect();
    let mut line_iter = line.char_indices().peekable();

    while let Some((pos, c)) = line_iter.next() {
        log::debug!(
            "Token processing {:?}, got {:?}, cur: {}",
            String::from_iter(&line_chars[pos..]),
            ret,
            c
        );
        match c {
            '=' => {
                if let Some((_, c)) = line_iter.next() {
                    if c == '=' {
                        if ret.as_slice().iter().any(|c| {
                            matches!(c, &NetworkCaptureToken::Equal(_))
                        }) {
                            return Err(NmstateError::new_policy_error(
                                "Nmpolicy does not allows two equal action"
                                    .to_string(),
                                line,
                                pos,
                            ));
                        }
                        ret.push(NetworkCaptureToken::Equal(pos));
                    } else {
                        return Err(NmstateError::new_policy_error(
                            "Invalid equal action string after =, \
                            expecting another '='"
                                .to_string(),
                            line,
                            pos + 1,
                        ));
                    }
                } else {
                    return Err(NmstateError::new_policy_error(
                        "Invalid equal action string after =, \
                        expecting another '=', but got nothing"
                            .to_string(),
                        line,
                        pos,
                    ));
                }
            }
            '|' => {
                if ret
                    .as_slice()
                    .iter()
                    .any(|t| matches!(t, &NetworkCaptureToken::Pipe(_)))
                {
                    return Err(NmstateError::new_policy_error(
                        "Nmpolicy does not allows two pipe action".to_string(),
                        line,
                        pos,
                    ));
                } else {
                    ret.push(NetworkCaptureToken::Pipe(pos));
                }
            }
            ':' => {
                if let Some((_, c)) = line_iter.next() {
                    if c == '=' {
                        if ret.as_slice().iter().any(|t| {
                            matches!(t, &NetworkCaptureToken::Replace(_))
                        }) {
                            return Err(NmstateError::new_policy_error(
                                "Nmpolicy does not allows two replace action"
                                    .to_string(),
                                line,
                                pos,
                            ));
                        } else {
                            ret.push(NetworkCaptureToken::Replace(pos));
                        }
                    } else {
                        return Err(NmstateError::new_policy_error(
                            "Invalid replace action string after =, \
                            expecting another '='"
                                .to_string(),
                            line,
                            pos + 1,
                        ));
                    }
                } else {
                    return Err(NmstateError::new_policy_error(
                        "Invalid replace action string after =, \
                        expecting another '=', but got nothing"
                            .to_string(),
                        line,
                        pos,
                    ));
                }
            }
            '"' => {
                // Continue till next double quote
                if pos + 1 >= line_chars.len()
                    || !&line_chars[pos + 1..].contains(&'"')
                {
                    return Err(NmstateError::new_policy_error(
                        "No ending double quote".to_string(),
                        line,
                        pos,
                    ));
                }
                let mut quoted_string_chars = Vec::new();
                for (_, c) in line_iter.by_ref() {
                    if c != '"' {
                        quoted_string_chars.push(c);
                    } else {
                        break;
                    }
                }
                if !quoted_string_chars.is_empty() {
                    ret.push(NetworkCaptureToken::Value(
                        String::from_iter(quoted_string_chars.as_slice()),
                        pos + 1,
                    ));
                }
            }
            _ => {
                if c.is_whitespace() {
                    continue;
                }
                let block = consume_till_next_token(c, &mut line_iter);
                if block.contains('.') {
                    ret.push(NetworkCaptureToken::Path(
                        block.split('.').map(|c| c.to_string()).collect(),
                        pos,
                    ));
                } else if !block.is_empty() {
                    if block.to_lowercase() == "null" {
                        ret.push(NetworkCaptureToken::Null(pos));
                    } else {
                        ret.push(NetworkCaptureToken::Value(block, pos));
                    }
                }
            }
        }
    }

    if let Some(pos) = ret
        .as_slice()
        .iter()
        .position(|c| matches!(c, &NetworkCaptureToken::Pipe(_)))
    {
        if ret.len() == pos + 1 {
            return Err(NmstateError::new_policy_error(
                "Invalid pipe action: no property path defined".to_string(),
                line,
                ret[pos].pos(),
            ));
        }
        if !matches!(&ret[pos + 1], &NetworkCaptureToken::Path(_, _)) {
            return Err(NmstateError::new_policy_error(
                "Invalid pipe action: only property path allowed after pipe"
                    .to_string(),
                line,
                ret[pos + 1].pos(),
            ));
        }
    }

    Ok(ret)
}

fn consume_till_next_token(
    leading_char: char,
    line_iter: &mut std::iter::Peekable<std::str::CharIndices>,
) -> String {
    let mut block = vec![leading_char];
    while let Some((_, c)) = line_iter.peek() {
        if NEXT_TOKEN_START_CHARS.contains(c) {
            break;
        } else if let Some((_, c)) = line_iter.next() {
            block.push(c);
        } else {
            break;
        }
    }
    String::from_iter(block.as_slice()).trim().to_string()
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub(crate) enum NetworkTemplateToken {
    Value(String, usize),
    ReferenceStart(usize),    // {{
    Path(Vec<String>, usize), // Example: routes.running.destination
    ReferenceEnd(usize),      // }}
}

impl NetworkTemplateToken {
    pub(crate) fn pos(&self) -> usize {
        match self {
            Self::Path(_, p)
            | Self::Value(_, p)
            | Self::ReferenceStart(p)
            | Self::ReferenceEnd(p) => *p,
        }
    }
}

pub(crate) fn parse_str_to_template_tokens(
    line: &str,
) -> Result<Vec<NetworkTemplateToken>, NmstateError> {
    let mut ret: Vec<NetworkTemplateToken> = Vec::new();
    let line_chars: Vec<char> = line.chars().collect();
    let mut line_iter = line.char_indices().peekable();

    while let Some((pos, c)) = line_iter.next() {
        log::debug!(
            "Token processing {:?}, got {:?}, cur: {}",
            String::from_iter(&line_chars[pos..]),
            ret,
            c
        );
        match c {
            '{' => {
                if let Some((_, c)) = line_iter.next() {
                    if c == '{' {
                        if ret.as_slice().iter().any(|c| {
                            matches!(
                                c,
                                &NetworkTemplateToken::ReferenceStart(_)
                            )
                        }) {
                            return Err(NmstateError::new_policy_error(
                                "Nmpolicy does not allow multiple references \
                                {{}}"
                                    .to_string(),
                                line,
                                pos,
                            ));
                        }
                        ret.push(NetworkTemplateToken::ReferenceStart(pos));
                        // Normally we have a white space after '}', let's
                        // remove it and move on the pos
                        while let Some((_, c)) = line_iter.peek() {
                            if c.is_whitespace() {
                                line_iter.next();
                            } else {
                                break;
                            }
                        }
                    } else {
                        return Err(NmstateError::new_policy_error(
                            "Invalid reference action string after {, \
                            expecting another '{'"
                                .to_string(),
                            line,
                            pos,
                        ));
                    }
                } else {
                    return Err(NmstateError::new_policy_error(
                        "Invalid reference action string after =, \
                        expecting another '{', but got nothing"
                            .to_string(),
                        line,
                        pos,
                    ));
                }
            }
            '}' => {
                if let Some((_, c)) = line_iter.next() {
                    if c == '}' {
                        if ret.as_slice().iter().any(|c| {
                            matches!(c, &NetworkTemplateToken::ReferenceEnd(_))
                        }) {
                            return Err(NmstateError::new_policy_error(
                                "Nmpolicy does not allow multiple references \
                                {{}}"
                                    .to_string(),
                                line,
                                pos,
                            ));
                        }
                        ret.push(NetworkTemplateToken::ReferenceEnd(pos));
                    } else {
                        return Err(NmstateError::new_policy_error(
                            "Invalid reference action string after }, \
                            expecting another '}'"
                                .to_string(),
                            line,
                            pos,
                        ));
                    }
                } else {
                    return Err(NmstateError::new_policy_error(
                        "Invalid reference action string after }, \
                        expecting another '}', but got nothing"
                            .to_string(),
                        line,
                        pos,
                    ));
                }
            }
            _ => {
                let mut chars = vec![c];
                while let Some((_, c)) = line_iter.peek() {
                    if ['{', '}'].contains(c) {
                        break;
                    } else if let Some((_, c)) = line_iter.next() {
                        if c.is_whitespace() {
                            break;
                        }
                        chars.push(c);
                    } else {
                        break;
                    }
                }
                // position should be first char after trimmed
                let subline =
                    String::from_iter(chars.as_slice()).trim().to_string();
                if subline.is_empty() {
                    continue;
                }
                if subline.contains('.') && !subline.starts_with('.') {
                    ret.push(NetworkTemplateToken::Path(
                        subline.split('.').map(|c| c.to_string()).collect(),
                        pos,
                    ));
                } else {
                    ret.push(NetworkTemplateToken::Value(subline, pos));
                }
            }
        }
    }

    // The reference start and end should be paired.
    if let Some(token_start) = ret
        .as_slice()
        .iter()
        .find(|c| matches!(c, &NetworkTemplateToken::ReferenceStart(_)))
    {
        if !ret
            .as_slice()
            .iter()
            .any(|c| matches!(c, &NetworkTemplateToken::ReferenceEnd(_)))
        {
            return Err(NmstateError::new_policy_error(
                "No reference end }} found".to_string(),
                line,
                token_start.pos(),
            ));
        }
    }

    if let Some(token_end) = ret
        .as_slice()
        .iter()
        .find(|c| matches!(c, &NetworkTemplateToken::ReferenceEnd(_)))
    {
        if !ret
            .as_slice()
            .iter()
            .any(|c| matches!(c, &NetworkTemplateToken::ReferenceStart(_)))
        {
            return Err(NmstateError::new_policy_error(
                "No reference start {{ found".to_string(),
                line,
                token_end.pos(),
            ));
        }
    }

    if let (Some(token_start_pos), Some(token_end_pos)) = (
        ret.as_slice().iter().position(|t| {
            matches!(t, &NetworkTemplateToken::ReferenceStart(_))
        }),
        ret.as_slice()
            .iter()
            .position(|t| matches!(t, &NetworkTemplateToken::ReferenceEnd(_))),
    ) {
        if token_start_pos >= token_end_pos {
            return Err(NmstateError::new_policy_error(
                "The reference start {{ can not be placed before \
                reference end }}"
                    .to_string(),
                line,
                ret[token_end_pos].pos(),
            ));
        }
        if token_start_pos + 1 >= token_end_pos {
            return Err(NmstateError::new_policy_error(
                "No property path between reference start {{ and \
                reference end }}"
                    .to_string(),
                line,
                ret[token_end_pos].pos() - 1,
            ));
        }
        if token_start_pos + 2 != token_end_pos {
            return Err(NmstateError::new_policy_error(
                "Only allows single property path between reference \
                start {{ and reference end }}"
                    .to_string(),
                line,
                ret[token_start_pos + 2].pos(),
            ));
        }
    }

    Ok(ret)
}