From b2162397cfef4c73dd7d5b794fdd3adb6d55b604 Mon Sep 17 00:00:00 2001 From: PradeepKiruvale Date: Wed, 29 Nov 2023 20:34:27 +0530 Subject: [PATCH] auto_register enable/disable (#2486) Signed-off-by: Pradeep Kumar K J --- .../src/tedge_config_cli/tedge_config.rs | 6 +++ .../extensions/c8y_mapper_ext/src/config.rs | 5 ++ .../c8y_mapper_ext/src/converter.rs | 51 ++++++++++--------- crates/extensions/c8y_mapper_ext/src/error.rs | 3 ++ crates/extensions/c8y_mapper_ext/src/tests.rs | 1 + .../tests/aws/aws_telemetry.robot | 2 +- .../registration/device_registration.robot | 14 ++++- 7 files changed, 56 insertions(+), 26 deletions(-) diff --git a/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs b/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs index a050a0d6bd1..15558fe8d86 100644 --- a/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs +++ b/crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs @@ -443,6 +443,12 @@ define_tedge_config! { local_cleansession: AutoFlag, } }, + + entity_store: { + /// Enable auto registration feature + #[tedge_config(example = "true", default(value = true))] + auto_register: bool, + }, }, #[tedge_config(deprecated_name = "azure")] // for 0.1.0 compatibility diff --git a/crates/extensions/c8y_mapper_ext/src/config.rs b/crates/extensions/c8y_mapper_ext/src/config.rs index 2e1b7560fa7..9712e4e5fa2 100644 --- a/crates/extensions/c8y_mapper_ext/src/config.rs +++ b/crates/extensions/c8y_mapper_ext/src/config.rs @@ -43,6 +43,7 @@ pub struct C8yMapperConfig { pub auth_proxy_port: u16, pub auth_proxy_protocol: Protocol, pub mqtt_schema: MqttSchema, + pub enable_auto_register: bool, } impl C8yMapperConfig { @@ -64,6 +65,7 @@ impl C8yMapperConfig { auth_proxy_port: u16, auth_proxy_protocol: Protocol, mqtt_schema: MqttSchema, + enable_auto_register: bool, ) -> Self { let ops_dir = config_dir.join("operations").join("c8y"); @@ -85,6 +87,7 @@ impl C8yMapperConfig { auth_proxy_port, auth_proxy_protocol, mqtt_schema, + enable_auto_register, } } @@ -125,6 +128,7 @@ impl C8yMapperConfig { }; let mut topics = Self::default_internal_topic_filter(&config_dir)?; + let enable_auto_register = tedge_config.c8y.entity_store.auto_register; // Add feature topic filters for cmd in [ @@ -179,6 +183,7 @@ impl C8yMapperConfig { auth_proxy_port, auth_proxy_protocol, mqtt_schema, + enable_auto_register, )) } diff --git a/crates/extensions/c8y_mapper_ext/src/converter.rs b/crates/extensions/c8y_mapper_ext/src/converter.rs index 167e014a2ce..a180e8cfe6e 100644 --- a/crates/extensions/c8y_mapper_ext/src/converter.rs +++ b/crates/extensions/c8y_mapper_ext/src/converter.rs @@ -468,8 +468,7 @@ impl CumulocityConverter { threshold: self.size_threshold.0, }); } - }; - + } Ok(mqtt_messages) } @@ -533,8 +532,7 @@ impl CumulocityConverter { self.http_proxy.send_event(create_event).await?; return Ok(vec![]); } - }; - + } Ok(messages) } @@ -976,25 +974,31 @@ impl CumulocityConverter { _ => { // if device is unregistered register using auto-registration if self.entity_store.get(&source).is_none() { - let auto_registration_messages = - 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)?, - ); + if self.config.enable_auto_register { + let auto_registration_messages = + 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)?, + ); + } + } else { + return Err(ConversionError::AutoRegistrationDisabled( + source.to_string(), + )); } } } @@ -3123,6 +3127,7 @@ pub(crate) mod tests { auth_proxy_port, auth_proxy_protocol, MqttSchema::default(), + true, ) } fn create_c8y_converter_from_config( diff --git a/crates/extensions/c8y_mapper_ext/src/error.rs b/crates/extensions/c8y_mapper_ext/src/error.rs index 86d47ec6189..9a55034c407 100644 --- a/crates/extensions/c8y_mapper_ext/src/error.rs +++ b/crates/extensions/c8y_mapper_ext/src/error.rs @@ -128,6 +128,9 @@ pub enum ConversionError { #[error("Unexpected error: {0:?}")] UnexpectedError(#[from] anyhow::Error), + + #[error("The provided entity: {0} was not found and could not be auto-registered either, because it is disabled")] + AutoRegistrationDisabled(String), } #[derive(thiserror::Error, Debug)] diff --git a/crates/extensions/c8y_mapper_ext/src/tests.rs b/crates/extensions/c8y_mapper_ext/src/tests.rs index 143ca198bbc..1f678ba9d7a 100644 --- a/crates/extensions/c8y_mapper_ext/src/tests.rs +++ b/crates/extensions/c8y_mapper_ext/src/tests.rs @@ -2596,6 +2596,7 @@ pub(crate) async fn spawn_c8y_mapper_actor( auth_proxy_port, Protocol::Http, MqttSchema::default(), + true, ); let mut mqtt_builder: SimpleMessageBoxBuilder = diff --git a/tests/RobotFramework/tests/aws/aws_telemetry.robot b/tests/RobotFramework/tests/aws/aws_telemetry.robot index 4b535957d2c..d0f1b57a142 100644 --- a/tests/RobotFramework/tests/aws/aws_telemetry.robot +++ b/tests/RobotFramework/tests/aws/aws_telemetry.robot @@ -14,7 +14,7 @@ Test Tags theme:mqtt theme:aws Publish measurements to te measurement topic ${timestamp}= Get Unix Timestamp Execute Command tedge mqtt pub te/device/main///m/ '{"temperature": 10}' - Should Have MQTT Messages aws/td/device:main/m/ message_contains="temperature" date_from=${timestamp} minimum=1 maximum=1 + Should Have MQTT Messages aws/td/device:main/m/ message_contains="temperature" date_from=${timestamp} minimum=1 maximum=1 Publish measurements to te measurement topic with measurement type ${timestamp}= Get Unix Timestamp diff --git a/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot b/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot index 6b761d2c7dd..7bf9e05ea89 100644 --- a/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot +++ b/tests/RobotFramework/tests/cumulocity/registration/device_registration.robot @@ -31,6 +31,17 @@ Child device registration Cumulocity.Set Device ${DEVICE_SN} Cumulocity.Should Be A Child Device Of Device ${CHILD_SN} +Auto register disabled + ${timestamp}= Get Unix Timestamp + Execute Command sudo tedge config set c8y.entity_store.auto_register false + Restart Service tedge-mapper-c8y + Service Health Status Should Be Up tedge-mapper-c8y + Execute Command sudo tedge mqtt pub 'te/device/auto_reg_device///a/temperature_high' '{ "severity": "critical", "text": "Temperature is very high", "time": "2021-01-01T05:30:45+00:00" }' -q 2 -r + Should Have MQTT Messages te/errors message_contains=The provided entity: device/auto_reg_device// was not found and could not be auto-registered either, because it is disabled date_from=${timestamp} minimum=1 maximum=1 + Execute Command sudo tedge config unset c8y.entity_store.auto_register + Restart Service tedge-mapper-c8y + + Register child device with defaults via MQTT Execute Command tedge mqtt pub --retain 'te/device/${CHILD_SN}//' '{"@type":"child-device"}' Check Child Device parent_sn=${DEVICE_SN} child_sn=${CHILD_XID} child_name=${CHILD_XID} child_type=thin-edge.io-child @@ -159,8 +170,7 @@ Register tedge-log-plugin when tedge-mapper-c8y is not running #2389 ... 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