From 5d7c9d9d08534817b35fdef6581627652c52d48e Mon Sep 17 00:00:00 2001 From: Albin Suresh Date: Tue, 31 Oct 2023 15:08:17 +0000 Subject: [PATCH 1/3] Fix auto-registration of child device op dirs #2389 If the child device directory the child device operation directories follow the default external id naming scheme, they are auto-registered with the right topic id and name derived from that external id. --- .../c8y_mapper_ext/src/converter.rs | 7 ++-- crates/extensions/c8y_mapper_ext/src/tests.rs | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/extensions/c8y_mapper_ext/src/converter.rs b/crates/extensions/c8y_mapper_ext/src/converter.rs index 55a46971a73..f097dee3e88 100644 --- a/crates/extensions/c8y_mapper_ext/src/converter.rs +++ b/crates/extensions/c8y_mapper_ext/src/converter.rs @@ -752,14 +752,15 @@ impl CumulocityConverter { continue; } }; - let child_topic_id = - EntityTopicId::default_child_device(child_external_id.as_ref()).unwrap(); + + let child_name = self.default_device_name_from_external_id(&child_external_id); + let child_topic_id = EntityTopicId::default_child_device(&child_name).unwrap(); let child_device_reg_msg = EntityRegistrationMessage { topic_id: child_topic_id, external_id: Some(child_external_id.clone()), r#type: EntityType::ChildDevice, parent: None, - other: json!({ "name": child_external_id.as_ref() }) + other: json!({ "name": child_name }) .as_object() .unwrap() .to_owned(), diff --git a/crates/extensions/c8y_mapper_ext/src/tests.rs b/crates/extensions/c8y_mapper_ext/src/tests.rs index 4c10450ee41..49d1b8042ab 100644 --- a/crates/extensions/c8y_mapper_ext/src/tests.rs +++ b/crates/extensions/c8y_mapper_ext/src/tests.rs @@ -1384,6 +1384,43 @@ async fn mapper_publishes_child_device_create_message() { .await; } +#[tokio::test] +async fn mapper_publishes_child_device_create_message_default_naming_scheme() { + let cfg_dir = TempTedgeDir::new(); + create_thin_edge_child_devices(&cfg_dir, vec!["test-device:device:child1"]); + + let (mqtt, _http, _fs, _timer, _dl) = spawn_c8y_mapper_actor(&cfg_dir, false).await; + let mut mqtt = mqtt.with_timeout(TEST_TIMEOUT_MS); + skip_init_messages(&mut mqtt).await; + + mqtt.send(MqttMessage::new( + &C8yTopic::downstream_topic(), + "106,child-one", + )) + .await + .expect("Send failed"); + + // Expect auto-registration message + assert_received_includes_json( + &mut mqtt, + [( + "te/device/child1//", + json!({"@type":"child-device", "name": "child1"}), + )], + ) + .await; + + // Expect smartrest message on `c8y/s/us` with expected payload "101,child1,child1,thin-edge.io-child". + assert_received_contains_str( + &mut mqtt, + [( + "c8y/s/us", + "101,test-device:device:child1,child1,thin-edge.io-child", + )], + ) + .await; +} + #[tokio::test] async fn mapper_publishes_supported_operations_for_child_device() { // The test assures tedge-mapper checks if there is a directory for operations for child devices, then it reads and From 99e42fa12ec76bd74c482a3320a9e107a3f8ae42 Mon Sep 17 00:00:00 2001 From: Albin Suresh Date: Tue, 31 Oct 2023 16:16:55 +0000 Subject: [PATCH 2/3] Addressing review comments --- crates/extensions/c8y_mapper_ext/src/converter.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/extensions/c8y_mapper_ext/src/converter.rs b/crates/extensions/c8y_mapper_ext/src/converter.rs index f097dee3e88..84f4e5abfd5 100644 --- a/crates/extensions/c8y_mapper_ext/src/converter.rs +++ b/crates/extensions/c8y_mapper_ext/src/converter.rs @@ -754,7 +754,16 @@ impl CumulocityConverter { }; let child_name = self.default_device_name_from_external_id(&child_external_id); - let child_topic_id = EntityTopicId::default_child_device(&child_name).unwrap(); + let child_topic_id = match EntityTopicId::default_child_device(&child_name) { + Ok(topic_id) => topic_id, + Err(err) => { + error!( + "Child device directory: {} ignored due to {}", + &child_name, err + ); + continue; + } + }; let child_device_reg_msg = EntityRegistrationMessage { topic_id: child_topic_id, external_id: Some(child_external_id.clone()), From efac99a2cd5f612be8848f8243cf4fb7534f9cc4 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Tue, 31 Oct 2023 18:54:52 +0100 Subject: [PATCH 3/3] add system test Signed-off-by: Reuben Miller --- .../registration/device_registration.robot | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot b/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot index ea8707e6f11..6b761d2c7dd 100644 --- a/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot +++ b/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot @@ -110,6 +110,57 @@ Register devices using custom MQTT schema Cumulocity.Set Device ${DEVICE_SN} Cumulocity.Device Should Have Measurements type=gateway_stats minimum=1 maximum=1 + +Register tedge-agent when tedge-mapper-c8y is not running #2389 + [Teardown] Start Service tedge-mapper-c8y + Device Should Exist ${DEVICE_SN} + + Stop Service tedge-mapper-c8y + Execute Command cmd=timeout 5 env TEDGE_RUN_LOCK_FILES=false tedge-agent --mqtt-device-topic-id device/offlinechild1// ignore_exit_code=${True} + Start Service tedge-mapper-c8y + + Should Be A Child Device Of Device ${DEVICE_SN}:device:offlinechild1 + Should Have MQTT Messages te/device/offlinechild1// minimum=1 + + Device Should Exist ${DEVICE_SN}:device:offlinechild1 + Cumulocity.Restart Device + Should Have MQTT Messages te/device/offlinechild1///cmd/restart/+ + + +Register tedge-configuration-plugin when tedge-mapper-c8y is not running #2389 + [Teardown] Start Service tedge-mapper-c8y + Device Should Exist ${DEVICE_SN} + + Stop Service tedge-mapper-c8y + Execute Command cmd=timeout 5 tedge-configuration-plugin --mqtt-device-topic-id device/offlinechild2// ignore_exit_code=${True} + Start Service tedge-mapper-c8y + + Should Be A Child Device Of Device ${DEVICE_SN}:device:offlinechild2 + Should Have MQTT Messages te/device/offlinechild2// minimum=1 + + Device Should Exist ${DEVICE_SN}:device:offlinechild2 + Cumulocity.Get Configuration dummy1 + Should Have MQTT Messages te/device/offlinechild2///cmd/config_snapshot/+ + + +Register tedge-log-plugin when tedge-mapper-c8y is not running #2389 + [Teardown] Start Service tedge-mapper-c8y + Device Should Exist ${DEVICE_SN} + + Stop Service tedge-mapper-c8y + Execute Command cmd=timeout 5 tedge-log-plugin --mqtt-device-topic-id device/offlinechild3// ignore_exit_code=${True} + Start Service tedge-mapper-c8y + + Should Be A Child Device Of Device ${DEVICE_SN}:device:offlinechild3 + Should Have MQTT Messages te/device/offlinechild3// minimum=1 + + Device Should Exist ${DEVICE_SN}:device:offlinechild3 + Cumulocity.Create Operation + ... description=Log file request + ... fragments={"c8y_LogfileRequest":{"dateFrom":"2023-01-01T01:00:00+0000","dateTo":"2023-01-02T01:00:00+0000","logFile":"example1","searchText":"first","maximumLines":10}} + Should Have MQTT Messages te/device/offlinechild3///cmd/log_upload/+ + + *** Keywords *** Check Child Device