amqp-dds-endpoint 1.0.0-rc.1

DDS-AMQP 1.0 Endpoint daemon: synchronous std-only TCP/TLS server bridging AMQP 1.0 brokers to DDS topics per OMG DDS-AMQP 1.0 §2.1.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors

//! DDS-Host Abstraktion fuer die AMQP-Daemon-Bridge.
//!
//! Spec dds-amqp-1.0:
//! * §2.1 Cl. 3 — Sender/Receiver-Links translaten zu
//!   DDS-DataWriter/DataReader-Operations.
//! * §7.4.4 DEADLINE — DDS-side deadline-tracking; Notifications
//!   ueber `$catalog`-Channel.
//! * §7.5 Discovery Bridging — `$catalog`-Address liefert Topic-
//!   Mappings.
//! * §7.7.1/7.7.2 — `dds:operation` lifecycle-Mapping.
//!
//! # Architektur
//!
//! Die Daemon-Crate kennt die DCPS-Crate nicht direkt; sie
//! definiert hier eine `DdsHost`-Trait, gegen die der Caller
//! seinen DDS-Stack anbindet:
//! * `InMemoryDdsHost` — mitgelieferte In-Memory-Implementierung
//!   (Tests + leichte Deployments ohne RTPS-Wire).
//! * Eine zukuenftige `DcpsDdsHost`-Variante wuerde die DCPS-Crate
//!   wirelevel anbinden (RTPS/UDP).
//!
//! # Integrations-Pattern
//!
//! 1. Daemon-Boot: `DdsHost::register_topic` pro `TopicMapping`
//!    aus der Configuration.
//! 2. Inbound-Attach (Sender-Link, AMQP→DDS): Daemon ruft
//!    `subscribe_dds_to_amqp` (kein Op auf DDS-Side; Bridge
//!    leitet AMQP-Transfers an `publish_to_dds`).
//! 3. Inbound-Attach (Receiver-Link, DDS→AMQP): Daemon ruft
//!    `subscribe_amqp_to_dds(topic, callback)`. DDS-Samples
//!    erzeugen Outbound-AMQP-Transfers via Callback.
//! 4. `$catalog`-Receiver: Daemon ruft `topics()` und schickt
//!    pro Eintrag ein Catalog-Sample.

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};

use zerodds_amqp_endpoint::annex_a::{LinkDirection, TopicMapping};
use zerodds_amqp_endpoint::management::{CatalogDirection, CatalogEntry, CatalogTypeId};
use zerodds_amqp_endpoint::routing::AddressResolution;

/// Eindeutiger Topic-Identifier in der DdsHost-Registry.
pub type TopicId = u64;

/// Eindeutiger Subscription-Identifier.
pub type SubscriptionId = u64;

/// Sample-Callback: wird aufgerufen wenn ein DDS-Sample fuer eine
/// AMQP→Subscription verfuegbar ist.
pub type SampleCallback = Arc<dyn Fn(&[u8]) + Send + Sync>;

/// DdsHost-Fehler.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DdsHostError {
    /// Topic existiert nicht in der Registry.
    UnknownTopic(TopicId),
    /// Topic-Address bereits registriert.
    DuplicateAddress(String),
    /// Subscription existiert nicht.
    UnknownSubscription(SubscriptionId),
}

impl core::fmt::Display for DdsHostError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::UnknownTopic(id) => write!(f, "unknown topic id {id}"),
            Self::DuplicateAddress(s) => write!(f, "duplicate amqp address: {s}"),
            Self::UnknownSubscription(id) => write!(f, "unknown subscription id {id}"),
        }
    }
}

impl std::error::Error for DdsHostError {}

/// DdsHost-Trait — das Bridge-Interface zur DDS-Side.
pub trait DdsHost {
    /// Spec §2.1 Cl. 3 — Topic-Mapping registrieren.
    /// Liefert die zugewiesene `TopicId`.
    ///
    /// # Errors
    /// `DuplicateAddress` wenn `mapping.amqp_address` bereits
    /// registriert ist.
    fn register_topic(&self, mapping: TopicMapping) -> Result<TopicId, DdsHostError>;

    /// Spec §2.1 Cl. 3 — AMQP → DDS Publish.
    ///
    /// `bytes` ist der XCDR2-/JSON-/AMQP-Native-Body je nach
    /// `mapping.mode`. Der Host ist verantwortlich fuer den
    /// DDS-Wire-Encode (oder Pass-Through bei MODE_PASSTHROUGH).
    ///
    /// # Errors
    /// `UnknownTopic`.
    fn publish_to_dds(&self, topic: TopicId, bytes: &[u8]) -> Result<(), DdsHostError>;

    /// Spec §2.1 Cl. 3 — DDS → AMQP Subscribe.
    ///
    /// `callback` wird invoked sobald DDS ein Sample liefert.
    /// Liefert eine `SubscriptionId` zur spaeteren Aufhebung.
    ///
    /// # Errors
    /// `UnknownTopic`.
    fn subscribe_amqp_to_dds(
        &self,
        topic: TopicId,
        callback: SampleCallback,
    ) -> Result<SubscriptionId, DdsHostError>;

    /// Subscription beenden.
    ///
    /// # Errors
    /// `UnknownSubscription`.
    fn unsubscribe(&self, subscription: SubscriptionId) -> Result<(), DdsHostError>;

    /// Spec §7.5 — alle registrierten Topic-Mappings als
    /// Catalog-Entries.
    fn topics(&self) -> Vec<CatalogEntry>;

    /// Topic-Lookup per AMQP-Address.
    fn lookup(&self, amqp_address: &str) -> Option<TopicId>;
}

// ============================================================
// In-Memory-Implementierung
// ============================================================

/// In-Memory `DdsHost` — Tests + leichte Deployments ohne
/// echte DCPS-Wire.
///
/// Topics werden in einer `BTreeMap<TopicId, ...>` gehalten.
/// `publish_to_dds(topic, bytes)` invoked alle aktiven
/// Subscriptions auf dem Topic synchron mit den Bytes.
#[derive(Debug, Default)]
pub struct InMemoryDdsHost {
    inner: Mutex<HostState>,
}

#[derive(Debug, Default)]
struct HostState {
    next_topic_id: TopicId,
    next_subscription_id: SubscriptionId,
    topics: BTreeMap<TopicId, TopicEntry>,
    address_index: BTreeMap<String, TopicId>,
}

struct TopicEntry {
    mapping: TopicMapping,
    subscriptions: BTreeMap<SubscriptionId, SampleCallback>,
}

impl core::fmt::Debug for TopicEntry {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TopicEntry")
            .field("mapping", &self.mapping)
            .field("subscription_count", &self.subscriptions.len())
            .finish()
    }
}

impl InMemoryDdsHost {
    /// Frischer Host ohne Topics.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Test-helper: Anzahl registrierter Topics.
    #[must_use]
    pub fn topic_count(&self) -> usize {
        self.with_state(|s| s.topics.len())
    }

    /// Test-helper: Anzahl Subscriptions auf einem Topic.
    #[must_use]
    pub fn subscription_count(&self, topic: TopicId) -> usize {
        self.with_state(|s| s.topics.get(&topic).map_or(0, |t| t.subscriptions.len()))
    }

    fn with_state<R>(&self, f: impl FnOnce(&HostState) -> R) -> R {
        let guard = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        f(&guard)
    }

    fn with_state_mut<R>(&self, f: impl FnOnce(&mut HostState) -> R) -> R {
        let mut guard = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        f(&mut guard)
    }
}

impl DdsHost for InMemoryDdsHost {
    fn register_topic(&self, mapping: TopicMapping) -> Result<TopicId, DdsHostError> {
        self.with_state_mut(|s| {
            if s.address_index.contains_key(&mapping.amqp_address) {
                return Err(DdsHostError::DuplicateAddress(mapping.amqp_address.clone()));
            }
            s.next_topic_id = s.next_topic_id.saturating_add(1);
            let id = s.next_topic_id;
            s.address_index.insert(mapping.amqp_address.clone(), id);
            s.topics.insert(
                id,
                TopicEntry {
                    mapping,
                    subscriptions: BTreeMap::new(),
                },
            );
            Ok(id)
        })
    }

    fn publish_to_dds(&self, topic: TopicId, bytes: &[u8]) -> Result<(), DdsHostError> {
        // Wir sammeln die Callbacks unter Lock und invoken sie
        // ausserhalb, damit ein Subscriber-Callback nicht
        // re-entrant publish() aufrufen kann.
        let callbacks: Vec<SampleCallback> = self.with_state(|s| {
            s.topics
                .get(&topic)
                .map(|t| t.subscriptions.values().cloned().collect())
                .unwrap_or_default()
        });
        // Topic existiert?
        if self.with_state(|s| !s.topics.contains_key(&topic)) {
            return Err(DdsHostError::UnknownTopic(topic));
        }
        for cb in callbacks {
            cb(bytes);
        }
        Ok(())
    }

    fn subscribe_amqp_to_dds(
        &self,
        topic: TopicId,
        callback: SampleCallback,
    ) -> Result<SubscriptionId, DdsHostError> {
        self.with_state_mut(|s| {
            let entry = s
                .topics
                .get_mut(&topic)
                .ok_or(DdsHostError::UnknownTopic(topic))?;
            s.next_subscription_id = s.next_subscription_id.saturating_add(1);
            let id = s.next_subscription_id;
            entry.subscriptions.insert(id, callback);
            Ok(id)
        })
    }

    fn unsubscribe(&self, subscription: SubscriptionId) -> Result<(), DdsHostError> {
        self.with_state_mut(|s| {
            for entry in s.topics.values_mut() {
                if entry.subscriptions.remove(&subscription).is_some() {
                    return Ok(());
                }
            }
            Err(DdsHostError::UnknownSubscription(subscription))
        })
    }

    fn topics(&self) -> Vec<CatalogEntry> {
        self.with_state(|s| {
            s.topics
                .values()
                .map(|t| topic_mapping_to_catalog(&t.mapping))
                .collect()
        })
    }

    fn lookup(&self, amqp_address: &str) -> Option<TopicId> {
        self.with_state(|s| s.address_index.get(amqp_address).copied())
    }
}

fn topic_mapping_to_catalog(m: &TopicMapping) -> CatalogEntry {
    let direction = match m.direction {
        LinkDirection::DirProducerToDds => CatalogDirection::ProducerToDds,
        LinkDirection::DirDdsToConsumer => CatalogDirection::DdsToConsumer,
        LinkDirection::DirBoth => CatalogDirection::Both,
    };
    CatalogEntry {
        amqp_address: m.amqp_address.clone(),
        dds: AddressResolution {
            topic: m.dds_topic.clone(),
            domain_id: m.dds_domain_id,
            partitions: m.dds_partition.clone(),
        },
        dds_type_name: m.dds_type_name.clone(),
        // Spec §7.5: ulong fuer DESC_TRUNCATED, symbol fuer DESC_FULL.
        // Da wir den Hash nicht kennen, geben wir einen Stub-Wert
        // zurueck — der Caller muss das mit echtem TypeIdentifier
        // anreichern, wenn er Spec-konforme Catalog-Entries braucht.
        type_id: CatalogTypeId::Truncated(0),
        direction,
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn mapping(addr: &str, topic: &str) -> TopicMapping {
        TopicMapping {
            amqp_address: addr.to_string(),
            dds_topic: topic.to_string(),
            dds_type_name: "T".to_string(),
            ..TopicMapping::default()
        }
    }

    #[test]
    fn register_topic_returns_unique_ids() {
        let h = InMemoryDdsHost::new();
        let id1 = h.register_topic(mapping("a", "TopicA")).unwrap();
        let id2 = h.register_topic(mapping("b", "TopicB")).unwrap();
        assert_ne!(id1, id2);
        assert_eq!(h.topic_count(), 2);
    }

    #[test]
    fn duplicate_address_yields_error() {
        let h = InMemoryDdsHost::new();
        h.register_topic(mapping("a", "TopicA")).unwrap();
        let err = h
            .register_topic(mapping("a", "DifferentTopic"))
            .unwrap_err();
        assert!(matches!(err, DdsHostError::DuplicateAddress(_)));
    }

    #[test]
    fn lookup_returns_topic_id() {
        let h = InMemoryDdsHost::new();
        let id = h.register_topic(mapping("foo", "T")).unwrap();
        assert_eq!(h.lookup("foo"), Some(id));
        assert_eq!(h.lookup("bar"), None);
    }

    #[test]
    fn subscribe_invokes_callback_on_publish() {
        let h = InMemoryDdsHost::new();
        let topic = h.register_topic(mapping("a", "T")).unwrap();
        let counter = Arc::new(AtomicUsize::new(0));
        let counter_cb = counter.clone();
        let cb: SampleCallback = Arc::new(move |bytes: &[u8]| {
            assert_eq!(bytes, b"hello");
            counter_cb.fetch_add(1, Ordering::Relaxed);
        });
        h.subscribe_amqp_to_dds(topic, cb).unwrap();

        h.publish_to_dds(topic, b"hello").unwrap();
        h.publish_to_dds(topic, b"hello").unwrap();
        assert_eq!(counter.load(Ordering::Relaxed), 2);
    }

    #[test]
    fn multiple_subscribers_all_get_called() {
        let h = InMemoryDdsHost::new();
        let topic = h.register_topic(mapping("a", "T")).unwrap();
        let c1 = Arc::new(AtomicUsize::new(0));
        let c2 = Arc::new(AtomicUsize::new(0));
        let c1_cb = c1.clone();
        let c2_cb = c2.clone();
        h.subscribe_amqp_to_dds(
            topic,
            Arc::new(move |_| {
                c1_cb.fetch_add(1, Ordering::Relaxed);
            }),
        )
        .unwrap();
        h.subscribe_amqp_to_dds(
            topic,
            Arc::new(move |_| {
                c2_cb.fetch_add(1, Ordering::Relaxed);
            }),
        )
        .unwrap();
        assert_eq!(h.subscription_count(topic), 2);
        h.publish_to_dds(topic, b"x").unwrap();
        assert_eq!(c1.load(Ordering::Relaxed), 1);
        assert_eq!(c2.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn unsubscribe_stops_callbacks() {
        let h = InMemoryDdsHost::new();
        let topic = h.register_topic(mapping("a", "T")).unwrap();
        let counter = Arc::new(AtomicUsize::new(0));
        let counter_cb = counter.clone();
        let sid = h
            .subscribe_amqp_to_dds(
                topic,
                Arc::new(move |_| {
                    counter_cb.fetch_add(1, Ordering::Relaxed);
                }),
            )
            .unwrap();
        h.publish_to_dds(topic, b"a").unwrap();
        h.unsubscribe(sid).unwrap();
        h.publish_to_dds(topic, b"b").unwrap();
        assert_eq!(counter.load(Ordering::Relaxed), 1);
        assert_eq!(h.subscription_count(topic), 0);
    }

    #[test]
    fn unsubscribe_unknown_yields_error() {
        let h = InMemoryDdsHost::new();
        let err = h.unsubscribe(99999).unwrap_err();
        assert!(matches!(err, DdsHostError::UnknownSubscription(_)));
    }

    #[test]
    fn publish_to_unknown_topic_yields_error() {
        let h = InMemoryDdsHost::new();
        let err = h.publish_to_dds(42, b"x").unwrap_err();
        assert!(matches!(err, DdsHostError::UnknownTopic(_)));
    }

    #[test]
    fn topics_returns_catalog_entries_for_all_registrations() {
        let h = InMemoryDdsHost::new();
        let mut m1 = mapping("a", "TopicA");
        m1.direction = LinkDirection::DirProducerToDds;
        let mut m2 = mapping("b", "TopicB");
        m2.direction = LinkDirection::DirDdsToConsumer;
        h.register_topic(m1).unwrap();
        h.register_topic(m2).unwrap();
        let cat = h.topics();
        assert_eq!(cat.len(), 2);
        let dirs: Vec<CatalogDirection> = cat.iter().map(|c| c.direction).collect();
        assert!(dirs.contains(&CatalogDirection::ProducerToDds));
        assert!(dirs.contains(&CatalogDirection::DdsToConsumer));
    }

    #[test]
    fn fresh_host_has_no_topics() {
        let h = InMemoryDdsHost::new();
        assert_eq!(h.topic_count(), 0);
        assert!(h.topics().is_empty());
        assert!(h.lookup("anything").is_none());
    }

    #[test]
    fn register_topic_preserves_partitions() {
        let h = InMemoryDdsHost::new();
        let mut m = mapping("addr", "Topic");
        m.dds_partition = vec!["p1".into(), "p2".into()];
        h.register_topic(m).unwrap();
        let cat = h.topics();
        assert_eq!(cat[0].dds.partitions, vec!["p1", "p2"]);
    }
}