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";
pub struct Resource {
id: String,
href: String,
name: String,
description: String,
category: String,
resource_version: String,
creation_date: Timestamp,
start_operating_date: Timestamp,
end_operating_date: Timestamp,
valid_for: TimePeriod,
operational_state: ResourceOperationalState,
usage_state: ResourceUsageState,
administrative_state: ResourceAdministrativeState,
lifecycle_state: ResourceLifecycleState,
alarm_status: Vec<ResourceAlarmStatus>,
procedural_status: ResourceProceduralStatus,
availability_status: ResourceAvailabilityStatus,
control_status: ResourceControlStatus,
allocation_status: String,
unknown_status: bool,
resource_specification: Ref<ResourceSpecification>,
activation_feature: Vec<Feature>,
resource_characteristic: Vec<Characteristic>,
resource_relationship: Vec<ResourceRelationship>,
supporting_resource: Vec<Resource>,
resource_order_item: Vec<RelatedResourceOrderItem>,
place: Vec<RelatedPlace>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
attachment: Vec<Attachment>,
intent: Ref<Intent>,
note: Vec<Note>,
value: String,
manufacture_date: Timestamp,
standby_status: ResourceStandbyStatus,
power_state: String,
power_consuming_state: ResourcePowerConsumingState,
power_consuming_level: i64,
serial_number: String,
batch_number: String,
version_number: String,
connection_point: Vec<Ref<ConnectionPoint>>,
connectivity: Vec<ResourceGraph>,
priority: i64,
role: String,
function_type: String,
auto_modification: Vec<Characteristic>,
schedule: Vec<Ref<Schedule>>,
last_update: Timestamp,
is_distributed_current: bool,
target_platform: String,
@renamed {
"@referredType" referred_type: String,
}
}
}
impl Resource {
#[must_use]
pub fn kind(&self) -> ResourceKind {
ResourceKind::from_type_name(self.type_name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ResourceKind {
Base,
Logical,
Physical,
Function,
Software,
Other,
}
impl ResourceKind {
#[must_use]
pub fn all() -> &'static [Self] {
&[
Self::Base,
Self::Logical,
Self::Physical,
Self::Function,
Self::Software,
]
}
#[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,
}
}
#[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",
}
}
#[must_use]
pub fn is_logical(self) -> bool {
matches!(self, Self::Logical | Self::Function | Self::Software)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceStandbyStatus {
#[serde(rename = "hotStandby")]
HotStandby,
#[serde(rename = "coldStandby")]
ColdStandby,
#[serde(rename = "providingService")]
ProvidingService,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourcePowerConsumingState {
#[serde(rename = "fullPower")]
FullPower,
#[serde(rename = "powerSaving")]
PowerSaving,
#[serde(rename = "sleeping")]
Sleeping,
#[serde(rename = "shutdown")]
Shutdown,
#[serde(untagged)]
Other(String),
}
tmf_entity!(Resource);
tmf_patch_body!(Resource);
pub type ResourceUpdate = Resource;
tmf_struct! {
@name = "Resource";
pub struct ResourceCreate {
id: String,
href: String,
name: String,
description: String,
category: String,
resource_version: String,
creation_date: Timestamp,
start_operating_date: Timestamp,
end_operating_date: Timestamp,
valid_for: TimePeriod,
operational_state: ResourceOperationalState,
usage_state: ResourceUsageState,
administrative_state: ResourceAdministrativeState,
lifecycle_state: ResourceLifecycleState,
alarm_status: Vec<ResourceAlarmStatus>,
procedural_status: ResourceProceduralStatus,
availability_status: ResourceAvailabilityStatus,
control_status: ResourceControlStatus,
allocation_status: String,
unknown_status: bool,
resource_specification: Ref<ResourceSpecification>,
activation_feature: Vec<Feature>,
resource_characteristic: Vec<Characteristic>,
resource_relationship: Vec<ResourceRelationship>,
supporting_resource: Vec<Resource>,
resource_order_item: Vec<RelatedResourceOrderItem>,
place: Vec<RelatedPlace>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
attachment: Vec<Attachment>,
intent: Ref<Intent>,
note: Vec<Note>,
}
}
tmf_struct! {
@name = "ResourceRelationship";
pub struct ResourceRelationship {
relationship_type: String,
resource: Ref<Resource>,
resource_relationship_characteristic: Vec<Characteristic>,
}
}
tmf_struct! {
@name = "RelatedResourceOrderItem";
pub struct RelatedResourceOrderItem {
resource_order_id: String,
resource_order_href: String,
item_id: String,
item_action: ItemAction,
role: String,
@renamed {
"@referredType" referred_type: String,
}
}
}
tmf_struct! {
@name = "RelatedPlaceRef";
pub struct RelatedPlace {
role: String,
place: Ref<Place>,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceOperationalState {
#[serde(rename = "enabled")]
Enabled,
#[serde(rename = "disabled")]
Disabled,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceUsageState {
#[serde(rename = "idle")]
Idle,
#[serde(rename = "active")]
Active,
#[serde(rename = "busy")]
Busy,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceAdministrativeState {
#[serde(rename = "locked")]
Locked,
#[serde(rename = "unlocked")]
Unlocked,
#[serde(rename = "shuttingDown")]
ShuttingDown,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceLifecycleState {
#[serde(rename = "planned")]
Planned,
#[serde(rename = "installed")]
Installed,
#[serde(rename = "pendingRemoval")]
PendingRemoval,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceAlarmStatus {
#[serde(rename = "underRepair")]
UnderRepair,
#[serde(rename = "critical")]
Critical,
#[serde(rename = "major")]
Major,
#[serde(rename = "minor")]
Minor,
#[serde(rename = "alarmOutstanding")]
AlarmOutstanding,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceProceduralStatus {
#[serde(rename = "initializationRequired")]
InitializationRequired,
#[serde(rename = "notInitialized")]
NotInitialized,
#[serde(rename = "initializing")]
Initializing,
#[serde(rename = "reporting")]
Reporting,
#[serde(rename = "terminating")]
Terminating,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceAvailabilityStatus {
#[serde(rename = "inTest")]
InTest,
#[serde(rename = "failed")]
Failed,
#[serde(rename = "powerOff")]
PowerOff,
#[serde(rename = "online")]
Online,
#[serde(rename = "offline")]
Offline,
#[serde(rename = "offDuty")]
OffDuty,
#[serde(rename = "dependency")]
Dependency,
#[serde(rename = "degraded")]
Degraded,
#[serde(rename = "notInstalled")]
NotInstalled,
#[serde(rename = "logFull")]
LogFull,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceControlStatus {
#[serde(rename = "subjectToTest")]
SubjectToTest,
#[serde(rename = "partOfServicesLocked")]
PartOfServicesLocked,
#[serde(rename = "reservedForTest")]
ReservedForTest,
#[serde(rename = "suspended")]
Suspended,
#[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() {
let update: ResourceUpdate = Resource::builder().name("renamed").build();
assert_eq!(update.name.as_deref(), Some("renamed"));
}
}