bgpfu-netconf 0.1.0

A toolset for working with IRR data
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
use std::{
    fmt::{self, Debug, Display},
    io::Write,
    ops::Deref,
    sync::Arc,
    time::Duration,
};

use iri_string::types::UriStr;
use quick_xml::{
    events::{BytesStart, BytesText},
    NsReader, Writer,
};
use uuid::Uuid;

use crate::{
    capabilities::{Capability, Requirements},
    message::{ReadError, ReadXml, WriteError, WriteXml},
    session::Context,
    Error,
};

use super::{DataReply, EmptyReply, IntoResult};

pub trait Operation: Debug + WriteXml + Send + Sync + Sized {
    const NAME: &'static str;
    const REQUIRED_CAPABILITIES: Requirements;

    type Builder<'a>: Builder<'a, Self>;
    type Reply: Debug + ReadXml + IntoResult;

    fn new<'a, F>(ctx: &'a Context, build_fn: F) -> Result<Self, Error>
    where
        F: FnOnce(Self::Builder<'a>) -> Result<Self, Error>,
    {
        Self::REQUIRED_CAPABILITIES
            .check(ctx.server_capabilities())
            .then(|| Self::Builder::new(ctx).build(build_fn))
            .ok_or(Error::UnsupportedOperation {
                operation_name: Self::NAME,
                required_capabilities: Self::REQUIRED_CAPABILITIES,
            })?
    }
}

pub trait Builder<'a, O: Operation>: Debug + Sized {
    fn new(ctx: &'a Context) -> Self;

    fn finish(self) -> Result<O, Error>;

    fn build<F>(self, build_fn: F) -> Result<O, Error>
    where
        F: FnOnce(Self) -> Result<O, Error>,
    {
        build_fn(self)
    }
}

mod params;

pub mod get;
#[doc(inline)]
pub use self::get::Get;

pub mod get_config;
#[doc(inline)]
pub use self::get_config::GetConfig;

pub mod edit_config;
#[doc(inline)]
pub use self::edit_config::EditConfig;

pub mod copy_config;
#[doc(inline)]
pub use self::copy_config::CopyConfig;

pub mod delete_config;
#[doc(inline)]
pub use self::delete_config::DeleteConfig;

pub mod lock;
#[doc(inline)]
pub use self::lock::{Lock, Unlock};

pub mod kill_session;
#[doc(inline)]
pub use self::kill_session::KillSession;

pub mod commit;
#[doc(inline)]
pub use self::commit::Commit;

pub mod cancel_commit;
#[doc(inline)]
pub use self::cancel_commit::CancelCommit;

pub mod discard_changes;
#[doc(inline)]
pub use self::discard_changes::DiscardChanges;

pub mod validate;
#[doc(inline)]
pub use self::validate::Validate;

pub(crate) mod close_session;
pub(crate) use self::close_session::CloseSession;

#[cfg(feature = "junos")]
pub mod junos;

#[derive(Debug, Default, Copy, Clone)]
pub enum Datastore {
    #[default]
    Running,
    Candidate,
    Startup,
}

impl Datastore {
    fn try_as_source(self, ctx: &Context) -> Result<Self, Error> {
        let required_capabilities = match self {
            Self::Running => Requirements::None,
            Self::Candidate => Requirements::One(Capability::Candidate),
            Self::Startup => Requirements::One(Capability::Startup),
        };
        if required_capabilities.check(ctx.server_capabilities()) {
            Ok(self)
        } else {
            Err(Error::UnsupportedSource {
                datastore: self,
                required_capabilities,
            })
        }
    }

    fn try_as_target(self, ctx: &Context) -> Result<Self, Error> {
        let required_capabilities = match self {
            Self::Running => Requirements::One(Capability::WritableRunning),
            Self::Candidate => Requirements::One(Capability::Candidate),
            Self::Startup => Requirements::One(Capability::Startup),
        };
        if required_capabilities.check(ctx.server_capabilities()) {
            Ok(self)
        } else {
            Err(Error::UnsupportedTarget {
                datastore: self,
                required_capabilities,
            })
        }
    }

    fn try_as_lock_target(self, ctx: &Context) -> Result<Self, Error> {
        let required_capabilities = match self {
            Self::Running => Requirements::None,
            Self::Candidate => Requirements::One(Capability::Candidate),
            Self::Startup => Requirements::One(Capability::Startup),
        };
        if required_capabilities.check(ctx.server_capabilities()) {
            Ok(self)
        } else {
            Err(Error::UnsupportedLockTarget {
                datastore: self,
                required_capabilities,
            })
        }
    }

    const fn as_str(self) -> &'static str {
        match self {
            Self::Running => "running",
            Self::Candidate => "candidate",
            Self::Startup => "startup",
        }
    }
}

impl WriteXml for Datastore {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), WriteError> {
        _ = writer.create_element(self.as_str()).write_empty()?;
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub enum Source {
    Datastore(Datastore),
    Config(String),
    Url(Url),
}

impl WriteXml for Source {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), WriteError> {
        match self {
            Self::Datastore(datastore) => datastore.write_xml(writer)?,
            Self::Config(config) => {
                _ = writer
                    .create_element("config")
                    .write_inner_content(|writer| {
                        writer
                            .get_mut()
                            .write_all(config.as_bytes())
                            .map_err(|err| WriteError::Other(err.into()))
                    })?;
            }
            Self::Url(url) => url.write_xml(writer)?,
        };
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub enum Filter {
    Subtree(String),
    XPath(String),
}

impl Filter {
    const fn as_str(&self) -> &'static str {
        match self {
            Self::Subtree(_) => "subtree",
            Self::XPath(_) => "xpath",
        }
    }

    fn try_use(self, ctx: &Context) -> Result<Self, Error> {
        let required_capabilities = match self {
            Self::Subtree(_) => Requirements::None,
            Self::XPath(_) => Requirements::One(Capability::XPath),
        };
        if required_capabilities.check(ctx.server_capabilities()) {
            Ok(self)
        } else {
            Err(Error::UnsupportedFilterType {
                filter: self.as_str(),
                required_capabilities,
            })
        }
    }
}

impl WriteXml for Filter {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), WriteError> {
        let elem = writer
            .create_element("filter")
            .with_attribute(("type", self.as_str()));
        _ = match self {
            Self::Subtree(filter) => elem.write_inner_content(|writer| {
                writer
                    .get_mut()
                    .write_all(filter.as_bytes())
                    .map_err(|err| WriteError::Other(err.into()))
            })?,
            Self::XPath(select) => elem
                .with_attribute(("select", select.as_str()))
                .write_empty()?,
        };
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Opaque {
    inner: Arc<str>,
}

impl<A: AsRef<str>> From<A> for Opaque {
    fn from(value: A) -> Self {
        let inner = value.as_ref().into();
        Self { inner }
    }
}

impl ReadXml for Opaque {
    #[tracing::instrument(skip_all, fields(tag = ?start.local_name()), level = "debug")]
    fn read_xml(reader: &mut NsReader<&[u8]>, start: &BytesStart<'_>) -> Result<Self, ReadError> {
        let end = start.to_end();
        let inner = reader.read_text(end.name())?.into();
        Ok(Self { inner })
    }
}

impl WriteXml for Opaque {
    #[tracing::instrument(skip(writer))]
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), WriteError> {
        writer
            .get_mut()
            .write_all(self.as_bytes())
            .map_err(|err| WriteError::Other(err.into()))
    }
}

impl Display for Opaque {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.inner, f)
    }
}

impl Deref for Opaque {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}
// impl AsRef<str> for Opaque {
//     fn as_ref(&self) -> &str {
//         self.inner.as_ref()
//     }
// }

#[derive(Debug, Clone)]
pub struct Url {
    inner: Arc<UriStr>,
}

impl Url {
    fn try_new<S: AsRef<str>>(s: S, ctx: &Context) -> Result<Self, Error> {
        let url = UriStr::new(s.as_ref())?;
        ctx.server_capabilities()
            .iter()
            .filter_map(|capability| {
                if let Capability::Url(schemes) = capability {
                    Some(schemes.iter())
                } else {
                    None
                }
            })
            .flatten()
            .find(|&scheme| url.scheme_str() == scheme.as_ref())
            .ok_or_else(|| Error::UnsupportedUrlScheme { url: url.into() })
            .map(|_| Self { inner: url.into() })
    }
}

impl AsRef<str> for Url {
    fn as_ref(&self) -> &str {
        self.inner.as_str()
    }
}

impl Display for Url {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.inner.as_ref(), f)
    }
}

impl WriteXml for Url {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), WriteError> {
        _ = writer
            .create_element("url")
            .write_text_content(BytesText::new(self.inner.as_str()))?;
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
    inner: Arc<str>,
}

impl Token {
    pub fn new<S: AsRef<str>>(token: S) -> Self {
        let inner = token.as_ref().into();
        Self { inner }
    }

    #[must_use]
    pub fn generate() -> Self {
        let inner = Arc::from(
            &*Uuid::new_v4()
                .urn()
                .encode_lower(&mut Uuid::encode_buffer()),
        );
        Self { inner }
    }
}

impl Display for Token {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.inner, f)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Timeout(Duration);

impl Timeout {
    fn seconds(&self) -> BytesText<'static> {
        BytesText::new(&self.0.as_secs().to_string()).into_owned()
    }

    #[cfg(feature = "junos")]
    fn minutes(&self) -> BytesText<'static> {
        BytesText::new(&self.0.as_secs().div_ceil(60).to_string()).into_owned()
    }
}

impl Default for Timeout {
    fn default() -> Self {
        Self(Duration::from_secs(600))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use quick_xml::events::Event;

    #[test]
    fn reply_from_xml() {
        let reply = "<configuration><top/></configuration>";
        let expect = Opaque {
            inner: reply.into(),
        };
        let msg = format!("<data>{reply}</data>");
        let mut reader = NsReader::from_str(msg.as_str());
        _ = reader.trim_text(true);
        if let Event::Start(start) = reader.read_event().unwrap() {
            assert_eq!(Opaque::read_xml(&mut reader, &start).unwrap(), expect);
        } else {
            panic!("missing <data> tag")
        }
    }
}