mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-22 01:26:20 +08:00
e73e71184d
## Summary Adds OPC UA WoT Connectivity 1.1 to the stack, together with the Types, source generation, Server and xRegistry work it is built on. This branch is the merge point for the fourteen-PR stack that replaced integration PR #4093; every constituent PR was reviewed and approved separately and is listed below. 414 files changed, +108,936 / -3,199. Roughly 46k added lines of product code, 57k of tests, plus samples, tools and documentation. ## Types - Complete `ExtensionObject` raw-body decoding for binary, XML and JSON bodies by resolving the concrete type through the message context's encodeable factory, and simplify the `Variant` structure helpers by delegating to it (#4128). - Add `IAtomicFileReplace` as an optional `IFileSystem` capability so existing external implementations keep working, with atomic publish for `LocalFileSystem` via `File.Replace`/`File.Move` and for `VirtualFileSystem` by re-keying the in-memory entry (#4128). - Add lossless conversion between WoT documents and NodeSet2 (`Opc.Ua.Wot.WotNodeSetConverter`). A byte-exact `uav:nodeSet` envelope is preserved when requested, the structured `uav:nodes` projection is used when the readable vocabulary is incomplete, and NodeSet2 is otherwise synthesized from readable WoT terms. Unmapped JSON members survive a round trip as pointer-addressed residue (#4131). ## Source generation - Improve the NodeSet to ModelDesign conversion and the generated node state (#4132). - Generate OPC UA models directly from WoT Thing Description files (#4134). ## Server - Add live NodeManager shadow and immediate reload. A reload materializes a new generation beside the active one and switches atomically; the superseded generation is retired gracefully so existing MonitoredItems keep being served until they drain, or immediately when the caller asks for it. Includes the request admission and drain machinery that lets an orderly shutdown wait for admitted requests instead of tearing down underneath them (#4147). - Make subscription transfer between sessions transactional, so a failed transfer leaves neither session holding a partially moved subscription (#4150). ## xRegistry - Allow clients to use explicit registry roots rather than assuming a well-known location (#4130). ## WoT Connectivity - Add the WoT Connectivity 1.1 information model: a registry-first revision layered on the abstract xRegistry base model, incorporating the complete published OPC 10100-1 v1.02 surface as deprecated nodes in the same namespace so existing 1.02 clients keep working (#4139). - Add the protocol binding abstractions and planners that compile WoT forms into executable plans (#4140). - Add the HTTP, Modbus, OPC UA and MQTT binding executors (#4144). - Add the registry and its stores, including document versioning, validation, dependency resolution and refresh (#4146). - Add the registry client (#4142). - Materialize WoT Thing Descriptions into the server address space, deriving types from Thing Models and instances from Thing Descriptions (#4154). - Restore the WoT asset registry bridge so the deprecated 1.02 asset surface is backed by the registry (#4171). - Add the WoT Connectivity samples and complete the documentation (#4156), and restore the remaining coverage and docs (#4172). ## New projects `src/Opc.Ua.WotCon.Bindings`, `src/Opc.Ua.WotCon.Bindings.Mqtt`, `tests/Opc.Ua.WotCon.Bindings.Tests`, `tests/Opc.Ua.WotCon.Samples.Tests`, and the `samples/WotCon` AggregationServer, AggregationClient and FlatTagServer samples. ## Constituent pull requests Merged into this branch top-down, each independently approved: | PR | Title | | --- | --- | | #4128 | [Types] Complete ExtensionObject decoding and add an atomic file replace capability | | #4131 | [Types] Add lossless conversion between WoT documents and NodeSet2 | | #4132 | [SourceGeneration] Improve NodeSet to ModelDesign conversion and generated node state | | #4134 | [SourceGeneration] Generate OPC UA models from WoT Thing Description files | | #4147 | [Server] Add live NodeManager shadow and immediate reload | | #4150 | [Server] Make subscription transfer between sessions transactional | | #4130 | [XRegistry] Allow clients to use explicit registry roots | | #4139 | [WotCon] Add the WoT Connectivity 1.1 information model | | #4140 | [WotCon] Add the WoT protocol binding abstractions and planners | | #4144 | [WotCon] Add the HTTP, Modbus, OPC UA and MQTT binding executors | | #4146 | [WotCon] Add the WoT Connectivity registry and its stores | | #4142 | [WotCon] Add the WoT Connectivity registry client | | #4154 | [WotCon] Materialize WoT Thing Descriptions into the server address space | | #4156 | [WotCon] Add the WoT Connectivity samples and complete the documentation | | #4171 | Restore WoT asset registry bridge | | #4172 | Restore WoT coverage and docs | Supersedes #4093. ## Follow-up The WoT Connectivity and WoT Binding drafts have moved to 1.1-draft2 since this work was authored. Aligning with that revision - the removed group vocabulary, the new projection/View construct, the remaining model vocabulary terms and the two new portable-identity validation rules - is tracked separately and will follow in its own pull request. ## Validation Built for every target framework with zero warnings and zero errors, and validated at the tip of the stack with `Opc.Ua.WotCon.Tests` at 999 passed / 0 failed and `Opc.Ua.Server.Tests` at 4024 passed / 0 failed. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9e6a5abf-3299-4cd1-9855-010fedbf0ad8
261 lines
14 KiB
C#
261 lines
14 KiB
C#
/* ========================================================================
|
|
* 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.Collections.Immutable;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
|
|
namespace Opc.Ua.SourceGeneration
|
|
{
|
|
/// <summary>
|
|
/// Generates server and client models using the model generator library
|
|
/// </summary>
|
|
[Generator(LanguageNames.CSharp)]
|
|
public sealed class ModelSourceGenerator : IIncrementalGenerator
|
|
{
|
|
/// <inheritdoc/>
|
|
public void Initialize(IncrementalGeneratorInitializationContext context)
|
|
{
|
|
SourceGenerator.AttachDebuggerIfRequested();
|
|
|
|
// Pair every AdditionalFile with its own per-file analyzer config
|
|
// options once, up front, so both the design/NodeSet2 filter and
|
|
// the WoT filter (which needs the per-file
|
|
// ModelSourceGeneratorWot opt-in metadata to recognize a plain
|
|
// .jsonld input) can be evaluated without recomputing options.
|
|
IncrementalValuesProvider<(AdditionalText Text, AnalyzerConfigOptions Options)> textsWithOptions =
|
|
context.AdditionalTextsProvider
|
|
.Combine(context.AnalyzerConfigOptionsProvider)
|
|
.Select(static (pair, _) => (pair.Left, pair.Right.GetOptions(pair.Left)));
|
|
|
|
IncrementalValueProvider<ImmutableArray<(AdditionalText Left, NodesetFileOptions)>> xmlInputFiles =
|
|
textsWithOptions
|
|
.Where(static pair => pair.Text.IsDesignOrNodeset2File())
|
|
.Select(static (pair, _) => (pair.Text, pair.Options.ToNodeSetOptions()))
|
|
.Collect();
|
|
|
|
// Snapshot the structural per-file options before conversion so
|
|
// downstream work remains cheaply cached per file. An ignored WoT
|
|
// input leaves the pipeline here, before its contents are read or
|
|
// it can produce diagnostics, dependencies or a virtual path claim.
|
|
IncrementalValuesProvider<(AdditionalText Text, NodesetFileOptions Options)> wotInputFiles =
|
|
textsWithOptions
|
|
.Where(static pair => pair.Text.IsWotFile(pair.Options))
|
|
.Select(static (pair, _) => (
|
|
pair.Text,
|
|
Options: pair.Options.ToNodeSetOptions()))
|
|
.Where(static pair => !pair.Options.Ignore);
|
|
|
|
// Every active WoT input is converted independently: parse, bounds,
|
|
// missing preservation/native mapping, dependency/resolver and
|
|
// conversion problems are captured as diagnostics on the outcome
|
|
// rather than thrown, so one malformed input can never abort the
|
|
// whole generator run.
|
|
IncrementalValueProvider<ImmutableArray<WotConversionOutcome>> wotOutcomes =
|
|
wotInputFiles
|
|
.Select(static (input, ct) => WotNodeSetAdditionalText.Convert(
|
|
input.Text, input.Options, ct))
|
|
.Collect();
|
|
|
|
// Resolve WoT outcomes against the explicit NodeSet2/ModelDesign
|
|
// inputs and each other: forwards every conversion diagnostic and
|
|
// drops (with a diagnostic) any WoT input whose synthesized
|
|
// virtual NodeSet2 path collides with another input, so a
|
|
// collision can never silently overwrite another model.
|
|
IncrementalValueProvider<(
|
|
ImmutableArray<(AdditionalText Text, NodesetFileOptions Options)> Accepted,
|
|
ImmutableArray<Diagnostic> Diagnostics)> resolvedWotInputs =
|
|
xmlInputFiles
|
|
.Combine(wotOutcomes)
|
|
.Select(static (pair, _) => pair.Left.ResolveWotInputs(pair.Right));
|
|
|
|
context.RegisterSourceOutput(resolvedWotInputs, static (spc, resolved) =>
|
|
{
|
|
foreach (Diagnostic diagnostic in resolved.Diagnostics)
|
|
{
|
|
spc.ReportDiagnostic(diagnostic);
|
|
}
|
|
});
|
|
|
|
IncrementalValueProvider<ImmutableArray<(AdditionalText Left, NodesetFileOptions)>> inputFiles =
|
|
xmlInputFiles
|
|
.Combine(resolvedWotInputs)
|
|
.Select(static (pair, _) => pair.Left.AddRange(pair.Right.Accepted));
|
|
IncrementalValueProvider<ImmutableArray<AdditionalText>> identifierFiles =
|
|
context.AdditionalTextsProvider
|
|
.Where(f => f.IsIdentifierFile())
|
|
.Collect();
|
|
IncrementalValueProvider<ImmutableArray<AdditionalText>> csvFiles =
|
|
context.AdditionalTextsProvider
|
|
.Where(f => f.HasFileExtension("csv"))
|
|
.Collect();
|
|
IncrementalValueProvider<ModelCompilationOptions> options =
|
|
context.AnalyzerConfigOptionsProvider
|
|
.Select((p, _) => ModelCompilationOptions.From(p));
|
|
IncrementalValueProvider<CompilationOptions> settings =
|
|
context.CompilationProvider
|
|
.Select((c, _) => CompilationOptions.From(c));
|
|
IncrementalValueProvider<ImmutableArray<ModelDependencyReference>> referencedModels =
|
|
context.CompilationProvider
|
|
.Select((c, _) => ReferencedModelDependencyScanner.Scan(c));
|
|
IncrementalValueProvider<ImmutableArray<ModelFluentAccessorProviderReference>>
|
|
referencedAccessorProviders = context.CompilationProvider
|
|
.Select((c, _) => ReferencedFluentAccessorProviderScanner.Scan(c));
|
|
IncrementalValueProvider<ImmutableHashSet<string>> stateTypeIndex =
|
|
context.CompilationProvider
|
|
.Select((c, _) => OpcUaStateTypeIndex.Build(c));
|
|
|
|
IncrementalValueProvider<ImmutableArray<NodeManagerAttributeDiscovery>> nodeManagerBindings =
|
|
context.SyntaxProvider.ForAttributeWithMetadataName(
|
|
"Opc.Ua.Server.Fluent.NodeManagerAttribute",
|
|
static (node, ct) => NodeManagerAttributeDiscovery.Handles(node, ct),
|
|
static (ctx, ct) => NodeManagerAttributeDiscovery.Create(ctx, ct))
|
|
.Where(static m => m is not null)
|
|
.Collect();
|
|
|
|
IncrementalValueProvider<
|
|
(
|
|
ImmutableArray<(AdditionalText Left, NodesetFileOptions)> InputFiles,
|
|
ImmutableArray<AdditionalText> CsvFiles,
|
|
ImmutableArray<AdditionalText> IdentifierFiles)> modelFiles = inputFiles
|
|
.Combine(csvFiles)
|
|
.Combine(identifierFiles)
|
|
.Select(static (pair, _) => (
|
|
InputFiles: pair.Left.Left,
|
|
CsvFiles: pair.Left.Right,
|
|
IdentifierFiles: pair.Right));
|
|
IncrementalValueProvider<
|
|
(ModelCompilationOptions Options, CompilationOptions CompilationOptions)>
|
|
modelSettings = options
|
|
.Combine(settings)
|
|
.Select(static (pair, _) => (
|
|
Options: pair.Left,
|
|
CompilationOptions: pair.Right));
|
|
IncrementalValueProvider<
|
|
(
|
|
ImmutableArray<ModelDependencyReference> ReferencedModels,
|
|
ImmutableArray<ModelFluentAccessorProviderReference> ReferencedAccessorProviders,
|
|
ImmutableArray<NodeManagerAttributeDiscovery> NodeManagerBindings)>
|
|
modelReferences = referencedModels
|
|
.Combine(referencedAccessorProviders)
|
|
.Select(static (pair, _) => (
|
|
ReferencedModels: pair.Left,
|
|
ReferencedAccessorProviders: pair.Right))
|
|
.Combine(nodeManagerBindings)
|
|
.Select(static (pair, _) => (
|
|
ReferencedModels: pair.Left.ReferencedModels,
|
|
ReferencedAccessorProviders: pair.Left.ReferencedAccessorProviders,
|
|
NodeManagerBindings: pair.Right));
|
|
IncrementalValueProvider<
|
|
(
|
|
ImmutableArray<ModelDependencyReference> ReferencedModels,
|
|
ImmutableArray<ModelFluentAccessorProviderReference> ReferencedAccessorProviders,
|
|
ImmutableArray<NodeManagerAttributeDiscovery> NodeManagerBindings,
|
|
ImmutableHashSet<string> AvailableStateTypeNames)> modelDependencies =
|
|
modelReferences
|
|
.Combine(stateTypeIndex)
|
|
.Select(static (pair, _) => (
|
|
ReferencedModels: pair.Left.ReferencedModels,
|
|
ReferencedAccessorProviders: pair.Left.ReferencedAccessorProviders,
|
|
NodeManagerBindings: pair.Left.NodeManagerBindings,
|
|
AvailableStateTypeNames: pair.Right));
|
|
IncrementalValueProvider<
|
|
(
|
|
ImmutableArray<(AdditionalText Left, NodesetFileOptions)> InputFiles,
|
|
ImmutableArray<AdditionalText> CsvFiles,
|
|
ImmutableArray<AdditionalText> IdentifierFiles,
|
|
ModelCompilationOptions Options,
|
|
CompilationOptions CompilationOptions)> configuredModel = modelFiles
|
|
.Combine(modelSettings)
|
|
.Select(static (pair, _) => (pair.Left.InputFiles,
|
|
pair.Left.CsvFiles,
|
|
pair.Left.IdentifierFiles,
|
|
pair.Right.Options,
|
|
pair.Right.CompilationOptions));
|
|
IncrementalValueProvider<ModelCompilationInput> modelCompilationInput =
|
|
configuredModel
|
|
.Combine(modelDependencies)
|
|
.Select(static (pair, _) => new ModelCompilationInput(
|
|
pair.Left.InputFiles,
|
|
pair.Left.CsvFiles,
|
|
pair.Left.IdentifierFiles,
|
|
pair.Left.Options,
|
|
pair.Left.CompilationOptions,
|
|
pair.Right.ReferencedModels,
|
|
pair.Right.ReferencedAccessorProviders,
|
|
pair.Right.NodeManagerBindings,
|
|
pair.Right.AvailableStateTypeNames));
|
|
|
|
context.RegisterSourceOutput(
|
|
modelCompilationInput,
|
|
static (context, input) => SourceGenerator.Guard(
|
|
context,
|
|
() => new ModelCompilation(
|
|
context,
|
|
input.InputFiles,
|
|
input.CsvFiles,
|
|
input.IdentifierFiles,
|
|
input.Options,
|
|
input.CompilationOptions,
|
|
input.ReferencedModels,
|
|
input.ReferencedAccessorProviders,
|
|
input.NodeManagerBindings,
|
|
input.AvailableStateTypeNames).Emit(context.CancellationToken)));
|
|
|
|
IncrementalValueProvider<bool> publicDataTypeExtensions =
|
|
context.AnalyzerConfigOptionsProvider
|
|
.Select((p, _) => p.GlobalOptions.GetBool(
|
|
"PublicDataTypeExtensions"));
|
|
|
|
context.RegisterSourceOutput(context.SyntaxProvider.ForAttributeWithMetadataName(
|
|
"Opc.Ua.DataTypeAttribute",
|
|
static (node, ct) => DataTypeCompilation.Handles(node, ct),
|
|
static (context, ct) => new DataTypeCompilation(context, ct))
|
|
.Where(static m => m is not null)
|
|
.Collect()
|
|
.Combine(publicDataTypeExtensions),
|
|
static (spc, pair) => SourceGenerator.Guard(
|
|
spc,
|
|
() => DataTypeCompilation.EmitBatch(
|
|
spc, pair.Left, pair.Right)));
|
|
}
|
|
|
|
private readonly record struct ModelCompilationInput(
|
|
ImmutableArray<(AdditionalText, NodesetFileOptions)> InputFiles,
|
|
ImmutableArray<AdditionalText> CsvFiles,
|
|
ImmutableArray<AdditionalText> IdentifierFiles,
|
|
ModelCompilationOptions Options,
|
|
CompilationOptions CompilationOptions,
|
|
ImmutableArray<ModelDependencyReference> ReferencedModels,
|
|
ImmutableArray<ModelFluentAccessorProviderReference> ReferencedAccessorProviders,
|
|
ImmutableArray<NodeManagerAttributeDiscovery> NodeManagerBindings,
|
|
ImmutableHashSet<string> AvailableStateTypeNames);
|
|
}
|
|
}
|