diff --git a/Cargo.lock b/Cargo.lock index 63a9e53c335..2aa715a660c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -799,6 +799,7 @@ dependencies = [ "c8y_auth_proxy", "c8y_http_proxy", "camino", + "capture-logger", "clock", "json-writer", "logged_command", @@ -837,6 +838,15 @@ dependencies = [ "serde", ] +[[package]] +name = "capture-logger" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "605a59c5054ce9e87cd329251bcc4c04579d5edbd206b1e4c45fbfe24b13ff82" +dependencies = [ + "log", +] + [[package]] name = "cc" version = "1.0.83" diff --git a/Cargo.toml b/Cargo.toml index 823d9a9ae4d..8a9b7b81299 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ certificate = { path = "crates/common/certificate" } clap = { version = "4.4", features = ["cargo", "derive"] } clock = { path = "crates/common/clock" } collectd_ext = { path = "crates/extensions/collectd_ext" } +capture-logger = "0.1" csv = "1.1" darling = "0.20" doku = "0.21" diff --git a/crates/core/tedge_api/src/entity_store.rs b/crates/core/tedge_api/src/entity_store.rs index 402d7418682..edf80ad2dac 100644 --- a/crates/core/tedge_api/src/entity_store.rs +++ b/crates/core/tedge_api/src/entity_store.rs @@ -586,7 +586,7 @@ pub enum Error { #[error("The specified entity {0} does not exist in the store")] UnknownEntity(String), - #[error("The specified topic id {0} does not match the default topic scheme: 'device//service/'")] + #[error("Auto registration of the entity with topic id {0} failed as it does not match the default topic scheme: 'device//service/'. Try explicit registration instead.")] NonDefaultTopicScheme(EntityTopicId), #[error(transparent)] diff --git a/crates/extensions/c8y_mapper_ext/Cargo.toml b/crates/extensions/c8y_mapper_ext/Cargo.toml index 51c0860deb6..824e5faa876 100644 --- a/crates/extensions/c8y_mapper_ext/Cargo.toml +++ b/crates/extensions/c8y_mapper_ext/Cargo.toml @@ -49,9 +49,11 @@ tracing = { workspace = true } [dev-dependencies] assert-json-diff = { workspace = true } assert_matches = { workspace = true } +capture-logger = { workspace = true } proptest = { workspace = true } rand = { workspace = true } tedge_actors = { workspace = true, features = ["test-helpers"] } tedge_mqtt_ext = { workspace = true, features = ["test-helpers"] } tedge_test_utils = { workspace = true } test-case = { workspace = true } + diff --git a/crates/extensions/c8y_mapper_ext/src/converter.rs b/crates/extensions/c8y_mapper_ext/src/converter.rs index c363680285e..0c87025007a 100644 --- a/crates/extensions/c8y_mapper_ext/src/converter.rs +++ b/crates/extensions/c8y_mapper_ext/src/converter.rs @@ -967,7 +967,20 @@ impl CumulocityConverter { // if device is unregistered register using auto-registration if self.entity_store.get(&source).is_none() { let auto_registration_messages = - self.entity_store.auto_register_entity(&source)?; + match self.entity_store.auto_register_entity(&source) { + Ok(auto_registration_messages) => auto_registration_messages, + Err(e) => match e { + Error::NonDefaultTopicScheme(eid) => { + debug!("{}", Error::NonDefaultTopicScheme(eid.clone())); + return Ok(vec![self.new_error_message( + ConversionError::FromEntityStoreError( + entity_store::Error::NonDefaultTopicScheme(eid), + ), + )]); + } + e => return Err(e.into()), + }, + }; for auto_registration_message in &auto_registration_messages { registration_messages.append( &mut self.register_and_convert_entity(auto_registration_message)?, @@ -3020,6 +3033,28 @@ pub(crate) mod tests { } } + #[tokio::test] + async fn try_auto_registration_on_custom_topic() -> Result<(), anyhow::Error> { + use capture_logger::begin_capture; + use capture_logger::pop_captured; + let tmp_dir = TempTedgeDir::new(); + let (mut converter, _http_proxy) = create_c8y_converter(&tmp_dir).await; + + let alarm_topic = "te/custom/child2///m/"; + let alarm_payload = json!({ "text": "" }).to_string(); + let alarm_message = Message::new(&Topic::new_unchecked(alarm_topic), alarm_payload); + begin_capture(); + let res = converter.try_convert(&alarm_message).await.unwrap(); + let expected_err_msg = Message::new(&Topic::new_unchecked("te/errors"), "Auto registration of the entity with topic id custom/child2// failed as it does not match the default topic scheme: 'device//service/'. Try explicit registration instead."); + assert_eq!(res[0], expected_err_msg); + let expected_log = "Auto registration of the entity with topic id custom/child2// failed as it does not match the default topic scheme: 'device//service/'. Try explicit registration instead."; + // skip other log messages + pop_captured().unwrap().message(); + pop_captured().unwrap().message(); + assert_eq!(pop_captured().unwrap().message(), expected_log); + Ok(()) + } + pub(crate) async fn create_c8y_converter( tmp_dir: &TempTedgeDir, ) -> (