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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// SPDX-License-Identifier: Apache-2.0

use serde::{de::IgnoredAny, Deserialize, Deserializer, Serialize};

const LLDP_SYS_CAP_OTHER: u16 = 1;
const LLDP_SYS_CAP_REPEATER: u16 = 2;
const LLDP_SYS_CAP_MAC_BRIDGE: u16 = 3;
const LLDP_SYS_CAP_AP: u16 = 4;
const LLDP_SYS_CAP_ROUTER: u16 = 5;
const LLDP_SYS_CAP_TELEPHONE: u16 = 6;
const LLDP_SYS_CAP_DOCSIS: u16 = 7;
const LLDP_SYS_CAP_STATION_ONLY: u16 = 8;
const LLDP_SYS_CAP_CVLAN: u16 = 9;
const LLDP_SYS_CAP_SVLAN: u16 = 10;
const LLDP_SYS_CAP_TWO_PORT_MAC_RELAY: u16 = 11;

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[repr(u8)]
#[serde(into = "u8")]
pub enum LldpNeighborTlvType {
    ChassisId = 1,
    Port = 2,
    SystemName = 5,
    SystemDescription = 6,
    SystemCapabilities = 7,
    ManagementAddress = 8,
    OrganizationSpecific = 127,
}

impl From<LldpNeighborTlvType> for u8 {
    fn from(value: LldpNeighborTlvType) -> Self {
        value as Self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[repr(u8)]
#[serde(into = "u8")]
pub enum LldpOrgSubtype {
    Vlan = 3,
    MacPhyConf = 1,
    Ppvids = 2,
    MaxFrameSize = 4,
}

impl From<LldpOrgSubtype> for u8 {
    fn from(value: LldpOrgSubtype) -> Self {
        value as Self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum LldpOrgOiu {
    #[serde(rename = "00:80:c2")]
    Vlan,
    #[serde(rename = "00:12:0f")]
    MacPhyConf,
    #[serde(rename = "00:80:c2")]
    Ppvids,
    #[serde(rename = "00:12:0f")]
    MaxFrameSize,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
#[non_exhaustive]
#[serde(deny_unknown_fields)]
pub struct LldpConfig {
    #[serde(deserialize_with = "crate::deserializer::bool_or_string")]
    pub enabled: bool,
    #[serde(
        default,
        deserialize_with = "skip",
        skip_serializing_if = "Vec::is_empty"
    )]
    pub neighbors: Vec<Vec<LldpNeighborTlv>>,
}

// The serde is treating skipped value as unknown field which trigger
// `serde(deny_unknown_fields)`, so we manually skip this field.
fn skip<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
    T: Default,
{
    // Ignore the data in the input.
    IgnoredAny::deserialize(deserializer)?;
    Ok(T::default())
}

impl LldpConfig {
    pub(crate) fn sanitize(&mut self) {
        // Remove since it is for query only
        self.neighbors = Vec::new();
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum LldpNeighborTlv {
    SystemName(LldpSystemName),
    SystemDescription(LldpSystemDescription),
    SystemCapabilities(LldpSystemCapabilities),
    ChassisId(LldpChassisId),
    PortId(LldpPortId),
    Ieee8021Vlans(LldpVlans),
    Ieee8023MacPhyConf(LldpMacPhy),
    Ieee8021Ppvids(LldpPpvids),
    ManagementAddresses(LldpMgmtAddrs),
    Ieee8023MaxFrameSize(LldpMaxFrameSize),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpSystemName {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub system_name: String,
}

impl LldpSystemName {
    pub fn new(value: String) -> Self {
        Self {
            system_name: value,
            ty: LldpNeighborTlvType::SystemName,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpSystemDescription {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub system_description: String,
}

impl LldpSystemDescription {
    pub fn new(value: String) -> Self {
        Self {
            system_description: value,
            ty: LldpNeighborTlvType::SystemDescription,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpChassisId {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub chassis_id: String,
    pub chassis_id_type: LldpChassisIdType,
    #[serde(rename = "_description")]
    pub description: String,
}

impl LldpChassisId {
    pub fn new(chassis_id: String, chassis_id_type: LldpChassisIdType) -> Self {
        Self {
            chassis_id,
            chassis_id_type: chassis_id_type.clone(),
            description: chassis_id_type.into(),
            ty: LldpNeighborTlvType::ChassisId,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[repr(u8)]
#[serde(into = "u8")]
pub enum LldpChassisIdType {
    Reserved = 0,
    ChassisComponent = 1,
    InterfaceAlias = 2,
    PortComponent = 3,
    MacAddress = 4,
    NetworkAddress = 5,
    InterfaceName = 6,
    LocallyAssigned = 7,
}

impl From<LldpChassisIdType> for u8 {
    fn from(value: LldpChassisIdType) -> Self {
        value as Self
    }
}

impl From<u8> for LldpChassisIdType {
    fn from(v: u8) -> LldpChassisIdType {
        if v == LldpChassisIdType::ChassisComponent as u8 {
            LldpChassisIdType::ChassisComponent
        } else if v == LldpChassisIdType::InterfaceAlias as u8 {
            LldpChassisIdType::InterfaceAlias
        } else if v == LldpChassisIdType::PortComponent as u8 {
            LldpChassisIdType::PortComponent
        } else if v == LldpChassisIdType::MacAddress as u8 {
            LldpChassisIdType::MacAddress
        } else if v == LldpChassisIdType::NetworkAddress as u8 {
            LldpChassisIdType::NetworkAddress
        } else if v == LldpChassisIdType::InterfaceName as u8 {
            LldpChassisIdType::InterfaceName
        } else if v == LldpChassisIdType::LocallyAssigned as u8 {
            LldpChassisIdType::LocallyAssigned
        } else {
            LldpChassisIdType::Reserved
        }
    }
}

impl From<LldpChassisIdType> for String {
    fn from(v: LldpChassisIdType) -> String {
        match v {
            LldpChassisIdType::Reserved => "Reserved",
            LldpChassisIdType::ChassisComponent => "Chasis compontent",
            LldpChassisIdType::InterfaceAlias => "Interface alias",
            LldpChassisIdType::PortComponent => "Port component",
            LldpChassisIdType::MacAddress => "MAC address",
            LldpChassisIdType::NetworkAddress => "Network address",
            LldpChassisIdType::InterfaceName => "Interface name",
            LldpChassisIdType::LocallyAssigned => "Locally assigned",
        }
        .to_string()
    }
}

impl Default for LldpChassisIdType {
    fn default() -> Self {
        Self::Reserved
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpSystemCapabilities {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub system_capabilities: Vec<LldpSystemCapability>,
}

impl LldpSystemCapabilities {
    pub fn new(system_capabilities: Vec<LldpSystemCapability>) -> Self {
        Self {
            system_capabilities,
            ty: LldpNeighborTlvType::SystemCapabilities,
        }
    }
}

impl From<u16> for LldpSystemCapabilities {
    fn from(caps: u16) -> Self {
        let mut ret = Vec::new();
        if (caps & 1 << (LLDP_SYS_CAP_OTHER - 1)) > 0 {
            ret.push(LldpSystemCapability::Other);
        }
        if (caps & 1 << (LLDP_SYS_CAP_REPEATER - 1)) > 0 {
            ret.push(LldpSystemCapability::Repeater);
        }
        if (caps & 1 << (LLDP_SYS_CAP_MAC_BRIDGE - 1)) > 0 {
            ret.push(LldpSystemCapability::MacBridgeComponent);
        }
        if (caps & 1 << (LLDP_SYS_CAP_AP - 1)) > 0 {
            ret.push(LldpSystemCapability::AccessPoint);
        }
        if (caps & 1 << (LLDP_SYS_CAP_ROUTER - 1)) > 0 {
            ret.push(LldpSystemCapability::Router);
        }
        if (caps & 1 << (LLDP_SYS_CAP_TELEPHONE - 1)) > 0 {
            ret.push(LldpSystemCapability::Telephone);
        }
        if (caps & 1 << (LLDP_SYS_CAP_DOCSIS - 1)) > 0 {
            ret.push(LldpSystemCapability::DocsisCableDevice);
        }
        if (caps & 1 << (LLDP_SYS_CAP_STATION_ONLY - 1)) > 0 {
            ret.push(LldpSystemCapability::StationOnly);
        }
        if (caps & 1 << (LLDP_SYS_CAP_CVLAN - 1)) > 0 {
            ret.push(LldpSystemCapability::CVlanComponent);
        }
        if (caps & 1 << (LLDP_SYS_CAP_SVLAN - 1)) > 0 {
            ret.push(LldpSystemCapability::SVlanComponent);
        }
        if (caps & 1 << (LLDP_SYS_CAP_TWO_PORT_MAC_RELAY - 1)) > 0 {
            ret.push(LldpSystemCapability::TwoPortMacRelayComponent);
        }
        Self::new(ret)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub enum LldpSystemCapability {
    Other,
    Repeater,
    #[serde(rename = "MAC Bridge component")]
    MacBridgeComponent,
    #[serde(rename = "802.11 Access Point (AP)")]
    AccessPoint,
    Router,
    Telephone,
    #[serde(rename = "DOCSIS cable device")]
    DocsisCableDevice,
    #[serde(rename = "Station Only")]
    StationOnly,
    #[serde(rename = "C-VLAN component")]
    CVlanComponent,
    #[serde(rename = "S-VLAN component")]
    SVlanComponent,
    #[serde(rename = "Two-port MAC Relay component")]
    TwoPortMacRelayComponent,
}

impl Default for LldpSystemCapability {
    fn default() -> Self {
        Self::Other
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpPortId {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub port_id: String,
    pub port_id_type: LldpPortIdType,
    #[serde(rename = "_description")]
    pub description: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[repr(u8)]
#[serde(into = "u8")]
pub enum LldpPortIdType {
    Reserved = 0,
    InterfaceAlias = 1,
    PortComponent = 2,
    MacAddress = 3,
    NetworkAddress = 4,
    InterfaceName = 5,
    AgentCircuitId = 6,
    LocallyAssigned = 7,
}

impl From<LldpPortIdType> for u8 {
    fn from(value: LldpPortIdType) -> Self {
        value as Self
    }
}

impl From<LldpPortIdType> for String {
    fn from(v: LldpPortIdType) -> String {
        match v {
            LldpPortIdType::Reserved => "Reserved",
            LldpPortIdType::InterfaceAlias => "Interface alias",
            LldpPortIdType::PortComponent => "Port component",
            LldpPortIdType::MacAddress => "MAC address",
            LldpPortIdType::NetworkAddress => "Network address",
            LldpPortIdType::InterfaceName => "Interface name",
            LldpPortIdType::AgentCircuitId => "Agent circuit ID",
            LldpPortIdType::LocallyAssigned => "Locally assigned",
        }
        .to_string()
    }
}

impl From<u8> for LldpPortIdType {
    fn from(v: u8) -> LldpPortIdType {
        if v == LldpPortIdType::InterfaceName as u8 {
            LldpPortIdType::InterfaceName
        } else if v == LldpPortIdType::PortComponent as u8 {
            LldpPortIdType::PortComponent
        } else if v == LldpPortIdType::MacAddress as u8 {
            LldpPortIdType::MacAddress
        } else if v == LldpPortIdType::NetworkAddress as u8 {
            LldpPortIdType::NetworkAddress
        } else if v == LldpPortIdType::AgentCircuitId as u8 {
            LldpPortIdType::AgentCircuitId
        } else if v == LldpPortIdType::LocallyAssigned as u8 {
            LldpPortIdType::LocallyAssigned
        } else {
            LldpPortIdType::Reserved
        }
    }
}

impl LldpPortId {
    pub fn new(port_id: String, port_id_type: LldpPortIdType) -> Self {
        Self {
            port_id,
            port_id_type: port_id_type.clone(),
            description: port_id_type.into(),
            ty: LldpNeighborTlvType::Port,
        }
    }
}

impl Default for LldpPortIdType {
    fn default() -> Self {
        Self::Reserved
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpVlans {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub ieee_802_1_vlans: Vec<LldpVlan>,
    pub oui: LldpOrgOiu,
    pub subtype: LldpOrgSubtype,
}

impl LldpVlans {
    pub fn new(ieee_802_1_vlans: Vec<LldpVlan>) -> Self {
        Self {
            ieee_802_1_vlans,
            oui: LldpOrgOiu::Vlan,
            subtype: LldpOrgSubtype::Vlan,
            ty: LldpNeighborTlvType::OrganizationSpecific,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Default)]
#[non_exhaustive]
pub struct LldpVlan {
    pub name: String,
    pub vid: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
pub struct LldpMacPhy {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub ieee_802_3_mac_phy_conf: LldpMacPhyConf,
    pub oui: LldpOrgOiu,
    pub subtype: LldpOrgSubtype,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
pub struct LldpMacPhyConf {
    pub autoneg: bool,
    pub operational_mau_type: u16,
    pub pmd_autoneg_cap: u16,
}

impl LldpMacPhy {
    pub fn new(
        autoneg: bool,
        operational_mau_type: u16,
        pmd_autoneg_cap: u16,
    ) -> Self {
        Self {
            ieee_802_3_mac_phy_conf: LldpMacPhyConf {
                autoneg,
                operational_mau_type,
                pmd_autoneg_cap,
            },
            oui: LldpOrgOiu::MacPhyConf,
            subtype: LldpOrgSubtype::MacPhyConf,
            ty: LldpNeighborTlvType::OrganizationSpecific,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpPpvids {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub ieee_802_1_ppvids: Vec<u32>,
    pub oui: LldpOrgOiu,
    pub subtype: LldpOrgSubtype,
}

impl LldpPpvids {
    pub fn new(ieee_802_1_ppvids: Vec<u32>) -> Self {
        Self {
            ieee_802_1_ppvids,
            oui: LldpOrgOiu::Ppvids,
            subtype: LldpOrgSubtype::Ppvids,
            ty: LldpNeighborTlvType::OrganizationSpecific,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
pub struct LldpMgmtAddrs {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub management_addresses: Vec<LldpMgmtAddr>,
}

impl LldpMgmtAddrs {
    pub fn new(management_addresses: Vec<LldpMgmtAddr>) -> Self {
        Self {
            management_addresses,
            ty: LldpNeighborTlvType::ManagementAddress,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Default)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
pub struct LldpMgmtAddr {
    pub address: String,
    pub address_subtype: LldpAddressFamily,
    pub interface_number: u32,
    pub interface_number_subtype: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub enum LldpAddressFamily {
    Unknown,
    #[serde(rename = "IPv4")]
    Ipv4,
    #[serde(rename = "IPv6")]
    Ipv6,
    #[serde(rename = "MAC")]
    Mac,
}

impl Default for LldpAddressFamily {
    fn default() -> Self {
        Self::Unknown
    }
}

const ADDRESS_FAMILY_IP4: u16 = 1;
const ADDRESS_FAMILY_IP6: u16 = 2;
const ADDRESS_FAMILY_MAC: u16 = 6;

impl From<u16> for LldpAddressFamily {
    fn from(i: u16) -> Self {
        match i {
            ADDRESS_FAMILY_IP4 => Self::Ipv4,
            ADDRESS_FAMILY_IP6 => Self::Ipv6,
            ADDRESS_FAMILY_MAC => Self::Mac,
            _ => Self::Unknown,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LldpMaxFrameSize {
    #[serde(rename = "type")]
    pub ty: LldpNeighborTlvType,
    pub ieee_802_3_max_frame_size: u32,
    pub oui: LldpOrgOiu,
    pub subtype: LldpOrgSubtype,
}

impl LldpMaxFrameSize {
    pub fn new(ieee_802_3_max_frame_size: u32) -> Self {
        Self {
            ieee_802_3_max_frame_size,
            oui: LldpOrgOiu::MaxFrameSize,
            subtype: LldpOrgSubtype::MaxFrameSize,
            ty: LldpNeighborTlvType::OrganizationSpecific,
        }
    }
}