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
use std::{ops::ControlFlow, sync::Arc, time::Duration};

use super::{
    config::Config,
    connection::{Client, Connection, ConnectionFactory},
    error::MqttError,
    metrics::MqttMetrics,
    status_reporter::MqttStatusReporter,
};

use crate::{
        common::status_reporter::{AnyStatusReporter, TargetStatusReporter},
    comms::{AnyDirectUpdate, DirectLink, DirectUpdate, Terminated},
    ingress,
    manager::{Component, TargetCommand, WaitPoint},
    payload::{Update, UpstreamStatus},
    targets::Target,
};
use crate::roto_runtime::types::OutputStreamMessage;

use arc_swap::{ArcSwap, ArcSwapOption};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use log::error;
use mqtt::{MqttOptions, QoS};
use non_empty_vec::NonEmpty;
use serde::Deserialize;
use tokio::{sync::mpsc, time::timeout};

#[derive(Debug, Deserialize)]
pub struct Mqtt {
    /// The set of units to receive messages from.
    sources: NonEmpty<DirectLink>,

    #[serde(flatten)]
    config: Config,
}

#[cfg(test)]
impl From<Config> for Mqtt {
    fn from(config: Config) -> Self {
        let (_gate, mut gate_agent) = crate::comms::Gate::new(0);
        let link = gate_agent.create_link();
        let sources = NonEmpty::new(link.into());
        Self { sources, config }
    }
}

pub(super) struct SenderMsg {
    pub received: DateTime<Utc>,
    pub content: String,
    pub topic: String,
}

impl Mqtt {
    pub async fn run(
        self,
        component: Component,
        cmd: mpsc::Receiver<TargetCommand>,
        waitpoint: WaitPoint,
    ) -> Result<(), Terminated> {
        MqttRunner::<mqtt::AsyncClient>::new(self.config, component)
            .run(self.sources, cmd, waitpoint)
            .await
    }
}

// Being generic over T enables use of a mock file I/O implementation when testing.
pub(super) struct MqttRunner<C> {
    component: Component,
    config: Arc<ArcSwap<Config>>,
    client: Arc<ArcSwapOption<C>>,
    pub_q_tx: Option<mpsc::UnboundedSender<SenderMsg>>,
    status_reporter: Arc<MqttStatusReporter>,
    ingresses: Arc<ingress::Register>,
}

impl<C: Client> MqttRunner<C>
where
    Self: ConnectionFactory<ClientType = C>,
{
    pub fn new(config: Config, mut component: Component) -> Self {
        let config = Arc::new(ArcSwap::from_pointee(config));

        let metrics = Arc::new(MqttMetrics::new());
        component.register_metrics(metrics.clone());

        let status_reporter =
            Arc::new(MqttStatusReporter::new(component.name(), metrics));

        let ingresses = component.ingresses().clone();
        Self {
            component,
            config,
            client: Default::default(),
            pub_q_tx: None,
            status_reporter,
            ingresses,
        }
    }

    #[cfg(test)]
    pub fn mock(
        config: Arc<ArcSwap<Config>>,
        pub_q_tx: Option<mpsc::UnboundedSender<SenderMsg>>,
    ) -> (Self, Arc<MqttStatusReporter>) {
        let metrics = Arc::new(MqttMetrics::new());

        let status_reporter =
            Arc::new(MqttStatusReporter::new("mock", metrics));

        let ingresses = Arc::new(ingress::Register::new());

        let res = Self {
            component: Default::default(),
            config,
            client: Default::default(),
            pub_q_tx,
            status_reporter: status_reporter.clone(),
            ingresses,
        };

        (res, status_reporter)
    }

    #[cfg(test)]
    pub fn client(&self) -> Option<Arc<C>> {
        use std::ops::Deref;
        self.client.load().deref().clone()
    }

    pub async fn run(
        mut self,
        mut sources: NonEmpty<DirectLink>,
        cmd_rx: mpsc::Receiver<TargetCommand>,
        waitpoint: WaitPoint,
    ) -> Result<(), Terminated> {
        let component = &mut self.component;
        let _unit_name = component.name().clone();

        let (pub_q_tx, pub_q_rx) = mpsc::unbounded_channel();
        self.pub_q_tx = Some(pub_q_tx);

        let arc_self = Arc::new(self);

        // Register as a direct update receiver with the linked gates.
        for link in sources.iter_mut() {
            link.connect(arc_self.clone(), false).await.unwrap();
        }

        // Wait for other components to be, and signal to other components
        // that we are, ready to start. All units and targets start together,
        // otherwise data passed from one component to another may be lost if
        // the receiving component is not yet ready to accept it.
        waitpoint.running().await;

        arc_self
            .do_run::<Self>(Some(sources), cmd_rx, pub_q_rx)
            .await
    }

    pub async fn do_run<F: ConnectionFactory<ClientType = C>>(
        self: &Arc<Self>,
        mut sources: Option<NonEmpty<DirectLink>>,
        mut cmd_rx: mpsc::Receiver<TargetCommand>,
        mut pub_q_rx: mpsc::UnboundedReceiver<SenderMsg>,
    ) -> Result<(), Terminated>
    where
        <F as ConnectionFactory>::EventLoopType: 'static,
    {
        loop {
            let connection =
                F::connect(&self.config.load(), self.status_reporter.clone());

            if let Err(Terminated) = self
                .process_events(
                    connection,
                    &mut sources,
                    &mut cmd_rx,
                    &mut pub_q_rx,
                )
                .await
            {
                self.status_reporter.terminated();
                return Err(Terminated);
            }
        }
    }

    pub async fn process_events(
        self: &Arc<Self>,
        mut connection: Connection<C>,
        sources: &mut Option<NonEmpty<DirectLink>>,
        cmd_rx: &mut mpsc::Receiver<TargetCommand>,
        pub_q_rx: &mut mpsc::UnboundedReceiver<SenderMsg>,
    ) -> Result<(), Terminated> {
        while connection.active() {
            tokio::select! {
                // Disable tokio::select!() random branch selection
                biased;

                client = connection.process() => {
                    self.client.store(client.map(Arc::new));
                }

                // If nothing happened above, check for new internal Rotonda
                // target commands to handle.
                cmd = cmd_rx.recv() => {
                    if let Some(cmd) = &cmd {
                        self.status_reporter.command_received(cmd);
                    }

                    match cmd {
                        Some(TargetCommand::Reconfigure { new_config: Target::Mqtt(new_config) }) => {
                            if self.reconfigure(sources, new_config, &mut connection).await.is_break() {
                                connection.disconnect().await;
                            }
                        }

                        Some(TargetCommand::Reconfigure { .. }) => unreachable!(),

                        Some(TargetCommand::ReportLinks { report }) => {
                            if let Some(sources) = sources {
                                report.set_sources(sources);
                            }
                            report.set_graph_status(
                                self.status_reporter.metrics(),
                            );
                        }

                        None | Some(TargetCommand::Terminate) => {
                            connection.disconnect().await;
                            return Err(Terminated);
                        }
                    }
                }

                // And finally if not doing anything else we can process
                // messages waiting in our internal queue to be published,
                // which were enqueued by the direct_update() method below.
                msg = pub_q_rx.recv() => {
                    match msg {
                        Some(SenderMsg {
                            received,
                            content,
                            topic,
                        }) => {
                            Self::publish_msg(
                                self.status_reporter.clone(),
                                connection.client(),
                                topic,
                                received,
                                content,
                                self.config.load().qos,
                                self.config.load().publish_max_secs,
                                None::<fn() -> Result<(), MqttError>>,
                            )
                            .await;
                        }

                        None => {
                            connection.disconnect().await;
                            return Err(Terminated);
                        }
                    }
                }
            }
        }

        self.client.store(None);

        Ok(())
    }

    async fn reconfigure(
        self: &Arc<Self>,
        sources: &mut Option<NonEmpty<DirectLink>>,
        Mqtt {
            sources: new_sources,
            config: new_config,
        }: Mqtt,
        connection: &mut Connection<C>,
    ) -> ControlFlow<()> {
        if let Some(sources) = sources {
            self.status_reporter
                .upstream_sources_changed(sources.len(), new_sources.len());

            *sources = new_sources;

            // Register as a direct update receiver with the new
            // set of linked gates.
            for link in sources.iter_mut() {
                link.connect(self.clone(), false).await.unwrap();
            }
        }

        // Check if the config changes impact the MQTT client
        let config = self.config.load();
        let reconnect = new_config.client_id != config.client_id
            || new_config.destination != config.destination
            || new_config.queue_size != config.queue_size;

        // Re-create the reconnect delay interval based on the new config
        connection.set_retry_delay(config.connect_retry_secs);

        // Store the changed configuration
        self.config.store(Arc::new(new_config));

        // Report that we have finished handling the reconfigure command
        self.status_reporter.reconfigured();

        if reconnect {
            // Advise the caller to stop using the current MQTT client
            // and instead to re-create it using the new config
            ControlFlow::Break(())
        } else {
            // Advise the caller to keep using the current MQTT client
            ControlFlow::Continue(())
        }
    }

    #[allow(clippy::too_many_arguments)]
    async fn publish_msg<F>(
        status_reporter: Arc<MqttStatusReporter>,
        client: Option<C>,
        topic: String,
        _received: DateTime<Utc>,
        content: String,
        qos: i32,
        duration: Duration,
        test_publish: Option<F>,
    ) where
        F: Fn() -> Result<(), MqttError> + Send + 'static,
    {
        status_reporter.publishing(&topic, &content);

        match Self::do_publish(
            client,
            &topic,
            content,
            qos,
            duration,
            test_publish,
        )
        .await
        {
            Ok(_) => {
                status_reporter.publish_ok(topic);
            }
            Err(err) => {
                status_reporter.publish_error(err);
            }
        }
    }

    async fn do_publish<F>(
        client: Option<C>,
        topic: &str,
        content: String,
        qos: i32,
        duration: Duration,
        test_publish: Option<F>,
    ) -> Result<(), MqttError>
    where
        F: Fn() -> Result<(), MqttError> + Send + 'static,
        C: Client,
    {
        let qos = match qos {
            0 => QoS::AtMostOnce,
            1 => QoS::AtLeastOnce,
            2 => QoS::ExactlyOnce,
            _ => unreachable!(),
        };

        if let Some(client) = client {
            match timeout(
                duration,
                client.publish(topic, qos, false, content),
            )
            .await
            {
                Err(_) => Err(MqttError::Timeout),
                Ok(Ok(())) => Ok(()),
                Ok(Err(err)) => Err(MqttError::Error(err)),
            }
        } else if let Some(test_publish) = test_publish {
            // instead of using duration as a timeout, use it as a delay
            tokio::time::sleep(duration).await;
            test_publish()
        } else {
            Ok(())
        }
    }

    pub fn output_stream_message_to_msg(
        &self,
        //osm: Arc<OutputStreamMessage>,
        osm: OutputStreamMessage,
    ) -> Option<SenderMsg> {
        if *osm.get_name() == **self.component.name() {
            let ingress_info =
                osm.get_ingress_id().and_then(|id| self.ingresses.get(id));

            match serde_json::to_string(&(ingress_info, osm.get_record())) {
                Ok(content) => {
                    let topic = self
                        .config
                        .load()
                        .topic_template
                        .replace("{id}", osm.get_topic());
                    return Some(SenderMsg {
                        received: Utc::now(),
                        content,
                        topic,
                    });
                }
                Err(err) => {
                    // TODO
                    error!("{err}");
                }
            }
        }

        None
    }
}

impl ConnectionFactory for MqttRunner<mqtt::AsyncClient> {
    type EventLoopType = mqtt::EventLoop;

    type ClientType = mqtt::AsyncClient;

    fn connect(
        config: &Config,
        status_reporter: Arc<MqttStatusReporter>,
    ) -> Connection<Self::ClientType> {
        let mut create_opts = MqttOptions::new(
            config.client_id.clone(),
            config.destination.host.clone(),
            config.destination.port,
        );
        create_opts.set_request_channel_capacity(config.queue_size.into());
        create_opts.set_clean_session(true);
        create_opts.set_inflight(1000);
        create_opts.set_keep_alive(Duration::from_secs(20));

        if let (Some(username), Some(password)) =
            (&config.username, &config.password)
        {
            create_opts.set_credentials(username, password);
        }

        Connection::new(
            create_opts,
            config.connect_retry_secs,
            status_reporter,
        )
    }
}

#[async_trait]
impl<C: Client> DirectUpdate for MqttRunner<C>
where
    Self: ConnectionFactory<ClientType = C>,
{
    async fn direct_update(&self, update: Update) {
        match update {
            Update::UpstreamStatusChange(UpstreamStatus::EndOfStream {
                ..
            }) => {
                // Nothing to do
            }

            Update::OutputStream(msgs) => {
                for osm in msgs {
                    if let Some(msg) = self.output_stream_message_to_msg(osm)
                    {
                        if let Err(err) =
                            self.pub_q_tx.as_ref().unwrap().send(msg)//.await
                        {
                            error!("failed to send MQTT message: {err}");
                        }
                    }
                }
            }

            _ => {
                // We may have received the output from another unit, but we
                // are not interested in it
            }
        }
    }
}

impl<C: Client> AnyDirectUpdate for MqttRunner<C> where
    Self: ConnectionFactory<ClientType = C>
{
}

impl<C: Client> std::fmt::Debug for MqttRunner<C> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MqttRunner").finish()
    }
}