trace2e_core/traceability/
mod.rs

1//! Traceability module.
2//!
3//! This module provides a complete traceability solution for distributed systems,
4//! enabling comprehensive data provenance tracking, policy enforcement, and compliance
5//! monitoring across process boundaries and network connections.
6pub mod api;
7pub mod error;
8pub mod infrastructure;
9pub mod services;
10
11// re-export types for convenience
12pub use api::types;
13
14/// Standard M2M API service stack with default component configuration.
15///
16/// Combines sequencer, provenance, and compliance services using the default
17/// waiting queue mechanism for flow coordination. Suitable for most production
18/// deployments requiring standard M2M functionality.
19pub type M2mApiDefaultStack = api::m2m::M2mApiService<
20    services::sequencer::WaitingQueueService<services::sequencer::SequencerService>,
21    services::provenance::ProvenanceService,
22    services::compliance::ComplianceService,
23>;
24
25/// Standard P2M API service stack parameterized by M2M client type.
26///
27/// Combines sequencer, provenance, and compliance services with a configurable
28/// M2M client for distributed coordination. The generic parameter `M` allows
29/// different M2M transport implementations to be used.
30pub type P2mApiDefaultStack<M> = api::p2m::P2mApiService<
31    services::sequencer::WaitingQueueService<services::sequencer::SequencerService>,
32    services::provenance::ProvenanceService,
33    services::compliance::ComplianceService,
34    M,
35>;
36
37/// Standard O2M API service stack with default component configuration.
38///
39/// Combines provenance and compliance services for administrative operations.
40/// Does not include sequencer as O2M operations are typically read-heavy
41/// and don't require flow coordination.
42pub type O2mApiDefaultStack<M> = api::o2m::O2mApiService<
43    services::provenance::ProvenanceService,
44    services::compliance::ComplianceService,
45    services::consent::ConsentService,
46    M,
47>;
48
49/// Initialize a complete middleware stack for production deployment.
50///
51/// Creates a fully configured middleware stack with all three API services
52/// (M2M, P2M, O2M) using default component configurations. This is the
53/// standard initialization method for production deployments.
54///
55/// # Arguments
56/// * `node_id` - Unique identifier for this middleware node in the distributed system
57/// * `max_retries` - Maximum retry attempts for the waiting queue (None for unlimited)
58/// * `m2m_client` - Client service for M2M communication with remote middleware
59/// * `enable_resource_validation` - Whether to enable resource validation for P2M requests
60///
61/// # Returns
62/// A tuple containing (M2M service, P2M service, O2M service) ready for use
63///
64/// # Type Parameters
65/// * `M` - M2M client type that implements the required service traits
66pub fn init_middleware<M>(
67    node_id: String,
68    max_retries: Option<u32>,
69    consent_timeout: u64,
70    m2m_client: M,
71    enable_resource_validation: bool,
72) -> (M2mApiDefaultStack, P2mApiDefaultStack<M>, O2mApiDefaultStack<M>)
73where
74    M: tower::Service<
75            api::M2mRequest,
76            Response = api::M2mResponse,
77            Error = error::TraceabilityError,
78        > + Clone
79        + Send
80        + 'static,
81    M::Future: Send,
82{
83    let sequencer = tower::ServiceBuilder::new()
84        .layer(tower::layer::layer_fn(|inner| {
85            services::sequencer::WaitingQueueService::new(inner, max_retries)
86        }))
87        .service(services::sequencer::SequencerService::default());
88    let provenance = services::provenance::ProvenanceService::new(node_id.clone());
89    let consent = services::consent::ConsentService::new(consent_timeout);
90    let compliance = services::compliance::ComplianceService::new(node_id, consent.clone());
91
92    let m2m_service: M2mApiDefaultStack =
93        api::m2m::M2mApiService::new(sequencer.clone(), provenance.clone(), compliance.clone());
94
95    let p2m_service: P2mApiDefaultStack<M> = api::p2m::P2mApiService::new(
96        sequencer,
97        provenance.clone(),
98        compliance.clone(),
99        m2m_client.clone(),
100    )
101    .with_resource_validation(enable_resource_validation);
102
103    let o2m_service: O2mApiDefaultStack<M> =
104        api::o2m::O2mApiService::new(provenance, compliance, consent, m2m_client);
105
106    (m2m_service, p2m_service, o2m_service)
107}