use serde::{Deserialize, Serialize};
use crate::core::macros::{tmf_entity, tmf_patch_body, tmf_struct, tmf_value};
use crate::core::{AlarmedObject, ExternalIdentifier, Ref, Threshold, Timestamp};
use crate::service::Service;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum AlarmType {
#[serde(rename = "communicationsAlarm")]
Communications,
#[serde(rename = "processingErrorAlarm")]
ProcessingError,
#[serde(rename = "environmentalAlarm")]
Environmental,
#[serde(rename = "qualityOfServiceAlarm")]
QualityOfService,
#[serde(rename = "equipmentAlarm")]
Equipment,
#[serde(rename = "integrityViolation")]
IntegrityViolation,
#[serde(rename = "operationalViolation")]
OperationalViolation,
#[serde(rename = "physicalViolation")]
PhysicalViolation,
#[serde(rename = "securityService")]
SecurityService,
#[serde(rename = "mechanismViolation")]
MechanismViolation,
#[serde(rename = "timeDomainViolation")]
TimeDomainViolation,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum PerceivedSeverity {
#[serde(rename = "critical")]
Critical,
#[serde(rename = "major")]
Major,
#[serde(rename = "minor")]
Minor,
#[serde(rename = "warning")]
Warning,
#[serde(rename = "indeterminate")]
Indeterminate,
#[serde(rename = "cleared")]
Cleared,
#[serde(untagged)]
Other(String),
}
impl PerceivedSeverity {
#[must_use]
pub fn is_active(&self) -> bool {
!matches!(self, Self::Cleared)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum AlarmState {
#[serde(rename = "raised")]
Raised,
#[serde(rename = "updated")]
Updated,
#[serde(rename = "cleared")]
Cleared,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum AckState {
#[serde(rename = "unacknowledged")]
Unacknowledged,
#[serde(rename = "acknowledged")]
Acknowledged,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum PlannedOutageIndicator {
#[serde(rename = "InService")]
InService,
#[serde(rename = "OutOfService")]
OutOfService,
#[serde(untagged)]
Other(String),
}
tmf_struct! {
@name = "Alarm", @ref = "AlarmRef";
pub struct Alarm {
id: String,
href: String,
name: String,
alarm_type: AlarmType,
perceived_severity: PerceivedSeverity,
probable_cause: String,
specific_problem: String,
alarm_details: String,
alarmed_object: Ref<AlarmedObject>,
alarmed_object_type: String,
state: AlarmState,
ack_state: AckState,
ack_system_id: String,
ack_user_id: String,
clear_system_id: String,
clear_user_id: String,
alarm_raised_time: Timestamp,
alarm_changed_time: Timestamp,
alarm_cleared_time: Timestamp,
alarm_reporting_time: Timestamp,
alarm_escalation: bool,
is_root_cause: bool,
service_affecting: bool,
planned_outage_indicator: PlannedOutageIndicator,
proposed_repaired_actions: String,
reporting_system_id: String,
source_system_id: String,
external_alarm_id: String,
correlated_alarm: Vec<Ref<Alarm>>,
parent_alarm: Vec<Ref<Alarm>>,
affected_service: Vec<Ref<Service>>,
place: Vec<RelatedPlace>,
comment: Vec<Comment>,
crossed_threshold_information: CrossedThresholdInformation,
@renamed {
"@referredType" referred_type: String,
}
}
}
tmf_struct! {
@name = "Alarm";
pub struct AlarmCreate {
@required {
alarm_type: AlarmType,
perceived_severity: PerceivedSeverity,
probable_cause: String,
alarmed_object: Ref<AlarmedObject>,
alarm_raised_time: Timestamp,
source_system_id: String,
state: AlarmState,
}
id: String,
href: String,
specific_problem: String,
alarm_details: String,
alarmed_object_type: String,
ack_state: AckState,
ack_system_id: String,
ack_user_id: String,
clear_system_id: String,
clear_user_id: String,
alarm_changed_time: Timestamp,
alarm_cleared_time: Timestamp,
alarm_reporting_time: Timestamp,
alarm_escalation: bool,
is_root_cause: bool,
service_affecting: bool,
planned_outage_indicator: PlannedOutageIndicator,
proposed_repaired_actions: String,
reporting_system_id: String,
external_alarm_id: String,
correlated_alarm: Vec<Ref<Alarm>>,
parent_alarm: Vec<Ref<Alarm>>,
affected_service: Vec<Ref<Service>>,
place: Vec<RelatedPlace>,
comment: Vec<Comment>,
crossed_threshold_information: CrossedThresholdInformation,
}
}
tmf_struct! {
@name = "Alarm";
pub struct AlarmUpdate {
id: String,
href: String,
alarm_type: AlarmType,
perceived_severity: PerceivedSeverity,
probable_cause: String,
specific_problem: String,
alarm_details: String,
alarmed_object: Ref<AlarmedObject>,
alarmed_object_type: String,
state: AlarmState,
ack_state: AckState,
ack_system_id: String,
ack_user_id: String,
clear_system_id: String,
clear_user_id: String,
alarm_changed_time: Timestamp,
alarm_cleared_time: Timestamp,
alarm_reporting_time: Timestamp,
alarm_escalation: bool,
is_root_cause: bool,
service_affecting: bool,
planned_outage_indicator: PlannedOutageIndicator,
proposed_repaired_actions: String,
reporting_system_id: String,
external_alarm_id: String,
correlated_alarm: Vec<Ref<Alarm>>,
parent_alarm: Vec<Ref<Alarm>>,
affected_service: Vec<Ref<Service>>,
place: Vec<RelatedPlace>,
comment: Vec<Comment>,
crossed_threshold_information: CrossedThresholdInformation,
}
}
tmf_struct! {
@name = "Comment";
pub struct Comment {
comment: String,
user_id: String,
system_id: String,
time: Timestamp,
}
}
tmf_value! {
pub struct CrossedThresholdInformation {
indicator_name: String,
indicator_unit: String,
observed_value: String,
direction: String,
granularity: String,
threshold_crossing_description: String,
threshold: Ref<Threshold>,
}
}
tmf_struct! {
@name = "RelatedPlace";
pub struct RelatedPlace {
related_place: Place,
role: String,
}
}
tmf_struct! {
@name = "Place", @ref = "PlaceRef";
pub struct Place {
id: String,
href: String,
external_identifier: Vec<ExternalIdentifier>,
}
}
tmf_entity!(Alarm);
tmf_patch_body!(AlarmUpdate);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_unknown_severity_is_still_active() {
let json = r#"{"@type":"Alarm","perceivedSeverity":"catastrophic"}"#;
let alarm: Alarm = serde_json::from_str(json).unwrap();
let severity = alarm.perceived_severity.clone().expect("a severity");
assert_eq!(severity, PerceivedSeverity::Other("catastrophic".into()));
assert!(severity.is_active());
assert_eq!(
serde_json::to_value(&alarm).unwrap(),
serde_json::from_str::<serde_json::Value>(json).unwrap()
);
}
#[test]
fn cleared_is_a_severity_not_a_state() {
assert!(!PerceivedSeverity::Cleared.is_active());
assert!(PerceivedSeverity::Critical.is_active());
}
#[test]
fn an_unknown_alarm_type_round_trips() {
let json = r#"{"@type":"Alarm","alarmType":"vendorSpecificAlarm"}"#;
let alarm: Alarm = serde_json::from_str(json).unwrap();
assert_eq!(
alarm.alarm_type,
Some(AlarmType::Other("vendorSpecificAlarm".into()))
);
}
}