rotonda 0.4.0

composable, programmable BGP engine
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
use std::cell::RefCell;
use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeSet;
use std::hash::Hash;
use std::net::SocketAddr;
use std::ops::ControlFlow;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

use bytes::Bytes;
use chrono::{DateTime, Utc};
use log::{debug, error, warn};
use inetnum::asn::Asn;
use rotonda_store::prefix_record::RouteStatus;
use routecore::bgp::message::{Message as BgpMsg, UpdateMessage};
use smallvec::{smallvec, SmallVec};
use tokio::net::TcpStream;
use tokio::sync::mpsc;

use routecore::bgp::fsm::session::{
    self,
    BgpConfig, // trait
    Command,
    DisconnectReason,
    Message,
    NegotiatedConfig,
    Session,
};

use crate::roto_runtime::types::{
    explode_announcements, explode_withdrawals, FreshRouteContext, Output, OutputStreamMessage, Provenance, RotoOutputStream,
};
use crate::comms::{Gate, GateStatus, Terminated};
use crate::ingress;
use crate::payload::{Payload, RotondaRoute, Update};
use crate::roto_runtime::Ctx;
use crate::units::bgp_tcp_in::status_reporter::BgpTcpInStatusReporter;
use crate::units::rib_unit::unit::RtrCache;
use crate::units::Unit;

use super::peer_config::{CombinedConfig, ConfigExt};
use super::unit::BgpTcpIn;
use super::unit::RotoFunc;

#[async_trait::async_trait]
trait BgpSession<C: BgpConfig + ConfigExt> {
    fn config(&self) -> &C;

    fn connected_addr(&self) -> Option<SocketAddr>;

    fn negotiated(&self) -> Option<&NegotiatedConfig>;

    async fn tick(&mut self) -> Result<(), session::Error>;
}

#[async_trait::async_trait]
impl BgpSession<CombinedConfig> for Session<CombinedConfig> {
    fn config(&self) -> &CombinedConfig {
        self.config()
    }

    fn connected_addr(&self) -> Option<SocketAddr> {
        self.connected_addr()
    }

    fn negotiated(&self) -> Option<&NegotiatedConfig> {
        self.negotiated()
    }

    #[must_use]
    #[allow(clippy::type_complexity, clippy::type_repetition_in_bounds)]
    async fn tick(&mut self) -> Result<(), session::Error> {
        self.tick().await
    }
}

struct Processor {
    roto_function: Option<RotoFunc>,
    gate: Gate,
    unit_cfg: BgpTcpIn,
    //bgp_ltime: u64, // XXX or should this be on Unit level?
    tx: mpsc::Sender<Command>,
    pdu_out_tx: mpsc::Sender<BgpMsg<Bytes>>,
    status_reporter: Arc<BgpTcpInStatusReporter>,
    ingresses: Arc<ingress::Register>,

    /// The 'overall' IngressId for the BGP-IN unit.
    ingress_id: ingress::IngressId,

    // Link to an empty RtrCache for now. Eventually, this should point to the
    // main all-encompassing RIB.
    rtr_cache: Arc<RtrCache>,
}

impl Processor {
    fn new(
        roto_function: Option<RotoFunc>,
        gate: Gate,
        unit_cfg: BgpTcpIn,
        tx: mpsc::Sender<Command>,
        pdu_out_tx: mpsc::Sender<BgpMsg<Bytes>>,
        status_reporter: Arc<BgpTcpInStatusReporter>,
        ingresses: Arc<ingress::Register>,
        ingress_id: ingress::IngressId,
    ) -> Self {
        Processor {
            roto_function,
            gate,
            unit_cfg,
            //bgp_ltime: 0,
            tx,
            pdu_out_tx,
            status_reporter,
            ingresses,
            ingress_id,
            rtr_cache: Default::default(),
        }
    }

    #[cfg(test)]
    fn mock(unit_cfg: BgpTcpIn) -> (Self, crate::comms::GateAgent) {
        let (gate, gate_agent) = Gate::new(0);

        let (cmds_tx, _) = mpsc::channel(16);
        let (pdu_out_tx, _) = mpsc::channel(16);

        let processor = Self {
            roto_function: None,
            gate,
            unit_cfg,
            //bgp_ltime: 0,
            tx: cmds_tx,
            pdu_out_tx,
            status_reporter: Default::default(),
            ingresses: Arc::new(ingress::Register::default()),
            ingress_id: 0,
            rtr_cache: Default::default(),
        };

        (processor, gate_agent)
    }

    async fn process<'a, C: BgpConfig + ConfigExt, T: BgpSession<C>>(
        &'a mut self,
        mut session: T,
        mut rx_sess: mpsc::Receiver<Message>,
        live_sessions: Arc<Mutex<super::unit::LiveSessions>>,
    ) -> (T, mpsc::Receiver<Message>) {
        let peer_addr_cfg = session.config().remote_prefix_or_exact();

        let mut rejected = false;
        let mut connection_id = DefaultHasher::new();
        session.connected_addr().hash(&mut connection_id);

        let session_ingress_id = self.ingress_id;

        // XXX is this all OK cancel-safety-wise?
        loop {
            tokio::select! {
                fsm_res = session.tick() => {
                    match fsm_res {
                        Ok(()) => { },
                        Err(e) => {
                            error!("error from fsm: {e}");
                            break;
                        }
                    }
                }
                res = self.gate.process() => {
                    match res {
                        Err(Terminated) => {
                            debug!("Terminated: {:?}", session.negotiated());
                            if let Some(remote_addr) = session.connected_addr() {
                                self.status_reporter.disconnect(remote_addr.ip());
                            }
                            let _ = self.tx.send(Command::Disconnect(
                                    DisconnectReason::Shutdown
                            )).await;
                            debug!("TODO send Payload::bgp_eof");
                            //break;
                        }
                        Ok(status) => match status {
                            GateStatus::Reconfiguring {
                                new_config: Unit::BgpTcpIn(new_unit),
                            } => {
                                // Checking whether we need to reconnect is a
                                // two-stage thing:
                                // if the 'main' config, i.e. my_asn or
                                // my_bgp_id or the address:port to listen on
                                // is changed, reconnect all peers.
                                // If those are unchanged, check the
                                // PeerConfig only for the connection this
                                // Processor handles

                                // Changes in main config?
                                // Based on own impl of PartialEq
                                if new_unit != self.unit_cfg {
                                    debug!("GateStatus::Reconfiguring, \
                                           change in unit config, break.");
                                    // XXX
                                    // unsure what the correct subcode is:
                                    // 4 Administrative Reset, or
                                    // 6 'Other Configuration Change'?
                                    // and perhaps, eventually, we'll need:
                                    // 3 'Peer deconfigured' when applicable

                                    let _ = self.tx.send(
                                        Command::Disconnect(
                                            DisconnectReason::Reconfiguration
                                        )).await;
                                    break;
                                } else {
                                    // Main unit has not changed, check for
                                    // this specific peer.
                                    // A peer might have been removed from the
                                    // config, which results in a specific
                                    // NOTIFICATION.
                                    if let Some(new_peer_config) = new_unit.peer_configs.get_exact(&peer_addr_cfg) {
                                        let current = self.unit_cfg.peer_configs.get_exact(&peer_addr_cfg).expect("must exist");
                                        if *new_peer_config != *current {
                                            let _ = self.tx.send(
                                                Command::Disconnect(
                                                    DisconnectReason::Reconfiguration
                                                    )
                                                ).await;
                                        } else {
                                            debug!("GateStatus::Reconfiguring, noop");
                                        }
                                    } else {
                                        // disconnect, de-configured
                                        if let Some(remote_addr) = session.connected_addr() {
                                            self.status_reporter.disconnect(remote_addr.ip());
                                        }
                                        debug!(
                                            "GateStatus::Reconfiguring, deconfigured {:?}",
                                            peer_addr_cfg
                                        );
                                        let _ = self.tx.send(
                                            Command::Disconnect(
                                                DisconnectReason::Deconfigured
                                                )).await;
                                        break;

                                    }

                                }

                            },
                            GateStatus::ReportLinks { report } => {
                                report.declare_source();
                            }
                            _ => { /* Nothing to do */ }
                        }
                    }
                }
                res = rx_sess.recv() => {
                    match res {
                        None => { break; }
                        Some(Message::UpdateMessage(bgp_msg)) => {
                            // We can only receive UPDATE messages over an
                            // established session, so not having a
                            // NegotiatedConfig should never happen.
                            let Some(negotiated) = session.negotiated() else {
                                error!("unexpected state: no NegotiatedConfig for session");
                                break
                            };
                            let provenance = Provenance::for_bgp(
                                session_ingress_id,
                                negotiated.remote_addr(),
                                negotiated.remote_asn(),
                            );

                            let output_stream = RotoOutputStream::new_rced();
                            let mut ctx = Ctx::new(output_stream, self.rtr_cache.clone());
                            let received = std::time::Instant::now();

                            let verdict = self.roto_function.as_ref().map(
                                |roto_function|
                            {
                                roto_function.call(
                                    &mut ctx,
                                    roto::Val(bgp_msg.clone()),
                                    roto::Val(provenance),
                                )
                            });


                            let Ctx {output, ..} = ctx;
                            let output_stream = RotoOutputStream::into_inner(output).into_messages();
                            if !output_stream.is_empty() {
                                let mut osms = smallvec![];
                                use crate::roto_runtime::types::Output;
                                for entry in output_stream {
                                    debug!("output stream entry {entry:?}");
                                    let osm = match entry {
                                        Output::Prefix(_prefix) => {
                                            OutputStreamMessage::prefix(
                                                None,
                                                Some(session_ingress_id),
                                            )
                                        }
                                        Output::Community(_u32) => {
                                            OutputStreamMessage::community(
                                                None,
                                                Some(session_ingress_id),
                                            )
                                        }
                                        Output::Asn(_u32) => {
                                            OutputStreamMessage::asn(
                                                None,
                                                Some(session_ingress_id),
                                            )
                                        }
                                        Output::Origin(_u32) => {
                                            OutputStreamMessage::origin(
                                                None,
                                                Some(session_ingress_id),
                                            )
                                        }
                                        Output::PeerDown => {
                                            debug!("Logged PeerDown from Rib unit, ignoring");
                                            continue
                                        }
                                        Output::Custom((id, local)) => {
                                            OutputStreamMessage::custom(
                                                id, local,
                                                Some(session_ingress_id),
                                            )
                                        }
                                        Output::Entry(entry) => {
                                            OutputStreamMessage::entry(
                                                entry,
                                                Some(session_ingress_id),
                                            )
                                        }
                                    };
                                    osms.push(osm);
                                }
                                self.gate.update_data(Update::OutputStream(osms)).await;
                            }

                            match verdict {
                                // Default action when no roto script is used
                                // is Accept (i.e. None here).
                                Some(roto::Verdict::Accept(_)) | None => {
                                    let update = self.process_update(
                                        received,
                                        bgp_msg,
                                        provenance,
                                    ).await;
                                    match update {
                                        Ok(update) => {
                                            self.gate.update_data(update).await;
                                        },
                                        Err(e) => {
                                            error!("unexpected state: {e}");
                                        },
                                    };
                                }
                                Some(roto::Verdict::Reject(_)) => {
                                    // increase metrics and continue
                                    debug!("bgp-in roto Reject");
                                }
                            }
                        }
                        Some(Message::NotificationMessage(pdu)) => {
                            debug!(
                                "received NOTIFICATION: {:?}",
                                pdu.details()
                            );
                        }
                        Some(Message::ConnectionLost(socket)) => {
                            //TODO clean up RIB etc?
                            self.status_reporter
                                .peer_connection_lost(socket);
                            if let Some(socket) = socket {
                                debug!(
                                    "Connection lost: {}@{}",
                                    session.negotiated()
                                        .map(|n| n.remote_asn())
                                        .unwrap_or(Asn::MIN),
                                    socket
                                    );
                                } else {
                                    debug!(
                                        "Connection lost: {}@UNKOWN",
                                        session.negotiated()
                                            .map(|n| n.remote_asn())
                                            .unwrap_or(Asn::MIN),
                                        );
                                }
                            break;
                        }
                        Some(Message::SessionNegotiated(negotiated)) => {
                            let key = (negotiated.remote_addr(), negotiated.remote_asn());
                            if live_sessions.lock().unwrap().contains_key(&key) {
                                error!("Already got a session for {:?}", key);
                                let _ = self.tx.send(Command::Disconnect(
                                        DisconnectReason::ConnectionRejected
                                )).await;
                                rejected = true;
                                break;
                            }
                            {
                            let mut live_sessions = live_sessions.lock().unwrap();
                            live_sessions.insert(
                                (negotiated.remote_addr(), negotiated.remote_asn()),
                                (self.tx.clone(), self.pdu_out_tx.clone())
                            );
                            debug!(
                                "inserted into live_sessions (current count: {})",
                                live_sessions.len()
                            );
                            }
                            // register ingress
                            //session_ingress_id = self.ingresses.register();

                            debug!(
                                "got assigned {} for this session",
                                session_ingress_id
                            );
                            debug!("get: {:?}", self.ingresses.get(session_ingress_id));
                            self.ingresses.update_info(
                                session_ingress_id,
                                ingress::IngressInfo::new()
                                    .with_name("some-bgp-session".to_string())
                                    .with_remote_addr(negotiated.remote_addr())
                                    .with_remote_asn(negotiated.remote_asn())
                                );
                            debug!("get 2: {:?}", self.ingresses.get(session_ingress_id));



                        }
                        Some(Message::Attributes(_)) => unimplemented!(),
                    }
                }
            }
        }

        // Done, for whatever reason. Remove ourselves form the live sessions.
        // But only if this was not an 'early reject' case, because we would
        // wrongfully remove the firstly inserted (IpAddr, Asn) (i.e., an
        // earlier session, not the currently rejected one) from the
        // live_sessions set.
        if !rejected {
            if let Some(negotiated) = session.negotiated() {
                live_sessions.lock().unwrap().remove(&(
                    negotiated.remote_addr(),
                    negotiated.remote_asn(),
                ));
                debug!(
                    "removed {}@{} from live_sessions (current count: {})",
                    negotiated.remote_asn(),
                    negotiated.remote_addr(),
                    live_sessions.lock().unwrap().len()
                );

                self.gate
                    .update_data(Update::Withdraw(session_ingress_id, None))
                    .await;
            }
        }

        (session, rx_sess)
    }

    #[allow(dead_code)]
    fn print_pcap<T: AsRef<[u8]>>(buf: T) {
        print!("000000 ");
        for b in buf.as_ref() {
            print!("{:02x} ", b);
        }
        println!();
    }

    // For every NLRI and every withdrawal, send out Bulk Payload to the next
    // unit.
    async fn process_update(
        &mut self,
        received: std::time::Instant,
        bgp_msg: UpdateMessage<bytes::Bytes>,
        provenance: Provenance,
    ) -> Result<Update, session::Error> {
        // When sending both v4 and v6 nlri using exabgp, exa sends a v4
        // NextHop in a v6 MP_REACH_NLRI, which is invalid.
        // However, routecore logs that the nexthop looks bogus but continues
        // and happily gives us the announced prefixes from the nlri in the
        // pdu.
        // What should we do here? One option is to let NextHop::check return
        // an Error in such case, and let Nlris::parse use NextHop::check
        // instead of NextHop::skip.
        // (For now, returning an Error from NextHop::check would perhaps be
        // sufficient, but in the near future we want to make all the parsing
        // even lazier.)
        //
        // ------
        //
        // Local copy of routecore now throws the Error, that seems to work.
        // The session is reset:
        //
        // [2023-08-02 21:13:18] WARN  routecore::bgp::message::nlri: Unimplemented NextHop AFI/SAFI Ipv6/Unicast len 4
        // [2023-08-02 21:13:18] ERROR rotonda_fsm::bgp::session: error: parse error
        // [2023-08-02 21:13:18] DEBUG rotonda_fsm::bgp::fsm: FSM Established -> Connect
        // [2023-08-02 21:13:18] ERROR rotonda::units::bgp_tcp_in::router_handler: error from fsm: error: error from read_frame
        // [2023-08-02 21:13:18] DEBUG rotonda::units::bgp_tcp_in::router_handler: removed AS200@10.1.0.2 from live_sessions (current count: 0)
        //
        // And all previously announced correct prefixes are withdrawn from
        // rib. Perhaps this can serve when further developing the part
        // where such an invalid PDU results in a specific NOTIFICATION that
        // needs to go out. Also, check whether 7606 comes into play here.
        let mut payloads = SmallVec::new();

        //  RotondaRoute announcements:
        let rr_reach = explode_announcements(&bgp_msg)?;
        let rr_unreach = explode_withdrawals(&bgp_msg)?;
        let context = FreshRouteContext::new(
            bgp_msg.clone(),
            RouteStatus::Active,
            provenance,
        );

        payloads.extend(
            rr_reach.into_iter().map(|rr| {
                Payload::with_received(
                    rr,
                    context.clone().into(),
                    None,
                    received,
                )
            }),
        );

        let context = FreshRouteContext {
            status: RouteStatus::Withdrawn,
            ..context
        };

        payloads.extend(rr_unreach.into_iter().map(|rr|
            Payload::with_received(
                rr,
                context.clone().into(),
                None,
                received
            )));

        Ok(payloads.into())
    }
}

#[allow(clippy::too_many_arguments)]
pub async fn handle_connection(
    roto_function: Option<RotoFunc>,
    gate: Gate,
    unit_config: BgpTcpIn,
    tcp_stream: TcpStream,
    candidate_config: CombinedConfig,
    cmds_tx: mpsc::Sender<Command>,
    cmds_rx: mpsc::Receiver<Command>,
    status_reporter: Arc<BgpTcpInStatusReporter>,
    live_sessions: Arc<Mutex<super::unit::LiveSessions>>,
    ingresses: Arc<ingress::Register>,
    connector_ingress_id: ingress::IngressId,
) {
    // NB: when testing with an FRR instance configured with
    //  "neighbor 1.2.3.4 timers delayopen 15"
    // the socket is not readable until their delayopen has passed.
    // Not sure whether this is correct/intentional.
    //
    // To work around that, instead of checking for both writability and
    // readability, we do not wait for the latter.
    // So instead of:
    //      let socket_status = tokio::join!(
    //          tcp_stream.writable(),
    //          tcp_stream.readable()
    //      );
    // we do:
    let _ = tcp_stream.writable().await;

    let (tcp_in, tcp_out) = tcp_stream.into_split();
    let (sess_tx, sess_rx) = mpsc::channel::<Message>(100);

    let (pdu_out_tx, mut pdu_out_rx) = mpsc::channel(10);


    //  - depending on candidate_config, with or without DelayOpen
    //  Ugly use of temp bool here, because candidate_config is moved.
    //  We do not want to put this logic in BgpSession itself, because this
    //  all looks a bit to rotonda-unit specific.

    let delay_open = !candidate_config.is_exact();
    debug!(
        "delay_open for {}: {}",
        candidate_config.peer_config().name(),
        delay_open
    );

    let cmds_tx2 = cmds_tx.clone();
    let sess_tx2 = sess_tx.clone();

    let mut session = Session::new(
        candidate_config,
        tcp_in,
        sess_tx,
        cmds_rx,
        pdu_out_tx.clone(),
    );

    if delay_open {
        session.enable_delay_open();
    }
    session.manual_start().await;
    session.connection_established().await;

    let mut p = Processor::new(
        roto_function,
        gate,
        unit_config,
        cmds_tx,
        pdu_out_tx,
        status_reporter,
        ingresses,
        connector_ingress_id,
    );

    tokio::spawn(async move {
        while let Some(pdu) = pdu_out_rx.recv().await {
            if let Err(e) = tcp_out.writable().await {
                warn!("error while awaiting tcp_out.writable(): {}", e);
            }
            match tcp_out.try_write(pdu.as_ref()) {
                Ok(_) => {}
                Err(ref e)
                    if e.kind() == tokio::io::ErrorKind::WouldBlock =>
                {
                    debug!("WouldBlock after writable().await");
                }
                Err(e) => {
                    warn!(
                        "error sending pdu ({:?}): {}",
                        tcp_out.peer_addr(),
                        e
                    );
                    break;
                }
            }
        }
        // Make sure we get rid of the other half of the TcpStream:
        let _ = cmds_tx2
            .send(Command::Disconnect(DisconnectReason::Other))
            .await;
        let _ = sess_tx2.send(Message::ConnectionLost(None)).await;
        debug!("pre tcp_out.forget()");
        tcp_out.forget();
        debug!("post tcp_out.forget()");
    });

    p.process(session, sess_rx, live_sessions).await;
}

#[cfg(test)]
mod tests {
    use std::{
        collections::HashMap,
        net::SocketAddr,
        sync::{Arc, Mutex},
    };

    use inetnum::asn::Asn;
    use routecore::bgp::fsm::session::{self, Message, NegotiatedConfig};
    use tokio::{sync::mpsc, task::JoinHandle};

    use crate::{
        common::status_reporter::AnyStatusReporter,
        comms::GateAgent,
        tests::util::internal::{
            enable_logging, get_testable_metrics_snapshot,
        },
        units::bgp_tcp_in::{
            peer_config::{CombinedConfig, PeerConfig, PrefixOrExact},
            router_handler::Processor,
            status_reporter::BgpTcpInStatusReporter,
            unit::BgpTcpIn,
        },
    };

    use super::BgpSession;

    #[tokio::test(flavor = "multi_thread")]
    async fn processor_should_abort_on_unit_termination() {
        enable_logging("trace");
        let (join_handle, status_reporter, gate_agent, sess_tx) =
            setup_test();

        gate_agent.terminate().await;

        // We should be able to just wait for the processor to abort but it
        // doesn't... because the mock session doesn't respond to the
        // Disconnect command that gets sent to which it would in turn send
        // a ConnectionLost message. However we still want to test unit
        // termination because it should also increment the disconnect metric
        // while sending only the ConnectionLost message does not.
        // join_handle.await.unwrap();

        // Note: the disconnect metric is only incremented if both
        // session.negotiated() and session.connected_addr() return Some.

        // Wait for the termination command to be handled:
        let mut count = 0;
        while count < 1 {
            let metrics = get_testable_metrics_snapshot(
                &status_reporter.metrics().unwrap(),
            );
            count = metrics.with_name::<usize>("bgp_tcp_in_disconnect_count");
        }

        // Emulate the real session behaviour of sending a ConnectionLost
        // message.
        let msg =
            Message::ConnectionLost(Some("10.0.0.2:12345".parse().unwrap()));
        let _ = sess_tx.send(msg).await;

        // Now it's safe to wait for the processor to abort.
        join_handle.await.unwrap();

        let metrics = get_testable_metrics_snapshot(
            &status_reporter.metrics().unwrap(),
        );
        assert_eq!(
            metrics.with_name::<usize>("bgp_tcp_in_connection_lost_count"),
            1
        );
        assert_eq!(
            metrics.with_name::<usize>("bgp_tcp_in_disconnect_count"),
            1
        );
    }

    #[tokio::test]
    async fn processor_should_abort_on_connection_lost() {
        let (join_handle, status_reporter, _gate_agent, sess_tx) =
            setup_test();

        let msg = Message::ConnectionLost("10.0.0.2:12345".parse().ok());
        let _ = sess_tx.send(msg).await;

        join_handle.await.unwrap();

        let metrics = get_testable_metrics_snapshot(
            &status_reporter.metrics().unwrap(),
        );
        assert_eq!(
            metrics.with_name::<usize>("bgp_tcp_in_connection_lost_count"),
            1
        );
    }

    //-------- Test helpers --------------------------------------------------

    #[allow(clippy::type_complexity)]
    fn setup_test() -> (
        JoinHandle<(MockBgpSession, mpsc::Receiver<Message>)>,
        Arc<BgpTcpInStatusReporter>,
        GateAgent,
        mpsc::Sender<Message>,
    ) {
        let unit_settings =
            BgpTcpIn::mock("dummy-listen-address", Asn::from_u32(12345));
        let peer_config = PeerConfig::mock();
        let remote_net = PrefixOrExact::Exact("10.0.0.1".parse().unwrap());
        let config = CombinedConfig::new(
            unit_settings.clone(),
            peer_config,
            remote_net,
        );
        let session = MockBgpSession(config, NegotiatedConfig::dummy());
        let (mut p, gate_agent) = Processor::mock(unit_settings);
        let (sess_tx, sess_rx) = mpsc::channel::<Message>(100);
        let live_sessions = Arc::new(Mutex::new(HashMap::new()));
        let status_reporter = p.status_reporter.clone();

        let join_handle =
            crate::tokio::spawn("mock_bgp_tcp_in_processor", async move {
                p.process(session, sess_rx, live_sessions).await
            });

        (join_handle, status_reporter, gate_agent, sess_tx)
    }

    struct MockBgpSession(CombinedConfig, NegotiatedConfig);

    #[async_trait::async_trait]
    impl BgpSession<CombinedConfig> for MockBgpSession {
        fn config(&self) -> &CombinedConfig {
            &self.0
        }

        fn connected_addr(&self) -> Option<SocketAddr> {
            Some("1.2.3.4:12345".parse().unwrap())
        }

        fn negotiated(&self) -> Option<&NegotiatedConfig> {
            Some(&self.1)
        }

        async fn tick(&mut self) -> Result<(), session::Error> {
            // Don't tick too fast otherwise process() spends all its time
            // handling ticks and won't do anything else.
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
            Ok(())
        }
    }
}