async-opcua-nodes 0.19.0

OPC UA node representation and import framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::hash::Hasher;

use hashbrown::{Equivalent, HashMap, HashSet};
use opcua_types::{
    node_id::{IdentifierRef, IntoNodeIdRef, NodeIdRef},
    BrowseDirection, Identifier, NodeId,
};

use crate::{ImportedReference, ReferenceDirection, TypeTree};

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
/// Owned OPC-UA reference.
pub struct Reference {
    /// Reference type ID.
    pub reference_type: NodeId,
    /// Target node ID.
    pub target_node: NodeId,
}

// Note, must have same hash and eq implementation as Reference.
#[derive(PartialEq, Eq, Clone, Debug)]
struct ReferenceKey<R: IdentifierRef, R2: IdentifierRef> {
    pub reference_type: NodeIdRef<R2>,
    pub target_node: NodeIdRef<R>,
}

impl<R, R2> std::hash::Hash for ReferenceKey<R, R2>
where
    R: IdentifierRef,
    R2: IdentifierRef,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.reference_type.hash(state);
        self.target_node.hash(state);
    }
}

impl<R, R2> Equivalent<Reference> for ReferenceKey<R, R2>
where
    R: IdentifierRef,
    R2: IdentifierRef,
{
    fn equivalent(&self, key: &Reference) -> bool {
        self.reference_type.equivalent(&key.reference_type)
            && self.target_node.equivalent(&key.target_node)
    }
}

impl<'a> From<&'a Reference> for ReferenceKey<&'a Identifier, &'a Identifier> {
    fn from(value: &'a Reference) -> Self {
        Self {
            reference_type: (&value.reference_type).into_node_id_ref(),
            target_node: (&value.target_node).into_node_id_ref(),
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
/// A borrowed version of an OPC-UA reference.
pub struct ReferenceRef<'a> {
    /// Reference type ID.
    pub reference_type: &'a NodeId,
    /// Target node ID.
    pub target_node: &'a NodeId,
    /// Reference direction.
    pub direction: ReferenceDirection,
}

// Note that there is a potentially significant benefit to using hashbrown directly here,
// (which is what the std HashMap is built on!), since it lets us remove references from
// the hash sets without cloning given node IDs.
#[derive(Debug, Default)]
/// Structure for storing and accessing OPC-UA references.
pub struct References {
    /// References by source node ID.
    by_source: HashMap<NodeId, HashSet<Reference>>,
    /// References by target node ID.
    by_target: HashMap<NodeId, HashSet<Reference>>,
}

impl References {
    /// Create a new empty reference store.
    pub fn new() -> Self {
        Self {
            by_source: HashMap::new(),
            by_target: HashMap::new(),
        }
    }

    /// Insert a list of references.
    pub fn insert<'a, S>(
        &mut self,
        source: &NodeId,
        references: &'a [(&'a NodeId, &S, ReferenceDirection)],
    ) where
        S: Into<NodeId> + Clone,
    {
        for (target, typ, direction) in references {
            let typ: NodeId = (*typ).clone().into();
            match direction {
                ReferenceDirection::Forward => self.insert_reference(source, target, typ),
                ReferenceDirection::Inverse => self.insert_reference(target, source, typ),
            }
        }
    }

    /// Insert a new reference.
    pub fn insert_reference(
        &mut self,
        source_node: &NodeId,
        target_node: &NodeId,
        reference_type: impl Into<NodeId>,
    ) {
        if source_node == target_node {
            panic!("Node id from == node id to {source_node}, self reference is not allowed");
        }

        let forward_refs = match self.by_source.get_mut(source_node) {
            Some(r) => r,
            None => self.by_source.entry(source_node.clone()).or_default(),
        };

        let reference_type = reference_type.into();

        if !forward_refs.insert(Reference {
            reference_type: reference_type.clone(),
            target_node: target_node.clone(),
        }) {
            // If the reference is already added, no reason to try adding it to the inverse.
            return;
        }

        let inverse_refs = match self.by_target.get_mut(target_node) {
            Some(r) => r,
            None => self.by_target.entry(target_node.clone()).or_default(),
        };

        inverse_refs.insert(Reference {
            reference_type,
            target_node: source_node.clone(),
        });
    }

    /// Insert a list of references (source, target, reference type)
    pub fn insert_references<'a>(
        &mut self,
        references: impl Iterator<Item = (&'a NodeId, &'a NodeId, impl Into<NodeId>)>,
    ) {
        for (source, target, typ) in references {
            self.insert_reference(source, target, typ);
        }
    }

    /// Import a reference from a nodeset.
    pub fn import_reference(&mut self, source_node: NodeId, rf: ImportedReference) {
        if source_node == rf.target_id {
            panic!("Node id from == node id to {source_node}, self reference is not allowed");
        }

        let mut source = source_node;
        let mut target = rf.target_id;
        if !rf.is_forward {
            (source, target) = (target, source);
        }

        let forward_refs = match self.by_source.get_mut(&source) {
            Some(r) => r,
            None => self.by_source.entry(source.clone()).or_default(),
        };

        if !forward_refs.insert(Reference {
            reference_type: rf.type_id.clone(),
            target_node: target.clone(),
        }) {
            // If the reference is already added, no reason to try adding it to the inverse.
            return;
        }

        let inverse_refs = match self.by_target.get_mut(&target) {
            Some(r) => r,
            None => self.by_target.entry(target).or_default(),
        };

        inverse_refs.insert(Reference {
            reference_type: rf.type_id,
            target_node: source,
        });
    }

    /// Delete a reference.
    pub fn delete_reference<'a>(
        &mut self,
        source_node: impl IntoNodeIdRef<'a>,
        target_node: impl IntoNodeIdRef<'a>,
        reference_type: impl IntoNodeIdRef<'a>,
    ) -> bool {
        let mut found = false;
        let reference_type = reference_type.into_node_id_ref();
        let source_node = source_node.into_node_id_ref();
        let target_node = target_node.into_node_id_ref();
        let rf = ReferenceKey {
            reference_type,
            target_node,
        };
        found |= self
            .by_source
            .get_mut(&source_node)
            .map(|f| f.remove(&rf))
            .unwrap_or_default();

        let rf = ReferenceKey {
            reference_type,
            target_node: source_node,
        };

        found |= self
            .by_target
            .get_mut(&target_node)
            .map(|f| f.remove(&rf))
            .unwrap_or_default();

        found
    }

    /// Delete references from  the given node.
    /// Optionally deleting references _to_ the given node.
    ///
    /// Returns whether any references were found.
    pub fn delete_node_references<'a>(
        &mut self,
        source_node: impl IntoNodeIdRef<'a>,
        delete_target_references: bool,
    ) -> bool {
        let mut found = false;
        let source_node = source_node.into_node_id_ref();
        let source = self.by_source.remove(&source_node);
        found |= source.is_some();
        if delete_target_references {
            for rf in source.into_iter().flatten() {
                if let Some(rec) = self.by_target.get_mut(&rf.target_node) {
                    rec.remove(&ReferenceKey {
                        reference_type: (&rf.reference_type).into_node_id_ref(),
                        target_node: source_node,
                    });
                }
            }
        }

        let target = self.by_target.remove(&source_node);
        found |= target.is_some();

        if delete_target_references {
            for rf in target.into_iter().flatten() {
                if let Some(rec) = self.by_source.get_mut(&rf.target_node) {
                    rec.remove(&ReferenceKey {
                        reference_type: (&rf.reference_type).into_node_id_ref(),
                        target_node: source_node,
                    });
                }
            }
        }

        found
    }

    /// Return `true` if the given reference exists.
    pub fn has_reference<'a>(
        &self,
        source_node: impl IntoNodeIdRef<'a>,
        target_node: impl IntoNodeIdRef<'a>,
        reference_type: impl IntoNodeIdRef<'a>,
    ) -> bool {
        let reference_type = reference_type.into_node_id_ref();
        let target_node = target_node.into_node_id_ref();
        self.by_source
            .get(&source_node.into_node_id_ref())
            .map(|n| {
                n.contains(&ReferenceKey {
                    reference_type,
                    target_node,
                })
            })
            .unwrap_or_default()
    }

    /// Return an iterator over references matching the given filters.
    pub fn find_references<'a: 'b, 'b>(
        &'a self,
        source_node: impl IntoNodeIdRef<'b>,
        filter: Option<(impl Into<NodeId>, bool)>,
        type_tree: &'b dyn TypeTree,
        direction: BrowseDirection,
    ) -> impl Iterator<Item = ReferenceRef<'a>> + 'b {
        ReferenceIterator::new(
            source_node.into_node_id_ref(),
            direction,
            self,
            filter.map(|f| (f.0.into(), f.1)),
            type_tree,
        )
    }
}

// Handy feature to let us easily return a concrete type from `find_references`.
struct ReferenceIterator<'a, 'b> {
    filter: Option<(NodeId, bool)>,
    type_tree: &'b dyn TypeTree,
    iter_s: Option<hashbrown::hash_set::Iter<'a, Reference>>,
    iter_t: Option<hashbrown::hash_set::Iter<'a, Reference>>,
}

impl<'a> Iterator for ReferenceIterator<'a, '_> {
    type Item = ReferenceRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let inner = self.next_inner()?;

            if let Some(filter) = &self.filter {
                if !filter.1 && inner.reference_type != &filter.0
                    || filter.1
                        && !self
                            .type_tree
                            .is_subtype_of(inner.reference_type, &filter.0)
                {
                    continue;
                }
            }

            break Some(inner);
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let mut lower = 0;
        let mut upper = None;
        if let Some(iter_s) = &self.iter_s {
            let (lower_i, upper_i) = iter_s.size_hint();
            lower = lower_i;
            upper = upper_i;
        }

        if let Some(iter_t) = &self.iter_t {
            let (lower_i, upper_i) = iter_t.size_hint();
            lower += lower_i;
            upper = match (upper, upper_i) {
                (Some(l), Some(r)) => Some(l + r),
                _ => None,
            }
        }

        (lower, upper)
    }
}

impl<'a, 'b> ReferenceIterator<'a, 'b> {
    fn new<R: IdentifierRef + 'b>(
        source_node: NodeIdRef<R>,
        direction: BrowseDirection,
        references: &'a References,
        filter: Option<(NodeId, bool)>,
        type_tree: &'b dyn TypeTree,
    ) -> Self {
        Self {
            filter,
            type_tree,
            iter_s: matches!(direction, BrowseDirection::Both | BrowseDirection::Forward)
                .then(|| references.by_source.get(&source_node))
                .flatten()
                .map(|r| r.iter()),
            iter_t: matches!(direction, BrowseDirection::Both | BrowseDirection::Inverse)
                .then(|| references.by_target.get(&source_node))
                .flatten()
                .map(|r| r.iter()),
        }
    }

    fn next_inner(&mut self) -> Option<ReferenceRef<'a>> {
        if let Some(iter_s) = &mut self.iter_s {
            match iter_s.next() {
                Some(r) => {
                    return Some(ReferenceRef {
                        reference_type: &r.reference_type,
                        target_node: &r.target_node,
                        direction: ReferenceDirection::Forward,
                    })
                }
                None => self.iter_s = None,
            }
        }

        if let Some(iter_t) = &mut self.iter_t {
            match iter_t.next() {
                Some(r) => {
                    return Some(ReferenceRef {
                        reference_type: &r.reference_type,
                        target_node: &r.target_node,
                        direction: ReferenceDirection::Inverse,
                    })
                }
                None => self.iter_t = None,
            }
        }

        None
    }
}