1
//! Pure MongoDB model.
2

            
3
use std::{error::Error as StdError, sync::Arc};
4

            
5
use async_trait::async_trait;
6
use mongodb::Database;
7

            
8
use super::{
9
    application, device, device_route, dldata_buffer,
10
    mongodb::{
11
        application::Model as ApplicationModel,
12
        conn::{self, Options},
13
        device::Model as DeviceModel,
14
        device_route::Model as DeviceRouteModel,
15
        dldata_buffer::Model as DlDataBufferModel,
16
        network::Model as NetworkModel,
17
        network_route::Model as NetworkRouteModel,
18
        unit::Model as UnitModel,
19
    },
20
    network, network_route, unit,
21
};
22

            
23
/// Pure MongoDB model.
24
#[derive(Clone)]
25
pub struct Model {
26
    conn: Arc<Database>,
27
    unit: Arc<UnitModel>,
28
    application: Arc<ApplicationModel>,
29
    network: Arc<NetworkModel>,
30
    device: Arc<DeviceModel>,
31
    device_route: Arc<DeviceRouteModel>,
32
    network_route: Arc<NetworkRouteModel>,
33
    dldata_buffer: Arc<DlDataBufferModel>,
34
}
35

            
36
impl Model {
37
    /// Create an instance.
38
12
    pub async fn new(opts: &Options) -> Result<Self, Box<dyn StdError>> {
39
12
        let conn = Arc::new(conn::connect(opts).await?);
40
        Ok(Model {
41
12
            conn: conn.clone(),
42
12
            unit: Arc::new(UnitModel::new(conn.clone()).await?),
43
12
            application: Arc::new(ApplicationModel::new(conn.clone()).await?),
44
12
            network: Arc::new(NetworkModel::new(conn.clone()).await?),
45
12
            device: Arc::new(DeviceModel::new(conn.clone()).await?),
46
12
            device_route: Arc::new(DeviceRouteModel::new(conn.clone()).await?),
47
12
            network_route: Arc::new(NetworkRouteModel::new(conn.clone()).await?),
48
12
            dldata_buffer: Arc::new(DlDataBufferModel::new(conn.clone()).await?),
49
        })
50
12
    }
51

            
52
    /// Get the raw database connection ([`Database`]).
53
356
    pub fn get_connection(&self) -> &Database {
54
356
        &self.conn
55
356
    }
56
}
57

            
58
#[async_trait]
59
impl super::Model for Model {
60
6
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
61
6
        Ok(())
62
12
    }
63

            
64
1405
    fn unit(&self) -> &dyn unit::UnitModel {
65
1405
        self.unit.as_ref()
66
1405
    }
67

            
68
1050
    fn application(&self) -> &dyn application::ApplicationModel {
69
1050
        self.application.as_ref()
70
1050
    }
71

            
72
1658
    fn network(&self) -> &dyn network::NetworkModel {
73
1658
        self.network.as_ref()
74
1658
    }
75

            
76
1751
    fn device(&self) -> &dyn device::DeviceModel {
77
1751
        self.device.as_ref()
78
1751
    }
79

            
80
884
    fn device_route(&self) -> &dyn device_route::DeviceRouteModel {
81
884
        self.device_route.as_ref()
82
884
    }
83

            
84
705
    fn network_route(&self) -> &dyn network_route::NetworkRouteModel {
85
705
        self.network_route.as_ref()
86
705
    }
87

            
88
767
    fn dldata_buffer(&self) -> &dyn dldata_buffer::DlDataBufferModel {
89
767
        self.dldata_buffer.as_ref()
90
767
    }
91
}