summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_core/src/lib.rs
blob: ede90e39dbd07b6c2616bb90d5b3f6b6d44d3c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#![doc = include_str!("../README.md")]

use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;

use itertools::Itertools;
use tedge_api::plugin::HandleTypes;
use tedge_api::PluginBuilder;
use tokio_util::sync::CancellationToken;
use tracing::debug;

use tracing::event;

use tracing::Level;

mod communication;
pub mod configuration;
mod core_task;
pub mod errors;
mod message_handler;
mod plugin_task;
mod reactor;
mod utils;

pub use crate::communication::PluginDirectory;
use crate::configuration::PluginInstanceConfiguration;
use crate::configuration::TedgeConfiguration;

use crate::errors::PluginConfigurationError;
use crate::errors::PluginKindUnknownError;
use crate::errors::TedgeApplicationBuilderError;
use crate::errors::TedgeApplicationError;

/// A TedgeApplication
///
/// This is the main entry point for building a thin-edge application. It provides functions for
/// setting up the application and then run it.
///
/// # Details
///
/// This type implements only the setup functionality, how to construct an application object. The
/// implementation of the orchestration and lifecycle of the application is implemented in
/// [`crate::reactor::Reactor`]. Note that this is solely for code seperation.
pub struct TedgeApplication {
    config_path: PathBuf,
    config: TedgeConfiguration,
    cancellation_token: CancellationToken,
    plugin_builders: HashMap<String, (HandleTypes, Box<dyn PluginBuilder<PluginDirectory>>)>,
}

impl std::fmt::Debug for TedgeApplication {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("TedgeApplication").finish()
    }
}

impl TedgeApplication {
    /// Retrieve a [`TedgeApplicationBuilder`] object that can be used to construct a
    /// [`TedgeApplication`] object easily.
    pub fn builder() -> TedgeApplicationBuilder {
        TedgeApplicationBuilder {
            cancellation_token: CancellationToken::new(),
            plugin_builders: HashMap::new(),
            errors: vec![],
        }
    }

    pub(crate) fn config_path(&self) -> &Path {
        &self.config_path
    }

    pub(crate) fn config(&self) -> &TedgeConfiguration {
        &self.config
    }

    pub(crate) fn plugin_builders(
        &self,
    ) -> &HashMap<String, (HandleTypes, Box<dyn PluginBuilder<PluginDirectory>>)> {
        &self.plugin_builders
    }

    pub(crate) fn cancellation_token(&self) -> &CancellationToken {
        &self.cancellation_token
    }

    /// Run the TedgeApplication that has been setup for running
    ///
    /// This function runs as long as there is no shutdown signalled to the application.
    ///
    /// # Note
    ///
    /// This function makes sure that the configuration is verified before the plugins are started.
    /// So there is no need to call [TedgeApplication::verify_configuration] before this.
    pub async fn run(self) -> Result<(), TedgeApplicationError> {
        crate::reactor::Reactor(self).run().await
    }

    /// Check whether all configured plugin kinds exist (are available in registered plugins)
    /// and that the configurations for the individual plugins are actually valid.
    #[tracing::instrument(skip(self))]
    pub async fn verify_configurations(&self) -> Result<(), TedgeApplicationError> {
        use futures::stream::StreamExt;

        debug!("Verifying configurations");
        let results = self
            .config()
            .plugins()
            .iter()
            .map(
                |(plugin_name, plugin_cfg): (&String, &PluginInstanceConfiguration)| {
                    let plugin_name = plugin_name.to_string();
                    async move {
                        if let Some((_, builder)) =
                            self.plugin_builders().get(plugin_cfg.kind().as_ref())
                        {
                            debug!("Verifying {}", plugin_cfg.kind().as_ref());
                            let res = plugin_cfg
                                .configuration()
                                .verify_with_builder(&plugin_name, &**builder, self.config_path())
                                .await;

                            Ok(res?)
                        } else {
                            Err(PluginConfigurationError::UnknownKind(
                                PluginKindUnknownError {
                                    name: plugin_cfg.kind().as_ref().to_string(),
                                    alternatives: None,
                                },
                            ))
                        }
                    }
                },
            )
            .collect::<futures::stream::FuturesUnordered<_>>()
            .collect::<Vec<Result<_, _>>>()
            .await;

        let (_oks, errors): (Vec<_>, Vec<_>) = results.into_iter().partition_result();

        if !errors.is_empty() {
            return Err(TedgeApplicationError::PluginConfigVerificationsError { errors });
        }

        Ok(())
    }
}

/// Helper type for constructing a [`TedgeApplication`]
pub struct TedgeApplicationBuilder {
    cancellation_token: CancellationToken,
    plugin_builders: HashMap<String, (HandleTypes, Box<dyn PluginBuilder<PluginDirectory>>)>,
    errors: Vec<TedgeApplicationBuilderError>,
}

impl TedgeApplicationBuilder {
    /// Register a [`tedge_api::PluginBuilder`]
    ///
    /// This function can be used to register a [`tedge_api::PluginBuilder`] within the
    /// [`TedgeApplication`] which is about to be built.
    ///
    /// Registering a [`PluginBuilder`] does not mean that a plugin from this builder will be
    /// running once the application starts up, but merely that the application _knows_ about this
    /// plugin builder and is able to construct a plugin with this builder, if necessary (e.g. if
    /// configured in a configuration file).
    pub fn with_plugin_builder<PB: PluginBuilder<PluginDirectory>>(mut self, builder: PB) -> Self {
        let handle_types = PB::kind_message_types();
        let kind_name = PB::kind_name();
        event!(
            Level::INFO,
            plugin.kind = kind_name,
            plugin.handled_types = ?handle_types,
            "Registered plugin builder"
        );

        if self.plugin_builders.contains_key(kind_name) {
            self.errors
                .push(TedgeApplicationBuilderError::DuplicateKind {
                    name: kind_name.to_string(),
                    builder_name: std::any::type_name::<PB>(),
                });
            return self;
        }

        self.plugin_builders
            .insert(kind_name.to_string(), (handle_types, Box::new(builder)));
        self
    }

    /// Finalize the [`TedgeApplication`] by instantiating it with a `TedgeConfiguration`]
    ///
    /// This instantiates the application object, but does not run it.
    pub async fn with_config_from_path(
        self,
        config_path: impl AsRef<Path>,
    ) -> Result<(TedgeApplicationCancelSender, TedgeApplication), TedgeApplicationError> {
        if !self.errors.is_empty() {
            return Err(TedgeApplicationError::ApplicationBuilderErrors {
                errors: self.errors,
            });
        }
        let config_path = config_path.as_ref();
        debug!(?config_path, "Loading config from path");

        let config_str = tokio::fs::read_to_string(&config_path).await.map_err(|e| {
            TedgeApplicationError::ApplicationBuilderErrors {
                errors: vec![TedgeApplicationBuilderError::PathNotReadable {
                    path: config_path.to_path_buf(),
                    error: e,
                }],
            }
        })?;
        let config = toml::de::from_str(&config_str).map_err(|e| {
            TedgeApplicationError::ApplicationBuilderErrors {
                errors: vec![TedgeApplicationBuilderError::ConfigNotParseable {
                    path: config_path.to_path_buf(),
                    error: e,
                }],
            }
        })?;
        let cancellation = TedgeApplicationCancelSender(self.cancellation_token.clone());
        let app = TedgeApplication {
            config_path: config_path.to_path_buf(),
            config,
            cancellation_token: self.cancellation_token,
            plugin_builders: self.plugin_builders,
        };

        Ok((cancellation, app))
    }

    /// Fetch the currently registered plugin kind names from the TedgeApplicationBuilder instance
    pub fn plugin_kind_names(&self) -> impl Iterator<Item = &str> {
        self.plugin_builders.keys().map(String::as_ref)
    }

    #[cfg(test)]
    pub fn plugin_builders(
        &self,
    ) -> &HashMap<String, (HandleTypes, Box<dyn PluginBuilder<PluginDirectory>>)> {
        &self.plugin_builders
    }
}

#[derive(Clone, Debug)]
pub struct TedgeApplicationCancelSender(CancellationToken);

impl TedgeApplicationCancelSender {
    pub fn cancel_app(&self) {
        debug!("Cancelling application");
        self.0.cancel()
    }

    pub fn is_cancelled(&self) -> bool {
        self.0.is_cancelled()
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_deser_empty_plugin_config() {
        let s = "";
        let _: tedge_api::PluginConfiguration = toml::de::from_str(s).unwrap();
    }
}