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    init_middleware_with_enrolled_resources(
84        node_id,
85        max_retries,
86        consent_timeout,
87        m2m_client,
88        enable_resource_validation,
89        0,
90        0,
91        0,
92    )
93}
94
95/// Initialize a middleware stack with pre-enrolled resources for testing.
96///
97/// Creates a middleware stack identical to `init_middleware()` but with
98/// pre-enrolled mock resources. This is useful for testing, benchmarking,
99/// and simulation scenarios where actual process interactions are not needed.
100///
101/// # Arguments
102/// * `node_id` - Unique identifier for this middleware node
103/// * `max_retries` - Maximum retry attempts for the waiting queue
104/// * `m2m_client` - Client service for M2M communication
105/// * `enable_resource_validation` - Whether to enable resource validation for P2M requests
106/// * `process_count` - Number of mock processes to pre-enroll
107/// * `per_process_file_count` - Number of files to enroll per process
108/// * `per_process_stream_count` - Number of streams to enroll per process
109///
110/// # Returns
111/// A tuple containing (M2M service, P2M service, O2M service) with pre-enrolled resources
112///
113/// # Warning
114/// Should only be used for testing purposes. Production deployments should use
115/// `init_middleware()` and rely on actual process enrollment.
116#[allow(clippy::too_many_arguments)]
117pub fn init_middleware_with_enrolled_resources<M>(
118    node_id: String,
119    max_retries: Option<u32>,
120    consent_timeout: u64,
121    m2m_client: M,
122    enable_resource_validation: bool,
123    _process_count: u32,
124    _per_process_file_count: u32,
125    _per_process_stream_count: u32,
126) -> (M2mApiDefaultStack, P2mApiDefaultStack<M>, O2mApiDefaultStack<M>)
127where
128    M: tower::Service<
129            api::M2mRequest,
130            Response = api::M2mResponse,
131            Error = error::TraceabilityError,
132        > + Clone
133        + Send
134        + 'static,
135    M::Future: Send,
136{
137    let sequencer = tower::ServiceBuilder::new()
138        .layer(tower::layer::layer_fn(|inner| {
139            services::sequencer::WaitingQueueService::new(inner, max_retries)
140        }))
141        .service(services::sequencer::SequencerService::default());
142    let provenance = services::provenance::ProvenanceService::new(node_id.clone());
143    let consent = services::consent::ConsentService::new(consent_timeout);
144    let compliance = services::compliance::ComplianceService::new(node_id, consent.clone());
145
146    let m2m_service: M2mApiDefaultStack =
147        api::m2m::M2mApiService::new(sequencer.clone(), provenance.clone(), compliance.clone());
148
149    #[cfg(test)]
150    let p2m_service: P2mApiDefaultStack<M> = api::p2m::P2mApiService::new(
151        sequencer,
152        provenance.clone(),
153        compliance.clone(),
154        m2m_client.clone(),
155    )
156    .with_resource_validation(enable_resource_validation)
157    .with_enrolled_resources(
158        _process_count,
159        _per_process_file_count,
160        _per_process_stream_count,
161    );
162    #[cfg(not(test))]
163    let p2m_service: P2mApiDefaultStack<M> = api::p2m::P2mApiService::new(
164        sequencer,
165        provenance.clone(),
166        compliance.clone(),
167        m2m_client.clone(),
168    )
169    .with_resource_validation(enable_resource_validation);
170
171    let o2m_service: O2mApiDefaultStack<M> =
172        api::o2m::O2mApiService::new(provenance, compliance, consent, m2m_client);
173
174    (m2m_service, p2m_service, o2m_service)
175}