mako-redispatch 0.5.0

Redispatch 2.0 process engine for German grid congestion management (§§ 13, 13a, 14 EnWG)
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
//! Generic acknowledge-and-forward workflow for Redispatch 2.0.
//!
//! Shared state machine used by:
//! - Verfügbarkeitsmeldung (`redispatch-verfuegbarkeit`)
//! - Netzengpassinformation (`redispatch-netzengpass`)
//! - Kaskade §13 Abs. 2 (`redispatch-kaskade`)
//! - Planungsdaten Abruffahrplan (`redispatch-planungsdaten`)
//! - Statusanfrage (`redispatch-statusanfrage`)
//! - Kostenblatt (`redispatch-kostenblatt`)
//!
//! Each of these processes follows the same pattern:
//! 1. Receive an XML document.
//! 2. Send an `AcknowledgementDocument` within **6 wall-clock hours** (UTC).
//! 3. Optionally forward to an upstream party.
//!
//! A separate workflow struct per process is defined below so that workflow
//! names, BDEW references, and deadline labels remain distinct.

use mako_engine::{
    deadline::Deadline,
    error::WorkflowError,
    ids::DeadlineId,
    workflow::{CommandPayload, EventPayload, Workflow, WorkflowOutput},
};
use serde::{Deserialize, Serialize};

// ── Generic events ─────────────────────────────────────────────────────────────

/// Events shared by all acknowledge-and-forward workflows.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum AckForwardEvent {
    /// XML document received.
    Received {
        /// MRID (UUID) of the received document.
        mrid: String,
        /// Document type string (e.g. `"Unavailability"`, `"Kaskade"`).
        doc_type: String,
        /// GLN of the sender.
        sender: String,
        /// GLN of the receiver.
        receiver: String,
        /// UTC receipt timestamp (ISO-8601).
        received_at: String,
    },
    /// `AcknowledgementDocument` dispatched within the 6h window.
    Acknowledged {
        /// MRID of the outbound `AcknowledgementDocument`.
        ack_mrid: String,
    },
    /// Document forwarded upstream (role-conditional).
    Forwarded {
        /// MRID of the forwarded document.
        upstream_mrid: String,
    },
    /// A registered deadline expired.
    DeadlineExpired {
        /// Unique ID of the expired deadline.
        deadline_id: DeadlineId,
        /// Label identifying the deadline.
        label: Box<str>,
    },
}

/// Commands shared by all acknowledge-and-forward workflows.
#[derive(Clone)]
pub enum AckForwardCommand {
    /// Inbound document received.
    Receive {
        /// MRID of the received document.
        mrid: String,
        /// Document type string.
        doc_type: String,
        /// Sender GLN.
        sender: String,
        /// Receiver GLN.
        receiver: String,
        /// UTC receipt timestamp.
        received_at: String,
    },
    /// `AcknowledgementDocument` dispatched.
    Acknowledge {
        /// MRID of the outbound `AcknowledgementDocument`.
        ack_mrid: String,
    },
    /// Document forwarded upstream.
    Forward {
        /// MRID of the forwarded document.
        upstream_mrid: String,
    },
    /// Deadline fired.
    TimeoutExpired {
        /// Unique ID of the expired deadline.
        deadline_id: DeadlineId,
        /// Label identifying the deadline.
        label: Box<str>,
    },
}

impl CommandPayload for AckForwardCommand {}

/// Core data captured on receipt.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceivedData {
    /// MRID of the received document.
    pub mrid: String,
    /// Document type string.
    pub doc_type: String,
    /// Sender GLN.
    pub sender: String,
    /// Receiver GLN.
    pub receiver: String,
    /// Receipt timestamp.
    pub received_at: String,
}

/// Generic state for acknowledge-and-forward workflows.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(tag = "status", content = "data")]
pub enum AckForwardState {
    /// No events yet.
    #[default]
    New,
    /// Document received; acknowledgement not yet sent.
    Received(ReceivedData),
    /// `AcknowledgementDocument` dispatched.
    Acknowledged(ReceivedData),
    /// Document forwarded upstream.
    Forwarded(ReceivedData),
    /// A registered deadline expired without acknowledgement.
    DeadlineExpired {
        /// Human-readable reason.
        reason: String,
    },
}

impl AckForwardState {
    /// Stable string label.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            Self::New => "New",
            Self::Received(_) => "Received",
            Self::Acknowledged(_) => "Acknowledged",
            Self::Forwarded(_) => "Forwarded",
            Self::DeadlineExpired { .. } => "DeadlineExpired",
        }
    }
}

impl EventPayload for AckForwardEvent {
    fn event_type(&self) -> &'static str {
        match self {
            Self::Received { .. } => "AckForwardReceived",
            Self::Acknowledged { .. } => "AckForwardAcknowledged",
            Self::Forwarded { .. } => "AckForwardForwarded",
            Self::DeadlineExpired { .. } => "AckForwardDeadlineExpired",
        }
    }
}

// ── Per-workflow event newtypes ────────────────────────────────────────────────
//
// Each workflow needs distinct event_type() strings so that event logs,
// projections, and observability tools can identify events unambiguously
// across all six ack-forward process families.
//
// The macro below generates a thin newtype `FooEvent(AckForwardEvent)` for each
// workflow, with `EventPayload::event_type()` returning prefixed names such as
// `"VerfuegbarkeitReceived"`.  All apply/handle logic delegates to the shared
// `AckForwardEvent` via `From<FooEvent> for AckForwardEvent`.

macro_rules! define_workflow_event {
    ($event_type:ident, $prefix:expr) => {
        /// Workflow-specific event newtype for one of the six ack-forward process
        /// families.
        ///
        /// Wraps [`AckForwardEvent`] and returns a workflow-specific prefix from
        /// [`EventPayload::event_type`] so events from different ack-forward
        /// workflows are distinguishable in projections and the event log.
        #[derive(Debug, Clone, Serialize, Deserialize)]
        #[serde(transparent)]
        pub struct $event_type(pub AckForwardEvent);

        impl From<AckForwardEvent> for $event_type {
            fn from(e: AckForwardEvent) -> Self {
                Self(e)
            }
        }

        impl From<$event_type> for AckForwardEvent {
            fn from(e: $event_type) -> AckForwardEvent {
                e.0
            }
        }

        impl EventPayload for $event_type {
            fn event_type(&self) -> &'static str {
                match &self.0 {
                    AckForwardEvent::Received { .. } => concat!($prefix, "Received"),
                    AckForwardEvent::Acknowledged { .. } => concat!($prefix, "Acknowledged"),
                    AckForwardEvent::Forwarded { .. } => concat!($prefix, "Forwarded"),
                    AckForwardEvent::DeadlineExpired { .. } => {
                        concat!($prefix, "DeadlineExpired")
                    }
                }
            }
        }
    };
}

define_workflow_event!(VerfuegbarkeitEvent, "Verfuegbarkeit");
define_workflow_event!(NetzengpassEvent, "Netzengpass");
define_workflow_event!(KaskadeEvent, "Kaskade");
define_workflow_event!(PlanungsdatenEvent, "Planungsdaten");
define_workflow_event!(StatusanfrageEvent, "Statusanfrage");
define_workflow_event!(KostenblattEvent, "Kostenblatt");

// ── Shared apply / handle logic ───────────────────────────────────────────────

/// Apply an `AckForwardEvent` to `AckForwardState`.
pub(crate) fn apply(state: AckForwardState, event: &AckForwardEvent) -> AckForwardState {
    match event {
        AckForwardEvent::Received {
            mrid,
            doc_type,
            sender,
            receiver,
            received_at,
        } => AckForwardState::Received(ReceivedData {
            mrid: mrid.clone(),
            doc_type: doc_type.clone(),
            sender: sender.clone(),
            receiver: receiver.clone(),
            received_at: received_at.clone(),
        }),

        AckForwardEvent::Acknowledged { .. } => match state {
            AckForwardState::Received(data) => AckForwardState::Acknowledged(data),
            other => other,
        },

        AckForwardEvent::Forwarded { .. } => match state {
            AckForwardState::Acknowledged(data) => AckForwardState::Forwarded(data),
            other => other,
        },

        AckForwardEvent::DeadlineExpired { label, .. } => AckForwardState::DeadlineExpired {
            reason: format!("deadline expired: {label}"),
        },
    }
}

/// Handle an `AckForwardCommand` against `AckForwardState`.
pub(crate) fn handle(
    state: &AckForwardState,
    command: AckForwardCommand,
    ack_window_label: &str,
) -> Result<WorkflowOutput<AckForwardEvent>, WorkflowError> {
    match command {
        AckForwardCommand::Receive {
            mrid,
            doc_type,
            sender,
            receiver,
            received_at,
        } => {
            if !matches!(state, AckForwardState::New) {
                return Ok(vec![].into());
            }
            Ok(vec![AckForwardEvent::Received {
                mrid,
                doc_type,
                sender,
                receiver,
                received_at,
            }]
            .into())
        }

        AckForwardCommand::Acknowledge { ack_mrid } => match state {
            AckForwardState::Received(_) => {
                Ok(vec![AckForwardEvent::Acknowledged { ack_mrid }].into())
            }
            AckForwardState::Acknowledged(_) | AckForwardState::Forwarded(_) => Ok(vec![].into()),
            other => Err(WorkflowError::rejected(format!(
                "Acknowledge not valid in state {}",
                other.label()
            ))),
        },

        AckForwardCommand::Forward { upstream_mrid } => match state {
            AckForwardState::Acknowledged(_) => {
                Ok(vec![AckForwardEvent::Forwarded { upstream_mrid }].into())
            }
            AckForwardState::Forwarded(_) => Ok(vec![].into()),
            other => Err(WorkflowError::rejected(format!(
                "Forward not valid in state {}",
                other.label()
            ))),
        },

        AckForwardCommand::TimeoutExpired { deadline_id, label } => match state {
            AckForwardState::Acknowledged(_)
            | AckForwardState::Forwarded(_)
            | AckForwardState::DeadlineExpired { .. } => Ok(vec![].into()),
            _ => {
                let _ = ack_window_label; // used by caller for label registration
                Ok(vec![AckForwardEvent::DeadlineExpired { deadline_id, label }].into())
            }
        },
    }
}

// ── Per-process workflow structs ───────────────────────────────────────────────

macro_rules! ack_forward_workflow {
    (
        $(#[$meta:meta])*
        $name:ident,
        $event_newtype:ident,
        $workflow_name:expr,
        $ack_label:expr,
        $event_prefix:expr $(,)?
    ) => {
        $(#[$meta])*
        pub struct $name;

        impl Workflow for $name {
            type State   = AckForwardState;
            type Event   = $event_newtype;
            type Command = AckForwardCommand;

            fn on_deadline(
                deadline: &Deadline,
                state: &Self::State,
            ) -> Option<Self::Command> {
                if deadline.label() == $ack_label {
                    if matches!(state, AckForwardState::Received(_)) {
                        return Some(AckForwardCommand::TimeoutExpired {
                            deadline_id: deadline.deadline_id(),
                            label: deadline.label().into(),
                        });
                    }
                }
                None
            }

            fn apply(state: Self::State, event: &Self::Event) -> Self::State {
                crate::ack_forward::apply(state, &event.0)
            }

            fn handle(
                state: &Self::State,
                command: Self::Command,
            ) -> Result<WorkflowOutput<Self::Event>, WorkflowError> {
                let output = crate::ack_forward::handle(state, command, $ack_label)?;
                Ok(WorkflowOutput::with_outbox(
                    output.events.into_iter().map($event_newtype).collect(),
                    output.outbox,
                ))
            }
        }

        impl $name {
            /// Return the event-type prefix for this workflow's events.
            #[must_use]
            pub fn event_prefix() -> &'static str {
                $event_prefix
            }
        }
    };
}

ack_forward_workflow!(
    /// Verfügbarkeitsmeldung workflow — `UnavailabilityMarketDocument`.
    ///
    /// ANB → VNB. Receiver acknowledges within 6h (BK6-20-059 §4.3).
    VerfuegbarkeitWorkflow,
    VerfuegbarkeitEvent,
    "redispatch-verfuegbarkeit",
    "redispatch-verfuegbarkeit-ack-window",
    "Verfuegbarkeit",
);

ack_forward_workflow!(
    /// Netzengpassinformation workflow — `NetworkConstraintDocument`.
    ///
    /// ÜNB ↔ VNB. Receiver acknowledges within 6h (BK6-20-059 §4.3).
    NetzengpassWorkflow,
    NetzengpassEvent,
    "redispatch-netzengpass",
    "redispatch-netzengpass-ack-window",
    "Netzengpass",
);

ack_forward_workflow!(
    /// Kaskade workflow — emergency measures per § 13 Abs. 2 `EnWG`.
    ///
    /// ÜNB → VNB → ANB. Receiver acknowledges within 6h (BK6-20-059 §4.3).
    /// Only active for `Marktrolle::Nb` and `Marktrolle::Unb` deployments.
    KaskadeWorkflow,
    KaskadeEvent,
    "redispatch-kaskade",
    "redispatch-kaskade-ack-window",
    "Kaskade",
);

ack_forward_workflow!(
    /// Planungsdaten (Abruffahrplan) workflow — `PlannedResourceScheduleDocument`.
    ///
    /// ÜNB → VNB → ANB. Receiver acknowledges within 6h (BK6-20-059 §4.3).
    PlanungsdatenWorkflow,
    PlanungsdatenEvent,
    "redispatch-planungsdaten",
    "redispatch-planungsdaten-ack-window",
    "Planungsdaten",
);

ack_forward_workflow!(
    /// Statusanfrage workflow — `StatusRequest_MarketDocument`.
    ///
    /// Addressed party responds within 24h (BK6-20-059 §4.4).
    StatusanfrageWorkflow,
    StatusanfrageEvent,
    "redispatch-statusanfrage",
    "redispatch-statusanfrage-response-window",
    "Statusanfrage",
);

ack_forward_workflow!(
    /// Kostenblatt workflow — monthly cost reconciliation.
    ///
    /// VNB → ÜNB. Receiver acknowledges within 6h (BK6-20-059 §4.3).
    /// VNB submits by the 15th of the following month (BK6-20-061 §7).
    KostenblattWorkflow,
    KostenblattEvent,
    "redispatch-kostenblatt",
    "redispatch-kostenblatt-ack-window",
    "Kostenblatt",
);

/// Workflow name constants for each process.
/// Workflow name constants for each process in the acknowledge-and-forward family.
pub mod names {
    /// Workflow name for `VerfuegbarkeitWorkflow`.
    pub const VERFUEGBARKEIT: &str = "redispatch-verfuegbarkeit";
    /// Workflow name for `NetzengpassWorkflow`.
    pub const NETZENGPASS: &str = "redispatch-netzengpass";
    /// Workflow name for `KaskadeWorkflow`.
    pub const KASKADE: &str = "redispatch-kaskade";
    /// Workflow name for `PlanungsdatenWorkflow`.
    pub const PLANUNGSDATEN: &str = "redispatch-planungsdaten";
    /// Workflow name for `StatusanfrageWorkflow`.
    pub const STATUSANFRAGE: &str = "redispatch-statusanfrage";
    /// Workflow name for `KostenblattWorkflow`.
    pub const KOSTENBLATT: &str = "redispatch-kostenblatt";
}

#[cfg(test)]
mod tests {
    use super::*;
    use mako_engine::workflow::EventPayload;

    #[test]
    fn verfuegbarkeit_receive_to_acknowledged() {
        let state = AckForwardState::New;
        let output = VerfuegbarkeitWorkflow::handle(
            &state,
            AckForwardCommand::Receive {
                mrid: "m1".into(),
                doc_type: "Unavailability".into(),
                sender: "s".into(),
                receiver: "r".into(),
                received_at: "2025-10-15T10:00:00Z".into(),
            },
        )
        .unwrap();
        assert_eq!(output.events.len(), 1);

        let state2 = VerfuegbarkeitWorkflow::apply(state, &output.events[0]);
        assert!(matches!(state2, AckForwardState::Received(_)));

        let output2 = VerfuegbarkeitWorkflow::handle(
            &state2,
            AckForwardCommand::Acknowledge {
                ack_mrid: "ack-1".into(),
            },
        )
        .unwrap();
        let state3 = VerfuegbarkeitWorkflow::apply(state2, &output2.events[0]);
        assert!(matches!(state3, AckForwardState::Acknowledged(_)));
    }

    #[test]
    fn kaskade_forward_requires_acknowledged_state() {
        let state = AckForwardState::Received(ReceivedData {
            mrid: "m".into(),
            doc_type: "Kaskade".into(),
            sender: "s".into(),
            receiver: "r".into(),
            received_at: "2025-10-15T10:00:00Z".into(),
        });
        let result = KaskadeWorkflow::handle(
            &state,
            AckForwardCommand::Forward {
                upstream_mrid: "u".into(),
            },
        );
        assert!(result.is_err());
    }

    /// Verify that each workflow's event types are unique and correctly prefixed.
    #[test]
    fn event_types_are_unique_per_workflow() {
        let inner = AckForwardEvent::Received {
            mrid: "m".into(),
            doc_type: "X".into(),
            sender: "s".into(),
            receiver: "r".into(),
            received_at: "t".into(),
        };

        let types: Vec<&'static str> = vec![
            VerfuegbarkeitEvent(inner.clone()).event_type(),
            NetzengpassEvent(inner.clone()).event_type(),
            KaskadeEvent(inner.clone()).event_type(),
            PlanungsdatenEvent(inner.clone()).event_type(),
            StatusanfrageEvent(inner.clone()).event_type(),
            KostenblattEvent(inner.clone()).event_type(),
        ];

        // All event types must be distinct.
        let unique: std::collections::HashSet<_> = types.iter().collect();
        assert_eq!(
            unique.len(),
            types.len(),
            "event_type() strings must be unique across all ack-forward workflows: {types:?}"
        );

        // All event types must be prefixed (not generic "AckForward…" names).
        for t in &types {
            assert!(
                !t.starts_with("AckForward"),
                "event_type '{t}' must not use the generic AckForward prefix"
            );
        }
    }
}