bgpsim 0.20.4

A network control-plane simulator
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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
// BgpSim: BGP Network Simulator written in Rust
// Copyright 2022-2024 Tibor Schneider <sctibor@ethz.ch>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Module that contains the implementation of a Link State Database, including shortest-path
//! computation.
//!
//! # Special remarks for the OSPF implementation
//!
//! We implement OSPF with some special rules:
//!
//! ## Aging LSAs
//!
//! This is a workaround for the case when old LSAs are present. In real OSPF, the age of LSAs is
//! periodically increased until they reach `MaxAge` (typically after 1 hour). At this point, those
//! LSAs are removed from the table. Other LSAs are periodically refreshed (typically after 30
//! minutes). However, we do not periodically re-advertise messages, and we do not automatically
//! increase the age. Thus, we might end up with LSAs that are unreachable.
//!
//! To circumvent this issue, we remove all LSAs for which the `router` is not reachable. This works
//! because of the following reasons:
//!
//! 1. We only have bi-directional links. Any link failure is bi-directional.
//! 2. When the router originating that LSA re-advertises it with a larger sequence number (to
//!    resetht age), the udpate would not propagate to this router (because the originator is
//!    unreachable).
//!
//! More precisely, for each area, and for each LSA, we chech whether we can still reach the
//! originating router within that area. If not, we remove the LSA (without flushing this update out
//! to neighbors). We repeat the same thing for External-LSAs, but check for a path in all available
//! areas.
//!
//! ## LSInfinity
//!
//! We ignore all checks for LSINfinity. There are two main reasons for that: First, the routing
//! table computation later filters out any entry that has a weight of infinity, and therefore, we
//! are allowed to keep it around in the RIB. Second, we keep the LSInfinity in the rib such that we
//! don't remove those entries (see aging LSAs above), even though the neighborhood is still
//! up-to-date.

use std::{
    cmp::Ordering,
    collections::{hash_map::Entry, BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet},
};

use itertools::{EitherOrBoth, Itertools};
use maplit::btreemap;
use ordered_float::NotNan;
use serde::{Deserialize, Serialize};
use serde_with::{As, Same};

use crate::{
    ospf::{LinkWeight, OspfArea},
    types::RouterId,
};

use super::{LinkType, Lsa, LsaData, LsaHeader, LsaKey, LsaType, RouterLsaLink, MAX_AGE, MAX_SEQ};

/// The OSPF RIB that contains all the different area datastructures.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OspfRib {
    /// The Router ID to whom this RIB belongs
    router_id: RouterId,
    /// all area data structures
    #[serde(with = "As::<Vec<(Same, Same)>>")]
    pub(super) areas: BTreeMap<OspfArea, AreaDataStructure>,
    /// list of external LSAs
    #[serde(with = "As::<Vec<(Same, Same)>>")]
    pub(super) external_lsas: HashMap<LsaKey, Lsa>,
    /// Whether to recompute the forwarding table for external LSAs using the algorithm presented in
    /// section 16.4 of RFC 2328. We store the external routers for which we need to update the
    /// computation.
    recompute_as_external: HashSet<RouterId>,
    /// The current RIB
    #[serde(with = "As::<Vec<(Same, Same)>>")]
    pub(super) rib: HashMap<RouterId, OspfRibEntry>,
}

impl OspfRib {
    /// Create a new, empty OSPF Rib.
    pub(super) fn new(router_id: RouterId) -> Self {
        Self {
            router_id,
            areas: Default::default(),
            rib: [(router_id, OspfRibEntry::empty(router_id))]
                .into_iter()
                .collect(),
            external_lsas: Default::default(),
            recompute_as_external: HashSet::new(),
        }
    }

    /// Get the number of configured areas
    pub fn num_areas(&self) -> usize {
        self.areas.len()
    }

    /// Get an iterator over all configured areas
    pub fn areas(&self) -> impl Iterator<Item = OspfArea> + '_ {
        self.areas.keys().copied()
    }

    /// Check whether an area is configured or not
    pub fn in_area(&self, area: OspfArea) -> bool {
        self.areas.contains_key(&area)
    }

    /// Ensrue that a specific area exists
    pub(super) fn insert_area(&mut self, area: OspfArea) {
        self.get_or_insert_area(area);
    }

    /// Get the area data structure of a given area. If the area does not exist, it will be created.
    fn get_or_insert_area(&mut self, area: OspfArea) -> &mut AreaDataStructure {
        self.areas
            .entry(area)
            .or_insert_with(|| AreaDataStructure::new(self.router_id, area))
    }

    /// Remove the area datastructure. This function requires that the router has no links connected
    /// to that area anymore. The function will panic if that is not the case!
    pub(super) fn remove_area(&mut self, area: OspfArea) {
        if let Some(area) = self.areas.remove(&area) {
            debug_assert!(area.get_router_lsa(self.router_id).unwrap().1.is_empty())
        }
    }

    /// Get a reference to the LSA list of a specific area. If `area` is `None`, then return the
    /// external-LSA list.
    pub fn get_lsa_list(&self, area: Option<OspfArea>) -> Option<&HashMap<LsaKey, Lsa>> {
        if let Some(area) = area {
            self.areas.get(&area).map(|ds| &ds.lsa_list)
        } else {
            Some(&self.external_lsas)
        }
    }

    /// Get a reference to the shortest-path tree datastructure of a specific area.
    pub fn get_spt(&self, area: OspfArea) -> Option<&HashMap<RouterId, SptNode>> {
        self.areas.get(&area).map(|ds| &ds.spt)
    }

    /// Construct the RIB for a specific target
    pub fn get_rib_entry(&self, target: RouterId) -> Option<OspfRibEntry> {
        OspfRibEntry::from_paths(
            self.areas
                .iter()
                .filter_map(|(a, ds)| ds.spt.get(&target).map(|n| (*a, n))),
        )
    }

    /// Get a reference to the current RIB.
    pub fn get_rib(&self) -> &HashMap<RouterId, OspfRibEntry> {
        &self.rib
    }

    /// Generate the summary list for a given area. This will combine the `area`'s lsa-list, and the
    /// external-lsa list. It will also remove all max-age lsas.
    pub(crate) fn get_summary_list(&self, area: OspfArea) -> HashMap<LsaKey, Lsa> {
        self.areas
            .get(&area)
            .into_iter()
            .flat_map(|ds| ds.lsa_list.iter())
            .chain(self.external_lsas.iter())
            .filter(|(_, lsa)| !lsa.is_max_age())
            .map(|(key, lsa)| (*key, lsa.clone()))
            .collect()
    }

    /// Get a reference to an LSA. `area` can be empty only if `key` is an external-LSA. Otherwise,
    /// `area` must be set, and the function will return the LSA stored in the specific area.
    pub fn get_lsa(&self, key: impl Into<LsaKey>, area: Option<OspfArea>) -> Option<&Lsa> {
        let key = key.into();
        if key.is_external() {
            self.external_lsas.get(&key)
        } else {
            area.and_then(|area| self.areas.get(&area))
                .and_then(|ds| ds.lsa_list.get(&key))
        }
    }

    /// Get the Router-LSA associated with the `router_id` in `area`.
    pub fn get_router_lsa(
        &self,
        router_id: RouterId,
        area: OspfArea,
    ) -> Option<(&LsaHeader, &Vec<RouterLsaLink>)> {
        self.areas
            .get(&area)
            .and_then(|ds| ds.get_router_lsa(router_id))
    }

    /// insert an LSA, ingoring anything that was stored before. If the `lsa` is not an
    /// external-LSA, then `area` must be `Some`. `area` will be ignored for external-LSAs.
    pub(super) fn insert(&mut self, lsa: Lsa, area: Option<OspfArea>) {
        if lsa.is_external() {
            let target = lsa.target();
            self.external_lsas.insert(lsa.key(), lsa);
            // remember that we need to recompute the as-external table.
            self.recompute_as_external.insert(target);
        } else {
            self.get_or_insert_area(area.unwrap()).insert(lsa);
        }
    }

    /// remove an LSA, ingoring anything that was stored before. If the `lsa` is not an
    /// `external-LSA`, then `area` must be `Some`. `area` will be ignored for `external-LSAs.
    pub(super) fn remove(&mut self, key: impl Into<LsaKey>, area: Option<OspfArea>) {
        let key = key.into();
        if key.is_external() {
            let target = key.target();
            self.external_lsas.remove(&key);
            // remember that we need to recompute the as-external table.
            self.recompute_as_external.insert(target);
        } else {
            self.get_or_insert_area(area.unwrap()).remove(key);
        }
    }

    /// prematurely age an LSA by setting both the `seq` to `MAX_SEQ` and `age` to `MAX_AGE`. If
    /// `lsa` is an external-LSA, then ignore the provided `area`. Otherwhise, `area` must be
    /// `Some`.
    pub(super) fn set_max_seq_and_age(
        &mut self,
        key: impl Into<LsaKey>,
        area: Option<OspfArea>,
    ) -> Option<&Lsa> {
        let key = key.into();
        if key.is_external() {
            let target = key.target.unwrap();
            if let Some(e) = self.external_lsas.get_mut(&key) {
                e.header.seq = MAX_SEQ;
                e.header.age = MAX_AGE;
            }
            self.recompute_as_external.insert(target);
            self.external_lsas.get(&key)
        } else {
            self.get_or_insert_area(area.unwrap())
                .set_max_seq_and_age(key)
        }
    }

    /// Set the sequence number of an LSA to a specific value. If `lsa` is an external-LSA, then
    /// `area` will be ignored. Otherwise, `area` must be `Some`. `target` must be lower than
    /// `MAX_SEQ`!
    pub(super) fn set_seq(
        &mut self,
        key: impl Into<LsaKey>,
        area: Option<OspfArea>,
        target: u32,
    ) -> Option<&Lsa> {
        let key = key.into();
        if key.is_external() {
            if let Some(e) = self.external_lsas.get_mut(&key) {
                e.header.seq = target;
            }
            self.external_lsas.get(&key)
        } else {
            self.get_or_insert_area(area.unwrap()).set_seq(key, target)
        }
    }

    /// Remove all LSAs that are unreachable.
    ///
    /// This is a workaround for the case when old LSAs are present. In real OSPF, the age of LSAs
    /// is periodically increased until they reach `MaxAge` (typically after 1 hour). At this point,
    /// those LSAs are removed from the table. Other LSAs are periodically refreshed (typically
    /// after 30 minutes). However, we do not periodically re-advertise messages, and we do not
    /// automatically increase the age. Thus, we might end up with LSAs that are unreachable.
    ///
    /// To circumvent this issue, we remove all LSAs for which the `router` is not reachable. This
    /// works because of the following reasons:
    ///
    /// 1. We only have bi-directional links. Any link failure is bi-directional.
    /// 2. When the router originating that LSA re-advertises it with a larger sequence number (to
    ///    resetht age), the udpate would not propagate to this router (because the originator is
    ///    unreachable).
    ///
    /// More precisely, for each area, and for each LSA, we chech whether we can still reach the
    /// originating router within that area. If not, we remove the LSA (without flushing this update
    /// out to neighbors). We repeat the same thing for External-LSAs, but check for a path in all
    /// available areas.
    ///
    /// We are not allowed to remove unreachable external LSAs. The reason is that, due to a
    /// misconfiguration in OSPF areas, other border routers may be unreachable, while their
    /// External-LSAs still would propagate towards the router.
    ///
    /// TODO: We need to find a better technique to deal with old LSAs! Because as it is now, old
    /// External-LSAs could remain in the network. However, the chance of them to remain is small,
    /// as OSPF must be disconnected when the external router is removed. One possible fix could be
    /// that this function expects a list of external routers that are still connected to the
    /// network, such that they will be kept around (well, those that are connected to the current
    /// router via internal links!)
    pub(super) fn remove_unreachable_lsas(&mut self) {
        // remove unreachable LSAs from all areas
        self.areas
            .values_mut()
            .for_each(|ds| ds.remove_unreachable_lsas());

        // removing unreachable externla-lsas is unsound!
        // self.external_lsas
        //     .retain(|k, _| k.router == self.router_id || self.rib.contains_key(&k.router))
    }

    /// Update the local RouterLSA and set the weight to a neighbor appropriately. Then, return the
    /// new LSA. There are a couple of possible outcomes:
    /// - `None`: The LSA must not be changed.
    /// - `Some((lsa, None))`: The LSA is updated regularly, and `lsa` is the reference to the
    ///   updated (currently stored) LSA.
    /// - `Some((lsa, Some(new_lsa)))`: The LSA reached `MAX_SEQ` and was aged prematurely. `lsa` is
    ///   a reference to the old (currently stored) LSA with `lsa.header.seq = MAX_SEQ` and
    ///   `lsa.header.age = MAX_AGE`. The `new_lsa` is the new LSA that should be set once the old
    ///   LSA was acknowledged by all neighbors.
    pub(super) fn update_local_lsa(
        &mut self,
        neighbor: RouterId,
        area: OspfArea,
        weight: Option<LinkWeight>,
    ) -> Option<(&Lsa, Option<Lsa>)> {
        self.get_or_insert_area(area)
            .update_local_lsa(neighbor, weight)
    }

    /// Update an external LSA that is advertised by this router. There are a couple of possible
    /// outcomes:
    /// - `None`: The LSA must not be changed.
    /// - `Some((lsa, None))`: The LSA is updated regularly, and `lsa` is the reference to the
    ///   updated (currently stored) LSA.
    /// - `Some((lsa, Some(None)))`: The LSA is prematurely aged, such that it is removed from all
    ///   databases. `lsa` is a reference to the old (currently stored) LSA with `lsa.header.seq =
    ///   MAX_SEQ`.
    /// - `Some((lsa, Some(Some(new_lsa))))`: The LSA reached `MAX_SEQ` and was aged
    ///   prematurely. `lsa` is a reference to the old (currently stored) LSA with `lsa.header.seq =
    ///   MAX_SEQ` and `lsa.header.age = MAX_AGE`. The `new_lsa` is the new LSA that should be set
    ///   once the old LSA was acknowledged by all neighbors.
    pub(super) fn update_external_lsa(
        &mut self,
        neighbor: RouterId,
        weight: Option<LinkWeight>,
    ) -> Option<(&Lsa, Option<Option<Lsa>>)> {
        let router = self.router_id;
        let target = Some(neighbor);
        let key = LsaKey {
            lsa_type: LsaType::External,
            router,
            target,
        };

        // if the key does not yet exist, then simply create it and return
        let Some(old_lsa) = self.external_lsas.get(&key) else {
            // the LSA does not yet exist! Simply create it
            let Some(weight) = weight else {
                // It must not be created!
                return None;
            };
            let data = LsaData::External(NotNan::new(weight).unwrap());
            let lsa = Lsa {
                header: LsaHeader {
                    lsa_type: key.lsa_type,
                    router,
                    target,
                    seq: 0,
                    age: 0,
                },
                data,
            };
            self.insert(lsa, None);
            return Some((self.external_lsas.get(&key).unwrap(), None));
        };

        // if the key does already exist, but weight is None, then remove the entry
        let Some(weight) = weight else {
            // remove the old entry
            let mut lsa = old_lsa.clone();
            lsa.header.age = MAX_AGE;
            self.insert(lsa, None);
            return Some((self.external_lsas.get(&key).unwrap(), Some(None)));
        };

        let data = LsaData::External(NotNan::new(weight).unwrap());

        // check if the old LSA must be updated
        if old_lsa.data == data {
            // nothing to do
            return None;
        }

        // check if we can update the lsa
        if old_lsa.header.seq < MAX_SEQ - 1 {
            // normally update the lsa
            let lsa = Lsa {
                header: LsaHeader {
                    seq: old_lsa.header.seq + 1,
                    ..old_lsa.header
                },
                data,
            };
            self.insert(lsa, None);
            return Some((self.external_lsas.get(&key).unwrap(), None));
        }

        // If we get here, then we need to prematurely age the current LSA
        let old_lsa = self.set_max_seq_and_age(key, None).unwrap();
        let new_lsa = Lsa {
            header: LsaHeader {
                lsa_type: LsaType::External,
                router,
                target,
                seq: 0,
                age: 0,
            },
            data,
        };
        Some((old_lsa, Some(Some(new_lsa))))
    }

    /// Update all necessary Summary-LSAs and generate the set of new LSAs that must be
    /// propagated. To that end, first redistribute routes into the backbone area, and then
    /// redistribute them from the backbone area into the others.
    #[allow(clippy::type_complexity)]
    pub(super) fn update_summary_lsas(
        &mut self,
    ) -> BTreeMap<OspfArea, (Vec<Lsa>, Vec<(LsaKey, Option<Lsa>)>)> {
        let mut result = BTreeMap::new();

        // only redistribute something if we are an area border router!
        if self.areas.contains_key(&OspfArea::BACKBONE) && self.areas.len() > 1 {
            // the router is an ABR! Re-compute the summary-LSAs.
            for (area, area_ds) in self.areas.iter_mut() {
                let (redistribute, track_max_age) = area_ds.redistribute_into(&self.rib);
                result.insert(*area, (redistribute, track_max_age));
            }
        } else {
            // The router is not an ABR! Update the area's redistribute, because we might need to
            // flush out old LSAs.
            for (area, area_ds) in self.areas.iter_mut() {
                let (redistribute, track_max_age) =
                    area_ds.redistribute_from_paths(Default::default());
                result.insert(*area, (redistribute, track_max_age));
            }
        }

        result
    }

    /// Redistribute all routes from a given area to all that need redistributing. Also return the
    /// information on tracking max-age. The function also returns a flag determining whether any of
    /// the forwarding tables has changed.
    pub(super) fn update_routing_table(&mut self) -> bool {
        let mut changed = false;
        for adv in self.areas.values_mut() {
            changed |= adv.update_spt();
        }

        // if there are no areas, we also need to compute the RIB.
        changed |= self.areas.is_empty();

        if changed {
            // construct the rib
            let mut rib: HashMap<RouterId, OspfRibEntry> =
                [(self.router_id, OspfRibEntry::empty(self.router_id))]
                    .into_iter()
                    .collect();
            for (area, area_ds) in self.areas.iter() {
                for path in area_ds.spt.values() {
                    match rib.entry(path.router_id) {
                        Entry::Occupied(mut e) => {
                            e.get_mut().update(path, *area);
                        }
                        Entry::Vacant(e) => {
                            e.insert(OspfRibEntry::new(path, *area));
                        }
                    }
                }
            }
            self.rib = rib;
        }

        if changed || !self.recompute_as_external.is_empty() {
            self.compute_as_external_routes(changed);
            self.recompute_as_external.clear();
            changed = true;
        }

        // update changed depending on whether we will need to update external-as routes
        changed
    }

    /// This algorithm computes the routes from External-LSAs using the algorithm presented in
    /// Section 16.4 of RFC 2328.
    ///
    /// If `all` is true, then simply update all external targets, assuming they are not present in
    /// the current `self.rib`. If `all` is false, then only remove the entries to be updated, and
    /// recompute those.
    ///
    /// AS external routes are calculated by examining AS-external-LSAs. Each of the
    /// AS-external-LSAs is considered in turn. Most AS- external-LSAs describe routes to specific
    /// IP destinations. An AS-external-LSA can also describe a default route for the Autonomous
    /// System (Destination ID = DefaultDestination, network/subnet mask = 0x00000000). For each
    /// AS-external-LSA:
    fn compute_as_external_routes(&mut self, all: bool) {
        if !all {
            self.rib
                .retain(|k, _| !self.recompute_as_external.contains(k));
        }

        for lsa in self.external_lsas.values() {
            if !(all || self.recompute_as_external.contains(&lsa.target())) {
                continue;
            }

            // compute the path. This performs steps (1), (2), (3), and (4).
            let Some(path) = compute_as_external_route(self.router_id, lsa, &self.rib) else {
                continue;
            };

            // (5) Look up the routing table entry for the destination TARGET. If no entry exists
            //     for TARGET, install the AS external path to TARGET, with next hop equal to the
            //     list of next hops to the forwarding address, and advertising router equal to
            //     ASBR. If the external metric type is 1, then the path-type is set to type 1
            //     external and the cost is equal to X+Y. If the external metric type is 2, the
            //     path-type is set to type 2 external, the link state component of the route's cost
            //     is X, and the type 2 cost is Y.
            match self.rib.entry(path.router_id) {
                Entry::Vacant(e) => {
                    e.insert(path);
                }

                // (6) Compare the AS external path described by the LSA with the existing paths in
                //     TARGET's routing table entry, as follows. If the new path is preferred, it
                //     replaces the present paths in TARGET's routing table entry. If the new path
                //     is of equal preference, it is added to TARGET's routing table entry's list of
                //     paths.
                Entry::Occupied(mut e) => {
                    // (a) Intra-area and inter-area paths are always preferred over AS external
                    //     paths.
                    // --> this is already ensured by the fact that we will never have ExternalLSAs
                    //     for targets that can be internal

                    // (b) Type 1 external paths are always preferred over type 2 external paths.
                    //     When all paths are type 2 external paths, the paths with the smallest
                    //     advertised type 2 metric are always preferred.
                    // --> we ignore the difference between Type 1 and Typ1 2 paths.

                    // (c) If the new AS external path is still indistinguishable from the current
                    //     paths in the TARGET's routing table entry, and RFC1583Compatibility is
                    //     set to "disabled", select the preferred paths based on the intra-AS paths
                    //     to the ASBR/forwarding addresses, as specified in Section 16.4.1.
                    // --> ignore this case

                    // (d) If the new AS external path is still indistinguishable from the current
                    //     paths in the TARGET's routing table entry, select the preferred path
                    //     based on a least cost comparison. Type 1 external paths are compared by
                    //     looking at the sum of the distance to the forwarding address and the
                    //     advertised type 1 metric (X+Y). Type 2 external paths advertising equal
                    //     type 2 metrics are compared by looking at the distance to the forwarding
                    //     addresses.
                    // --> ignore that case

                    // (f) When multiple intra-AS paths are available to ASBRs/forwarding addresses,
                    //     the following rules indicate which paths are preferred. These rules apply
                    //     when the same ASBR is reachable through multiple areas, or when trying to
                    //     decide which of several AS-external-LSAs should be preferred. In the
                    //     former case the paths all terminate at the same ASBR, while in the latter
                    //     the paths terminate at separate ASBRs/forwarding addresses. In either
                    //     case, each path is represented by a separate routing table entry as
                    //     defined in Section 11.
                    *e.get_mut() += path;
                }
            }
        }
    }
}

/// A single entry in the routing table
#[derive(Clone, Serialize, Deserialize)]
pub struct OspfRibEntry {
    /// The target router
    pub router_id: RouterId,

    /// The list of next hops for the current set of shortest paths from the root to this
    /// vertex. There can be multiple shortest paths due to the equal-cost multipath
    /// capability. Each next hop indicates the outgoing router interface to use when forwarding
    /// traffic to the destination. On broadcast, Point-to-MultiPoint and NBMA networks, the next
    /// hop also includes the IP address of the next router (if any) in the path towards the
    /// destination.
    pub fibs: BTreeSet<RouterId>,

    /// The link state cost of the current set of shortest paths from the root to the vertex. The
    /// link state cost of a path is calculated as the sum of the costs of the path's constituent
    /// links (as advertised in router-LSAs and network-LSAs). One path is said to be "shorter" than
    /// another if it has a smaller link state cost.
    pub cost: NotNan<LinkWeight>,

    /// Whether the path is an intra-area or inter-area
    pub inter_area: bool,

    /// The set of areas from which the route was learned, together with their associated LSA key
    #[serde(with = "As::<Vec<(Same, Same)>>")]
    pub keys: BTreeMap<Option<OspfArea>, LsaKey>,
}

impl std::fmt::Debug for OspfRibEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "OspfRibEntry({} --> {}, cost {} {} from {})",
            self.router_id.index(),
            if self.fibs.is_empty() {
                "XX".to_string()
            } else {
                self.fibs.iter().map(|x| x.index()).join(" || ")
            },
            self.cost,
            if self.inter_area { "R" } else { "I" },
            self.keys
                .keys()
                .map(|x| x.map(|x| x.to_string()).unwrap_or("External".to_string()))
                .join(" & ")
        )
    }
}

impl PartialEq for OspfRibEntry {
    fn eq(&self, other: &Self) -> bool {
        (
            self.router_id,
            &self.fibs,
            self.cost,
            self.inter_area,
            self.keys.keys().collect::<Vec<_>>(),
        ) == (
            other.router_id,
            &other.fibs,
            other.cost,
            other.inter_area,
            other.keys.keys().collect::<Vec<_>>(),
        )
    }
}

impl OspfRibEntry {
    /// Create an empty RIB with zero cost.
    pub(crate) fn empty(router_id: RouterId) -> Self {
        Self {
            router_id,
            fibs: Default::default(),
            cost: NotNan::default(),
            inter_area: false,
            keys: Default::default(),
        }
    }

    /// Construct a new entry from an SptNode and OspfArea
    pub(crate) fn new(path: &SptNode, area: OspfArea) -> Self {
        Self {
            router_id: path.router_id,
            fibs: path.fibs.clone(),
            cost: path.cost,
            inter_area: path.inter_area,
            keys: btreemap! {Some(area) => path.key},
        }
    }

    /// Construct a new RibEntry from a set of paths.
    fn from_paths<'a>(paths: impl IntoIterator<Item = (OspfArea, &'a SptNode)>) -> Option<Self> {
        let mut paths = paths.into_iter();
        let (first_area, first_path) = paths.next()?;
        let mut rib = Self::new(first_path, first_area);

        // go through all other paths
        for (area, path) in paths {
            rib.update(path, area)
        }

        Some(rib)
    }

    /// Update an existing entry by comparing it with a path from a specific area.
    pub(crate) fn update(&mut self, path: &SptNode, area: OspfArea) {
        match (self.inter_area, self.cost).cmp(&(path.inter_area, path.cost)) {
            // the current cost is lower than the new path
            Ordering::Less => {}
            // Both paths are equally preferred. Extend the next-hops
            Ordering::Equal => {
                self.fibs.extend(path.fibs.iter().copied());
                self.keys.insert(Some(area), path.key);
            }
            // The new path is better. Replace it.
            Ordering::Greater => {
                self.fibs.clone_from(&path.fibs);
                self.cost = path.cost;
                self.inter_area = path.inter_area;
                self.keys = btreemap! {Some(area) => path.key};
            }
        }
    }
}

impl std::ops::AddAssign for OspfRibEntry {
    /// The path preference rules, stated from highest to lowest preference, are as follows. Note
    /// that as a result of these rules, there may still be multiple paths of the highest
    /// preference. In this case, the path to use must be determined based on cost, as described in
    /// Section 16.4.
    /// - Intra-area paths using non-backbone areas are always the most preferred.
    /// - The other paths, intra-area backbone paths and inter-area paths, are of equal preference.
    fn add_assign(&mut self, rhs: Self) {
        match (self.inter_area, self.cost).cmp(&(rhs.inter_area, rhs.cost)) {
            // the current cost is lower than the new path
            Ordering::Less => {}
            // Both paths are equally preferred. Extend the next-hops
            Ordering::Equal => {
                self.fibs.extend(rhs.fibs);
                self.keys.extend(rhs.keys);
            }
            // The new path is better. Replace it.
            Ordering::Greater => {
                *self = rhs;
            }
        }
    }
}

/// The Area data structure as described in RFC 2328. Each router has one of these for each
/// area it is a part of. Holds its corresponding Area ID,the list of router and summary-LSAs
/// and the shortest path tree with this router as root
///
/// Per specification this should also include a reference to all interfaces at this router
/// that belong to this area, maybe replace with reference to all neighbour routers?
///
/// Assumption: point-to-point only,
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(super) struct AreaDataStructure {
    /// The current router.
    router_id: RouterId,
    /// The area of that datastructure
    area: OspfArea,
    /// the list of all LSAs
    #[serde(with = "As::<Vec<(Same, Same)>>")]
    lsa_list: HashMap<LsaKey, Lsa>,
    /// This parameter indicates whether the area can carry data traffic that neither originates nor
    /// terminates in the area itself. This parameter is calculated when the area's shortest-path
    /// tree is built (see Section 16.1, where TransitCapability is set to TRUE if and only if there
    /// are one or more fully adjacent virtual links using the area as Transit area), and is used as
    /// an input to a subsequent step of the routing table build process (see Section 16.3). When an
    /// area's TransitCapability is set to TRUE, the area is said to be a "transit area".
    transit_capability: bool,
    /// The result of the shortest path tree computation.
    #[serde(with = "As::<Vec<(Same, Same)>>")]
    spt: HashMap<RouterId, SptNode>,
    /// Whether to re-execute the Dijkstra algorithm for constructing the internal-area forwarding
    /// state using the algorithm presented in Section 16.1 of RFC 2328.
    recompute_intra_area: bool,
    /// Whether to recompute the forwarding table for summary LSAs using the algorithm presented in
    /// section 16.2 of RFC 2328. Here, we store the inter-area destinations for which we need to
    /// recompute the results
    recompute_inter_area: BTreeSet<RouterId>,
    /// The set of targets that are redistributed by this router.
    #[serde(with = "As::<Vec<(Same, Same)>>")]
    redistributed_paths: BTreeMap<RouterId, NotNan<LinkWeight>>,
}

impl PartialEq for AreaDataStructure {
    fn eq(&self, other: &Self) -> bool {
        (self.router_id, self.area, &self.lsa_list) == (other.router_id, other.area, &self.lsa_list)
    }
}

impl AreaDataStructure {
    fn new(router_id: RouterId, area: OspfArea) -> Self {
        let mut s = Self {
            router_id,
            area,
            lsa_list: HashMap::new(),
            transit_capability: Default::default(),
            spt: HashMap::from_iter([(router_id, SptNode::new(router_id))]),
            redistributed_paths: Default::default(),
            recompute_intra_area: false,
            recompute_inter_area: Default::default(),
        };
        // insert self as an RouterLSA
        let self_lsa = Lsa {
            header: LsaHeader {
                lsa_type: LsaType::Router,
                router: router_id,
                target: None,
                seq: 0,
                age: 0,
            },
            data: LsaData::Router(Vec::new()),
        };
        s.lsa_list.insert(self_lsa.key(), self_lsa);
        s
    }

    /// Create a new area datastructure from raw data
    pub(super) fn from_raw(
        router_id: RouterId,
        area: OspfArea,
        lsa_list: HashMap<LsaKey, Lsa>,
        spt: HashMap<RouterId, SptNode>,
        redistributed_paths: BTreeMap<RouterId, NotNan<LinkWeight>>,
    ) -> Self {
        let mut s = Self {
            router_id,
            area,
            lsa_list,
            transit_capability: Default::default(),
            spt,
            redistributed_paths,
            recompute_intra_area: false,
            recompute_inter_area: Default::default(),
        };

        // ensure that self is in the lsa
        if s.lsa_list.is_empty() {
            let self_lsa = Lsa {
                header: LsaHeader {
                    lsa_type: LsaType::Router,
                    router: router_id,
                    target: None,
                    seq: 0,
                    age: 0,
                },
                data: LsaData::Router(Vec::new()),
            };
            s.lsa_list.insert(self_lsa.key(), self_lsa);
        }

        // ensure the spt is correct
        if s.spt.is_empty() {
            s.spt = HashMap::from_iter([(router_id, SptNode::new(router_id))]);
        }

        s
    }

    /// Turn the area datastructure into its raw parts, consisting of the `lsa_list`, the `spt`, and
    /// the `redistributed_paths`.
    #[allow(clippy::type_complexity)]
    pub(super) fn into_raw(
        self,
    ) -> (
        HashMap<LsaKey, Lsa>,
        HashMap<RouterId, SptNode>,
        BTreeMap<RouterId, NotNan<LinkWeight>>,
    ) {
        (self.lsa_list, self.spt, self.redistributed_paths)
    }

    /// Get the Router-LSA associated with the `router_id` in `area`.
    fn get_router_lsa(&self, router_id: RouterId) -> Option<(&LsaHeader, &Vec<RouterLsaLink>)> {
        let key = LsaKey::router(router_id);
        self.lsa_list.get(&key).and_then(|x| match &x.data {
            LsaData::Router(r) => Some((&x.header, r)),
            _ => None,
        })
    }

    /// Update the local RouterLSA and set the weight to a neighbor appropriately. Then, return the
    /// new LSA. There are a couple of possible outcomes:
    /// - `None`: The LSA must not be changed.
    /// - `Some((lsa, None))`: The LSA is updated regularly, and `lsa` is the reference to the
    ///   updated (currently stored) LSA.
    /// - `Some((lsa, Some(new_lsa)))`: The LSA reached `MAX_SEQ` and was aged prematurely. `lsa` is
    ///   a reference to the old (currently stored) LSA with `lsa.header.seq = MAX_SEQ` and
    ///   `lsa.header.age = MAX_AGE`. The `new_lsa` is the new LSA that should be set once the old
    ///   LSA was acknowledged by all neighbors.
    fn update_local_lsa(
        &mut self,
        neighbor: RouterId,
        weight: Option<LinkWeight>,
    ) -> Option<(&Lsa, Option<Lsa>)> {
        let router_id = self.router_id;
        let (header, links) = self.get_router_lsa(router_id).unwrap();
        let key = header.key();
        let header = *header;
        let mut links = links.clone();

        // update the local neighbors
        if let Some(new_weight) = weight {
            // update the LSA
            if let Some(pos) = links.iter().position(|l| l.target == neighbor) {
                if links[pos].weight == new_weight {
                    // link weight does not need to be changed. Do nothing.
                    return None;
                }
                links[pos].weight = NotNan::new(new_weight).unwrap();
            } else {
                links.push(RouterLsaLink {
                    link_type: LinkType::PointToPoint,
                    target: neighbor,
                    weight: NotNan::new(new_weight).unwrap(),
                })
            }
        } else if let Some(pos) = links.iter().position(|l| l.target == neighbor) {
            links.remove(pos);
        } else {
            // the link was not there to begin with. Do nothing
            return None;
        }

        // construct the new LSA
        let mut lsa = Lsa {
            header,
            data: LsaData::Router(links),
        };

        // check if we can still increment the sequence number
        if lsa.header.seq < MAX_SEQ - 1 {
            lsa.header.seq += 1;
            self.insert(lsa);
            Some((self.lsa_list.get(&key).unwrap(), None))
        } else {
            // we need to advertise a max-age LSA, and replace it with a new one.
            let old_lsa = self.set_max_seq_and_age(lsa.key()).unwrap();
            // reset the sequence number and the age
            lsa.header.seq = 0;
            lsa.header.age = 0;
            Some((old_lsa, Some(lsa)))
        }
    }

    /// prematurely age an LSA by setting both the `seq` to `MAX_SEQ` and `age` to `MAX_AGE`.
    fn set_max_seq_and_age(&mut self, key: impl Into<LsaKey>) -> Option<&Lsa> {
        let key = key.into();
        if let Some(e) = self.lsa_list.get_mut(&key) {
            e.header.seq = MAX_SEQ;
            e.header.age = MAX_AGE;
        }

        // remember what we need to recompute
        if key.is_router() {
            self.recompute_intra_area = true;
        } else if key.is_summary() {
            self.recompute_inter_area.insert(key.target());
        } else if key.is_external() {
            unreachable!()
        }

        self.lsa_list.get(&key)
    }

    /// Set the sequence number of an LSA to a specific value.
    fn set_seq(&mut self, key: impl Into<LsaKey>, target: u32) -> Option<&Lsa> {
        let key = key.into();
        if let Some(e) = self.lsa_list.get_mut(&key) {
            e.header.seq = target;
        }
        self.lsa_list.get(&key)
    }

    /// Insert an LSA into the datastructure, ignoring the old content. This function will update
    /// the datastructure to remember which parts of the algorithm must be re-executed.
    fn insert(&mut self, lsa: Lsa) {
        let key = lsa.key();
        self.lsa_list.insert(key, lsa);

        // remember what we need to recompute
        if key.is_router() {
            self.recompute_intra_area = true;
        } else if key.is_summary() {
            self.recompute_inter_area.insert(key.target());
        } else if key.is_external() {
            unreachable!()
        }
    }

    /// Remove an LSA into the datastructure, ignoring the old content. This function will update
    /// the datastructure to remember which parts of the algorithm must be re-executed.
    fn remove(&mut self, key: impl Into<LsaKey>) {
        let key = key.into();
        self.lsa_list.remove(&key);

        // remember what we need to recompute
        if key.is_router() {
            self.recompute_intra_area = true;
        } else if key.is_summary() {
            self.recompute_inter_area.insert(key.target());
        } else if key.is_external() {
            unreachable!()
        }
    }

    /// Update the SPT computation (both intra-area paths, inter-area paths and as-external paths).
    /// The function returns `true` if the SPT was updated.
    ///
    /// This calculation yields the set of intra-area routes associated with an area (called
    /// hereafter Area A). A router calculates the shortest-path tree using itself as the root.[22]
    /// The formation of the shortest path tree is done here in two stages. In the first stage, only
    /// links between routers and transit networks are considered. Using the Dijkstra algorithm, a
    /// tree is formed from this subset of the link state database. In the second stage, leaves are
    /// added to the tree by considering the links to stub networks.
    ///
    /// The procedure will be explained using the graph terminology that was introduced in Section
    /// 2. The area's link state database is represented as a directed graph. The graph's vertices
    /// are routers, transit networks and stub networks. The first stage of the procedure concerns
    /// only the transit vertices (routers and transit networks) and their connecting links.
    fn update_spt(&mut self) -> bool {
        let mut modified = false;

        // recompute dijkstra if necessary
        if self.recompute_intra_area {
            self.spt.clear();
            self.spt = compute_intra_area_routes(self.router_id, &self.lsa_list);
            self.recompute_intra_area = false;
            modified = true;
        }

        if modified || !self.recompute_inter_area.is_empty() {
            // todo!("implement partial updater here!");
            self.compute_inter_area_routes(modified);
            self.recompute_inter_area.clear();
            modified = true;
        }

        modified
    }

    /// This algorithm computes the routes from Summary-LSAs using the algorithm presented in
    /// Section 16.2 of RFC 2328. If `all` is set to `true`, then go through all summary-LSAs to
    /// build the SPT for each inter-area destination (in that case, the SPT was cleared
    /// before). Otherwhise, only compute the entries related to the summary-LSAs that were actually
    /// changed.
    ///
    /// The inter-area routes are calculated by examining summary-LSAs. If the router has active
    /// attachments to multiple areas, only backbone summary-LSAs are examined. Routers attached to
    /// a single area examine that area's summary-LSAs. In either case, the summary-LSAs examined
    /// below are all part of a single area's link state database (call it Area A).
    ///
    /// Summary-LSAs are originated by the area border routers. Each summary-LSA in Area A is
    /// considered in turn. Remember that the destination described by a summary-LSA is either a
    /// network (Type 3 summary-LSAs) or an AS boundary router (Type 4 summary-LSAs). For each
    /// summary-LSA:
    fn compute_inter_area_routes(&mut self, recompute_all: bool) {
        let mut new_paths: HashMap<RouterId, SptNode> = HashMap::new();

        // remove all old values if we don't recompute all entries.
        if !recompute_all {
            for r in self.recompute_inter_area.iter() {
                // remove, but only if the path was learned via an inter_area
                if let Entry::Occupied(e) = self.spt.entry(*r) {
                    if e.get().inter_area {
                        e.remove();
                    }
                }
            }
        }

        for lsa in self.lsa_list.values() {
            let target = lsa.target();

            // only continue if we either recompute all targets, or if the target is mentioned in
            // `self.recompute_intra_area`.
            if !(recompute_all || self.recompute_inter_area.contains(&target)) {
                continue;
            }

            // get the inter-area route
            let Some(path) = compute_inter_area_route(self.router_id, lsa, &self.spt) else {
                continue;
            };

            // (7) Else, the paths present in the routing table are also inter-area paths. Install
            //     the new path through BR if it is cheaper, overriding the paths in the routing
            //     table. Otherwise, if the new path is the same cost, add it to the list of paths
            //     that appear in the routing table entry.
            // --> This behavior is implemented in `std::ops::AddAssign` for `SptNode`.
            match new_paths.entry(target) {
                Entry::Occupied(mut e) => {
                    *e.get_mut() += path;
                }
                Entry::Vacant(e) => {
                    e.insert(path);
                }
            }
        }

        // extend the spt with the new paths
        self.spt.extend(new_paths);
    }

    /// Redistribute routes from `rib` into `self`. This function relies on `is_path_redistributed`.
    fn redistribute_into(
        &mut self,
        rib: &HashMap<RouterId, OspfRibEntry>,
    ) -> (Vec<Lsa>, Vec<(LsaKey, Option<Lsa>)>) {
        // collect the next-hops of the target area
        let neighbors: BTreeSet<RouterId> = self
            .get_router_lsa(self.router_id)
            .into_iter()
            .flat_map(|(_, l)| l)
            .map(|l| l.target)
            .collect();
        if neighbors.is_empty() {
            // if target_neighbors is empty, we are no longer part of that area!
            return (Vec::new(), Vec::new());
        }

        let redistributed_paths = rib
            .values()
            .filter(|path| is_path_redistributed(self.router_id, self.area, &neighbors, path))
            .map(|path| (path.router_id, path.cost))
            .collect();

        self.redistribute_from_paths(redistributed_paths)
    }

    /// Redistribute the given paths into the area. In contrast to `Self::redistribute_into`, it
    /// simply redistributes the generated Summary-LSAs, keeping in mind what it has redistributed
    /// earlier. `Self::redistribute_into` calls `Self::redistribute_from_paths`.
    fn redistribute_from_paths(
        &mut self,
        redistributed_paths: BTreeMap<RouterId, NotNan<LinkWeight>>,
    ) -> (Vec<Lsa>, Vec<(LsaKey, Option<Lsa>)>) {
        let mut redistribute = Vec::new();
        let mut track_max_age = Vec::new();

        // prepare all paths to redistribute
        for m in redistributed_paths
            .iter()
            .merge_join_by(self.redistributed_paths.iter(), |(a, _), (b, _)| a.cmp(b))
        {
            match m {
                EitherOrBoth::Both((&r, &new), (_, &old)) if old != new => {
                    // Path is updated
                    let key = LsaKey::summary(self.router_id, r);
                    let lsa = self.lsa_list.get_mut(&key).expect("Key must exist");
                    // check if the lsa already has max_age
                    if lsa.is_max_age() {
                        // in that case, update the tracking information
                        let mut new_lsa = lsa.clone();
                        new_lsa.data = LsaData::Summary(new);
                        new_lsa.header.seq = 0;
                        new_lsa.header.age = 0;
                        track_max_age.push((key, Some(new_lsa)));
                    } else if lsa.header.seq >= MAX_SEQ - 1 {
                        // sequence number overflow! --> premature aging
                        lsa.header.seq = MAX_SEQ;
                        lsa.header.age = MAX_AGE;
                        redistribute.push(lsa.clone());
                        // keep track of max-age
                        let mut new_lsa = lsa.clone();
                        new_lsa.data = LsaData::Summary(new);
                        new_lsa.header.seq = 0;
                        new_lsa.header.age = 0;
                        track_max_age.push((key, Some(new_lsa)));
                    } else {
                        // increment the sequence number, update the weight, and flood
                        lsa.header.seq += 1;
                        lsa.data = LsaData::Summary(new);
                        redistribute.push(lsa.clone());
                    }
                }
                EitherOrBoth::Both(_, _) => {
                    // path is not modified. Nothing to do here
                }
                EitherOrBoth::Left((&r, &new)) => {
                    // Path is newly advertised!
                    let lsa = Lsa {
                        header: LsaHeader {
                            lsa_type: LsaType::Summary,
                            router: self.router_id,
                            target: Some(r),
                            seq: 0,
                            age: 0,
                        },
                        data: LsaData::Summary(new),
                    };
                    let key = lsa.key();
                    // before insertint into the datastructure, check if it is not already present.
                    match self.lsa_list.entry(key) {
                        Entry::Occupied(e) => {
                            // the lsa must be max-age
                            debug_assert!(e.get().is_max_age());
                            // in that case, update the tracking information
                            track_max_age.push((key, Some(lsa)));
                        }
                        Entry::Vacant(e) => {
                            e.insert(lsa.clone());
                            redistribute.push(lsa);
                        }
                    }
                }
                EitherOrBoth::Right((&r, &old)) => {
                    // Path is no longer advertised!
                    //
                    // If a router advertises a summary-LSA for a destination which then becomes
                    // unreachable, the router must then flush the LSA from the routing domain by
                    // setting its age to MaxAge and reflooding (see Section 14.1). Also, if the
                    // destination is still reachable, yet can no longer be advertised according to
                    // the above procedure (e.g., it is now an inter-area route, when it used to be
                    // an intra-area route associated with some non-backbone area; it would thus no
                    // longer be advertisable to the backbone), the LSA should also be flushed from
                    // the routing domain.
                    let key = LsaKey::summary(self.router_id, r);
                    let seq = self
                        .lsa_list
                        .get(&key)
                        .map(|x| x.header.seq)
                        .unwrap_or(MAX_SEQ);
                    let lsa = Lsa {
                        header: LsaHeader {
                            lsa_type: key.lsa_type,
                            router: key.router,
                            target: key.target,
                            seq,
                            age: MAX_AGE,
                        },
                        data: LsaData::Summary(old),
                    };
                    // simply insert the LSA into the list, replacing the old value
                    self.lsa_list.insert(key, lsa.clone());
                    // ensure that the update is flooded out
                    redistribute.push(lsa);
                    track_max_age.push((key, None));
                }
            }
        }

        // update the redistributed paths
        self.redistributed_paths = redistributed_paths;

        // return the result
        (redistribute, track_max_age)
    }

    /// Remove all LSAs that are unreachable using a intra-area path. See the function
    /// `OspfRib::remove_unreachable_lsas` for more details.
    fn remove_unreachable_lsas(&mut self) {
        // remove the unreachable External-LSAs
        self.lsa_list.retain(|k, _| {
            k.router == self.router_id
                || self
                    .spt
                    .get(&k.router)
                    .map(|path| !path.inter_area)
                    .unwrap_or(false)
        })
    }
}

/// Recompute the distance and next-hops towards each destination using Dijkstra's algorithm.
/// This function will update `self.spt`.
///
/// The algorithm does *not directly* resemble the algorithm described in 16.1, because there
/// are lots of aspects that we ignore, e.g., stub networks. Instead, we implement an optimized
/// Dijkstra algorithm that keeps track of the next-hops from the source.
///
/// The arguments to that function are the following:
/// - `root`: The root from which to compute the SPT.
/// - `lsa-list`: List of LSAs
///
pub(crate) fn compute_intra_area_routes(
    root: RouterId,
    lsa_list: &HashMap<LsaKey, Lsa>,
) -> HashMap<RouterId, SptNode> {
    // closure that captures `lsa_list` to get the router-lsa
    let get_router_lsa = |r| {
        let key = LsaKey::router(r);
        lsa_list
            .get(&key)
            .and_then(|lsa| lsa.data.router().map(|links| (&lsa.header, links)))
    };

    // use a heap to always explore the shortest paths first
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    struct HeapEntry {
        node: RouterId,
        parent: RouterId,
        cost: NotNan<LinkWeight>,
    }

    impl PartialOrd for HeapEntry {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }

    impl Ord for HeapEntry {
        fn cmp(&self, other: &Self) -> Ordering {
            other.cost.cmp(&self.cost)
        }
    }

    let mut spt = HashMap::new();
    let mut visit_next = BinaryHeap::new();
    spt.insert(root, SptNode::new(root));

    // fill in the first nodes. To that end, first compute all btreesets of length 1 and store
    // them.
    visit_next.extend(
        get_router_lsa(root)
            .into_iter()
            .filter(|(h, _)| !h.is_max_age())
            .flat_map(|(_, l)| l)
            .filter(|l| l.is_p2p())
            .map(|l| HeapEntry {
                node: l.target,
                parent: root,
                cost: l.weight,
            }),
    );

    while let Some(HeapEntry { node, parent, cost }) = visit_next.pop() {
        let mut from_fibs = spt.get(&parent).expect("not yet visited").fibs.clone();
        // if from_fibs is empty, that means that `parent` must be the root. In that case,
        // insert `node` as the fib, as `node` is a direct neighbor of `root`.
        if from_fibs.is_empty() {
            debug_assert!(parent == root);
            from_fibs.insert(node);
        }

        // check if already visited
        match spt.entry(node) {
            Entry::Occupied(mut e) => {
                // check if the cost is the same. If so, extend fibs
                let e = e.get_mut();
                match cost.cmp(&e.cost) {
                    Ordering::Less => unreachable!("Negative link weights are not allowed!"),
                    Ordering::Equal => e.fibs.extend(from_fibs),
                    Ordering::Greater => {}
                }
            }
            Entry::Vacant(e) => {
                // insert the new node
                e.insert(SptNode::from(node, cost, from_fibs));
                // extend the heap
                visit_next.extend(
                    get_router_lsa(node)
                        .into_iter()
                        .filter(|(h, _)| !h.is_max_age())
                        .flat_map(|(_, l)| l)
                        .filter(|l| l.is_p2p())
                        .map(|l| HeapEntry {
                            node: l.target,
                            parent: node,
                            cost: cost + l.weight,
                        }),
                );
            }
        }
    }

    spt
}

/// This algorithm computes the Summary-LSAs using the algorithm presented in Section 16.2 of
/// RFC 2328.
///
/// The arguments to that function are the following:
/// - `router_id`: The ID of the computing router.
/// - `lsa`: The SummaryLsa to compute the route to.
/// - `spt`: The currently known (intra-area or inter-area) paths.
///
/// The inter-area routes are calculated by examining summary-LSAs. If the router has active
/// attachments to multiple areas, only backbone summary-LSAs are examined. Routers attached to
/// a single area examine that area's summary-LSAs. In either case, the summary-LSAs examined
/// below are all part of a single area's link state database (call it Area A).
///
/// Summary-LSAs are originated by the area border routers. Each summary-LSA in Area A is
/// considered in turn. Remember that the destination described by a summary-LSA is either a
/// network (Type 3 summary-LSAs) or an AS boundary router (Type 4 summary-LSAs). For each
/// summary-LSA:
pub(crate) fn compute_inter_area_route(
    router_id: RouterId,
    lsa: &Lsa,
    spt: &HashMap<RouterId, SptNode>,
) -> Option<SptNode> {
    // only look at Summary-LSAs
    let weight = lsa.data.summary()?;
    let target = lsa.target();

    // (1) If the cost specified by the LSA is LSInfinity, or if the LSA's LS age is equal
    //     to MaxAge, then examine the the next LSA.
    // --> ignore LS-INFINITY! We are dealing with that later.
    if lsa.header.is_max_age() {
        return None;
    }

    // (2) If the LSA was originated by the calculating router itself, examine the next LSA.
    if lsa.header.router == router_id {
        return None;
    }

    // (3) If it is a Type 3 summary-LSA, and the collection of destinations described by
    //     the summary-LSA equals one of the router's configured area address ranges (see
    //     Section 3.5), and the particular area address range is active, then the
    //     summary-LSA should be ignored. "Active" means that there are one or more
    //     reachable (by intra-area paths) networks contained in the area range.
    let adv_node = spt.get(&lsa.header.router)?;

    // (6) Else, if the paths present in the table are intra-area paths, do nothing with the
    //     LSA (intra-area paths are always preferred).
    if let Some(current_path) = spt.get(&target) {
        if !current_path.inter_area {
            // already know an intra-area path towards `target`.
            return None;
        }
    }

    // (4) Else, call the destination described by the LSA TARGET (for Type 3 summary-LSAs,
    //     TARGET's address is obtained by masking the LSA's Link State ID with the
    //     network/subnet mask contained in the body of the LSA), and the area border
    //     originating the LSA BR. Look up the routing table entry for BR having Area A as
    //     its associated area. If no such entry exists for router BR (i.e., BR is
    //     unreachable in Area A), do nothing with this LSA and consider the next in the
    //     list. Else, this LSA describes an inter-area path to destination N, whose cost is
    //     the distance to BR plus the cost specified in the LSA. Call the cost of this
    //     inter-area path IAC.
    let path = SptNode {
        router_id: target,
        key: lsa.key(),
        fibs: adv_node.fibs.clone(),
        cost: adv_node.cost + weight,
        inter_area: true,
    };

    // (5) Next, look up the routing table entry for the destination TARGET. (If TARGET is
    //     an AS boundary router, look up the "router" routing table entry associated with
    //     Area A). If no entry exists for TARGET or if the entry's path type is "type 1
    //     external" or "type 2 external", then install the inter-area path to N, with
    //     associated area Area A, cost IAC, next hop equal to the list of next hops to
    //     router BR, and Advertising router equal to B
    // --> we ignore type 1 or type 2 external paths!

    // if we reach this point, then the path should be advertised
    Some(path)
}

/// This algorithm computes the routes from External-LSAs using the algorithm presented in
/// Section 16.4 of RFC 2328.
///
/// The arguments to that function are the following:
/// - `router_id`: The ID of the computing router.
/// - `lsa`: The SummaryLsa to compute the route to.
/// - `rib`: the curently known RIB.
///
/// AS external routes are calculated by examining AS-external-LSAs. Each of the
/// AS-external-LSAs is considered in turn. Most AS- external-LSAs describe routes to specific
/// IP destinations. An AS-external-LSA can also describe a default route for the Autonomous
/// System (Destination ID = DefaultDestination, network/subnet mask = 0x00000000). For each
/// AS-external-LSA:
pub(crate) fn compute_as_external_route(
    router_id: RouterId,
    lsa: &Lsa,
    rib: &HashMap<RouterId, OspfRibEntry>,
) -> Option<OspfRibEntry> {
    // only look at External-LSAs
    let weight = lsa.data.external()?;
    let target = lsa.target();

    // (1) If the cost specified by the LSA is LSInfinity, or if the LSA's LS age is equal
    //     to MaxAge, then examine the next LSA.
    // --> ignore LSInfinity
    if lsa.header.is_max_age() {
        return None;
    }

    // (2) If the LSA was originated by the calculating router itself, examine the next LSA.
    // --> Contrary what the spec tells, we do NOT ignore external LSAs advertised by the
    //     current router itself.

    // (3) Call the destination described by the LSA TARGET. TARGET's address is obtained by
    //     masking the LSA's Link State ID with the network/subnet mask contained in the body of
    //     the LSA. Look up the routing table entries (potentially one per attached area) for
    //     the AS boundary router (ASBR) that originated the LSA. If no entries exist for router
    //     ASBR (i.e., ASBR is unreachable), do nothing with this LSA and consider the next in
    //     the list.
    // --> this is the only behavior that we implement!
    //
    //     Else, this LSA describes an AS external path to destination TARGET. Examine the
    //     forwarding address specified in the AS- external-LSA. This indicates the IP
    //     address to which packets for the destination should be forwarded.
    // --> We don't implement that behavior
    //
    //     If the forwarding address is set to 0.0.0.0, packets should be sent to the ASBR
    //     itself. Among the multiple routing table entries for the ASBR, select the
    //     preferred entry as follows. If RFC1583 Compatibility is set to "disabled", prune
    //     the set of routing table entries for the ASBR as described in Section 16.4.1. In
    //     any case, among the remaining routing table entries, select the routing table
    //     entry with the least cost; when there are multiple least cost routing table
    //     entries the entry whose associated area has the largest OSPF Area ID (when
    //     considered as an unsigned 32-bit integer) is chosen.
    // --> We don't implement that behavior
    //
    //     If the forwarding address is non-zero, look up the forwarding address in the
    //     routing table.[24] The matching routing table entry must specify an intra-area or
    //     inter-area path; if no such path exists, do nothing with the LSA and consider the
    //     next in the list.
    // --> We don't implement that behavior
    let adv_rib = rib.get(&lsa.header.router)?;

    // (4) Let X be the cost specified by the preferred routing table entry for the
    //     ASBR/forwarding address, and Y the cost specified in the LSA. X is in terms of
    //     the link state metric, and Y is a type 1 or 2 external metric.
    // --> we don't model type 1 or type 2 external metrics
    let mut path = OspfRibEntry {
        router_id: target,
        keys: btreemap! {None => lsa.key()},
        fibs: adv_rib.fibs.clone(),
        cost: adv_rib.cost + weight,
        inter_area: adv_rib.inter_area,
    };
    // set the fibs appropriately, in case it is directly connected
    if lsa.header.router == router_id {
        debug_assert!(path.fibs.is_empty());
        path.fibs.insert(target);
    }

    // return the computed path
    Some(path)
}

/// Decision process whether to redistribute a given path into an area.
///
/// The arguments to that function are the following:
/// - `router_id`: The ID of the computing router (the router that decides whether to re-advertise
///   the route).
/// - `area`: The OSPF Area into which we should re-advertise the route.
/// - `neighbors`: The neighbors of the computing router in the `area` (other neighbors must not be
///   present in this set)!
/// - `path`: The path which might be advertised
///
/// Summary-LSAs are originated by area border routers. The precise summary routes to advertise
/// into an area are determined by examining the routing table structure (see Section 11) in
/// accordance with the algorithm described below. Note that only intra-area routes are
/// advertised into the backbone, while both intra-area and inter-area routes are advertised
/// into the other areas.
///
/// To determine which routes to advertise into an attached Area A, each routing table entry is
/// processed as follows. Remember that each routing table entry describes a set of equal-cost
/// best paths to a particular destination.
pub(crate) fn is_path_redistributed(
    router_id: RouterId,
    area: OspfArea,
    neighbors: &BTreeSet<RouterId>,
    path: &OspfRibEntry,
) -> bool {
    // do not redistribute Router-LSAs that are generated by this router itself.
    if path.router_id == router_id {
        return false;
    }

    // - Only Destination Types of network and AS boundary router are advertised in
    //   summary-LSAs. If the routing table entry's Destination Type is area border router,
    //   examine the next routing table entry.
    // --> we do not model this thing.

    // - AS external routes are never advertised in summary-LSAs. If the routing table entry
    //   has Path-type of type 1 external or type 2 external, examine the next routing table
    //   entry.
    if path.keys.values().any(|key| key.is_external()) {
        return false;
    }

    // - Else, if the area associated with this set of paths is the Area A itself, do not
    //   generate a summary-LSA for the route.[17]
    if path.keys.contains_key(&Some(area)) {
        return false;
    }

    // - Else, if the next hops associated with this set of paths belong to Area A itself,
    //   do not generate a summary-LSA for the route.[18] This is the logical equivalent of
    //   a Distance Vector protocol's split horizon logic.
    if path.fibs.iter().any(|nh| neighbors.contains(nh)) {
        return false;
    }

    // - Else, if the routing table cost equals or exceeds the value LSInfinity, a
    //   summary-LSA cannot be generated for this route.
    if path.cost.is_infinite() {
        return false;
    }

    // - Else, if the destination of this route is an AS boundary router, a summary-LSA
    //   should be originated if and only if the routing table entry describes the preferred
    //   path to the AS boundary router (see Step 3 of Section 16.4). If so, a Type 4
    //   summary-LSA is originated for the destination, with Link State ID equal to the AS
    //   boundary router's Router ID and metric equal to the routing table entry's
    //   cost. Note: these LSAs should not be generated if Area A has been configured as a
    //   stub area.
    // --> we only have network types

    // - Else, the Destination type is network. If this is an inter-area route, generate a
    //   Type 3 summary-LSA for the destination, with Link State ID equal to the network's
    //   address (if necessary, the Link State ID can also have one or more of the network's
    //   host bits set; see Appendix E for details) and metric equal to the routing table
    //   cost.

    // - The one remaining case is an intra-area route to a network. This means that the
    //   network is contained in one of the router's directly attached areas. In general,
    //   this information must be condensed before appearing in summary-LSAs. Remember that
    //   an area has a configured list of address ranges, each range consisting of an
    //   [address,mask] pair and a status indication of either Advertise or
    //   DoNotAdvertise. At most a single Type 3 summary-LSA is originated for each
    //   range. When the range's status indicates Advertise, a Type 3 summary-LSA is
    //   generated with Link State ID equal to the range's address (if necessary, the Link
    //   State ID can also have one or more of the range's "host" bits set; see Appendix E
    //   for details) and cost equal to the largest cost of any of the component
    //   networks. When the range's status indicates DoNotAdvertise, the Type 3 summary-LSA
    //   is suppressed and the component networks remain hidden from other areas.

    //   By default, if a network is not contained in any explicitly configured address
    //   range, a Type 3 summary-LSA is generated with Link State ID equal to the network's
    //   address (if necessary, the Link State ID can also have one or more of the network's
    //   "host" bits set; see Appendix E for details) and metric equal to the network's
    //   routing table cost.

    //   If an area is capable of carrying transit traffic (i.e., its TransitCapability is
    //   set to TRUE), routing information concerning backbone networks should not be
    //   condensed before being summarized into the area. Nor should the advertisement of
    //   backbone networks into transit areas be suppressed. In other words, the backbone's
    //   configured ranges should be ignored when originating summary-LSAs into transit
    //   areas.

    // We treat all of these steps above differently! first, check if we should re-advertise
    // the path. We want to redistribute all paths except if the area the backbone, and the
    // path is an inter_area path.
    if area.is_backbone() && path.inter_area {
        return false;
    }

    // if we get here, then the path must be re-advertised
    true
}

/// Throughout the shortest path calculation, the following data is also associated with each transit
/// vertex:
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SptNode {
    /// A 32-bit number which together with the vertex type (router or network) uniquely identifies
    /// the vertex. For router vertices the Vertex ID is the router's OSPF Router ID. For network
    /// vertices, it is the IP address of the network's Designated Router.
    pub router_id: RouterId,

    /// Each transit vertex has an associated LSA. For router vertices, this is a router-LSA. For
    /// transit networks, this is a network-LSA (which is actually originated by the network's
    /// Designated Router). In any case, the LSA's Link State ID is always equal to the above Vertex
    /// ID.
    pub key: LsaKey,

    /// The list of next hops for the current set of shortest paths from the root to this
    /// vertex. There can be multiple shortest paths due to the equal-cost multipath
    /// capability. Each next hop indicates the outgoing router interface to use when forwarding
    /// traffic to the destination. On broadcast, Point-to-MultiPoint and NBMA networks, the next
    /// hop also includes the IP address of the next router (if any) in the path towards the
    /// destination.
    pub fibs: BTreeSet<RouterId>,

    /// The link state cost of the current set of shortest paths from the root to the vertex. The
    /// link state cost of a path is calculated as the sum of the costs of the path's constituent
    /// links (as advertised in router-LSAs and network-LSAs). One path is said to be "shorter" than
    /// another if it has a smaller link state cost.
    pub cost: NotNan<LinkWeight>,

    /// Whether the path is an intra-area oor inter-area
    pub inter_area: bool,
}

impl std::ops::AddAssign for SptNode {
    fn add_assign(&mut self, rhs: Self) {
        match (self.inter_area, self.cost).cmp(&(rhs.inter_area, rhs.cost)) {
            Ordering::Less => {
                // do nothing, self is preferred over other
            }
            Ordering::Equal => {
                // extend the next-hops from self by those from rhs
                self.fibs.extend(rhs.fibs);
            }
            Ordering::Greater => {
                // replace self by rhs
                *self = rhs;
            }
        }
    }
}

impl SptNode {
    /// Create the empty, initial SptNode
    pub fn new(router_id: RouterId) -> Self {
        Self {
            router_id,
            key: LsaKey::router(router_id),
            fibs: Default::default(),
            cost: Default::default(),
            inter_area: false,
        }
    }

    pub fn from(router_id: RouterId, cost: NotNan<LinkWeight>, fibs: BTreeSet<RouterId>) -> Self {
        Self {
            router_id,
            key: LsaKey::router(router_id),
            fibs,
            cost,
            inter_area: false,
        }
    }
}

impl<'n, P: crate::types::Prefix, Q> crate::formatter::NetworkFormatter<'n, P, Q, super::LocalOspf>
    for OspfRib
{
    fn fmt(&self, net: &'n crate::network::Network<P, Q, super::LocalOspf>) -> String {
        let external_lsas = format!(
            "external LSAs: {{\n  {}\n}}",
            self.external_lsas
                .iter()
                .sorted_by_key(|(k, _)| *k)
                .map(|(_, lsa)| lsa.fmt(net))
                .join("\n  ")
        );
        self.areas
            .values()
            .map(|ds| ds.fmt(net))
            .chain(std::iter::once(external_lsas))
            .join("\n")
    }
}

impl<'n, P: crate::types::Prefix, Q> crate::formatter::NetworkFormatter<'n, P, Q, super::LocalOspf>
    for AreaDataStructure
{
    fn fmt(&self, net: &'n crate::network::Network<P, Q, super::LocalOspf>) -> String {
        format!(
            "{} LSAs: {{\n  {}\n}}",
            self.area,
            self.lsa_list
                .iter()
                .sorted_by_key(|(k, _)| *k)
                .map(|(_, lsa)| lsa.fmt(net))
                .join("\n  ")
        )
    }
}