rutmf 0.1.0

Ergonomic, v5-first Rust types and clients for the TM Forum Open API domain model (unofficial)
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
//! The six alarm tasks — TMF642.
//!
//! Acknowledging, clearing, commenting on, grouping and ungrouping are
//! collections you `POST` to, not fields you `PATCH`. Each has a read model and
//! a `…Create`, and deliberately **no** `…Update`: TMF642 defines `POST` and
//! `GET` on these paths and nothing else, so a type that could be sent to a
//! `PATCH` would be a type no endpoint accepts.
//!
//! # Why these are written out rather than generated
//!
//! The six look alike and are not. Four carry an `alarmPattern` for bulk
//! action; the two grouping tasks do not, because a group is defined by naming
//! its members rather than by matching them. Each `_FVO` requires a different
//! set. A macro that assumed the common shape produced types the specification
//! does not define — so the field lists are written per task, and
//! `tests/coverage.rs` is what checks them.

use serde::{Deserialize, Serialize};

use crate::core::macros::{tmf_entity, tmf_struct};
use crate::core::{Ref, Timestamp};

use super::{Alarm, Comment};

/// How far one of the six alarm tasks has got.
///
/// All six share this vocabulary, which is not the same as an alarm's own
/// [`AlarmState`](super::AlarmState) — a task can be `done` while the alarm it
/// acted on is still `raised`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum AlarmTaskState {
    /// Accepted and queued.
    #[serde(rename = "acknowledged")]
    Acknowledged,
    /// Being carried out.
    #[serde(rename = "inProgress")]
    InProgress,
    /// Carried out.
    #[serde(rename = "done")]
    Done,
    /// Asked to stop.
    #[serde(rename = "cancel")]
    Cancel,
    /// Stopped before completing.
    #[serde(rename = "canceled")]
    Canceled,
    /// Failed.
    #[serde(rename = "terminatedWithError")]
    TerminatedWithError,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

impl AlarmTaskState {
    /// Whether the task has stopped moving.
    ///
    /// An unrecognised state is **not** finished, so a client polling a task
    /// keeps polling rather than giving up on a state it does not know.
    #[must_use]
    pub fn is_finished(&self) -> bool {
        matches!(
            self,
            Self::Done | Self::Canceled | Self::TerminatedWithError
        )
    }
}

tmf_struct! {
    @name = "AckAlarm";
    /// A request to acknowledge one or more alarms.
    pub struct AckAlarm {
        /// Server-assigned identifier of the task.
        id: String,
        /// Canonical URI of the task.
        href: String,
        /// How far the task has got.
        state: AlarmTaskState,
        /// A filter selecting the alarms to act on, for bulk requests.
        alarm_pattern: Vec<Ref<Alarm>>,
        /// The alarms this request acknowledged.
        acked_alarm: Vec<Ref<Alarm>>,
        /// When they were acknowledged.
        ack_time: Timestamp,
        /// The system that acknowledged them.
        ack_system_id: String,
        /// The user who acknowledged them.
        ack_user_id: String,
    }
}

tmf_struct! {
    @name = "AckAlarm";
    /// Body of a `POST /ackAlarm` — the v5 `AckAlarm_FVO`.
    ///
    /// There is no matching update type: TMF642 defines `POST` and `GET` on
    /// this collection and nothing else.
    pub struct AckAlarmCreate {
        @required {
            /// Which alarms to acknowledge. **Required.**
            alarm_pattern: Vec<Ref<Alarm>>,
            /// The system acknowledging them. **Required.**
            ack_system_id: String,
            /// The user acknowledging them. **Required.**
            ack_user_id: String,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// The state to open the task in.
        state: AlarmTaskState,
        /// The alarms acknowledged.
        acked_alarm: Vec<Ref<Alarm>>,
        /// When they were acknowledged.
        ack_time: Timestamp,
    }
}

tmf_struct! {
    @name = "UnAckAlarm";
    /// A request to withdraw an acknowledgement.
    pub struct UnAckAlarm {
        /// Server-assigned identifier of the task.
        id: String,
        /// Canonical URI of the task.
        href: String,
        /// How far the task has got.
        state: AlarmTaskState,
        /// A filter selecting the alarms to act on.
        alarm_pattern: Vec<Ref<Alarm>>,
        /// The alarms this request un-acknowledged.
        un_acked_alarm: Vec<Ref<Alarm>>,
        /// When the acknowledgement was withdrawn.
        ack_time: Timestamp,
        /// The system that withdrew it.
        ack_system_id: String,
        /// The user who withdrew it.
        ack_user_id: String,
    }
}

tmf_struct! {
    @name = "UnAckAlarm";
    /// Body of a `POST /unAckAlarm` — the v5 `UnAckAlarm_FVO`.
    pub struct UnAckAlarmCreate {
        @required {
            /// Which alarms to un-acknowledge. **Required.**
            alarm_pattern: Vec<Ref<Alarm>>,
            /// The system withdrawing the acknowledgement. **Required.**
            ack_system_id: String,
            /// The user withdrawing it. **Required.**
            ack_user_id: String,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// The state to open the task in.
        state: AlarmTaskState,
        /// The alarms un-acknowledged.
        un_acked_alarm: Vec<Ref<Alarm>>,
        /// When the acknowledgement was withdrawn.
        ack_time: Timestamp,
    }
}

tmf_struct! {
    @name = "ClearAlarm";
    /// A request to clear one or more alarms.
    ///
    /// Clearing is a task rather than a severity edit because it is an
    /// *assertion by an operator* that the condition is gone — which the
    /// network may disagree with by raising the alarm again.
    pub struct ClearAlarm {
        /// Server-assigned identifier of the task.
        id: String,
        /// Canonical URI of the task.
        href: String,
        /// How far the task has got.
        state: AlarmTaskState,
        /// A filter selecting the alarms to act on.
        alarm_pattern: Vec<Ref<Alarm>>,
        /// The alarms this request cleared.
        cleared_alarm: Vec<Ref<Alarm>>,
        /// When they were cleared.
        alarm_cleared_time: Timestamp,
        /// The system that cleared them.
        clear_system_id: String,
        /// The user who cleared them.
        clear_user_id: String,
    }
}

tmf_struct! {
    @name = "ClearAlarm";
    /// Body of a `POST /clearAlarm` — the v5 `ClearAlarm_FVO`.
    pub struct ClearAlarmCreate {
        @required {
            /// Which alarms to clear. **Required.**
            alarm_pattern: Vec<Ref<Alarm>>,
            /// When they were cleared. **Required.**
            alarm_cleared_time: Timestamp,
            /// The system clearing them. **Required.**
            clear_system_id: String,
            /// The user clearing them. **Required.**
            clear_user_id: String,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// The state to open the task in.
        state: AlarmTaskState,
        /// The alarms cleared.
        cleared_alarm: Vec<Ref<Alarm>>,
    }
}

tmf_struct! {
    @name = "CommentAlarm";
    /// A request to add a comment to one or more alarms.
    pub struct CommentAlarm {
        /// Server-assigned identifier of the task.
        id: String,
        /// Canonical URI of the task.
        href: String,
        /// How far the task has got.
        state: AlarmTaskState,
        /// A filter selecting the alarms to act on.
        alarm_pattern: Vec<Ref<Alarm>>,
        /// The alarms this request commented on.
        commented_alarm: Vec<Ref<Alarm>>,
        /// The comment added.
        comment: Comment,
    }
}

tmf_struct! {
    @name = "CommentAlarm";
    /// Body of a `POST /commentAlarm` — the v5 `CommentAlarm_FVO`.
    pub struct CommentAlarmCreate {
        @required {
            /// Which alarms to comment on. **Required.**
            alarm_pattern: Vec<Ref<Alarm>>,
            /// The comment to add. **Required.**
            comment: Comment,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// The state to open the task in.
        state: AlarmTaskState,
        /// The alarms commented on.
        commented_alarm: Vec<Ref<Alarm>>,
    }
}

tmf_struct! {
    @name = "GroupAlarm";
    /// A request to group alarms under a parent, for correlation.
    ///
    /// Note the absence of an `alarmPattern`: a group is defined by naming its
    /// members, not by matching them.
    pub struct GroupAlarm {
        /// Server-assigned identifier of the task.
        id: String,
        /// Canonical URI of the task.
        href: String,
        /// How far the task has got.
        state: AlarmTaskState,
        /// The alarms being grouped.
        grouped_alarm: Vec<Ref<Alarm>>,
        /// The alarms correlated by the grouping.
        correlated_alarm: Vec<Ref<Alarm>>,
        /// The alarm they are grouped under.
        parent_alarm: Ref<Alarm>,
        /// When the grouping happened.
        alarm_changed_time: Timestamp,
        /// The system that grouped them.
        source_system_id: String,
    }
}

tmf_struct! {
    @name = "GroupAlarm";
    /// Body of a `POST /groupAlarm` — the v5 `GroupAlarm_FVO`.
    pub struct GroupAlarmCreate {
        @required {
            /// The alarms to correlate. **Required.**
            correlated_alarm: Vec<Ref<Alarm>>,
            /// The alarm to group them under. **Required.**
            parent_alarm: Ref<Alarm>,
            /// When the grouping happened. **Required.**
            alarm_changed_time: Timestamp,
            /// The system grouping them. **Required.**
            source_system_id: String,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// The state to open the task in.
        state: AlarmTaskState,
        /// The alarms grouped.
        grouped_alarm: Vec<Ref<Alarm>>,
    }
}

tmf_struct! {
    @name = "UnGroupAlarm";
    /// A request to break a correlation group apart.
    pub struct UnGroupAlarm {
        /// Server-assigned identifier of the task.
        id: String,
        /// Canonical URI of the task.
        href: String,
        /// How far the task has got.
        state: AlarmTaskState,
        /// The alarms being removed from the group.
        un_grouped_alarm: Vec<Ref<Alarm>>,
        /// The alarms that were correlated.
        correlated_alarm: Vec<Ref<Alarm>>,
        /// The alarm they were grouped under.
        parent_alarm: Ref<Alarm>,
        /// When the grouping was broken.
        alarm_changed_time: Timestamp,
        /// The system that broke it.
        source_system_id: String,
    }
}

tmf_struct! {
    @name = "UnGroupAlarm";
    /// Body of a `POST /unGroupAlarm` — the v5 `UnGroupAlarm_FVO`.
    pub struct UnGroupAlarmCreate {
        @required {
            /// The alarms to un-correlate. **Required.**
            correlated_alarm: Vec<Ref<Alarm>>,
            /// The alarm they were grouped under. **Required.**
            parent_alarm: Ref<Alarm>,
            /// When the grouping was broken. **Required.**
            alarm_changed_time: Timestamp,
            /// The system breaking it. **Required.**
            source_system_id: String,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// The state to open the task in.
        state: AlarmTaskState,
        /// The alarms removed from the group.
        un_grouped_alarm: Vec<Ref<Alarm>>,
    }
}

tmf_entity!(
    AckAlarm,
    UnAckAlarm,
    ClearAlarm,
    CommentAlarm,
    GroupAlarm,
    UnGroupAlarm
);

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

    #[test]
    fn an_unknown_task_state_is_not_finished() {
        // A client polling a task must keep polling through a state it does not
        // recognise; treating unknown as finished would report an operation as
        // complete on the strength of not understanding it.
        let unknown = AlarmTaskState::Other("awaitingApproval".into());
        assert!(!unknown.is_finished());
        assert!(!AlarmTaskState::InProgress.is_finished());
        assert!(AlarmTaskState::TerminatedWithError.is_finished());
    }

    #[test]
    fn task_states_round_trip_their_wire_spelling() {
        for (state, wire) in [
            (AlarmTaskState::InProgress, "inProgress"),
            (AlarmTaskState::TerminatedWithError, "terminatedWithError"),
            (AlarmTaskState::Canceled, "canceled"),
        ] {
            assert_eq!(
                serde_json::to_string(&state).unwrap(),
                format!("\"{wire}\"")
            );
            assert_eq!(
                serde_json::from_str::<AlarmTaskState>(&format!("\"{wire}\"")).unwrap(),
                state
            );
        }
    }

    #[test]
    fn a_task_carries_a_pattern_for_bulk_action() {
        // The reason four of these are collections rather than a `PATCH`: one
        // request acts on everything matching, instead of a loop.
        let ack = AckAlarmCreate::builder()
            .alarm_pattern(vec![Ref::new("alarm-1"), Ref::new("alarm-2")])
            .ack_system_id("noc")
            .ack_user_id("operator")
            .build();

        let json = serde_json::to_value(&ack).unwrap();
        assert_eq!(json["alarmPattern"].as_array().unwrap().len(), 2);
        assert_eq!(json["@type"], "AckAlarm");
    }

    #[test]
    fn grouping_names_its_members_rather_than_matching_them() {
        // `GroupAlarm` has no `alarmPattern`, which is why the six tasks are
        // written out rather than generated from one shape.
        let group = GroupAlarmCreate::builder()
            .correlated_alarm(vec![Ref::new("a-1")])
            .parent_alarm(Ref::new("a-root"))
            .alarm_changed_time(
                "2026-08-27T00:00:00Z"
                    .parse::<crate::core::Timestamp>()
                    .unwrap(),
            )
            .source_system_id("correlator")
            .build();

        let json = serde_json::to_value(&group).unwrap();
        assert!(json.get("alarmPattern").is_none());
        assert_eq!(json["parentAlarm"]["id"], "a-root");
    }

    #[test]
    fn every_task_declares_its_own_discriminator() {
        use crate::core::TmfType;
        assert_eq!(AckAlarm::TYPE_NAME, "AckAlarm");
        assert_eq!(UnAckAlarm::TYPE_NAME, "UnAckAlarm");
        assert_eq!(ClearAlarm::TYPE_NAME, "ClearAlarm");
        assert_eq!(CommentAlarm::TYPE_NAME, "CommentAlarm");
        assert_eq!(GroupAlarm::TYPE_NAME, "GroupAlarm");
        assert_eq!(UnGroupAlarm::TYPE_NAME, "UnGroupAlarm");
    }
}