Files
Marc Schier 5b9bcedc62 Add OPC UA ISA-95 Common Model and Job Control support (#4089)
# Description

Adds first-class support for the OPC UA ISA-95 companion specifications:

- OPC-10030 Common Model.
- OPC-10031-4 Job Control V1.
- OPC-10031-4 Job Control V2.

The change introduces `Opc.Ua.ISA95`, `Opc.Ua.ISA95.Client`, and
`Opc.Ua.ISA95.Server`, together with a NativeAOT-compatible minimal
server sample and comprehensive tests.

## Highlights

- Compiles the three canonical NodeSets and identifier mapping CSV files
through the source generator.
- Adds NodeSet identifier mapping validation, typed method
arguments/results, companion event records, and generated method
argument nodes.
- Provides typed Common Model builders with normative relationships and
an injectable `GeoSpatialLocationType` provider.
- Implements lossless V1/V2 Job Control providers over a shared
version-neutral store, including state paths, status events, comments,
response queries, catalog changes, and cohesive DI registration.
- Adds subtype-aware typed clients, standalone encodeable registration,
`IIsa95ClientFactory`, and V2 status-event streaming.
- Adds the `MinimalIsa95Server` sample, package documentation, and an
ISA-95 guide with conformance and HA guidance.
- Tracks typed RSL/GPOS integration as follow-up issue #4095.

## Validation

- `Opc.Ua.ISA95.Tests`: 137/137 passed on `net10.0` and `net48`.
- Source-generator suites: 81/81 model, 90/90 stack, 3718 passed / 8
skipped core.
- `EventRecordFieldReaders` feedback tests: 6/6 on `net10.0` and
`net48`.
- `EventRecordFieldReaders` changed-method patch coverage: 100% line
coverage (36/36) and 100% branch coverage (22/22).
- `Opc.Ua.ISA95`, `.Client`, and `.Server` build across all configured
target frameworks without new diagnostics.
- Cobertura line coverage: 88.08% model, 90.04% client, 91.19% server.
- NativeAOT sample publishes and passes a live typed V2
Store/status-event validation.
- The ISA-95 NativeAOT smoke test passes in the source-generated native
test executable.
- OPC UA KB NodeSet validation reports zero errors for Common, V1, and
V2.

The broader NativeAOT executable still reports unrelated existing GDS
and complex-type test failures; the ISA-95-specific native test passes.

## Node state generator fixes

Generating method arguments inline for imported nodesets regressed every
model that already emits `InputArguments` / `OutputArguments` child node
factories:

- The inline block ran before the child factories.
`MethodState.CreateOrReplaceInputArguments` keeps the property that was
assigned first, so the child carrying the well-known NodeId was dropped
and a NodeId-less placeholder remained in the address space. This broke
browsing (reference descriptions without a target) and made method
arguments unreachable at their well-known NodeIds.
- The `MethodDeclarationId` fallback keyed on an autogenerated display
name redirected every declared method of the standard address space to
its own NodeId, which made method calls fail.

The inline block is now emitted after the child factories and reduced to
a value assignment when a child factory exists. The value is only
re-emitted when an argument references a namespace other than OPC UA,
because the value of an imported property is the verbatim XML of the
source nodeset whose namespace indices do not survive the import. The
`MethodDeclarationId` fallback is restricted to declarations without a
resolvable NodeId. The generated OPC UA address space is byte-identical
to `master` again.

## Related Issues

- Fixes #4088
- Follow-up: #4095
2026-07-28 12:01:57 +02:00

363 lines
13 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2026 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.Globalization;
using System.IO;
using System.Linq;
using Opc.Ua.Schema.Model;
namespace Opc.Ua.SourceGeneration
{
/// <summary>
/// The category of an explicit NodeSet identifier sidecar validation error.
/// </summary>
public enum NodesetIdentifierValidationErrorKind
{
/// <summary>
/// The configured sidecar was not provided as an additional file.
/// </summary>
MissingFile,
/// <summary>
/// A CSV symbolic name occurs more than once.
/// </summary>
DuplicateSymbolicName,
/// <summary>
/// A CSV numeric identifier occurs more than once.
/// </summary>
DuplicateNumericId,
/// <summary>
/// A CSV symbol was not imported from the NodeSet.
/// </summary>
UnknownSymbol,
/// <summary>
/// A CSV numeric identifier differs from the NodeSet node identifier.
/// </summary>
NumericIdMismatch,
/// <summary>
/// A CSV node class differs from the NodeSet node class.
/// </summary>
NodeClassMismatch,
/// <summary>
/// An explicit sidecar was configured for more than one NodeSet.
/// </summary>
AssignedToMultipleModels,
/// <summary>
/// A CSV row does not follow the identifier CSV format.
/// </summary>
InvalidRow
}
/// <summary>
/// A validation error for an explicit NodeSet identifier CSV sidecar.
/// </summary>
public sealed record class NodesetIdentifierValidationError
{
/// <summary>
/// The error category.
/// </summary>
public NodesetIdentifierValidationErrorKind Kind { get; init; }
/// <summary>
/// The NodeSet XML file declaring the sidecar.
/// </summary>
public string NodeSetFilePath { get; init; }
/// <summary>
/// The configured or resolved sidecar path.
/// </summary>
public string IdentifierFilePath { get; init; }
/// <summary>
/// The related symbolic name, if applicable.
/// </summary>
public string SymbolicName { get; init; }
/// <summary>
/// The related value, if applicable.
/// </summary>
public string Value { get; init; }
/// <summary>
/// Creates a missing-sidecar validation error.
/// </summary>
public static NodesetIdentifierValidationError MissingFile(
string nodeSetFilePath,
string identifierFilePath)
{
return new NodesetIdentifierValidationError
{
Kind = NodesetIdentifierValidationErrorKind.MissingFile,
NodeSetFilePath = nodeSetFilePath,
IdentifierFilePath = identifierFilePath
};
}
/// <summary>
/// Creates a shared-sidecar validation error.
/// </summary>
public static NodesetIdentifierValidationError AssignedToMultipleModels(
string nodeSetFilePath,
string identifierFilePath)
{
return new NodesetIdentifierValidationError
{
Kind = NodesetIdentifierValidationErrorKind.AssignedToMultipleModels,
NodeSetFilePath = nodeSetFilePath,
IdentifierFilePath = identifierFilePath
};
}
}
internal static class NodesetIdentifierFileValidator
{
public static string ResolvePath(
string nodeSetFilePath,
string identifierFilePath,
IReadOnlyList<string> csvFiles)
{
if (csvFiles == null || csvFiles.Count == 0)
{
return null;
}
string relativePath = identifierFilePath.Trim();
string adjacentPath = Path.IsPathRooted(relativePath)
? relativePath
: Path.Combine(Path.GetDirectoryName(nodeSetFilePath) ?? string.Empty, relativePath);
string directMatch = csvFiles.FirstOrDefault(path =>
string.Equals(path, adjacentPath, StringComparison.Ordinal));
if (directMatch != null)
{
return directMatch;
}
string suffix = relativePath
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
.TrimStart([Path.DirectorySeparatorChar]);
string[] matches = [.. csvFiles
.Where(path => path.EndsWith(suffix, StringComparison.Ordinal))
.OrderBy(path => path, StringComparer.Ordinal)];
return matches.Length == 1 ? matches[0] : null;
}
public static IEnumerable<NodesetIdentifierValidationError> Validate(
IFileSystem fileSystem,
string nodeSetFilePath,
string modelUri,
string identifierFilePath,
ITelemetryContext telemetry)
{
var settings = new NodeSetReaderSettings();
var importer = new NodeSetToModelDesign(
fileSystem,
nodeSetFilePath,
settings,
telemetry);
var symbols = importer
.GetImportedSymbols(modelUri)
.ToDictionary(symbol => symbol.SymbolicName, StringComparer.Ordinal);
List<NodesetIdentifierCsvRow> rows = Parse(identifierFilePath, fileSystem);
var errors = new List<NodesetIdentifierValidationError>();
var names = new HashSet<string>(StringComparer.Ordinal);
var ids = new HashSet<uint>();
foreach (NodesetIdentifierCsvRow row in rows)
{
if (!row.IsValid)
{
errors.Add(CreateError(
NodesetIdentifierValidationErrorKind.InvalidRow,
nodeSetFilePath,
identifierFilePath,
null,
row.LineNumber.ToString(CultureInfo.InvariantCulture)));
continue;
}
if (!names.Add(row.SymbolicName))
{
errors.Add(CreateError(
NodesetIdentifierValidationErrorKind.DuplicateSymbolicName,
nodeSetFilePath,
identifierFilePath,
row.SymbolicName,
null));
}
if (!ids.Add(row.NumericId))
{
errors.Add(CreateError(
NodesetIdentifierValidationErrorKind.DuplicateNumericId,
nodeSetFilePath,
identifierFilePath,
row.SymbolicName,
row.NumericId.ToString(CultureInfo.InvariantCulture)));
}
if (!symbols.TryGetValue(row.SymbolicName, out NodesetImportedSymbol symbol))
{
errors.Add(CreateError(
NodesetIdentifierValidationErrorKind.UnknownSymbol,
nodeSetFilePath,
identifierFilePath,
row.SymbolicName,
null));
continue;
}
if (symbol.NumericId != row.NumericId)
{
errors.Add(CreateError(
NodesetIdentifierValidationErrorKind.NumericIdMismatch,
nodeSetFilePath,
identifierFilePath,
row.SymbolicName,
row.NumericId.ToString(CultureInfo.InvariantCulture)));
}
if (symbol.NodeClass != row.NodeClass)
{
errors.Add(CreateError(
NodesetIdentifierValidationErrorKind.NodeClassMismatch,
nodeSetFilePath,
identifierFilePath,
row.SymbolicName,
row.NodeClass.ToString()));
}
}
return errors;
}
private static List<NodesetIdentifierCsvRow> Parse(
string identifierFilePath,
IFileSystem fileSystem)
{
var rows = new List<NodesetIdentifierCsvRow>();
using Stream stream = fileSystem.OpenRead(identifierFilePath);
using var reader = new StreamReader(stream);
int lineNumber = 0;
while (reader.ReadLine() is string line)
{
lineNumber++;
line = line.TrimStart('\uFEFF').Trim();
if (string.IsNullOrEmpty(line) ||
line.StartsWith('#'))
{
continue;
}
string[] columns = line.Split(',');
if (IsHeader(columns))
{
continue;
}
if (columns.Length != 3 ||
!uint.TryParse(
columns[1].Trim(),
NumberStyles.None,
CultureInfo.InvariantCulture,
out uint numericId) ||
!Enum.TryParse(columns[2].Trim(), true, out NodeClass nodeClass) ||
nodeClass == NodeClass.Unspecified ||
string.IsNullOrWhiteSpace(columns[0]))
{
rows.Add(new NodesetIdentifierCsvRow { LineNumber = lineNumber });
continue;
}
rows.Add(new NodesetIdentifierCsvRow
{
LineNumber = lineNumber,
SymbolicName = columns[0].Trim(),
NumericId = numericId,
NodeClass = nodeClass,
IsValid = true
});
}
return rows;
}
private static bool IsHeader(string[] columns)
{
return columns.Length >= 3 &&
string.Equals(
columns[0].TrimStart('\uFEFF').Trim(),
"SymbolicName",
StringComparison.OrdinalIgnoreCase) &&
(string.Equals(columns[1].Trim(), "Id", StringComparison.OrdinalIgnoreCase) ||
string.Equals(columns[1].Trim(), "NodeId", StringComparison.OrdinalIgnoreCase) ||
string.Equals(columns[1].Trim(), "NumericId", StringComparison.OrdinalIgnoreCase)) &&
string.Equals(columns[2].Trim(), "NodeClass", StringComparison.OrdinalIgnoreCase);
}
private static NodesetIdentifierValidationError CreateError(
NodesetIdentifierValidationErrorKind kind,
string nodeSetFilePath,
string identifierFilePath,
string symbolicName,
string value)
{
return new NodesetIdentifierValidationError
{
Kind = kind,
NodeSetFilePath = nodeSetFilePath,
IdentifierFilePath = identifierFilePath,
SymbolicName = symbolicName,
Value = value
};
}
private sealed record class NodesetIdentifierCsvRow
{
public int LineNumber { get; init; }
public string SymbolicName { get; init; }
public uint NumericId { get; init; }
public NodeClass NodeClass { get; init; }
public bool IsValid { get; init; }
}
}
}