use crate::core::macros::{tmf_entity, tmf_patch_body, tmf_struct, tmf_value};
use crate::core::{
Attachment, CharacteristicSpecification, ConnectionPointSpecification, EndpointSpecification,
ExternalIdentifier, FeatureRelationshipType, IntentSpecification, Policy, Ref, RelatedParty,
TimePeriod, Timestamp,
};
use serde::{Deserialize, Serialize};
tmf_struct! {
@name = "ResourceSpecification", @ref = "ResourceSpecificationRef";
pub struct ResourceSpecification {
id: String,
href: String,
name: String,
description: String,
version: String,
category: String,
last_update: Timestamp,
lifecycle_status: String,
is_bundle: bool,
valid_for: TimePeriod,
attachment: Vec<Attachment>,
resource_spec_characteristic: Vec<CharacteristicSpecification>,
resource_spec_relationship: Vec<ResourceSpecificationRelationship>,
feature_specification: Vec<FeatureSpecification>,
target_resource_schema: TargetResourceSchema,
intent_specification: Ref<IntentSpecification>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
model: String,
part: String,
sku: String,
vendor: String,
connection_point_specification: Vec<Ref<ConnectionPointSpecification>>,
connectivity_specification: Vec<ResourceGraphSpecification>,
@renamed {
"@referredType" referred_type: String,
}
}
}
impl ResourceSpecification {
#[must_use]
pub fn kind(&self) -> ResourceSpecificationKind {
ResourceSpecificationKind::from_type_name(self.type_name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ResourceSpecificationKind {
Base,
Logical,
Physical,
Function,
Other,
}
impl ResourceSpecificationKind {
#[must_use]
pub fn all() -> &'static [Self] {
&[Self::Base, Self::Logical, Self::Physical, Self::Function]
}
#[must_use]
pub fn from_type_name(name: &str) -> Self {
match name {
"ResourceSpecification" => Self::Base,
"LogicalResourceSpecification" => Self::Logical,
"PhysicalResourceSpecification" => Self::Physical,
"ResourceFunctionSpecification" => Self::Function,
_ => Self::Other,
}
}
#[must_use]
pub fn type_name(self) -> &'static str {
match self {
Self::Base | Self::Other => "ResourceSpecification",
Self::Logical => "LogicalResourceSpecification",
Self::Physical => "PhysicalResourceSpecification",
Self::Function => "ResourceFunctionSpecification",
}
}
}
tmf_struct! {
@name = "ResourceSpecification";
pub struct ResourceSpecificationCreate {
@required {
name: String,
}
id: String,
description: String,
version: String,
category: String,
last_update: Timestamp,
lifecycle_status: String,
is_bundle: bool,
valid_for: TimePeriod,
attachment: Vec<Attachment>,
resource_spec_characteristic: Vec<CharacteristicSpecification>,
resource_spec_relationship: Vec<ResourceSpecificationRelationship>,
feature_specification: Vec<FeatureSpecification>,
target_resource_schema: TargetResourceSchema,
intent_specification: Ref<IntentSpecification>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
}
}
tmf_struct! {
@name = "ResourceSpecification";
pub struct ResourceSpecificationUpdate {
name: String,
description: String,
version: String,
category: String,
last_update: Timestamp,
lifecycle_status: String,
is_bundle: bool,
valid_for: TimePeriod,
attachment: Vec<Attachment>,
resource_spec_characteristic: Vec<CharacteristicSpecification>,
resource_spec_relationship: Vec<ResourceSpecificationRelationship>,
feature_specification: Vec<FeatureSpecification>,
target_resource_schema: TargetResourceSchema,
intent_specification: Ref<IntentSpecification>,
related_party: Vec<RelatedParty>,
external_identifier: Vec<ExternalIdentifier>,
}
}
tmf_struct! {
@name = "ResourceSpecificationRelationship";
pub struct ResourceSpecificationRelationship {
id: String,
href: String,
name: String,
relationship_type: String,
role: String,
default_quantity: i64,
maximum_quantity: i64,
minimum_quantity: i64,
characteristic: Vec<CharacteristicSpecification>,
valid_for: TimePeriod,
}
}
tmf_struct! {
@name = "FeatureSpecification";
pub struct FeatureSpecification {
id: String,
name: String,
version: String,
is_bundle: bool,
is_enabled: bool,
feature_spec_characteristic: Vec<CharacteristicSpecification>,
feature_spec_relationship: Vec<FeatureSpecificationRelationship>,
policy_constraint: Vec<Ref<Policy>>,
valid_for: TimePeriod,
}
}
tmf_struct! {
@name = "FeatureSpecificationRelationship";
pub struct FeatureSpecificationRelationship {
feature_id: String,
name: String,
parent_specification_id: String,
parent_specification_href: String,
relationship_type: FeatureRelationshipType,
valid_for: TimePeriod,
}
}
tmf_struct! {
@name = "ResourceGraphSpecification";
pub struct ResourceGraphSpecification {
id: String,
href: String,
name: String,
description: String,
connection_specification: Vec<ConnectionSpecification>,
graph_specification_relationship: Vec<ResourceGraphSpecificationRelationship>,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ConnectionAssociationType {
#[serde(rename = "pointtoPoint")]
PointToPoint,
#[serde(rename = "pointtoMultipoint")]
PointToMultipoint,
#[serde(untagged)]
Other(String),
}
tmf_struct! {
@name = "ConnectionSpecification";
pub struct ConnectionSpecification {
id: String,
href: String,
name: String,
association_type: ConnectionAssociationType,
endpoint_specification: Vec<Ref<EndpointSpecification>>,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum ResourceGraphRelationshipType {
#[serde(rename = "adjacency")]
Adjacency,
#[serde(rename = "connectivity")]
Connectivity,
#[serde(untagged)]
Other(String),
}
tmf_value! {
pub struct ResourceGraphSpecificationRelationship {
relationship_type: ResourceGraphRelationshipType,
resource_graph: Ref<ResourceGraphSpecification>,
}
}
tmf_value! {
pub struct TargetResourceSchema {
@renamed {
"@type" at_type: String,
"@schemaLocation" at_schema_location: String,
}
}
}
tmf_entity!(ResourceSpecification);
tmf_patch_body!(ResourceSpecificationUpdate);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn subclass_members_sit_at_the_top_level() {
let json =
r#"{"@type":"PhysicalResourceSpecification","vendor":"Acme","sku":"X1","part":"P9"}"#;
let spec: ResourceSpecification = serde_json::from_str(json).unwrap();
assert_eq!(spec.kind(), ResourceSpecificationKind::Physical);
assert_eq!(spec.vendor.as_deref(), Some("Acme"));
assert!(spec.extensions.is_empty(), "subclass members must be typed");
}
#[test]
fn an_unknown_subclass_round_trips_as_other() {
let json = r#"{"@type":"VendorResourceSpecification","name":"n","quirk":1}"#;
let spec: ResourceSpecification = serde_json::from_str(json).unwrap();
assert_eq!(spec.kind(), ResourceSpecificationKind::Other);
assert_eq!(spec.extensions.get("quirk").unwrap(), 1);
assert_eq!(
serde_json::to_value(&spec).unwrap(),
serde_json::from_str::<serde_json::Value>(json).unwrap()
);
}
#[test]
fn every_declared_kind_round_trips_through_its_name() {
for kind in [
ResourceSpecificationKind::Base,
ResourceSpecificationKind::Logical,
ResourceSpecificationKind::Physical,
ResourceSpecificationKind::Function,
] {
assert_eq!(
ResourceSpecificationKind::from_type_name(kind.type_name()),
kind
);
}
}
}