rutmf 0.2.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! The resource domain: what the network is made of.
//!
//! Mirrors **TMF639 Resource Inventory Management v5.0.0**. A [`Resource`] is a
//! physical or logical component of the infrastructure — a port, a SIM, a
//! virtual machine, a licence. Services run on resources, and products are
//! realised by services, which is the chain
//! [`Product`](crate::product::Product) → [`Service`](crate::service::Service)
//! → [`Resource`] this crate models end to end.
//!
//! # Nine status dimensions, not one
//!
//! Where most TM Forum resources have a single `state`, TMF639 inherits the
//! TMN/ITU-T X.731 state model and splits status across nine independent
//! members: [`ResourceOperationalState`], [`ResourceUsageState`],
//! [`ResourceAdministrativeState`], [`ResourceLifecycleState`],
//! [`ResourceAlarmStatus`], [`ResourceProceduralStatus`],
//! [`ResourceAvailabilityStatus`], [`ResourceControlStatus`] and
//! `allocation_status`. They are orthogonal: a resource can be
//! administratively `locked`, operationally `enabled` and availability
//! `degraded` at the same time. Each is its own type, so none can be assigned
//! to another.
//!
//! `allocationStatus` is the odd one out — TMF639 describes its values
//! (`available`, `reserved`, `allocated`, `partiallyAllocated`) in prose but
//! declares no enumeration, so it stays a [`String`].
//!
//! # One type for four subclasses
//!
//! TMF639 sub-types `Resource` twice over: `LogicalResource` and
//! `PhysicalResource` extend it, and `ResourceFunction` and `SoftwareResource`
//! extend `LogicalResource` in turn. Rather than four near-identical structs,
//! [`Resource`] carries the union of their members and [`ResourceKind`] recovers
//! which one a server sent — the same call [`ResourceSpecification`] makes on
//! the catalog side, so the two halves of the domain read alike.
//!
//! A server that sends a vendor subclass parses as [`ResourceKind::Other`]
//! rather than failing.
//!
//! # What is wired to what
//!
//! A `ResourceFunction`'s internal connectivity is a [`ResourceGraph`]:
//! [`Connection`] edges over [`Endpoint`] vertices. That is the instance
//! counterpart of [`ResourceGraphSpecification`] — the specification says what
//! *may* be wired together, the graph says what *is*.
//!
//! # There is no patch body
//!
//! TMF639 declares no `Resource_MVO`. Its `PATCH /resource/{id}` takes the
//! plain `Resource` schema, so [`Resource`] is *itself* the patch body — the
//! only resource in this crate where the read model and the update model are
//! one type. See [`ResourceUpdate`], which is an alias rather than a struct.

use serde::{Deserialize, Serialize};

use crate::core::macros::{tmf_entity, tmf_patch_body, tmf_struct};
use crate::core::{
    Attachment, Characteristic, ConnectionPoint, ExternalIdentifier, Intent, ItemAction, Note,
    Place, Ref, RelatedParty, Schedule, TimePeriod, Timestamp,
};

mod catalog;
mod feature;
mod specification;
mod topology;

pub use catalog::{
    ResourceCandidate, ResourceCandidateCreate, ResourceCandidateUpdate, ResourceCatalog,
    ResourceCatalogCreate, ResourceCatalogUpdate, ResourceCategory, ResourceCategoryCreate,
    ResourceCategoryUpdate,
};
pub use feature::{Feature, FeatureRelationship};
pub use specification::{
    ConnectionAssociationType, ConnectionSpecification, FeatureSpecification,
    FeatureSpecificationRelationship, ResourceGraphSpecification,
    ResourceGraphSpecificationRelationship, ResourceSpecification, ResourceSpecificationCreate,
    ResourceSpecificationKind, ResourceSpecificationRelationship, ResourceSpecificationUpdate,
    TargetResourceSchema,
};
pub use topology::{Connection, Endpoint, ResourceGraph, ResourceGraphRelationship};

tmf_struct! {
    @name = "Resource", @ref = "ResourceRef";
    /// A physical or logical component of the infrastructure.
    ///
    /// This is the **read model** of TMF639 Resource Inventory. Use
    /// [`ResourceCreate`] for `POST`, and [`Resource`] itself for `PATCH` —
    /// see the [module docs](self) for why.
    ///
    /// The `supporting_resource` member is the v5 `ResourceRefOrValue`, a
    /// `oneOf` over this type and a bare reference to one; the reference form
    /// is this type carrying only `id`/`href`.
    ///
    /// ```
    /// use rutmf::resource::{Resource, ResourceAdministrativeState, ResourceOperationalState};
    ///
    /// let json = r#"{
    ///   "id": "3472",
    ///   "name": "Optical port 3/1/2",
    ///   "operationalState": "enabled",
    ///   "administrativeState": "locked",
    ///   "@type": "Resource"
    /// }"#;
    /// let port: Resource = serde_json::from_str(json).unwrap();
    ///
    /// // The dimensions are independent, and typed apart.
    /// assert_eq!(port.operational_state, Some(ResourceOperationalState::Enabled));
    /// assert_eq!(port.administrative_state, Some(ResourceAdministrativeState::Locked));
    /// ```
    pub struct Resource {
        /// Server-assigned identifier.
        id: String,
        /// Canonical URI of this resource.
        href: String,
        /// Name the resource is known by.
        name: String,
        /// Narrative description.
        description: String,
        /// Category the resource falls in.
        category: String,
        /// Version of the resource, distinct from its specification's version.
        resource_version: String,
        /// When the resource record was created.
        creation_date: Timestamp,
        /// When the resource started operating.
        start_operating_date: Timestamp,
        /// When the resource stopped operating.
        end_operating_date: Timestamp,
        /// Period during which the resource record is valid.
        valid_for: TimePeriod,
        /// Whether the resource is able to perform its function.
        operational_state: ResourceOperationalState,
        /// Whether the resource is in use.
        usage_state: ResourceUsageState,
        /// Whether an operator has permitted the resource to be used.
        administrative_state: ResourceAdministrativeState,
        /// Where the resource is in its plan-install-remove lifecycle.
        lifecycle_state: ResourceLifecycleState,
        /// Severities of any outstanding alarms — TMF639 permits several.
        alarm_status: Vec<ResourceAlarmStatus>,
        /// Where the resource is in its initialisation procedure.
        procedural_status: ResourceProceduralStatus,
        /// Why the resource is unavailable, when it is.
        availability_status: ResourceAvailabilityStatus,
        /// Operator-imposed restriction on the resource.
        control_status: ResourceControlStatus,
        /// How much of the resource is spoken for — `available`, `reserved`,
        /// `allocated`, `partiallyAllocated`. TMF639 names these values in
        /// prose but declares no enumeration, so this is a free string.
        allocation_status: String,
        /// Whether the status of the resource could not be determined.
        unknown_status: bool,
        /// The specification this resource conforms to.
        resource_specification: Ref<ResourceSpecification>,
        /// Configurable features of the resource.
        activation_feature: Vec<Feature>,
        /// Characteristics of the resource.
        resource_characteristic: Vec<Characteristic>,
        /// Relationships to other resources.
        resource_relationship: Vec<ResourceRelationship>,
        /// Resources this one is built on — inline or by reference.
        supporting_resource: Vec<Resource>,
        /// Order lines that acted on this resource — TMF652.
        resource_order_item: Vec<RelatedResourceOrderItem>,
        /// Places the resource is installed at.
        place: Vec<RelatedPlace>,
        /// Parties related to the resource.
        related_party: Vec<RelatedParty>,
        /// Identifiers for the resource in external systems.
        external_identifier: Vec<ExternalIdentifier>,
        /// Documents attached to the resource.
        attachment: Vec<Attachment>,
        /// Intent governing the resource — TMF921.
        intent: Ref<Intent>,
        /// Free-form notes.
        note: Vec<Note>,

        /// The value the resource carries — `LogicalResource` and below.
        ///
        /// A logical resource *is* its value: an MSISDN, an IP address, a TPE.
        value: String,

        /// When the item was manufactured — `PhysicalResource`.
        manufacture_date: Timestamp,
        /// Whether the item is serving or standing by — `PhysicalResource`.
        standby_status: ResourceStandbyStatus,
        /// Current power status of the hardware — `PhysicalResource`.
        ///
        /// TMF639 describes this member's values in prose without declaring an
        /// enumeration, so it stays a [`String`] — the same call
        /// `allocation_status` gets, and for the same reason.
        power_state: String,
        /// The item's power-consumption policy — `PhysicalResource`.
        power_consuming_state: ResourcePowerConsumingState,
        /// How much power the item is drawing — `PhysicalResource`.
        power_consuming_level: i64,
        /// Manufacturer's serial number — `PhysicalResource`.
        serial_number: String,
        /// Manufacturing batch — `PhysicalResource`.
        batch_number: String,
        /// Hardware version — `PhysicalResource`.
        version_number: String,

        /// Service access points for the function's inputs and outputs —
        /// `ResourceFunction`.
        connection_point: Vec<Ref<ConnectionPoint>>,
        /// Internal wiring of the contained functions — `ResourceFunction`.
        connectivity: Vec<ResourceGraph>,
        /// Ranking against other functions — `ResourceFunction`.
        priority: i64,
        /// The part the function plays — `ResourceFunction`.
        role: String,
        /// What the function does — `ResourceFunction`.
        function_type: String,
        /// Characteristics the function may change by itself —
        /// `ResourceFunction`.
        auto_modification: Vec<Characteristic>,
        /// Schedules the function runs to — `ResourceFunction`.
        schedule: Vec<Ref<Schedule>>,

        /// When the software resource was last changed — `SoftwareResource`.
        last_update: Timestamp,
        /// Whether the software is currently spread across several nodes —
        /// `SoftwareResource`.
        is_distributed_current: bool,
        /// The platform the software is deployed on — `SoftwareResource`.
        target_platform: String,
        @renamed {
            /// The concrete class of the resource, when this is the reference
            /// form of `ResourceRefOrValue`.
            "@referredType" referred_type: String,
        }
    }
}

impl Resource {
    /// Recovers the subclass implied by `@type`.
    ///
    /// ```
    /// use rutmf::resource::{Resource, ResourceKind};
    ///
    /// let json = r#"{"@type":"PhysicalResource","serialNumber":"SN-1","batchNumber":"B7"}"#;
    /// let resource: Resource = serde_json::from_str(json).unwrap();
    ///
    /// assert_eq!(resource.kind(), ResourceKind::Physical);
    /// assert_eq!(resource.serial_number.as_deref(), Some("SN-1"));
    /// ```
    #[must_use]
    pub fn kind(&self) -> ResourceKind {
        ResourceKind::from_type_name(self.type_name())
    }
}

/// The subclass of a [`Resource`], recovered from its `@type`.
///
/// TMF639 sub-types `Resource` twice over: `LogicalResource` and
/// `PhysicalResource` extend it, and `ResourceFunction` and `SoftwareResource`
/// extend `LogicalResource` in turn. Rather than four near-identical Rust
/// structs, [`Resource`] carries the union of their members and this recovers
/// which one a server sent — the same call
/// [`ResourceSpecificationKind`] makes on the catalog side, so the instance and
/// specification halves of the domain read alike.
///
/// [`Other`](Self::Other) means a vendor subclass this crate does not know,
/// which parses rather than failing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ResourceKind {
    /// The abstract base, carrying no subclass-specific member.
    Base,
    /// A logical resource: adds `value`.
    Logical,
    /// A physical resource: serial and batch numbers, power and standby status.
    Physical,
    /// A network function: connection points, internal connectivity, schedule.
    ///
    /// A `LogicalResource` in the schema hierarchy, so it carries `value` too.
    Function,
    /// A software resource: target platform and distribution state.
    ///
    /// Also a `LogicalResource`, and likewise carries `value`.
    Software,
    /// A subclass this crate does not know.
    Other,
}

impl ResourceKind {
    /// Every subclass the v5 documents declare, base first.
    ///
    /// Excludes [`Other`](Self::Other), which stands for a class the documents
    /// do *not* declare. Checked against the specification's own
    /// `discriminator.mapping` by `every_subclass_enumeration_is_the_declared_mapping`
    /// in `tests/coverage.rs`.
    #[must_use]
    pub fn all() -> &'static [Self] {
        &[
            Self::Base,
            Self::Logical,
            Self::Physical,
            Self::Function,
            Self::Software,
        ]
    }

    /// Maps a `@type` value to its kind; unknown names become [`Self::Other`].
    #[must_use]
    pub fn from_type_name(name: &str) -> Self {
        match name {
            "Resource" => Self::Base,
            "LogicalResource" => Self::Logical,
            "PhysicalResource" => Self::Physical,
            "ResourceFunction" => Self::Function,
            "SoftwareResource" => Self::Software,
            _ => Self::Other,
        }
    }

    /// The canonical `@type` for this kind.
    ///
    /// [`Self::Other`] has no canonical name and maps to the abstract base.
    #[must_use]
    pub fn type_name(self) -> &'static str {
        match self {
            Self::Base | Self::Other => "Resource",
            Self::Logical => "LogicalResource",
            Self::Physical => "PhysicalResource",
            Self::Function => "ResourceFunction",
            Self::Software => "SoftwareResource",
        }
    }

    /// Whether this kind is a `LogicalResource` or one of its subclasses.
    ///
    /// TMF639's hierarchy is two levels deep, so "is this logical" is not the
    /// same question as "is `@type` exactly `LogicalResource`".
    #[must_use]
    pub fn is_logical(self) -> bool {
        matches!(self, Self::Logical | Self::Function | Self::Software)
    }
}

/// Whether a physical resource is serving or standing by (ITU-T X.731).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceStandbyStatus {
    /// Standing by, ready to take over immediately.
    #[serde(rename = "hotStandby")]
    HotStandby,
    /// Standing by, and needs initialising before it can take over.
    #[serde(rename = "coldStandby")]
    ColdStandby,
    /// Currently doing the work.
    #[serde(rename = "providingService")]
    ProvidingService,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// The power-consumption policy of a physical resource (ITU-T M.3701).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourcePowerConsumingState {
    /// Drawing full power.
    #[serde(rename = "fullPower")]
    FullPower,
    /// Drawing reduced power.
    #[serde(rename = "powerSaving")]
    PowerSaving,
    /// Suspended, and wakeable.
    #[serde(rename = "sleeping")]
    Sleeping,
    /// Powered down.
    #[serde(rename = "shutdown")]
    Shutdown,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

tmf_entity!(Resource);
tmf_patch_body!(Resource);

/// Body of a `PATCH /resource/{id}`.
///
/// TMF639 declares no `Resource_MVO`: its patch operation takes the plain
/// `Resource` schema. This alias exists so the naming stays uniform across the
/// crate, and to make the absence deliberate rather than an oversight — but it
/// really is the same type, and `id` and `href` really are on it.
pub type ResourceUpdate = Resource;

tmf_struct! {
    @name = "Resource";
    /// Body of a `POST /resource` — the v5 `Resource_FVO`.
    ///
    /// Unusually, TMF639's create schema retains every member of the read
    /// model, `creationDate` included. Nothing is required.
    pub struct ResourceCreate {
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Client-supplied URI, where the server permits one.
        href: String,
        /// Name the resource is known by.
        name: String,
        /// Narrative description.
        description: String,
        /// Category the resource falls in.
        category: String,
        /// Version of the resource.
        resource_version: String,
        /// Creation timestamp, which this API unusually accepts on create.
        creation_date: Timestamp,
        /// When the resource started operating.
        start_operating_date: Timestamp,
        /// When the resource stopped operating.
        end_operating_date: Timestamp,
        /// Period during which the resource record is valid.
        valid_for: TimePeriod,
        /// Initial operational state.
        operational_state: ResourceOperationalState,
        /// Initial usage state.
        usage_state: ResourceUsageState,
        /// Initial administrative state.
        administrative_state: ResourceAdministrativeState,
        /// Initial lifecycle state.
        lifecycle_state: ResourceLifecycleState,
        /// Initial alarm statuses.
        alarm_status: Vec<ResourceAlarmStatus>,
        /// Initial procedural status.
        procedural_status: ResourceProceduralStatus,
        /// Initial availability status.
        availability_status: ResourceAvailabilityStatus,
        /// Initial control status.
        control_status: ResourceControlStatus,
        /// Initial allocation status.
        allocation_status: String,
        /// Whether the status of the resource is unknown.
        unknown_status: bool,
        /// The specification this resource conforms to.
        resource_specification: Ref<ResourceSpecification>,
        /// Configurable features of the resource.
        activation_feature: Vec<Feature>,
        /// Characteristics of the resource.
        resource_characteristic: Vec<Characteristic>,
        /// Relationships to other resources.
        resource_relationship: Vec<ResourceRelationship>,
        /// Resources this one is built on.
        supporting_resource: Vec<Resource>,
        /// Order lines that acted on this resource.
        resource_order_item: Vec<RelatedResourceOrderItem>,
        /// Places the resource is installed at.
        place: Vec<RelatedPlace>,
        /// Parties related to the resource.
        related_party: Vec<RelatedParty>,
        /// Identifiers for the resource in external systems.
        external_identifier: Vec<ExternalIdentifier>,
        /// Documents attached to the resource.
        attachment: Vec<Attachment>,
        /// Intent governing the resource.
        intent: Ref<Intent>,
        /// Free-form notes.
        note: Vec<Note>,
    }
}

tmf_struct! {
    @name = "ResourceRelationship";
    /// A typed link from one resource to another.
    pub struct ResourceRelationship {
        /// Kind of relationship, e.g. `contains`, `dependsOn`.
        relationship_type: String,
        /// The resource at the other end.
        resource: Ref<Resource>,
        /// Characteristics qualifying the relationship.
        resource_relationship_characteristic: Vec<Characteristic>,
    }
}

tmf_struct! {
    @name = "RelatedResourceOrderItem";
    /// A resource order line that acted on this resource — TMF652.
    pub struct RelatedResourceOrderItem {
        /// Identifier of the resource order.
        resource_order_id: String,
        /// URI of the resource order.
        resource_order_href: String,
        /// Identifier of the line within that order.
        item_id: String,
        /// What the line asked for.
        item_action: ItemAction,
        /// Role the order played for this resource.
        role: String,
        @renamed {
            /// The concrete class of the referenced order.
            "@referredType" referred_type: String,
        }
    }
}

tmf_struct! {
    @name = "RelatedPlaceRef";
    /// A place a resource is installed at, with the role it plays.
    ///
    /// Unlike TMF622 and TMF637, which permit an inline place, TMF639 accepts
    /// only a reference — the member is `RelatedPlaceRef`, not
    /// `RelatedPlaceRefOrValue`.
    pub struct RelatedPlace {
        /// Role the place plays, e.g. `installationSite`.
        role: String,
        /// The place itself.
        place: Ref<Place>,
    }
}

/// Whether a resource is able to perform its function.
///
/// The X.731 *operational state*: a statement about the resource itself,
/// independent of whether an operator has permitted its use — that is
/// [`ResourceAdministrativeState`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceOperationalState {
    /// The resource is able to perform its function.
    #[serde(rename = "enabled")]
    Enabled,
    /// The resource is not able to perform its function.
    #[serde(rename = "disabled")]
    Disabled,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// Whether a resource is in use, and how heavily.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceUsageState {
    /// Not in use.
    #[serde(rename = "idle")]
    Idle,
    /// In use, with capacity to spare.
    #[serde(rename = "active")]
    Active,
    /// In use, with no capacity to spare.
    #[serde(rename = "busy")]
    Busy,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// Whether an operator has permitted the resource to be used.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceAdministrativeState {
    /// Use is administratively prohibited.
    #[serde(rename = "locked")]
    Locked,
    /// Use is administratively permitted.
    #[serde(rename = "unlocked")]
    Unlocked,
    /// Existing users may finish; no new users are admitted.
    #[serde(rename = "shuttingDown")]
    ShuttingDown,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// Where a resource is in its plan-install-remove lifecycle.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceLifecycleState {
    /// Designed but not yet installed.
    #[serde(rename = "planned")]
    Planned,
    /// Physically or logically in place.
    #[serde(rename = "installed")]
    Installed,
    /// Scheduled for removal.
    #[serde(rename = "pendingRemoval")]
    PendingRemoval,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// Severity of an outstanding alarm on a resource.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceAlarmStatus {
    /// Being repaired.
    #[serde(rename = "underRepair")]
    UnderRepair,
    /// Critical alarm outstanding.
    #[serde(rename = "critical")]
    Critical,
    /// Major alarm outstanding.
    #[serde(rename = "major")]
    Major,
    /// Minor alarm outstanding.
    #[serde(rename = "minor")]
    Minor,
    /// An alarm is outstanding whose severity is not given.
    #[serde(rename = "alarmOutstanding")]
    AlarmOutstanding,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// Where a resource is in its initialisation procedure.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceProceduralStatus {
    /// Needs initialising before it can be used.
    #[serde(rename = "initializationRequired")]
    InitializationRequired,
    /// Not initialised, and does not require it.
    #[serde(rename = "notInitialized")]
    NotInitialized,
    /// Initialising now.
    #[serde(rename = "initializing")]
    Initializing,
    /// Producing a report.
    #[serde(rename = "reporting")]
    Reporting,
    /// Shutting down.
    #[serde(rename = "terminating")]
    Terminating,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// Why a resource is unavailable, when it is.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceAvailabilityStatus {
    /// Undergoing a test.
    #[serde(rename = "inTest")]
    InTest,
    /// Has an internal fault.
    #[serde(rename = "failed")]
    Failed,
    /// Powered off.
    #[serde(rename = "powerOff")]
    PowerOff,
    /// Connected and reachable.
    #[serde(rename = "online")]
    Online,
    /// Requires a routine operation before it can be used.
    #[serde(rename = "offline")]
    Offline,
    /// Outside its scheduled service period.
    #[serde(rename = "offDuty")]
    OffDuty,
    /// Unavailable because something it depends on is.
    #[serde(rename = "dependency")]
    Dependency,
    /// Working, but below its specified capability.
    #[serde(rename = "degraded")]
    Degraded,
    /// Not installed.
    #[serde(rename = "notInstalled")]
    NotInstalled,
    /// Its log is full.
    #[serde(rename = "logFull")]
    LogFull,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

/// An operator-imposed restriction on a resource.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceControlStatus {
    /// Reserved for testing, but still carrying traffic.
    #[serde(rename = "subjectToTest")]
    SubjectToTest,
    /// Locked as part of a wider service lock.
    #[serde(rename = "partOfServicesLocked")]
    PartOfServicesLocked,
    /// Reserved for testing, and not carrying traffic.
    #[serde(rename = "reservedForTest")]
    ReservedForTest,
    /// Service has been administratively suspended.
    #[serde(rename = "suspended")]
    Suspended,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

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

    #[test]
    fn the_status_dimensions_are_independent() {
        let json = r#"{"id":"1","operationalState":"enabled","administrativeState":"locked","availabilityStatus":"degraded","usageState":"busy"}"#;
        let r: Resource = serde_json::from_str(json).unwrap();
        assert_eq!(r.operational_state, Some(ResourceOperationalState::Enabled));
        assert_eq!(
            r.administrative_state,
            Some(ResourceAdministrativeState::Locked)
        );
        assert_eq!(
            r.availability_status,
            Some(ResourceAvailabilityStatus::Degraded)
        );
        assert_eq!(r.usage_state, Some(ResourceUsageState::Busy));
    }

    #[test]
    fn an_unknown_status_value_survives_the_round_trip() {
        let r: Resource = serde_json::from_str(r#"{"operationalState":"quiesced"}"#).unwrap();
        assert_eq!(
            r.operational_state,
            Some(ResourceOperationalState::Other("quiesced".into()))
        );
        assert_eq!(
            serde_json::to_string(&r).unwrap(),
            r#"{"operationalState":"quiesced"}"#
        );
    }

    #[test]
    fn the_read_model_is_the_patch_body() {
        // TMF639 declares no `Resource_MVO`; this is the point of the alias.
        let update: ResourceUpdate = Resource::builder().name("renamed").build();
        assert_eq!(update.name.as_deref(), Some("renamed"));
    }
}