Skip to content

Commit

Permalink
auto_register enable/disable (#2486)
Browse files Browse the repository at this point in the history
Signed-off-by: Pradeep Kumar K J <pkj@softwareag.com>
  • Loading branch information
PradeepKiruvale authored Nov 29, 2023
1 parent 56eaa9d commit b216239
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions crates/extensions/c8y_mapper_ext/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");

Expand All @@ -85,6 +87,7 @@ impl C8yMapperConfig {
auth_proxy_port,
auth_proxy_protocol,
mqtt_schema,
enable_auto_register,
}
}

Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -179,6 +183,7 @@ impl C8yMapperConfig {
auth_proxy_port,
auth_proxy_protocol,
mqtt_schema,
enable_auto_register,
))
}

Expand Down
51 changes: 28 additions & 23 deletions crates/extensions/c8y_mapper_ext/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ impl CumulocityConverter {
threshold: self.size_threshold.0,
});
}
};

}
Ok(mqtt_messages)
}

Expand Down Expand Up @@ -533,8 +532,7 @@ impl CumulocityConverter {
self.http_proxy.send_event(create_event).await?;
return Ok(vec![]);
}
};

}
Ok(messages)
}

Expand Down Expand Up @@ -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(),
));
}
}
}
Expand Down Expand Up @@ -3123,6 +3127,7 @@ pub(crate) mod tests {
auth_proxy_port,
auth_proxy_protocol,
MqttSchema::default(),
true,
)
}
fn create_c8y_converter_from_config(
Expand Down
3 changes: 3 additions & 0 deletions crates/extensions/c8y_mapper_ext/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions crates/extensions/c8y_mapper_ext/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MqttMessage, MqttMessage> =
Expand Down
2 changes: 1 addition & 1 deletion tests/RobotFramework/tests/aws/aws_telemetry.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit b216239

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
379 1 3 380 99.74 1h2m25.241s

Failed Tests

Name Message ⏱️ Duration Suite
Check health status of tedge-mapper-c8y service on broker restart Setup failed: /setup/bootstrap.sh returned an unexpected exit code stdout: ---------------------------------------------------------- Initializing ---------------------------------------------------------- Detected version from positional argument. version= Using local dpkg install method as local .deb files were found in folder: . ---------------------------------------------------------- Installing thin-edge.io ---------------------------------------------------------- Executing the setup script for the 'thinedge/tedge-main' repository ... [1K [32m RUN[39;49m: Checking for required executable 'curl' ...[1K [1K [32m OK[39;49m: Checking for required executable 'curl' ... [1K [32m RUN[39;49m: Checking for required executable 'apt-get' ...[1K [ Message content over the limit has been removed. ] .......+........+............+....+..+..........+.....+....+...+..+...+......+.+........+.+..+...+.+......+..................+.....+.+...+..+.........+....+..+......+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ....+......+..+...+.........+...+.............+...+..............+.............+...+.....+....+..+.............+........+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.........+......+.....+.......+..+......+............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..+.......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ----- Certificate request self-signature ok subject=C = US, ST = Denial, L = Springfield, O = Dis, CN = 9643a3c4bcec Certificate request self-signature ok subject=C = US, ST = Denial, L = Springfield, O = Dis, CN = client1 Certificate was successfully created Error: failed to upload root certificate Caused by: 0: error sending request for url (https://qaenvironment.eu-latest.cumulocity.com/tenant/currentTenant): operation timed out 1: operation timed out 40.617 s Service Monitoring

Please sign in to comment.