-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathLdsGdsScanner.cs
More file actions
360 lines (317 loc) · 14.5 KB
/
Copy pathLdsGdsScanner.cs
File metadata and controls
360 lines (317 loc) · 14.5 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
/* ========================================================================
* 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Opc.Ua.Gds.Server.Database;
// Justification: sample diagnostics log exception type/message directly; the
// extra evaluation when logging is disabled is negligible for a sample tool.
#pragma warning disable CA1873
namespace Opc.Ua.Gds.Server
{
/// <summary>
/// A discovered server that is a candidate for registration in the GDS.
/// </summary>
public sealed class LdsScanCandidate
{
/// <summary>The LDS discovery URL the candidate was found through.</summary>
#pragma warning disable CA1056 // Justification: sample DTO keeps the discovery URL as a string, matching the surrounding GDS sample code.
public string SourceLdsUrl { get; set; }
#pragma warning restore CA1056
/// <summary>The <see cref="ServerOnNetwork"/> record returned by FindServersOnNetwork.</summary>
public ServerOnNetwork Network { get; set; }
/// <summary>The resolved application description (from FindServers), when available.</summary>
public ApplicationDescription Application { get; set; }
/// <summary>
/// Maps the candidate onto an <see cref="ApplicationRecordDataType"/>
/// suitable for <see cref="IApplicationsDatabase.RegisterApplication"/>.
/// </summary>
public ApplicationRecordDataType ToApplicationRecord()
{
ApplicationDescription app = Application;
var discoveryUrls = new List<string>();
if (app != null && !app.DiscoveryUrls.IsNull)
{
discoveryUrls.AddRange(app.DiscoveryUrls.ToArray());
}
else if (!string.IsNullOrEmpty(Network?.DiscoveryUrl))
{
discoveryUrls.Add(Network.DiscoveryUrl);
}
var serverCapabilities = new List<string>();
if (Network != null && !Network.ServerCapabilities.IsNull)
{
serverCapabilities.AddRange(Network.ServerCapabilities.ToArray());
}
return new ApplicationRecordDataType
{
ApplicationType = app?.ApplicationType ?? ApplicationType.Server,
ApplicationUri = app?.ApplicationUri,
ProductUri = app?.ProductUri,
ApplicationNames = new LocalizedText[]
{
app?.ApplicationName ?? new LocalizedText(Network?.ServerName ?? string.Empty)
},
DiscoveryUrls = discoveryUrls,
ServerCapabilities = serverCapabilities
};
}
}
/// <summary>
/// Scans one or more Local Discovery Servers (LDS / LDS-ME) for servers on
/// the network and stages them for registration in the GDS.
/// </summary>
/// <remarks>
/// This implements the LDS-to-GDS auto-populate feature described in the GDS
/// README and tracked in issue #329. From the GDS side an LDS is just another
/// discovery endpoint, so the scan is driven with the standard UA client
/// discovery services: <c>FindServersOnNetwork</c> to enumerate what the LDS
/// has observed (including its own mDNS/LDS-ME view) and <c>FindServers</c> to
/// resolve each candidate's full <see cref="ApplicationDescription"/>.
/// Discovered servers are never registered automatically; the host must gate
/// registration behind a <c>DiscoveryAdmin</c> approval before calling
/// <see cref="RegisterApproved"/>.
/// </remarks>
public sealed class LdsGdsScanner
{
private readonly ApplicationConfiguration m_configuration;
private readonly IApplicationsDatabase m_database;
private readonly ITelemetryContext m_telemetry;
private readonly ILogger m_logger;
/// <summary>
/// Creates a scanner bound to the GDS application configuration and
/// applications database.
/// </summary>
public LdsGdsScanner(
ApplicationConfiguration configuration,
IApplicationsDatabase database,
ITelemetryContext telemetry)
{
m_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
m_database = database ?? throw new ArgumentNullException(nameof(database));
m_telemetry = telemetry ?? throw new ArgumentNullException(nameof(telemetry));
m_logger = telemetry.CreateLogger<LdsGdsScanner>();
}
/// <summary>
/// Scans every configured LDS discovery URL and returns the servers that
/// are not yet registered in the GDS database.
/// </summary>
/// <param name="ldsDiscoveryUrls">The LDS discovery URLs to scan.</param>
/// <param name="ct">A cancellation token.</param>
/// <returns>The candidates awaiting approval.</returns>
public async Task<IList<LdsScanCandidate>> ScanAsync(
IEnumerable<string> ldsDiscoveryUrls,
CancellationToken ct = default)
{
if (ldsDiscoveryUrls == null)
{
throw new ArgumentNullException(nameof(ldsDiscoveryUrls));
}
var candidates = new List<LdsScanCandidate>();
var seenApplicationUris = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (string ldsUrl in ldsDiscoveryUrls)
{
if (string.IsNullOrWhiteSpace(ldsUrl))
{
continue;
}
IList<ServerOnNetwork> onNetwork;
try
{
onNetwork = await ListServersOnNetworkAsync(ldsUrl, ct).ConfigureAwait(false);
}
catch (Exception ex)
{
m_logger.LogError(
"LDS scan: FindServersOnNetwork failed for {LdsUrl}. Error=({ExceptionType}){Message}",
ldsUrl, ex.GetType().Name, ex.Message);
continue;
}
foreach (ServerOnNetwork server in onNetwork)
{
if (string.IsNullOrEmpty(server?.DiscoveryUrl))
{
continue;
}
ApplicationDescription application =
await ResolveApplicationAsync(server.DiscoveryUrl, ct).ConfigureAwait(false);
// Skip discovery servers (LDS/GDS) - they are directories, not
// registerable applications.
if (application != null &&
(application.ApplicationType == ApplicationType.DiscoveryServer))
{
continue;
}
string applicationUri = application?.ApplicationUri;
// De-duplicate within this scan and against the GDS database.
if (!string.IsNullOrEmpty(applicationUri))
{
if (!seenApplicationUris.Add(applicationUri))
{
continue;
}
if (IsAlreadyRegistered(applicationUri))
{
m_logger.LogInformation(
"LDS scan: {ApplicationUri} is already registered in the GDS - skipping.",
applicationUri);
continue;
}
}
candidates.Add(new LdsScanCandidate
{
SourceLdsUrl = ldsUrl,
Network = server,
Application = application
});
}
}
m_logger.LogInformation("LDS scan complete: {Count} new candidate(s).", candidates.Count);
return candidates;
}
/// <summary>
/// Registers the approved candidates in the GDS applications database.
/// </summary>
/// <param name="approved">The candidates a DiscoveryAdmin has approved.</param>
/// <returns>The application node ids assigned by the database.</returns>
public IList<NodeId> RegisterApproved(IEnumerable<LdsScanCandidate> approved)
{
if (approved == null)
{
throw new ArgumentNullException(nameof(approved));
}
var registered = new List<NodeId>();
foreach (LdsScanCandidate candidate in approved)
{
ApplicationRecordDataType record = candidate.ToApplicationRecord();
try
{
NodeId applicationId = m_database.RegisterApplication(record);
registered.Add(applicationId);
m_logger.LogInformation(
"LDS scan: registered {ApplicationUri} as {ApplicationId}.",
record.ApplicationUri, applicationId);
}
catch (Exception ex)
{
m_logger.LogError(
"LDS scan: failed to register {ApplicationUri}. Error=({ExceptionType}){Message}",
record.ApplicationUri, ex.GetType().Name, ex.Message);
}
}
return registered;
}
private bool IsAlreadyRegistered(string applicationUri)
{
try
{
ApplicationRecordDataType[] existing = m_database.FindApplications(applicationUri);
return existing != null && existing.Length > 0;
}
catch (Exception ex)
{
m_logger.LogWarning(
"LDS scan: FindApplications failed for {ApplicationUri}. Error=({ExceptionType}){Message}",
applicationUri, ex.GetType().Name, ex.Message);
return false;
}
}
private async Task<IList<ServerOnNetwork>> ListServersOnNetworkAsync(
string ldsUrl,
CancellationToken ct)
{
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(m_configuration);
var results = new List<ServerOnNetwork>();
using DiscoveryClient client = await DiscoveryClient.CreateAsync(
new Uri(ldsUrl),
endpointConfiguration,
m_telemetry,
DiagnosticsMasks.None,
ct).ConfigureAwait(false);
uint startingRecordId = 0;
while (true)
{
(ArrayOf<ServerOnNetwork> servers, _) = await client.FindServersOnNetworkAsync(
startingRecordId,
maxRecordsToReturn: 100,
serverCapabilityFilter: new List<string>(),
ct).ConfigureAwait(false);
if (servers.IsNull || servers.Count == 0)
{
break;
}
results.AddRange(servers.ToArray());
startingRecordId = servers.ToArray().Max(s => s.RecordId) + 1;
}
return results;
}
private async Task<ApplicationDescription> ResolveApplicationAsync(
string discoveryUrl,
CancellationToken ct)
{
try
{
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(m_configuration);
using DiscoveryClient client = await DiscoveryClient.CreateAsync(
new Uri(discoveryUrl),
endpointConfiguration,
m_telemetry,
DiagnosticsMasks.None,
ct).ConfigureAwait(false);
ArrayOf<ApplicationDescription> applications =
await client.FindServersAsync(ArrayOf<string>.Empty, ct).ConfigureAwait(false);
if (applications.IsNull || applications.Count == 0)
{
return null;
}
// Prefer a non-discovery-server description that advertises the
// discovery URL we connected to.
ApplicationDescription[] array = applications.ToArray();
ApplicationDescription match = array.FirstOrDefault(a =>
a.ApplicationType != ApplicationType.DiscoveryServer &&
!a.DiscoveryUrls.IsNull &&
a.DiscoveryUrls.ToArray().Any(u => string.Equals(u, discoveryUrl, StringComparison.OrdinalIgnoreCase)));
return match
?? array.FirstOrDefault(a => a.ApplicationType != ApplicationType.DiscoveryServer)
?? array[0];
}
catch (Exception ex)
{
m_logger.LogWarning(
"LDS scan: FindServers failed for {DiscoveryUrl}. Error=({ExceptionType}){Message}",
discoveryUrl, ex.GetType().Name, ex.Message);
return null;
}
}
}
}
#pragma warning restore CA1873