async-opcua-server 0.18.0

OPC UA server API
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use opcua_types::{
    AddNodeAttributes, AddNodesItem, AddNodesResult, AddReferencesItem, DeleteNodesItem,
    DeleteReferencesItem, DiagnosticBits, DiagnosticInfo, ExpandedNodeId, NodeClass, NodeId,
    QualifiedName, StatusCode,
};

use super::IntoResult;

#[derive(Debug, Clone)]
/// Container for a single node being added in an `AddNode` service call.
pub struct AddNodeItem {
    parent_node_id: ExpandedNodeId,
    reference_type_id: NodeId,
    requested_new_node_id: NodeId,
    browse_name: QualifiedName,
    node_class: NodeClass,
    node_attributes: AddNodeAttributes,
    type_definition_id: ExpandedNodeId,
    diagnostic_bits: DiagnosticBits,

    result_node_id: NodeId,
    status: StatusCode,
    diagnostic_info: Option<DiagnosticInfo>,
}

impl AddNodeItem {
    pub(crate) fn new(item: AddNodesItem, diagnostic_bits: DiagnosticBits) -> Self {
        let mut status = StatusCode::BadNotSupported;
        let attributes = match AddNodeAttributes::from_extension_object(item.node_attributes) {
            Ok(attr) => attr,
            Err(e) => {
                status = e;
                AddNodeAttributes::None
            }
        };
        if item.requested_new_node_id.server_index != 0 {
            status = StatusCode::BadNodeIdRejected;
        }

        Self::validate_attributes(item.node_class, &attributes, &mut status);

        if item.reference_type_id.is_null() {
            status = StatusCode::BadReferenceTypeIdInvalid;
        }
        if item.parent_node_id.is_null() {
            status = StatusCode::BadParentNodeIdInvalid;
        }

        match (item.node_class, item.type_definition.is_null()) {
            (NodeClass::Object | NodeClass::Variable, true) => {
                status = StatusCode::BadTypeDefinitionInvalid
            }
            (NodeClass::Object | NodeClass::Variable, false) => (),
            (_, false) => status = StatusCode::BadTypeDefinitionInvalid,
            _ => (),
        }

        Self {
            parent_node_id: item.parent_node_id,
            reference_type_id: item.reference_type_id,
            requested_new_node_id: item.requested_new_node_id.node_id,
            browse_name: item.browse_name,
            node_class: item.node_class,
            node_attributes: attributes,
            type_definition_id: item.type_definition,
            result_node_id: NodeId::null(),
            status,
            diagnostic_info: None,
            diagnostic_bits,
        }
    }

    fn validate_attributes(
        node_class: NodeClass,
        attributes: &AddNodeAttributes,
        status: &mut StatusCode,
    ) {
        match (node_class, attributes) {
            (NodeClass::Object, AddNodeAttributes::Object(_))
            | (NodeClass::Variable, AddNodeAttributes::Variable(_))
            | (NodeClass::Method, AddNodeAttributes::Method(_))
            | (NodeClass::ObjectType, AddNodeAttributes::ObjectType(_))
            | (NodeClass::VariableType, AddNodeAttributes::VariableType(_))
            | (NodeClass::ReferenceType, AddNodeAttributes::ReferenceType(_))
            | (NodeClass::DataType, AddNodeAttributes::DataType(_))
            | (NodeClass::View, AddNodeAttributes::View(_)) => {}
            (NodeClass::Unspecified, _) => *status = StatusCode::BadNodeClassInvalid,
            (_, AddNodeAttributes::None | AddNodeAttributes::Generic(_)) => {}
            _ => *status = StatusCode::BadNodeAttributesInvalid,
        }
    }

    /// Set the result of the operation. `node_id` is the node ID of the created node.
    pub fn set_result(&mut self, node_id: NodeId, status: StatusCode) {
        self.result_node_id = node_id;
        self.status = status;
    }

    /// The requested parent node ID.
    pub fn parent_node_id(&self) -> &ExpandedNodeId {
        &self.parent_node_id
    }

    /// The requested reference type ID.
    pub fn reference_type_id(&self) -> &NodeId {
        &self.reference_type_id
    }

    /// The requested new node ID. May be null, in which case the node manager picks the new
    /// node ID.
    pub fn requested_new_node_id(&self) -> &NodeId {
        &self.requested_new_node_id
    }

    /// Requested browse name of the new node.
    pub fn browse_name(&self) -> &QualifiedName {
        &self.browse_name
    }

    /// Requested node class of the new node.
    pub fn node_class(&self) -> NodeClass {
        self.node_class
    }

    /// Collection of requested attributes for the new node.
    pub fn node_attributes(&self) -> &AddNodeAttributes {
        &self.node_attributes
    }

    /// Requested type definition ID.
    pub fn type_definition_id(&self) -> &ExpandedNodeId {
        &self.type_definition_id
    }

    /// Current result status code.
    pub fn status(&self) -> StatusCode {
        self.status
    }

    /// Header diagnostic bits for requesting operation-level diagnostics.
    pub fn diagnostic_bits(&self) -> DiagnosticBits {
        self.diagnostic_bits
    }

    /// Set diagnostic infos, you don't need to do this if
    /// `diagnostic_bits` are not set.
    pub fn set_diagnostic_info(&mut self, diagnostic_info: DiagnosticInfo) {
        self.diagnostic_info = Some(diagnostic_info);
    }
}

impl IntoResult for AddNodeItem {
    type Result = AddNodesResult;

    fn into_result(self) -> (Self::Result, Option<DiagnosticInfo>) {
        (
            AddNodesResult {
                status_code: self.status,
                added_node_id: self.result_node_id,
            },
            self.diagnostic_info,
        )
    }
}

#[derive(Debug, Clone)]
/// Container for a single reference being added in an `AddReferences` service call.
pub struct AddReferenceItem {
    source_node_id: NodeId,
    reference_type_id: NodeId,
    target_node_id: ExpandedNodeId,
    is_forward: bool,
    diagnostic_bits: DiagnosticBits,

    source_status: StatusCode,
    target_status: StatusCode,
    diagnostic_info: Option<DiagnosticInfo>,
}

impl AddReferenceItem {
    pub(crate) fn new(item: AddReferencesItem, diagnostic_bits: DiagnosticBits) -> Self {
        let mut status = StatusCode::BadNotSupported;
        if item.source_node_id.is_null() {
            status = StatusCode::BadSourceNodeIdInvalid;
        }
        if item.target_node_id.is_null() {
            status = StatusCode::BadTargetNodeIdInvalid;
        }
        if item.reference_type_id.is_null() {
            status = StatusCode::BadReferenceTypeIdInvalid;
        }
        if !item.target_server_uri.is_null() || item.target_node_id.server_index != 0 {
            status = StatusCode::BadReferenceLocalOnly;
        }
        Self {
            source_node_id: item.source_node_id,
            reference_type_id: item.reference_type_id,
            target_node_id: item.target_node_id,
            is_forward: item.is_forward,
            source_status: status,
            target_status: status,
            diagnostic_bits,
            diagnostic_info: None,
        }
    }

    /// Requested source node ID.
    pub fn source_node_id(&self) -> &NodeId {
        &self.source_node_id
    }

    /// Requested reference type ID.
    pub fn reference_type_id(&self) -> &NodeId {
        &self.reference_type_id
    }

    /// Requested target node ID.
    pub fn target_node_id(&self) -> &ExpandedNodeId {
        &self.target_node_id
    }

    /// Current result status, as a summary of source status and target status.
    pub(crate) fn result_status(&self) -> StatusCode {
        if self.source_status.is_good() {
            return self.source_status;
        }
        if self.target_status.is_good() {
            return self.target_status;
        }
        self.source_status
    }

    /// Set the result of this operation for the _source_ end of the reference.
    pub fn set_source_result(&mut self, status: StatusCode) {
        self.source_status = status;
    }

    /// Set the result of this operation for the _target_ end of the reference.
    pub fn set_target_result(&mut self, status: StatusCode) {
        self.target_status = status;
    }

    /// Requested reference direction.
    pub fn is_forward(&self) -> bool {
        self.is_forward
    }

    /// Current target status.
    pub fn target_status(&self) -> StatusCode {
        self.target_status
    }

    /// Current source status.
    pub fn source_status(&self) -> StatusCode {
        self.source_status
    }

    /// Header diagnostic bits for requesting operation-level diagnostics.
    pub fn diagnostic_bits(&self) -> DiagnosticBits {
        self.diagnostic_bits
    }

    /// Set diagnostic infos, you don't need to do this if
    /// `diagnostic_bits` are not set.
    pub fn set_diagnostic_info(&mut self, diagnostic_info: DiagnosticInfo) {
        self.diagnostic_info = Some(diagnostic_info);
    }
}

impl IntoResult for AddReferenceItem {
    type Result = StatusCode;

    fn into_result(self) -> (Self::Result, Option<DiagnosticInfo>) {
        (self.result_status(), self.diagnostic_info)
    }
}

#[derive(Debug)]
/// Container for a single item in a `DeleteNodes` service call.
pub struct DeleteNodeItem {
    node_id: NodeId,
    delete_target_references: bool,
    diagnostic_bits: DiagnosticBits,

    status: StatusCode,
    diagnostic_info: Option<DiagnosticInfo>,
}

impl DeleteNodeItem {
    pub(crate) fn new(item: DeleteNodesItem, diagnostic_bits: DiagnosticBits) -> Self {
        let mut status = StatusCode::BadNodeIdUnknown;
        if item.node_id.is_null() {
            status = StatusCode::BadNodeIdInvalid;
        }

        Self {
            node_id: item.node_id,
            delete_target_references: item.delete_target_references,
            status,
            diagnostic_bits,
            diagnostic_info: None,
        }
    }

    /// Current status of the operation.
    pub fn status(&self) -> StatusCode {
        self.status
    }

    /// Set the result of the node deletion operation.
    pub fn set_result(&mut self, status: StatusCode) {
        self.status = status;
    }

    /// Whether the request should delete references that point to this node or not.
    pub fn delete_target_references(&self) -> bool {
        self.delete_target_references
    }

    /// Node ID to delete.
    pub fn node_id(&self) -> &NodeId {
        &self.node_id
    }

    /// Header diagnostic bits for requesting operation-level diagnostics.
    pub fn diagnostic_bits(&self) -> DiagnosticBits {
        self.diagnostic_bits
    }

    /// Set diagnostic infos, you don't need to do this if
    /// `diagnostic_bits` are not set.
    pub fn set_diagnostic_info(&mut self, diagnostic_info: DiagnosticInfo) {
        self.diagnostic_info = Some(diagnostic_info);
    }
}

impl IntoResult for DeleteNodeItem {
    type Result = StatusCode;

    fn into_result(self) -> (Self::Result, Option<DiagnosticInfo>) {
        (self.status(), self.diagnostic_info)
    }
}

#[derive(Debug)]
/// Container for a single reference being deleted in an `DeleteReferences` service call.
pub struct DeleteReferenceItem {
    source_node_id: NodeId,
    reference_type_id: NodeId,
    is_forward: bool,
    target_node_id: ExpandedNodeId,
    delete_bidirectional: bool,
    diagnostic_bits: DiagnosticBits,

    source_status: StatusCode,
    target_status: StatusCode,
    diagnostic_info: Option<DiagnosticInfo>,
}

impl DeleteReferenceItem {
    pub(crate) fn new(item: DeleteReferencesItem, diagnostic_bits: DiagnosticBits) -> Self {
        let mut status = StatusCode::BadNotSupported;
        if item.source_node_id.is_null() {
            status = StatusCode::BadSourceNodeIdInvalid;
        }
        if item.target_node_id.is_null() {
            status = StatusCode::BadTargetNodeIdInvalid;
        }
        if item.reference_type_id.is_null() {
            status = StatusCode::BadReferenceTypeIdInvalid;
        }
        if item.target_node_id.server_index != 0 {
            status = StatusCode::BadReferenceLocalOnly;
        }

        Self {
            source_node_id: item.source_node_id,
            reference_type_id: item.reference_type_id,
            is_forward: item.is_forward,
            target_node_id: item.target_node_id,
            delete_bidirectional: item.delete_bidirectional,
            diagnostic_bits,

            source_status: status,
            target_status: status,
            diagnostic_info: None,
        }
    }

    /// Source node ID of the reference being deleted.
    pub fn source_node_id(&self) -> &NodeId {
        &self.source_node_id
    }

    /// Reference type ID of the reference being deleted.
    pub fn reference_type_id(&self) -> &NodeId {
        &self.reference_type_id
    }

    /// Target node ID of the reference being deleted.
    pub fn target_node_id(&self) -> &ExpandedNodeId {
        &self.target_node_id
    }

    pub(crate) fn result_status(&self) -> StatusCode {
        if self.source_status.is_good() {
            return self.source_status;
        }
        if self.target_status.is_good() {
            return self.target_status;
        }
        self.source_status
    }

    /// Set the result of this operation for the _source_ end of the reference.
    pub fn set_source_result(&mut self, status: StatusCode) {
        self.source_status = status;
    }

    /// Set the result of this operation for the _target_ end of the reference.
    pub fn set_target_result(&mut self, status: StatusCode) {
        self.target_status = status;
    }

    /// Direction of the reference being deleted.
    pub fn is_forward(&self) -> bool {
        self.is_forward
    }

    /// Current target status.
    pub fn target_status(&self) -> StatusCode {
        self.target_status
    }

    /// Current source status.
    pub fn source_status(&self) -> StatusCode {
        self.source_status
    }

    /// Whether to delete the reference in both directions.
    pub fn delete_bidirectional(&self) -> bool {
        self.delete_bidirectional
    }

    /// Header diagnostic bits for requesting operation-level diagnostics.
    pub fn diagnostic_bits(&self) -> DiagnosticBits {
        self.diagnostic_bits
    }

    /// Set diagnostic infos, you don't need to do this if
    /// `diagnostic_bits` are not set.
    pub fn set_diagnostic_info(&mut self, diagnostic_info: DiagnosticInfo) {
        self.diagnostic_info = Some(diagnostic_info);
    }
}

impl IntoResult for DeleteReferenceItem {
    type Result = StatusCode;

    fn into_result(self) -> (Self::Result, Option<DiagnosticInfo>) {
        (self.result_status(), self.diagnostic_info)
    }
}