From d37948464967b8e997b870fb32931ba57fe1b491 Mon Sep 17 00:00:00 2001 From: Rina Fujino <18257209+rina23q@users.noreply.github.com> Date: Fri, 13 May 2022 03:18:03 +0200 Subject: Resend the supported config list after the plugin configuration updated Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com> --- plugins/c8y_configuration_plugin/src/config.rs | 14 ++-- plugins/c8y_configuration_plugin/src/download.rs | 4 +- plugins/c8y_configuration_plugin/src/main.rs | 92 ++++++++++++++---------- 3 files changed, 61 insertions(+), 49 deletions(-) (limited to 'plugins') diff --git a/plugins/c8y_configuration_plugin/src/config.rs b/plugins/c8y_configuration_plugin/src/config.rs index dafa9c96..ce4dc102 100644 --- a/plugins/c8y_configuration_plugin/src/config.rs +++ b/plugins/c8y_configuration_plugin/src/config.rs @@ -1,4 +1,4 @@ -use crate::error::ConfigManagementError; +use crate::{error::ConfigManagementError, DEFAULT_PLUGIN_CONFIG_TYPE}; use c8y_smartrest::topic::C8yTopic; use mqtt_channel::Message; use serde::Deserialize; @@ -6,11 +6,9 @@ use std::borrow::Borrow; use std::collections::HashSet; use std::fs; use std::hash::{Hash, Hasher}; -use std::path::{Path, PathBuf}; +use std::path::Path; use tracing::{info, warn}; -const DEFAULT_PLUGIN_CONFIG_TYPE: &str = "c8y-configuration-plugin"; - #[derive(Deserialize, Debug, Default)] #[serde(deny_unknown_fields)] struct RawPluginConfig { @@ -61,8 +59,8 @@ impl FileEntry { } impl RawPluginConfig { - fn new(config_file_path: PathBuf) -> Self { - let path_str = config_file_path.as_path().display().to_string(); + fn new(config_file_path: &Path) -> Self { + let path_str = config_file_path.display().to_string(); info!("Reading the config file from {}", path_str); match fs::read_to_string(config_file_path) { Ok(contents) => match toml::from_str(contents.as_str()) { @@ -84,8 +82,8 @@ impl RawPluginConfig { } impl PluginConfig { - pub fn new(config_file_path: PathBuf) -> Self { - let plugin_config = Self::new_with_config_file_entry(&config_file_path); + pub fn new(config_file_path: &Path) -> Self { + let plugin_config = Self::new_with_config_file_entry(config_file_path); let raw_config = RawPluginConfig::new(config_file_path); plugin_config.add_entries_from_raw_config(raw_config) } diff --git a/plugins/c8y_configuration_plugin/src/download.rs b/plugins/c8y_configuration_plugin/src/download.rs index a1b71420..9bfc334f 100644 --- a/plugins/c8y_configuration_plugin/src/download.rs +++ b/plugins/c8y_configuration_plugin/src/download.rs @@ -1,5 +1,5 @@ use crate::error::ConfigManagementError; -use crate::{error, PluginConfig}; +use crate::{error, PluginConfig, CONFIG_CHANGE_TOPIC}; use c8y_api::http_proxy::{C8YHttpProxy, JwtAuthHttpProxy}; use c8y_smartrest::error::SmartRestSerializerError; use c8y_smartrest::smartrest_deserializer::SmartRestConfigDownloadRequest; @@ -17,8 +17,6 @@ use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use tracing::{info, warn}; -const CONFIG_CHANGE_TOPIC: &str = "tedge/configuration_change"; - pub async fn handle_config_download_request( plugin_config: &PluginConfig, smartrest_request: SmartRestConfigDownloadRequest, diff --git a/plugins/c8y_configuration_plugin/src/main.rs b/plugins/c8y_configuration_plugin/src/main.rs index 7bf1c40c..be2cbb29 100644 --- a/plugins/c8y_configuration_plugin/src/main.rs +++ b/plugins/c8y_configuration_plugin/src/main.rs @@ -22,7 +22,10 @@ use tedge_config::{ use tedge_utils::file::{create_directory_with_user_group, create_file_with_user_group}; use tracing::{debug, error, info}; -const DEFAULT_PLUGIN_CONFIG_FILE_PATH: &str = "/etc/tedge/c8y/c8y-configuration-plugin.toml"; +pub const DEFAULT_PLUGIN_CONFIG_FILE_PATH: &str = "/etc/tedge/c8y/c8y-configuration-plugin.toml"; +pub const DEFAULT_PLUGIN_CONFIG_TYPE: &str = "c8y-configuration-plugin"; +pub const CONFIG_CHANGE_TOPIC: &str = "tedge/configuration_change"; + const AFTER_HELP_TEXT: &str = r#"On start, `c8y_configuration_plugin` notifies the cloud tenant of the managed configuration files, listed in the `CONFIG_FILE`, sending this list with a `119` on `c8y/s/us`. `c8y_configuration_plugin` subscribes then to `c8y/s/ds` listening for configuration operation requests (messages `524` and `526`). notifying the Cumulocity tenant of their progress (messages `501`, `502` and `503`). @@ -62,11 +65,13 @@ async fn create_mqtt_client( tedge_config: &TEdgeConfig, ) -> Result { let mqtt_port = tedge_config.query(MqttPortSetting)?.into(); + let mut topic_filter = + mqtt_channel::TopicFilter::new_unchecked(C8yTopic::SmartRestRequest.as_str()); + let _ = topic_filter + .add_unchecked(format!("{CONFIG_CHANGE_TOPIC}/{DEFAULT_PLUGIN_CONFIG_TYPE}").as_str()); let mqtt_config = mqtt_channel::Config::default() .with_port(mqtt_port) - .with_subscriptions(mqtt_channel::TopicFilter::new_unchecked( - C8yTopic::SmartRestRequest.as_str(), - )); + .with_subscriptions(topic_filter); let mqtt_client = mqtt_channel::Connection::new(&mqtt_config).await?; Ok(mqtt_client) @@ -100,7 +105,7 @@ async fn main() -> Result<(), anyhow::Error> { let mut mqtt_client = create_mqtt_client(&tedge_config).await?; let mut http_client = create_http_client(&tedge_config).await?; - let plugin_config = PluginConfig::new(config_plugin_opt.config_file); + let plugin_config = PluginConfig::new(&config_plugin_opt.config_file); // Publish supported configuration types let msg = plugin_config.to_supported_config_types_message()?; @@ -110,39 +115,50 @@ async fn main() -> Result<(), anyhow::Error> { while let Some(message) = mqtt_client.received.next().await { debug!("Received {:?}", message); if let Ok(payload) = message.payload_str() { - let result = match payload.split(',').next().unwrap_or_default() { - "524" => { - let config_download_request = - SmartRestConfigDownloadRequest::from_smartrest(payload)?; - - let tmp_dir = tedge_config.query(TmpPathSetting)?.into(); - - handle_config_download_request( - &plugin_config, - config_download_request, - tmp_dir, - &mut mqtt_client, - &mut http_client, - ) - .await - } - "526" => { - // retrieve config file upload smartrest request from payload - let config_upload_request = - SmartRestConfigUploadRequest::from_smartrest(payload)?; - - // handle the config file upload request - handle_config_upload_request( - &plugin_config, - config_upload_request, - &mut mqtt_client, - &mut http_client, - ) - .await - } - _ => { - // Ignore operation messages not meant for this plugin - Ok(()) + let result = if let "tedge/configuration_change/c8y-configuration-plugin" = + message.topic.name.as_str() + { + // Reload the plugin config file + let plugin_config = PluginConfig::new(&config_plugin_opt.config_file); + // Resend the supported config types + let msg = plugin_config.to_supported_config_types_message()?; + mqtt_client.published.send(msg).await?; + Ok(()) + } else { + match payload.split(',').next().unwrap_or_default() { + "524" => { + let config_download_request = + SmartRestConfigDownloadRequest::from_smartrest(payload)?; + + let tmp_dir = tedge_config.query(TmpPathSetting)?.into(); + + handle_config_download_request( + &plugin_config, + config_download_request, + tmp_dir, + &mut mqtt_client, + &mut http_client, + ) + .await + } + "526" => { + // retrieve config file upload smartrest request from payload + let config_upload_request = + SmartRestConfigUploadRequest::from_smartrest(payload)?; + + // handle the config file upload request + handle_config_upload_request( + &plugin_config, + config_upload_request, + &mut mqtt_client, + &mut http_client, + ) + .await + } + _ => { + // Ignore operation messages not meant for this plugin + Ok(()) + } } }; -- cgit v1.2.3