Skip to main content

mabi_opcua/
lib.rs

1//! # trap-sim-opcua
2//!
3//! OPC UA server simulator for the TRAP protocol simulator.
4//!
5//! This crate provides a comprehensive OPC UA server simulation capability with:
6//!
7//! - **Server Configuration**: Flexible server setup with security policies and endpoints
8//! - **Address Space Management**: Hierarchical node organization with 100,000+ node support
9//! - **Node Types**: Full support for Objects, Variables, Methods, and Type nodes
10//! - **Subscriptions**: Data change monitoring with 10,000+ concurrent subscription support
11//! - **Historical Access**: Raw and aggregated historical data with configurable retention
12//! - **High Performance**: LRU caching, concurrent access, and efficient memory management
13//!
14//! ## Architecture
15//!
16//! ```text
17//! ┌─────────────────────────────────────────────────────────┐
18//! │                    OPC UA Server                         │
19//! ├─────────────────────────────────────────────────────────┤
20//! │  ┌─────────────┐  ┌──────────────┐  ┌───────────────┐   │
21//! │  │   Session   │  │ Subscription │  │    History    │   │
22//! │  │   Manager   │  │   Manager    │  │    Store      │   │
23//! │  └─────────────┘  └──────────────┘  └───────────────┘   │
24//! ├─────────────────────────────────────────────────────────┤
25//! │  ┌─────────────────────────────────────────────────────┐│
26//! │  │              Address Space (Nodes)                   ││
27//! │  │  ┌─────────┐ ┌──────────┐ ┌────────┐ ┌───────────┐  ││
28//! │  │  │ Objects │ │ Variables│ │ Methods│ │   Types   │  ││
29//! │  │  └─────────┘ └──────────┘ └────────┘ └───────────┘  ││
30//! │  └─────────────────────────────────────────────────────┘│
31//! ├─────────────────────────────────────────────────────────┤
32//! │  ┌───────────────┐  ┌──────────────────────────────────┐│
33//! │  │  Node Cache   │  │     Variable Factory             ││
34//! │  │    (LRU)      │  │  (Analog, Discrete, Batch)       ││
35//! │  └───────────────┘  └──────────────────────────────────┘│
36//! └─────────────────────────────────────────────────────────┘
37//! ```
38//!
39//! ## Quick Start
40//!
41//! ```rust,no_run
42//! use mabi_opcua::{
43//!     OpcUaServerConfig, OpcUaDevice,
44//!     types::{NodeId, Variant, DataValue},
45//!     nodes::{AddressSpace, AddressSpaceConfig, VariableBuilder, NodeBuilder},
46//!     services::{SubscriptionManager, SubscriptionManagerConfig, HistoryStore, HistoryStoreConfig},
47//! };
48//!
49//! // Create an OPC UA device
50//! let mut device = OpcUaDevice::new("opc-server-1", "My OPC UA Server");
51//!
52//! // Create address space with nodes
53//! let address_space = AddressSpace::new(AddressSpaceConfig::default());
54//!
55//! // Add a variable node using the address space API
56//! address_space.add_variable(
57//!     NodeId::numeric(2, 1001),
58//!     "Temperature",
59//!     "Temperature",
60//!     NodeId::numeric(0, 11), // Double data type
61//!     Variant::Double(25.0),
62//!     &NodeId::numeric(0, 85), // Objects folder
63//! ).unwrap();
64//!
65//! // Create subscription manager for data changes
66//! let subscriptions = SubscriptionManager::new();
67//!
68//! // Create history store for historical access
69//! let history = HistoryStore::new(HistoryStoreConfig::default());
70//! ```
71//!
72//! ## Module Organization
73//!
74//! - [`types`]: Core OPC UA types (NodeId, Variant, DataValue, etc.)
75//! - [`nodes`]: Node classes and address space management
76//! - [`services`]: Session, subscription, and history services
77//! - [`security`]: Security policies, certificates, encryption, and authentication
78//! - [`config`]: Server configuration
79//! - [`device`]: Device trait implementation
80//! - [`factory`]: Device factory for creating OPC UA devices
81
82pub mod codec;
83pub mod config;
84pub mod device;
85pub mod error;
86pub mod factory;
87pub mod nodes;
88pub mod security;
89pub mod server;
90pub mod services;
91pub mod transport;
92pub mod channel;
93pub mod service;
94pub mod types;
95
96// Re-exports for convenience
97pub use config::{
98    OpcUaServerConfig, SecurityPolicy, MessageSecurityMode, EndpointConfig, UserTokenConfig,
99};
100pub use device::OpcUaDevice;
101pub use error::{OpcUaError, OpcUaResult};
102pub use factory::{OpcUaDeviceFactory, OpcUaDeviceBuilder};
103pub use server::{OpcUaServer, OpcUaServerBuilder, ServerState, ServerStats, ServerEvent};
104
105// Type re-exports
106pub use types::{
107    NodeId, StatusCode, Variant, DataValue, AttributeId, AccessLevel, DataTypeId,
108};
109
110// Node re-exports
111pub use nodes::{
112    Node, NodeClass, NodeBase, QualifiedName, LocalizedText,
113    ObjectNode, VariableNode, MethodNode,
114    AddressSpace, AddressSpaceConfig, NodeStoreStats,
115    VariableBuilder, ObjectBuilder, FolderBuilder, BatchVariableBuilder, AddToAddressSpace,
116    NodeCache, NodeCacheConfig, CacheStats,
117    Reference, ReferenceTypeId, BrowseDirection, BrowseResult,
118    VariableFactory, AnalogVariable, DiscreteVariable,
119    // Batch node creation
120    BatchNodeCreator, BatchConfig, BatchProgress, ProgressCallback,
121    VariableTemplate, ObjectTemplate, ValueGeneratorType,
122    // Prefetching
123    NodePrefetcher, PrefetchConfig, PrefetchStats,
124    AsyncPrefetchWorker, PrefetchingAddressSpace,
125};
126
127// Service re-exports
128pub use services::{
129    SessionManager, SessionManagerConfig, Session, SessionInfo,
130    SubscriptionManager, SubscriptionManagerConfig, Subscription, SubscriptionConfig,
131    MonitoredItem, MonitoredItemConfig, MonitoredItemNotification, MonitoringMode,
132    DataChangeFilter, DataChangeTrigger, DeadbandType,
133    HistoryStore, HistoryStoreConfig, HistoricalDataPoint, AggregateType,
134};
135
136// Security re-exports
137pub use security::{
138    SecurityManager, SecurityManagerConfig, SecurityContext,
139    CertificateManager, CertificateManagerConfig, Certificate, CertificateStore,
140    CertificateValidator, ValidationResult,
141    CryptoProvider, CryptoProviderConfig, SymmetricAlgorithm, AsymmetricAlgorithm,
142    HashAlgorithm, EncryptionResult, DecryptionResult, SignatureResult,
143    UserAuthenticator, UserAuthConfig, AuthenticationResult, UserCredentials, UserToken,
144    SecurityPolicyConfig, SecurityPolicyProvider,
145};
146
147/// Crate version.
148pub const VERSION: &str = env!("CARGO_PKG_VERSION");
149
150/// OPC UA specification version supported.
151pub const OPCUA_SPEC_VERSION: &str = "1.04";
152
153/// Maximum recommended nodes for optimal performance.
154pub const MAX_RECOMMENDED_NODES: usize = 100_000;
155
156/// Maximum recommended subscriptions for optimal performance.
157pub const MAX_RECOMMENDED_SUBSCRIPTIONS: usize = 10_000;
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_version() {
165        assert!(!VERSION.is_empty());
166    }
167
168    #[test]
169    fn test_spec_version() {
170        assert_eq!(OPCUA_SPEC_VERSION, "1.04");
171    }
172}