use rutmf::api::{
FilterOp, Query, tmf634::ResourceCatalogClient, tmf637::ProductInventoryClient,
tmf638::ServiceInventoryClient, tmf639::ResourceInventoryClient,
};
use rutmf::core::{Ref, ServiceSpecification};
use rutmf::mock::MockTmfServer;
use rutmf::product::{ProductCreate, ProductStatus};
use rutmf::resource::{
Resource, ResourceAdministrativeState, ResourceAlarmStatus, ResourceAvailabilityStatus,
ResourceCreate, ResourceOperationalState, ResourceSpecificationCreate,
};
use rutmf::service::{ServiceCreate, ServiceOperatingStatus, ServiceState, ServiceUpdate};
#[allow(
clippy::too_many_lines,
reason = "a worked example reads better as one narrative"
)]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = MockTmfServer::new();
let catalog = ResourceCatalogClient::new(server.base_url(), server.transport())?;
let products = ProductInventoryClient::new(server.base_url(), server.transport())?;
let services = ServiceInventoryClient::new(server.base_url(), server.transport())?;
let resources = ResourceInventoryClient::new(server.base_url(), server.transport())?;
println!("== the specification (TMF634) ==");
let spec = catalog
.create_resource_specification(
&ResourceSpecificationCreate::builder()
.name("SFP+ 10G Optical Port")
.lifecycle_status("Active")
.description("A 10-gigabit optical port on an access node")
.build(),
)
.await?;
let spec_id = spec.id.clone().expect("the server assigns an id");
println!(
" specification {spec_id}: {} ({:?})",
display(spec.name.as_deref()),
spec.kind(),
);
println!("\n== the resource (TMF639) ==");
let port = resources
.create_resource(
&ResourceCreate::builder()
.name("Optical port 3/1/2")
.category("PhysicalPort")
.resource_specification(Ref::new(&spec_id))
.operational_state(ResourceOperationalState::Enabled)
.administrative_state(ResourceAdministrativeState::Unlocked)
.availability_status(ResourceAvailabilityStatus::Online)
.build(),
)
.await?;
let port_id = port.id.clone().expect("the server assigns an id");
println!(" resource {port_id}: {}", display(port.name.as_deref()));
println!(
" operational={:?} administrative={:?} availability={:?}",
port.operational_state, port.administrative_state, port.availability_status
);
println!("\n== the service (TMF638) ==");
let access = services
.create_service(
&ServiceCreate::builder()
.state(ServiceState::Active)
.service_specification(Ref::<ServiceSpecification>::new("SS-broadband"))
.name("Broadband access")
.supporting_resource(vec![Ref::<Resource>::new(&port_id)])
.build(),
)
.await?;
let service_id = access.id.clone().expect("the server assigns an id");
println!(
" service {service_id}: {}",
display(access.name.as_deref())
);
println!(" lifecycle state: {:?}", access.state);
println!(" operating status: {:?}", access.operating_status);
println!("\n== the product (TMF637) ==");
let subscription = products
.create_product(
&ProductCreate::builder()
.name("Fibre 500")
.status(ProductStatus::Active)
.is_customer_visible(true)
.realizing_service(vec![Ref::new(&service_id)])
.build(),
)
.await?;
let product_id = subscription.id.clone().expect("the server assigns an id");
println!(
" product {product_id}: {} ({:?})",
display(subscription.name.as_deref()),
subscription.status
);
println!("\n== walking the chain ==");
let product = products.get_product(&product_id, &Query::new()).await?;
println!(" {}", display(product.name.as_deref()));
for service_ref in product.realizing_service.iter().flatten() {
let service = services.get_service(&service_ref.id, &Query::new()).await?;
println!(" └─ {}", display(service.name.as_deref()));
for resource_ref in service.supporting_resource.iter().flatten() {
let resource = resources
.get_resource(&resource_ref.id, &Query::new())
.await?;
println!(" └─ {}", display(resource.name.as_deref()));
}
}
println!("\n== the port takes an alarm ==");
let faulty = resources
.update_resource(
&port_id,
&Resource::builder()
.availability_status(ResourceAvailabilityStatus::Degraded)
.alarm_status(vec![ResourceAlarmStatus::Minor])
.build(),
)
.await?;
println!(
" resource: availability={:?} alarms={:?}",
faulty.availability_status,
faulty.alarm_status.as_deref().unwrap_or_default()
);
let degraded = services
.update_service(
&service_id,
&ServiceUpdate::builder()
.operating_status(ServiceOperatingStatus::Degraded)
.build(),
)
.await?;
println!(
" service: state={:?} operating={:?}",
degraded.state, degraded.operating_status
);
println!(" the customer still has the product; it is just not working well.");
println!("\n== every service that is not simply running ==");
let unhealthy = services
.list_services(&Query::new().filter_op("operatingStatus", FilterOp::Ne, "running"))
.await?;
for service in &unhealthy.items {
println!(
" {:<10} {:<20} {:?}",
display(service.id.as_deref()),
display(service.name.as_deref()),
service.operating_status
);
}
Ok(())
}
fn display(value: Option<&str>) -> &str {
value.unwrap_or("-")
}