Skip to main content

mako_redispatch/
lib.rs

1//! `mako-redispatch` — Redispatch 2.0 process engine for German grid
2//! congestion management (§§ 13, 13a, 14 `EnWG`).
3//!
4//! # Three-crate architecture for Redispatch 2.0
5//!
6//! | Crate | Responsibility |
7//! |---|---|
8//! | `edi-energy` | IFTSTA status messages (EDIFACT, PIDs 21037/21038) |
9//! | `redispatch-xml` | XML/XSD format parsing (`ActivationDocument`, `Stammdaten`, …) |
10//! | `mako-redispatch` ← **this crate** | Process engine — workflows, routing, deadlines |
11//!
12//! # Domain background
13//!
14//! **Redispatch 2.0** entered into force on **1 October 2021** via the
15//! Netzausbaubeschleunigungsgesetz (NABEG). It requires all German TSOs
16//! (ÜNB) and DSOs (VNB) to coordinate congestion management across
17//! transmission and distribution networks using CIM/IEC 62325 XML documents.
18//!
19//! Unlike GPKE/WiM/GeLi Gas (EDIFACT `RFF+Z13` Prüfidentifikatoren), routing
20//! here is document-type-driven via [`RedispatchRouter`].
21//!
22//! # Regulatory basis
23//!
24//! | `BNetzA` decision | Topic |
25//! |---|---|
26//! | BK6-20-059 | `AcknowledgementDocument` (6h), `StatusRequest` (24h) |
27//! | BK6-20-060 | `Stammdaten` (1 Werktag), Activation (5 min) |
28//! | BK6-20-061 | `Kostenblatt` (15th of following month) |
29//!
30//! # Regulatory deadlines
31//!
32//! | Obligation | Deadline | Clock |
33//! |---|---|---|
34//! | `AcknowledgementDocument` | 6 wall-clock hours | **UTC** |
35//! | `StatusRequest` response | 24 wall-clock hours | **UTC** |
36//! | Stammdaten forward (VNB→ÜNB) | 1 Werktag | German local time |
37//! | Activation (ACO) response | **5 minutes** | **UTC** |
38//! | Kostenblatt submission | 15th of following month | German local time |
39//!
40//! > **Clock semantics differ from GPKE/WiM.** Redispatch 2.0 uses UTC
41//! > wall-clock hours for the acknowledgement and activation deadlines.
42//! > Only the Stammdaten-forwarding and Kostenblatt obligations follow
43//! > German local time (CET/CEST) + Werktag rules.
44//!
45//! # Deployment role gate
46//!
47//! `RedispatchModule` should only be registered when `DeploymentRoles` contains
48//! at least one of `Marktrolle::Nb`, `Marktrolle::Unb`, or `Marktrolle::Anb`.
49//! Lieferant (LF) and MSB deployments are out of scope for Redispatch 2.0.
50//!
51//! # IFTSTA PIDs (confirmed from IFTSTA AHB 2.1 + PID 4.0)
52//!
53//! | PID   | Perspective | Process |
54//! |-------|-------------|---------|
55//! | 21037 | NB (VNB)    | Kommunikationsprozesse Redispatch — Ansicht NB |
56//! | 21038 | BTR         | Kommunikationsprozesse Redispatch — Ansicht BTR |
57//!
58//! These PIDs are registered into the `PidRouter` by [`RedispatchModule`] and
59//! route to the [`aktivierung`] workflow via conversation-ID lookup.
60//!
61//! # Module overview
62//!
63//! | Module | Workflow name | Document type |
64//! |---|---|---|
65//! | [`stammdaten`] | `redispatch-stammdaten` | `Stammdaten` |
66//! | [`aktivierung`] | `redispatch-aktivierung` | `ActivationDocument` |
67//! | [`ack_forward`] (Verfügbarkeit) | `redispatch-verfuegbarkeit` | `UnavailabilityMarketDocument` |
68//! | [`ack_forward`] (Netzengpass) | `redispatch-netzengpass` | `NetworkConstraintDocument` |
69//! | [`ack_forward`] (Kaskade) | `redispatch-kaskade` | `Kaskade` |
70//! | [`ack_forward`] (Planungsdaten) | `redispatch-planungsdaten` | `PlannedResourceScheduleDocument` |
71//! | [`ack_forward`] (Statusanfrage) | `redispatch-statusanfrage` | `StatusRequest_MarketDocument` |
72//! | [`ack_forward`] (Kostenblatt) | `redispatch-kostenblatt` | `Kostenblatt` |
73
74#![deny(unsafe_code)]
75#![deny(missing_docs)]
76#![warn(clippy::pedantic)]
77
78pub mod ack_forward;
79pub mod aktivierung;
80pub mod ausfallarbeit;
81pub mod bilarem;
82pub mod router;
83pub mod stammdaten;
84
85pub use router::{RedispatchDocumentKind, RedispatchRouter};
86
87use mako_engine::{builder::EngineModule, pid_router::PidRouter, profile::ProfileRequirement};
88
89// ── RedispatchModule ──────────────────────────────────────────────────────────
90
91/// Engine module for the Redispatch 2.0 process family.
92///
93/// Registers:
94/// - All 8 Redispatch 2.0 workflows into the caller's `RedispatchRouter`
95///   (XML document-type routing, not PID routing).
96/// - IFTSTA PIDs 21037 and 21038 into the `PidRouter`
97///   (EDIFACT-based Vollzugsmeldung, routes to `redispatch-aktivierung`).
98///
99/// # Deployment gate
100///
101/// Only register this module when `DeploymentRoles` contains at least one of
102/// `Marktrolle::Nb`, `Marktrolle::Unb`, or `Marktrolle::Anb`:
103///
104/// ```rust,ignore
105/// if roles.contains_any(&[Marktrolle::Nb, Marktrolle::Unb, Marktrolle::Anb]) {
106///     builder.register(Box::new(RedispatchModule));
107/// }
108/// ```
109pub struct RedispatchModule;
110
111impl RedispatchModule {
112    /// Build a fully-populated [`RedispatchRouter`] for `makod` inbound dispatch.
113    ///
114    /// Called once during daemon startup, before the HTTP/AS4 servers are bound.
115    ///
116    /// # Acknowledgement routing
117    ///
118    /// `AcknowledgementDocument` is intentionally **not** registered in this
119    /// router. Inbound ACKs carry a `ReceivingDocumentIdentification` field that
120    /// identifies the workflow instance they belong to. The `makod` dispatcher
121    /// resolves that correlation key against the `ProcessRegistry` and delivers
122    /// the ACK directly to the correct workflow instance — no document-type
123    /// routing is needed.
124    #[must_use]
125    pub fn build_router() -> RedispatchRouter {
126        let mut router = RedispatchRouter::new();
127        router.register(
128            RedispatchDocumentKind::Activation,
129            aktivierung::WORKFLOW_NAME,
130        );
131        router.register(
132            RedispatchDocumentKind::PlannedResourceSchedule,
133            ack_forward::names::PLANUNGSDATEN,
134        );
135        // Acknowledgement is routed by correlation (ReceivingDocumentIdentification),
136        // not by document kind — do NOT register it here.
137        router.register(
138            RedispatchDocumentKind::Stammdaten,
139            stammdaten::WORKFLOW_NAME,
140        );
141        router.register(
142            RedispatchDocumentKind::StatusRequest,
143            ack_forward::names::STATUSANFRAGE,
144        );
145        router.register(
146            RedispatchDocumentKind::Unavailability,
147            ack_forward::names::VERFUEGBARKEIT,
148        );
149        router.register(RedispatchDocumentKind::Kaskade, ack_forward::names::KASKADE);
150        router.register(
151            RedispatchDocumentKind::NetworkConstraint,
152            ack_forward::names::NETZENGPASS,
153        );
154        router.register(
155            RedispatchDocumentKind::Kostenblatt,
156            ack_forward::names::KOSTENBLATT,
157        );
158        router
159    }
160}
161
162impl EngineModule for RedispatchModule {
163    fn name(&self) -> &'static str {
164        "redispatch"
165    }
166
167    fn workflow_names(&self) -> &'static [&'static str] {
168        &[
169            stammdaten::WORKFLOW_NAME,
170            aktivierung::WORKFLOW_NAME,
171            ack_forward::names::VERFUEGBARKEIT,
172            ack_forward::names::NETZENGPASS,
173            ack_forward::names::KASKADE,
174            ack_forward::names::PLANUNGSDATEN,
175            ack_forward::names::STATUSANFRAGE,
176            ack_forward::names::KOSTENBLATT,
177        ]
178    }
179
180    fn register_pids(&self, router: &mut PidRouter) {
181        // Redispatch 2.0 uses XML document-type routing, not EDIFACT PIDs.
182        // EDIFACT IFTSTA Vollzugsmeldungen for Redispatch 2.0:
183        //
184        // PID 21037 — Vollzugsmeldung (NB view)
185        // PID 21038 — Vollzugsmeldung (BTR view)
186        //
187        // 21035/21036/21040 are NOT Redispatch PIDs (see aktivierung.rs).
188        // Source: IFTSTA AHB 2.1 + PID 4.0 (01.04.2026).
189        // These route to the Aktivierung workflow via conversation-ID lookup.
190        for &pid in aktivierung::IFTSTA_PIDS {
191            router.register(pid, aktivierung::WORKFLOW_NAME);
192        }
193
194        // Redispatch 2.0 MSCONS time-series data (PIDs 13020–13023, 13026).
195        //
196        // These carry Ausfallarbeit, meteorological data, and EEG
197        // transfer time-series correlated to the Aktivierung process.
198        for &pid in aktivierung::MSCONS_PIDS {
199            router.register(pid, aktivierung::WORKFLOW_NAME);
200        }
201
202        // Redispatch 2.0 ORDERS and ORDRSP PIDs (Ausfallarbeit / Abo-Verwaltung).
203        //
204        // ORDERS 17209/17210/17211: anfNB requests Ausfallarbeit or
205        // Lieferantenausfallarbeitsclearingliste, or files a Reklamation.
206        // ORDRSP 19204/19301/19302: BTR/ÜNB responds to subscription/aggregation requests.
207        for &pid in aktivierung::ORDERS_PIDS {
208            router.register(pid, aktivierung::WORKFLOW_NAME);
209        }
210        for &pid in aktivierung::ORDRSP_PIDS {
211            router.register(pid, aktivierung::WORKFLOW_NAME);
212        }
213    }
214
215    fn profile_requirements(&self) -> &'static [ProfileRequirement] {
216        &[
217            ProfileRequirement {
218                message_type: "IFTSTA",
219                label: "IFTSTA (Redispatch 2.0 Statusmeldungen — PIDs 21035/21036/21037/21038/21040)",
220            },
221            ProfileRequirement {
222                message_type: "MSCONS",
223                label: "MSCONS Redispatch Ausfallarbeit/EEG (13020–13023, 13026)",
224            },
225            ProfileRequirement {
226                message_type: "ORDERS",
227                label: "ORDERS Redispatch Ausfallarbeit (17209–17211)",
228            },
229            ProfileRequirement {
230                message_type: "ORDRSP",
231                label: "ORDRSP Redispatch Abo/Aggregation (19204, 19301–19302)",
232            },
233        ]
234    }
235
236    fn configure(&self) -> Result<(), String> {
237        // Verify that the router covers all document kinds that use kind-based routing.
238        // Acknowledgement is excluded: it is routed by correlation key, not
239        // by document kind (see build_router() doc comment).
240        let router = Self::build_router();
241        for dk in [
242            RedispatchDocumentKind::Activation,
243            RedispatchDocumentKind::PlannedResourceSchedule,
244            RedispatchDocumentKind::Stammdaten,
245            RedispatchDocumentKind::StatusRequest,
246            RedispatchDocumentKind::Unavailability,
247            RedispatchDocumentKind::NetworkConstraint,
248            RedispatchDocumentKind::Kaskade,
249            RedispatchDocumentKind::Kostenblatt,
250        ] {
251            router.route(dk).map_err(|e| format!("redispatch: {e}"))?;
252        }
253        Ok(())
254    }
255}
256
257#[cfg(test)]
258mod tests {
259    use super::*;
260
261    #[test]
262    fn build_router_covers_all_primary_doc_types() {
263        let router = RedispatchModule::build_router();
264        // All document kinds that use document-kind routing must be registered.
265        // Acknowledgement is excluded: it uses correlation-key routing.
266        for dk in [
267            RedispatchDocumentKind::Activation,
268            RedispatchDocumentKind::PlannedResourceSchedule,
269            RedispatchDocumentKind::Stammdaten,
270            RedispatchDocumentKind::StatusRequest,
271            RedispatchDocumentKind::Unavailability,
272            RedispatchDocumentKind::Kaskade,
273            RedispatchDocumentKind::NetworkConstraint,
274            RedispatchDocumentKind::Kostenblatt,
275        ] {
276            assert!(
277                router.is_registered(dk),
278                "RedispatchDocumentKind {dk:?} must be registered in RedispatchModule router"
279            );
280        }
281        // Acknowledgement must NOT be registered — it is routed by correlation key.
282        assert!(
283            !router.is_registered(RedispatchDocumentKind::Acknowledgement),
284            "Acknowledgement must not be in the document-kind router"
285        );
286    }
287
288    #[test]
289    fn configure_succeeds() {
290        assert!(RedispatchModule.configure().is_ok());
291    }
292
293    #[test]
294    fn iftsta_pids_are_correct() {
295        // Confirmed from IFTSTA AHB 2.1 §8 and PID 4.0 (2026-04-01).
296        // Only PIDs 21037 (Ansicht NB/VNB) and 21038 (Ansicht BTR) belong to
297        // Redispatch 2.0. PIDs 21035 (GPKE Rückmeldung Lieferstelle → gpke-supplier-change),
298        // 21036 (WiM Strom Teil 1, unassigned), and 21040 (AWH Sperrprozesse Gas, unassigned)
299        // are not Redispatch PIDs — see docs/pid-reference.md.
300        assert_eq!(aktivierung::IFTSTA_PIDS, &[21_037, 21_038]);
301    }
302
303    #[test]
304    fn mscons_pids_are_correct() {
305        // Confirmed from MSCONS AHB (Redispatch 2.0 Annex) + PID 4.0.
306        assert_eq!(
307            aktivierung::MSCONS_PIDS,
308            &[13_020, 13_021, 13_022, 13_023, 13_026]
309        );
310    }
311
312    #[test]
313    fn workflow_names_are_non_empty() {
314        assert!(!RedispatchModule.workflow_names().is_empty());
315        for name in RedispatchModule.workflow_names() {
316            assert!(
317                name.starts_with("redispatch-"),
318                "workflow name '{name}' must start with 'redispatch-'"
319            );
320        }
321    }
322}