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.
6//!
7//! ## Core Architecture
8//!
9//! The traceability system is built around three main API layers:
10//!
11//! ### Process-to-Middleware (P2M) API
12//! Primary interface for application processes to register resources and request
13//! I/O operations with traceability guarantees. Handles resource enrollment,
14//! authorization requests, and completion reporting.
15//!
16//! ### Middleware-to-Middleware (M2M) API
17//! Enables communication between distributed middleware instances for cross-node
18//! policy evaluation, flow coordination, and provenance synchronization.
19//!
20//! ### Operator-to-Middleware (O2M) API
21//! Administrative interface for policy management, compliance configuration,
22//! and provenance analysis by external operators and organizations.
23//!
24//! ## Main Components
25//!
26//! ### Core Services
27//! - **Sequencer**: Manages flow ordering and resource reservations to prevent race conditions
28//! - **Provenance**: Tracks data provenance across operations
29//! - **Compliance**: Enforces organizational policies and regulatory requirements
30//!
31//! ### Infrastructure Primitives
32//! - **Validation**: Validates incoming requests and resource accessibility
33//! - **Naming**: Provides unified resource identification and naming conventions
34//! - **Error Handling**: Comprehensive error types and handling for operational monitoring
35pub mod api;
36pub mod core;
37pub mod error;
38pub mod m2m;
39pub mod naming;
40pub mod o2m;
41pub mod p2m;
42pub mod validation;
43
44/// Standard M2M API service stack with default component configuration.
45///
46/// Combines sequencer, provenance, and compliance services using the default
47/// waiting queue mechanism for flow coordination. Suitable for most production
48/// deployments requiring standard M2M functionality.
49pub type M2mApiDefaultStack = m2m::M2mApiService<
50 core::sequencer::WaitingQueueService<core::sequencer::SequencerService>,
51 core::provenance::ProvenanceService,
52 core::compliance::ComplianceService,
53>;
54
55/// Standard P2M API service stack parameterized by M2M client type.
56///
57/// Combines sequencer, provenance, and compliance services with a configurable
58/// M2M client for distributed coordination. The generic parameter `M` allows
59/// different M2M transport implementations to be used.
60pub type P2mApiDefaultStack<M> = p2m::P2mApiService<
61 core::sequencer::WaitingQueueService<core::sequencer::SequencerService>,
62 core::provenance::ProvenanceService,
63 core::compliance::ComplianceService,
64 M,
65>;
66
67/// Standard O2M API service stack with default component configuration.
68///
69/// Combines provenance and compliance services for administrative operations.
70/// Does not include sequencer as O2M operations are typically read-heavy
71/// and don't require flow coordination.
72pub type O2mApiDefaultStack =
73 o2m::O2mApiService<core::provenance::ProvenanceService, core::compliance::ComplianceService>;
74
75/// Initialize a complete middleware stack for production deployment.
76///
77/// Creates a fully configured middleware stack with all three API services
78/// (M2M, P2M, O2M) using default component configurations. This is the
79/// standard initialization method for production deployments.
80///
81/// # Arguments
82/// * `node_id` - Unique identifier for this middleware node in the distributed system
83/// * `max_retries` - Maximum retry attempts for the waiting queue (None for unlimited)
84/// * `m2m_client` - Client service for M2M communication with remote middleware
85/// * `enable_resource_validation` - Whether to enable resource validation for P2M requests
86///
87/// # Returns
88/// A tuple containing (M2M service, P2M service, O2M service) ready for use
89///
90/// # Type Parameters
91/// * `M` - M2M client type that implements the required service traits
92pub fn init_middleware<M>(
93 node_id: String,
94 max_retries: Option<u32>,
95 consent_timeout: u64,
96 m2m_client: M,
97 enable_resource_validation: bool,
98) -> (M2mApiDefaultStack, P2mApiDefaultStack<M>, O2mApiDefaultStack)
99where
100 M: tower::Service<
101 api::M2mRequest,
102 Response = api::M2mResponse,
103 Error = error::TraceabilityError,
104 > + Clone
105 + Send
106 + 'static,
107 M::Future: Send,
108{
109 init_middleware_with_enrolled_resources(
110 node_id,
111 max_retries,
112 consent_timeout,
113 m2m_client,
114 enable_resource_validation,
115 0,
116 0,
117 0,
118 )
119}
120
121/// Initialize a middleware stack with pre-enrolled resources for testing.
122///
123/// Creates a middleware stack identical to `init_middleware()` but with
124/// pre-enrolled mock resources. This is useful for testing, benchmarking,
125/// and simulation scenarios where actual process interactions are not needed.
126///
127/// # Arguments
128/// * `node_id` - Unique identifier for this middleware node
129/// * `max_retries` - Maximum retry attempts for the waiting queue
130/// * `m2m_client` - Client service for M2M communication
131/// * `enable_resource_validation` - Whether to enable resource validation for P2M requests
132/// * `process_count` - Number of mock processes to pre-enroll
133/// * `per_process_file_count` - Number of files to enroll per process
134/// * `per_process_stream_count` - Number of streams to enroll per process
135///
136/// # Returns
137/// A tuple containing (M2M service, P2M service, O2M service) with pre-enrolled resources
138///
139/// # Warning
140/// Should only be used for testing purposes. Production deployments should use
141/// `init_middleware()` and rely on actual process enrollment.
142#[allow(clippy::too_many_arguments)]
143pub fn init_middleware_with_enrolled_resources<M>(
144 node_id: String,
145 max_retries: Option<u32>,
146 consent_timeout: u64,
147 m2m_client: M,
148 enable_resource_validation: bool,
149 _process_count: u32,
150 _per_process_file_count: u32,
151 _per_process_stream_count: u32,
152) -> (M2mApiDefaultStack, P2mApiDefaultStack<M>, O2mApiDefaultStack)
153where
154 M: tower::Service<
155 api::M2mRequest,
156 Response = api::M2mResponse,
157 Error = error::TraceabilityError,
158 > + Clone
159 + Send
160 + 'static,
161 M::Future: Send,
162{
163 let sequencer = tower::ServiceBuilder::new()
164 .layer(tower::layer::layer_fn(|inner| {
165 core::sequencer::WaitingQueueService::new(inner, max_retries)
166 }))
167 .service(core::sequencer::SequencerService::default());
168 let provenance = core::provenance::ProvenanceService::new(node_id);
169 let consent = core::consent::ConsentService::new(consent_timeout);
170 let compliance = core::compliance::ComplianceService::new_with_consent(consent);
171
172 let m2m_service: M2mApiDefaultStack =
173 m2m::M2mApiService::new(sequencer.clone(), provenance.clone(), compliance.clone());
174
175 #[cfg(test)]
176 let p2m_service: P2mApiDefaultStack<M> =
177 p2m::P2mApiService::new(sequencer, provenance.clone(), compliance.clone(), m2m_client)
178 .with_resource_validation(enable_resource_validation)
179 .with_enrolled_resources(
180 _process_count,
181 _per_process_file_count,
182 _per_process_stream_count,
183 );
184 #[cfg(not(test))]
185 let p2m_service: P2mApiDefaultStack<M> =
186 p2m::P2mApiService::new(sequencer, provenance.clone(), compliance.clone(), m2m_client)
187 .with_resource_validation(enable_resource_validation);
188
189 let o2m_service: O2mApiDefaultStack = o2m::O2mApiService::new(provenance, compliance);
190
191 (m2m_service, p2m_service, o2m_service)
192}