Skip to main content

opcua_server/node_manager/utils/
operations.rs

1use crate::node_manager::{
2    view::{ExternalReferenceRequest, NodeMetadata},
3    NodeManagerCollection, RequestContext,
4};
5use hashbrown::HashMap;
6use opcua_types::{BrowseDescriptionResultMask, NamespaceMap, NodeId};
7
8/// Fetch external references by requesting them from their owning node manager.
9///
10/// This calls `resolve_external_references` on each node manager with the ids
11/// in `ids` that they return `true` on `owns_node` for.
12pub async fn get_node_metadata(
13    context: &RequestContext,
14    node_managers: &impl NodeManagerCollection,
15    ids: &[NodeId],
16) -> Vec<Option<NodeMetadata>> {
17    let mut reqs: Vec<_> = ids
18        .iter()
19        .map(|n| ExternalReferenceRequest::new(n, BrowseDescriptionResultMask::all()))
20        .collect();
21    for mgr in node_managers.iter_node_managers() {
22        let mut owned: Vec<_> = reqs
23            .iter_mut()
24            .filter(|n| mgr.owns_node(n.node_id()))
25            .collect();
26
27        mgr.resolve_external_references(context, &mut owned).await;
28    }
29
30    reqs.into_iter().map(|r| r.into_inner()).collect()
31}
32
33/// Get the namespaces visible to the current user by calling `namespaces_for_user`
34/// on each node manager.
35pub fn get_namespaces_for_user(
36    context: &RequestContext,
37    node_managers: &impl NodeManagerCollection,
38) -> NamespaceMap {
39    let nss: HashMap<_, _> = node_managers
40        .iter_node_managers()
41        .flat_map(|n| n.namespaces_for_user(context))
42        .map(|ns| (ns.namespace_uri, ns.namespace_index))
43        .collect();
44
45    NamespaceMap::new_full(nss)
46}