1
//! Traits and implementations for accessing databases and caches.
2
//!
3
//! Currently we only provide pure MongoDB/SQLite implementation. Mixing implementation is
4
//! possible.
5

            
6
use std::{error::Error as StdError, sync::Arc};
7

            
8
use async_trait::async_trait;
9

            
10
pub mod application_dldata;
11
pub mod application_uldata;
12
pub mod coremgr_opdata;
13
pub mod network_dldata;
14
pub mod network_uldata;
15

            
16
mod model_mongodb;
17
mod model_sqlite;
18
mod mongodb;
19
mod sqlite;
20

            
21
pub use self::{
22
    mongodb::conn::{self as mongodb_conn, Options as MongoDbOptions},
23
    sqlite::conn::{self as sqlite_conn, Options as SqliteOptions},
24
};
25
pub use model_mongodb::Model as MongoDbModel;
26
pub use model_sqlite::Model as SqliteModel;
27

            
28
/// Database connection options for model implementation.
29
pub enum ConnOptions {
30
    // Pure MongoDB model implementation.
31
    MongoDB(MongoDbOptions),
32
    /// Pure SQLite model implementation.
33
    Sqlite(SqliteOptions),
34
}
35

            
36
/// The top level trait to get all models (tables/collections).
37
#[async_trait]
38
pub trait Model: Send + Sync {
39
    /// Close database connection.
40
    async fn close(&self) -> Result<(), Box<dyn StdError>>;
41

            
42
    /// To get the application downlink data model.
43
    fn application_dldata(&self) -> &dyn application_dldata::ApplicationDlDataModel;
44

            
45
    /// To get the application uplink data model.
46
    fn application_uldata(&self) -> &dyn application_uldata::ApplicationUlDataModel;
47

            
48
    /// To get the coremgr operation data model.
49
    fn coremgr_opdata(&self) -> &dyn coremgr_opdata::CoremgrOpDataModel;
50

            
51
    /// To get the network downlink data model.
52
    fn network_dldata(&self) -> &dyn network_dldata::NetworkDlDataModel;
53

            
54
    /// To get the network uplink data model.
55
    fn network_uldata(&self) -> &dyn network_uldata::NetworkUlDataModel;
56
}
57

            
58
/// To create the database model with the specified database implementation.
59
11
pub async fn new(opts: &ConnOptions) -> Result<Arc<dyn Model>, Box<dyn StdError>> {
60
11
    let model: Arc<dyn Model> = match opts {
61
61
        ConnOptions::MongoDB(opts) => Arc::new(MongoDbModel::new(opts).await?),
62
103
        ConnOptions::Sqlite(opts) => Arc::new(SqliteModel::new(opts).await?),
63
    };
64
22
    model.application_dldata().init().await?;
65
22
    model.application_uldata().init().await?;
66
22
    model.coremgr_opdata().init().await?;
67
21
    model.network_dldata().init().await?;
68
22
    model.network_uldata().init().await?;
69
11
    Ok(model)
70
11
}