-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathApplicationsDatabaseBase.cs
More file actions
659 lines (561 loc) · 20.2 KB
/
ApplicationsDatabaseBase.cs
File metadata and controls
659 lines (561 loc) · 20.2 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using Opc.Ua.Security;
using Opc.Ua.Types;
namespace Opc.Ua.Gds.Server.Database
{
[Serializable]
public abstract class ApplicationsDatabaseBase : IApplicationsDatabase
{
public virtual void Initialize()
{
}
public ushort NamespaceIndex { get; set; }
public virtual NodeId RegisterApplication(ApplicationRecordDataType application)
{
if (application == null)
{
throw new ArgumentNullException(nameof(application));
}
if (application.ApplicationUri == null)
{
throw new ArgumentNullException(nameof(application), "ApplicationUri is null");
}
if (!Uri.IsWellFormedUriString(application.ApplicationUri, UriKind.Absolute))
{
throw new ArgumentException(
application.ApplicationUri + " is not a valid URI.",
nameof(application));
}
if (application
.ApplicationType is < ApplicationType.Server or > ApplicationType.DiscoveryServer)
{
throw new ArgumentException(
application.ApplicationType + " is not a valid ApplicationType.",
nameof(application));
}
if (application.ApplicationNames.IsEmpty ||
application.ApplicationNames[0].IsNullOrEmpty)
{
throw new ArgumentException(
"At least one ApplicationName must be provided.",
nameof(application));
}
if (string.IsNullOrEmpty(application.ProductUri))
{
throw new ArgumentException("A ProductUri must be provided.", nameof(application));
}
if (!Uri.IsWellFormedUriString(application.ProductUri, UriKind.Absolute))
{
throw new ArgumentException(
application.ProductUri + " is not a valid URI.",
nameof(application));
}
foreach (string discoveryUrl in application.DiscoveryUrls)
{
if (string.IsNullOrEmpty(discoveryUrl))
{
continue;
}
if (!Uri.IsWellFormedUriString(discoveryUrl, UriKind.Absolute))
{
throw new ArgumentException(
discoveryUrl + " is not a valid URL.",
nameof(application));
}
}
if (application.ApplicationType != ApplicationType.Client)
{
if (application.DiscoveryUrls.IsEmpty)
{
throw new ArgumentException(
"At least one DiscoveryUrl must be provided.",
nameof(application));
}
if (application.ServerCapabilities.IsEmpty)
{
throw new ArgumentException(
"At least one ServerCapability must be provided.",
nameof(application));
}
}
else if (!application.DiscoveryUrls.IsEmpty)
{
throw new ArgumentException(
"DiscoveryUrls must not be specified for clients.",
nameof(application));
}
if (application.ApplicationId.IsNull)
{
return default;
}
// verify node integrity
switch (application.ApplicationId.IdType)
{
case IdType.String:
case IdType.Guid:
return application.ApplicationId.WithNamespaceIndex(NamespaceIndex);
case IdType.Numeric:
case IdType.Opaque:
throw new ArgumentException(
"The ApplicationId has invalid type {0}",
nameof(application));
default:
throw new ArgumentException(
"The ApplicationId has unexpected type {0}",
nameof(application));
}
}
public virtual void UnregisterApplication(NodeId applicationId)
{
ValidateApplicationNodeId(applicationId);
}
public virtual ApplicationRecordDataType GetApplication(NodeId applicationId)
{
ValidateApplicationNodeId(applicationId);
return null;
}
public virtual ApplicationRecordDataType[] FindApplications(string applicationUri)
{
if (string.IsNullOrWhiteSpace(applicationUri))
{
throw new ServiceResultException(
StatusCodes.BadInvalidArgument);
}
return null;
}
public virtual ServerOnNetwork[] QueryServers(
uint startingRecordId,
uint maxRecordsToReturn,
string applicationName,
string applicationUri,
string productUri,
ArrayOf<string> serverCapabilities,
out DateTimeUtc lastCounterResetTime)
{
lastCounterResetTime = DateTimeUtc.MinValue;
return null;
}
public virtual ApplicationDescription[] QueryApplications(
uint startingRecordId,
uint maxRecordsToReturn,
string applicationName,
string applicationUri,
uint applicationType,
string productUri,
ArrayOf<string> serverCapabilities,
out DateTimeUtc lastCounterResetTime,
out uint nextRecordId)
{
lastCounterResetTime = DateTimeUtc.MinValue;
nextRecordId = 0;
if (applicationType > 2)
{
throw new ServiceResultException(
StatusCodes.BadInvalidArgument);
}
if (serverCapabilities.Contains("NA", StringComparer.OrdinalIgnoreCase) &&
serverCapabilities.Count > 1)
{
throw new ServiceResultException(
StatusCodes.BadInvalidArgument);
}
return null;
}
public virtual bool SetApplicationCertificate(
NodeId applicationId,
string certificateTypeId,
ByteString certificate)
{
ValidateApplicationNodeId(applicationId);
return false;
}
public virtual bool GetApplicationCertificate(
NodeId applicationId,
string certificateTypeId,
out ByteString certificate)
{
certificate = default;
ValidateApplicationNodeId(applicationId);
return false;
}
public virtual bool SetApplicationTrustLists(
NodeId applicationId,
string certificateTypeId,
string trustListId)
{
ValidateApplicationNodeId(applicationId);
return false;
}
public virtual bool GetApplicationTrustLists(
NodeId applicationId,
string certificateTypeId,
out string trustListId)
{
trustListId = null;
ValidateApplicationNodeId(applicationId);
return false;
}
/// <summary>
/// Returns true if the target string matches the UA pattern string.
/// The pattern string may include UA wildcards %_\[]!
/// </summary>
/// <param name="target">String to check for a pattern match.</param>
/// <param name="pattern">Pattern to match with the target string.</param>
/// <returns>true if the target string matches the pattern, otherwise false.</returns>
public static bool Match(string target, string pattern)
{
if (string.IsNullOrEmpty(target))
{
return false;
}
if (string.IsNullOrEmpty(pattern))
{
return true;
}
List<string> tokens = Parse(pattern);
int targetIndex = 0;
for (int ii = 0; ii < tokens.Count; ii++)
{
targetIndex = Match(target, targetIndex, tokens, ref ii);
if (targetIndex < 0)
{
return false;
}
}
return targetIndex >= target.Length;
}
/// <summary>
/// Returns true if the pattern string contains a UA pattern.
/// The pattern string may include UA wildcards %_\[]!
/// </summary>
public static bool IsMatchPattern(string pattern)
{
char[] patternChars = ['%', '_', '\\', '[', ']', '!'];
if (string.IsNullOrEmpty(pattern))
{
return false;
}
foreach (char patternChar in patternChars)
{
if (pattern.Contains(patternChar, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
public string ServerCapabilities(ApplicationRecordDataType application)
{
if (application.ApplicationType != ApplicationType.Client &&
application.ServerCapabilities.IsEmpty)
{
throw new ArgumentException(
"At least one Server Capability must be provided.",
nameof(application));
}
if (application.ServerCapabilities.IsEmpty)
{
return string.Empty;
}
var uniqueCapabilities = application.ServerCapabilities.ToList();
var capabilities = new StringBuilder();
uniqueCapabilities.Sort();
foreach (string capability in uniqueCapabilities)
{
if (string.IsNullOrEmpty(capability))
{
continue;
}
if (capabilities.Length > 0)
{
capabilities.Append(',');
}
capabilities.Append(capability);
}
return capabilities.ToString();
}
protected Guid GetNodeIdGuid(NodeId nodeId)
{
if (nodeId.IsNull)
{
throw new ArgumentNullException(nameof(nodeId));
}
if (NamespaceIndex != nodeId.NamespaceIndex ||
!nodeId.TryGetValue(out Guid id))
{
throw new ServiceResultException(StatusCodes.BadNotFound);
}
return id;
}
protected string GetNodeIdString(NodeId nodeId)
{
if (nodeId.IsNull)
{
return null;
}
if (NamespaceIndex != nodeId.NamespaceIndex)
{
throw new ServiceResultException(StatusCodes.BadNodeIdUnknown);
}
if (!nodeId.TryGetValue(out string id))
{
throw new ServiceResultException(StatusCodes.BadNodeIdUnknown);
}
return id;
}
protected void ValidateApplicationNodeId(NodeId nodeId)
{
if (nodeId.IsNull)
{
throw new ArgumentNullException(nameof(nodeId));
}
if ((nodeId.IdType != IdType.Guid && nodeId.IdType != IdType.String) ||
NamespaceIndex != nodeId.NamespaceIndex)
{
throw new ServiceResultException(StatusCodes.BadNotFound);
}
}
private static List<string> Parse(string pattern)
{
var tokens = new List<string>();
int ii = 0;
var buffer = new StringBuilder();
while (ii < pattern.Length)
{
char ch = pattern[ii];
if (ch == '\\')
{
ii++;
if (ii >= pattern.Length)
{
break;
}
buffer.Append(pattern[ii]);
ii++;
continue;
}
if (ch == '_')
{
if (buffer.Length > 0)
{
tokens.Add(buffer.ToString());
buffer.Length = 0;
}
tokens.Add("_");
ii++;
continue;
}
if (ch == '%')
{
if (buffer.Length > 0)
{
tokens.Add(buffer.ToString());
buffer.Length = 0;
}
tokens.Add("%");
ii++;
while (ii < pattern.Length && pattern[ii] == '%')
{
ii++;
}
continue;
}
if (ch == '[')
{
if (buffer.Length > 0)
{
tokens.Add(buffer.ToString());
buffer.Length = 0;
}
buffer.Append(ch);
ii++;
while (ii < pattern.Length && pattern[ii] != ']')
{
if (pattern[ii] == '-' && ii > 0 && ii < pattern.Length - 1)
{
int start = Convert.ToInt32(pattern[ii - 1]) + 1;
int end = Convert.ToInt32(pattern[ii + 1]);
while (start < end)
{
buffer.Append(Convert.ToChar(start));
start++;
}
buffer.Append(Convert.ToChar(end));
ii += 2;
continue;
}
buffer.Append(pattern[ii]);
ii++;
}
buffer.Append(']');
tokens.Add(buffer.ToString());
buffer.Length = 0;
ii++;
continue;
}
buffer.Append(ch);
ii++;
}
if (buffer.Length > 0)
{
tokens.Add(buffer.ToString());
buffer.Length = 0;
}
return tokens;
}
private static int SkipToNext(
string target,
int targetIndex,
IList<string> tokens,
ref int tokenIndex)
{
if (targetIndex >= target.Length - 1)
{
return targetIndex + 1;
}
if (tokenIndex >= tokens.Count - 1)
{
return target.Length + 1;
}
if (!tokens[tokenIndex + 1].StartsWith("[^", StringComparison.Ordinal))
{
int nextTokenIndex = tokenIndex + 1;
// skip over unmatched chars.
while (targetIndex < target.Length &&
Match(target, targetIndex, tokens, ref nextTokenIndex) < 0)
{
targetIndex++;
nextTokenIndex = tokenIndex + 1;
}
nextTokenIndex = tokenIndex + 1;
// skip over duplicate matches.
while (targetIndex < target.Length &&
Match(target, targetIndex, tokens, ref nextTokenIndex) >= 0)
{
targetIndex++;
nextTokenIndex = tokenIndex + 1;
}
// return last match.
if (targetIndex <= target.Length)
{
return targetIndex - 1;
}
}
else
{
int start = targetIndex;
int nextTokenIndex = tokenIndex + 1;
// skip over matches.
while (targetIndex < target.Length &&
Match(target, targetIndex, tokens, ref nextTokenIndex) >= 0)
{
targetIndex++;
nextTokenIndex = tokenIndex + 1;
}
// no match in string.
if (targetIndex < target.Length)
{
return -1;
}
// try the next token.
if (tokenIndex >= tokens.Count - 2)
{
return target.Length + 1;
}
tokenIndex++;
return SkipToNext(target, start, tokens, ref tokenIndex);
}
return -1;
}
private static int Match(
string target,
int targetIndex,
IList<string> tokens,
ref int tokenIndex)
{
if (tokens == null || tokenIndex < 0 || tokenIndex >= tokens.Count)
{
return -1;
}
if (target == null || targetIndex < 0 || targetIndex >= target.Length)
{
if (tokens[tokenIndex] == "%" && tokenIndex == tokens.Count - 1)
{
return targetIndex;
}
return -1;
}
string token = tokens[tokenIndex];
if (token == "_")
{
if (targetIndex >= target.Length)
{
return -1;
}
return targetIndex + 1;
}
if (token == "%")
{
return SkipToNext(target, targetIndex, tokens, ref tokenIndex);
}
if (token.StartsWith('['))
{
bool inverse = false;
bool match = false;
for (int ii = 1; ii < token.Length - 1; ii++)
{
if (token[ii] == '^')
{
inverse = true;
continue;
}
if (!inverse && target[targetIndex] == token[ii])
{
return targetIndex + 1;
}
match |= inverse && target[targetIndex] == token[ii];
}
if (inverse && !match)
{
return targetIndex + 1;
}
return -1;
}
if (target[targetIndex..].StartsWith(token, StringComparison.Ordinal))
{
return targetIndex + token.Length;
}
return -1;
}
}
}