Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C8y mapper handling c8y_LogfileRequest #2108

Merged
merged 16 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions crates/common/mqtt_channel/src/topics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ impl TryInto<TopicFilter> for &str {
}
}

impl FromIterator<TopicFilter> for TopicFilter {
fn from_iter<T: IntoIterator<Item = TopicFilter>>(filters: T) -> Self {
let mut combined_filters = TopicFilter::empty();
for filter in filters.into_iter() {
combined_filters.add_all(filter)
}
combined_filters
}
}

impl TryInto<TopicFilter> for Vec<&str> {
type Error = MqttError;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ define_tedge_config! {
#[tedge_config(default(value = "te/+/+/+/+/m/+,tedge/alarms/+/+,tedge/alarms/+/+/+,tedge/events/+,tedge/events/+/+,tedge/health/+,tedge/health/+/+"))]
topics: TemplatesSet,

enable: {
/// Enable log management
// TODO turn the default to true, when c8y-log-plugin will be deprecated
#[tedge_config(example = "true", default(value = false))]
log_management: bool,
}

},

#[tedge_config(deprecated_name = "azure")] // for 0.1.0 compatibility
Expand Down
13 changes: 13 additions & 0 deletions crates/core/c8y_api/src/smartrest/topic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use mqtt_channel::MqttError;
use mqtt_channel::Topic;
use mqtt_channel::TopicFilter;
use tedge_api::entity_store::EntityMetadata;
use tedge_api::entity_store::EntityType;
use tedge_api::topic::ResponseTopic;
use tedge_api::TopicError;

Expand Down Expand Up @@ -88,6 +90,17 @@ impl From<C8yTopic> for TopicFilter {
}
}

// FIXME this From conversion is error prone as this can only be used for responses.
impl From<&EntityMetadata> for C8yTopic {
fn from(value: &EntityMetadata) -> Self {
match value.r#type {
EntityType::MainDevice => Self::SmartRestResponse,
EntityType::ChildDevice => Self::ChildSmartRestResponse(value.entity_id.clone()),
EntityType::Service => Self::SmartRestResponse, // TODO how services are handled by c8y?
Copy link
Member

Choose a reason for hiding this comment

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

I guess c8y/s/us/<external_id> like child device. Need to get an answer from @reubenmiller.

Copy link
Contributor

@reubenmiller reubenmiller Sep 3, 2023

Choose a reason for hiding this comment

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

Yes services are referenced via their external id (like child devices) (if that was the question).

Though generally before publishing on the c8y/s/us/<external_id> topic, the service should be registered in Cumulocity via the 102 SmartREST message.

}
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum MapperSubscribeTopic {
C8yTopic(C8yTopic),
Expand Down
16 changes: 11 additions & 5 deletions crates/core/tedge_api/src/entity_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::collections::HashMap;

use crate::entity_store;
use crate::mqtt_topics::EntityTopicId;
use crate::mqtt_topics::TopicIdError;
use mqtt_channel::Message;
use mqtt_channel::Topic;

Expand Down Expand Up @@ -70,6 +71,7 @@ impl EntityStore {

let entity_id = main_device.entity_id?;
let metadata = EntityMetadata {
topic_id: main_device.topic_id.clone(),
entity_id: entity_id.clone(),
r#type: main_device.r#type,
parent: None,
Expand Down Expand Up @@ -170,6 +172,7 @@ impl EntityStore {
.entity_id
.unwrap_or_else(|| self.derive_entity_id(&message.topic_id));
let entity_metadata = EntityMetadata {
topic_id: message.topic_id.clone(),
r#type: message.r#type,
entity_id: entity_id.clone(),
parent,
Expand All @@ -179,7 +182,7 @@ impl EntityStore {
// device is affected if it was previously registered and was updated
let previous = self
.entities
.insert(message.topic_id.clone(), entity_metadata);
.insert(entity_metadata.topic_id.clone(), entity_metadata);

if previous.is_some() {
affected_entities.push(message.topic_id);
Expand Down Expand Up @@ -283,6 +286,7 @@ impl EntityStore {

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EntityMetadata {
pub topic_id: EntityTopicId,
pub parent: Option<EntityTopicId>,
pub r#type: EntityType,
pub entity_id: String,
Expand All @@ -297,9 +301,10 @@ pub enum EntityType {
}

impl EntityMetadata {
/// Creates a entity metadata for a child device.
/// Creates a entity metadata for the main device.
pub fn main_device(device_id: String) -> Self {
Self {
topic_id: EntityTopicId::default_main_device(),
entity_id: device_id,
r#type: EntityType::MainDevice,
parent: None,
Expand All @@ -308,13 +313,14 @@ impl EntityMetadata {
}

/// Creates a entity metadata for a child device.
pub fn child_device(child_device_id: String) -> Self {
Self {
pub fn child_device(child_device_id: String) -> Result<Self, TopicIdError> {
Ok(Self {
topic_id: EntityTopicId::default_child_device(&child_device_id)?,
Bravo555 marked this conversation as resolved.
Show resolved Hide resolved
entity_id: child_device_id,
r#type: EntityType::ChildDevice,
parent: Some(EntityTopicId::default_main_device()),
other: serde_json::json!({}),
}
})
}
}

Expand Down
9 changes: 4 additions & 5 deletions crates/core/tedge_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
pub mod error;
pub mod messages;
mod software;
pub mod topic;

pub mod alarm;
pub mod builder;
pub mod data;
pub mod entity_store;
pub mod error;
pub mod event;
pub mod group;
pub mod health;
pub mod measurement;
pub mod messages;
pub mod mqtt_topics;
pub mod parser;
pub mod serialize;
mod software;
pub mod topic;
pub mod utils;

pub use download::*;
Expand Down
55 changes: 55 additions & 0 deletions crates/core/tedge_api/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use mqtt_channel::Topic;
use nanoid::nanoid;
use serde::Deserialize;
use serde::Serialize;
use time::OffsetDateTime;

const SOFTWARE_LIST_REQUEST_TOPIC: &str = "tedge/commands/req/software/list";
const SOFTWARE_LIST_RESPONSE_TOPIC: &str = "tedge/commands/res/software/list";
Expand Down Expand Up @@ -534,6 +535,60 @@ impl RestartOperationResponse {
}
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Copy, Eq, Clone)]
#[serde(rename_all = "camelCase")]
pub enum CommandStatus {
Init,
didier-wenzek marked this conversation as resolved.
Show resolved Hide resolved
Executing,
Successful,
Failed,
}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LogMetadata {
pub types: Vec<String>,
}

impl<'a> Jsonify<'a> for LogMetadata {}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LogUploadCmdPayload {
pub status: CommandStatus, //Define a different enum if this op needs more states,
pub tedge_url: String,
#[serde(rename = "type")]
pub log_type: String,
#[serde(with = "time::serde::rfc3339")]
pub date_from: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub date_to: OffsetDateTime,
#[serde(skip_serializing_if = "Option::is_none")]
pub search_text: Option<String>,
pub lines: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}

impl<'a> Jsonify<'a> for LogUploadCmdPayload {}

impl LogUploadCmdPayload {
pub fn executing(&mut self) {
self.status = CommandStatus::Executing;
self.reason = None;
}

pub fn successful(&mut self) {
self.status = CommandStatus::Successful;
self.reason = None;
}

pub fn failed(&mut self, reason: impl Into<String>) {
self.status = CommandStatus::Failed;
self.reason = Some(reason.into());
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading