use serde::{Deserialize, Serialize};
use crate::core::macros::{tmf_entity, tmf_patch_body, tmf_struct, tmf_value};
use crate::core::{
AnyEntity, Characteristic, Constraint, ExternalIdentifier, FeatureRelationshipType, Intent,
ItemAction, Note, Ref, RelatedParty, RelatedPlace, ServiceSpecification, TimePeriod, Timestamp,
};
use crate::resource::Resource;
tmf_struct! {
@name = "Service", @ref = "ServiceRef";
pub struct Service {
id: String,
href: String,
name: String,
description: String,
category: String,
service_type: String,
state: ServiceState,
operating_status: ServiceOperatingStatus,
is_service_enabled: bool,
is_bundle: bool,
is_stateful: bool,
has_started: bool,
start_mode: String,
start_date: Timestamp,
end_date: Timestamp,
service_date: String,
service_specification: Ref<ServiceSpecification>,
feature: Vec<Feature>,
service_characteristic: Vec<Characteristic>,
service_relationship: Vec<ServiceRelationship>,
supporting_service: Vec<Service>,
supporting_resource: Vec<Ref<Resource>>,
related_entity: Vec<RelatedEntity>,
service_order_item: Vec<RelatedServiceOrderItem>,
place: Vec<RelatedPlace>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
intent: Ref<Intent>,
note: Vec<Note>,
@renamed {
"@referredType" referred_type: String,
}
}
}
tmf_entity!(Service);
tmf_struct! {
@name = "Service";
pub struct ServiceCreate {
@required {
state: ServiceState,
service_specification: Ref<ServiceSpecification>,
}
id: String,
name: String,
description: String,
category: String,
service_type: String,
is_service_enabled: bool,
is_bundle: bool,
is_stateful: bool,
has_started: bool,
start_mode: String,
start_date: Timestamp,
end_date: Timestamp,
feature: Vec<Feature>,
service_characteristic: Vec<Characteristic>,
service_relationship: Vec<ServiceRelationship>,
supporting_service: Vec<Service>,
supporting_resource: Vec<Ref<Resource>>,
related_entity: Vec<RelatedEntity>,
service_order_item: Vec<RelatedServiceOrderItem>,
place: Vec<RelatedPlace>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
intent: Ref<Intent>,
note: Vec<Note>,
}
}
tmf_struct! {
@name = "Service";
pub struct ServiceUpdate {
name: String,
description: String,
category: String,
service_type: String,
state: ServiceState,
operating_status: ServiceOperatingStatus,
is_service_enabled: bool,
is_bundle: bool,
is_stateful: bool,
has_started: bool,
start_mode: String,
start_date: Timestamp,
end_date: Timestamp,
service_specification: Ref<ServiceSpecification>,
feature: Vec<Feature>,
service_characteristic: Vec<Characteristic>,
service_relationship: Vec<ServiceRelationship>,
supporting_service: Vec<Service>,
supporting_resource: Vec<Ref<Resource>>,
related_entity: Vec<RelatedEntity>,
service_order_item: Vec<RelatedServiceOrderItem>,
place: Vec<RelatedPlace>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
intent: Ref<Intent>,
note: Vec<Note>,
}
}
tmf_patch_body!(ServiceUpdate);
tmf_struct! {
@name = "Feature";
pub struct Feature {
id: String,
name: String,
is_enabled: bool,
is_bundle: bool,
feature_characteristic: Vec<Characteristic>,
feature_relationship: Vec<FeatureRelationship>,
constraint: Vec<Ref<Constraint>>,
}
}
tmf_struct! {
@name = "ServiceRelationship";
pub struct ServiceRelationship {
relationship_type: String,
service: Service,
service_relationship_characteristic: Vec<Characteristic>,
}
}
tmf_struct! {
@name = "RelatedEntityRefOrValue";
pub struct RelatedEntity {
role: String,
entity: Ref<AnyEntity>,
}
}
tmf_struct! {
@name = "RelatedServiceOrderItem";
pub struct RelatedServiceOrderItem {
service_order_id: String,
service_order_href: String,
item_id: String,
item_action: ItemAction,
role: String,
@renamed {
"@referredType" referred_type: String,
}
}
}
tmf_value! {
pub struct FeatureRelationship {
id: String,
name: String,
relationship_type: FeatureRelationshipType,
valid_for: TimePeriod,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ServiceState {
#[serde(rename = "feasibilityChecked")]
FeasibilityChecked,
#[serde(rename = "designed")]
Designed,
#[serde(rename = "reserved")]
Reserved,
#[serde(rename = "inactive")]
Inactive,
#[serde(rename = "active")]
Active,
#[serde(rename = "suspended")]
Suspended,
#[serde(rename = "terminated")]
Terminated,
#[serde(untagged)]
Other(String),
}
impl ServiceState {
#[must_use]
pub fn is_terminal(&self) -> bool {
matches!(self, Self::Terminated)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ServiceOperatingStatus {
#[serde(rename = "pending")]
Pending,
#[serde(rename = "configured")]
Configured,
#[serde(rename = "starting")]
Starting,
#[serde(rename = "running")]
Running,
#[serde(rename = "degraded")]
Degraded,
#[serde(rename = "failed")]
Failed,
#[serde(rename = "limited")]
Limited,
#[serde(rename = "stopping")]
Stopping,
#[serde(rename = "stopped")]
Stopped,
#[serde(rename = "unknown")]
Unknown,
#[serde(untagged)]
Other(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lifecycle_and_operation_are_separate_dimensions() {
let svc: Service =
serde_json::from_str(r#"{"state":"active","operatingStatus":"degraded"}"#).unwrap();
assert_eq!(svc.state, Some(ServiceState::Active));
assert_eq!(svc.operating_status, Some(ServiceOperatingStatus::Degraded));
assert!(!svc.state.unwrap().is_terminal());
}
#[test]
fn a_supporting_service_may_be_a_bare_reference() {
let svc: Service = serde_json::from_str(
r#"{"id":"1","supportingService":[{"id":"2","@type":"ServiceRef"}]}"#,
)
.unwrap();
let supporting = &svc.supporting_service.as_ref().unwrap()[0];
assert_eq!(supporting.id.as_deref(), Some("2"));
assert_eq!(supporting.type_name(), "ServiceRef");
}
#[test]
fn the_create_body_demands_a_state_and_a_specification() {
let body = ServiceCreate::builder()
.state(ServiceState::Reserved)
.service_specification(Ref::<ServiceSpecification>::new("SS-1"))
.name("Broadband access")
.build();
assert_eq!(body.state, ServiceState::Reserved);
assert_eq!(body.service_specification.id, "SS-1");
}
}