mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-17 05:11:13 +08:00
b25c1f6ad4
# Description Stacked on #4650. Until that is merged, this PR also shows its commits; the six commits of this PR start after `fix(server): keep conformance unit aggregation idempotent and ordered with registrations`. This adds OPC 10000-110 Asset Management Basics 1.01.1 (`http://opcfoundation.org/UA/AMB/`) as a companion SDK, in the pattern of the DI, Machinery, Pumps and Scales packages. The tracking issue #4649 describes the scope, the design and the open questions. In short: | Package | Content | |---|---| | `Opc.Ua.AMB` | the model generated by the source generator from the unmodified NodeSet, plus the contracts server and client share (`AssetFaultSeverity`, `AssetLocationKind`, `MaintenanceStateKind`, `DocumentationLinkProperties`) | | `Opc.Ua.AMB.Server` | a sidecar node manager next to the owner of the Device Integration address space (`AddOpcUaDi`, `AddMachinery`, `AddPumps` or `AddScales`), the asset registry `IAssetManagement` with a fluent builder, `AddAssetManagement()` hosting, memory and file system stores, the AMB conformance units and server facet | | `Opc.Ua.AMB.Client` | `AmbClient`: discovery, identification, health alarm and maintenance streams, documentation links, locations, structure and snapshots, plus `AddAssetManagementClient()` | **Sidecar.** An asset stays in the node manager that owns it. Everything AMB adds to it (`DeviceHealth`, health alarms, maintenance conditions, the DocumentationLinks AddIn, AssetId and the other properties) is created there. The AMB node manager serves the AMB model, the alias categories `Assets`, `AssetsByProductInstanceUri` and `AssetsByAssetId` below `0:Aliases`, the location hierarchies below `0:Locations` and the server-specific alarm and condition types. Owners register assets with the shared registry from their setup, in either startup order. **Implemented conformance units** (Table 54): Asset Identification, Configurable Asset Identification, Discovery by ProductInstanceUri and by AssetId, Health Status Base, Alarms, Root Causes and Alarm Categories, Version Information, Operation Counters (inspected only; AMB defines no counters), DocumentationLinks Base, Edit Base and Edit Advanced, Requirements, Capabilities, Classification, Current and Future Maintenance Activities, Local Time, the three location properties and both location object hierarchies, Sub-assets and Asset relations. The two history units (Health Tracking, Past Maintenance Activities) are not part of this PR. `docs/AMB.md` has the matrix with the rule the server applies for each unit and whether a test checks it over a session. **Dependency on #4650.** The health alarms and maintenance conditions are created through `CreateAlarm<TState>`, and the conformance units follow the registered assets through the recomputation in `ConformanceUnitsManager`. ## Changes Six commits, each building on its own: 1. **`feat(amb)`: model package** (`src/Opc.Ua.AMB/`, about 2.4k lines, of which the NodeSet and the two identifier tables are 1.6k). - The NodeSet is the publication from `UA-Nodeset/latest/AMB`, byte for byte (git blob `21f9475b646e9c5508171277ffe5b74edbeb0a58`). `.gitattributes` checks it out with LF endings, because a test pins its SHA-256. - The published identifier table is vendored unchanged; a derived table pins the symbol-to-NodeId mapping, and the generator fails the build when they disagree. `tools/nodesets/README.md` documents the one new kind of renamed symbol (top-level instances below `0:Aliases` and `0:Locations`). 2. **`feat(amb)`: server package** (`src/Opc.Ua.AMB.Server/`, about 10.7k lines). Node manager, registry and builders, discovery, health, maintenance (`MaintenanceEventStateMachine` with all transitions), documentation links, locations, version information, classification (IRDI dictionary entries below `Server/Dictionaries`) and structure, stores, conformance evaluation, hosting. Log messages use the EventId block 8300 to 8379, after Pumps (8100) and Scales (8200). 3. **`feat(amb)`: client package** (`src/Opc.Ua.AMB.Client/`, about 3.0k lines). Calls AddLink and RemoveLink through the generated `DocumentationLinksTypeClient`. 4. **`test(amb)`: test suite** (`tests/Opc.Ua.AMB.Tests/`, 163 tests, about 8.8k lines; commit 6 adds one more). 5. **`docs(amb)`: guide and samples**: `docs/AMB.md`, links from `README.md`, `docs/README.md`, `docs/Samples.md` and `docs/AliasNames.md`; `samples/AMB/AmbServer` (AMB next to `AddMachinery`: two DI devices and a Machinery press as assets, with a simulation) and `samples/AMB/AmbClient`. The sample server listens on 62553, which no other sample uses. 6. **`feat(amb)`: tell the links users added from the links of the manufacturer.** OPC 10000-110 gives a client no way to see which links `RemoveLink` accepts (§10.5.4: only those `AddLink` created). As §10.5.1 lets vendors add metadata Properties to the links, the server gives every link `AddLink` created, and every such link restored after a restart, the `Boolean` Property `UserLink` (`true`), qualified with its namespace of server-specific types, with a NodeId derived from the persisted link identifier; declared links carry none, and `RemoveLink` deletes it with the link. `DocumentationLinkRecord` gets the additive `IsUserLink`, which `ReadDocumentationLinksAsync` fills with one Browse over all links and one Read of the markers found (comparing the name only, as the namespace is configurable; `false` for a server that does not mark links). The name is shared as `DocumentationLinkProperties.UserLink`. `UA.slnx` and `.azurepipelines/expected-packages.txt` get the new projects and packages in the commit that adds them. All projects target `net48;net8.0;net9.0;net10.0` through the repository properties (`LibTargetFrameworks`, `TestsTargetFrameworks`, `AppTargetFrameworks`); `IsAotCompatible` and `PublishAot` are set from net10.0 on, as in the other companion projects. ## Tests - **`Opc.Ua.AMB.Tests`: 164 tests**, most of them against a hosted server over real client sessions: - the model: the SHA-256 of the NodeSet and the generator behaviours AMB is the first companion model to rely on; - each building block in-process and over a session: discovery through every category with NodeVersion and model change events, AssetId writes, health alarms and maintenance activities through all transitions with acknowledgement, AddLink and RemoveLink, locations and structure, the advertised conformance units and the facet; - a restart with the file system stores (links and AssetId survive); - the `UserLink` marker: present on an added link only, the same NodeId after a restart, removed with `RemoveLink`, and `IsUserLink` on the client in the snapshot, after `AddLink`, after a restart and after `RemoveLink`; - composition with `AddOpcUaDi` (both startup orders), `AddPumps` and `AddMachinery`; - `AmbClient` end to end against a hosted server, including a Machinery machine as an asset. - **Results (macOS, local):** 164 passed / 0 failed on net10.0 and on net8.0. .NET Framework is covered by the fork CI's Windows legs. - **Coverage** of the three AMB assemblies (coverlet with `tests/coverlet.runsettings.xml`, net10.0, generated `*.g.cs`, `*TypeClient` proxies and `*Log` classes excluded): lines 93.9 % (4890 of 5206), branches 84.0 % (1294 of 1539); model 100 %, client 94.5 %, server 93.4 %. - **Build:** the three packages, the tests and the two samples build without warnings for net48, net8.0, net9.0 and net10.0; `UA.slnx` builds in Release (its warnings are all in files this PR does not touch, as on master). `dotnet format` (whitespace, style, analyzers with `--severity info`) reports nothing on the 95 changed C# files. - **Samples:** `AmbServer` and `AmbClient` ran against each other on net10.0: the client discovered the three assets, printed their snapshots and the location hierarchies, added and removed a documentation link and streamed alarms and maintenance. - Fork CI (full matrix incl. macOS) and CodeQL: green on the same five package commits before the rebase onto the current #4650 head — [CI run 37144486107](https://github.com/biancode/UA-.NETStandard/actions/runs/37144486107), [CodeQL run 37144487873](https://github.com/biancode/UA-.NETStandard/actions/runs/37144487873). The run on the current head (rebased, plus the user-link commit; AMB tests 164/164 locally on net10.0) is in progress: [CI run 37203517457](https://github.com/biancode/UA-.NETStandard/actions/runs/37203517457). ## Notes for review - **Size.** About 26.8k lines, larger than each of the companion PRs #4577 to #4580. Without changes to the code it splits into the model, server and server tests, and the client, end-to-end tests, samples and docs on top; the issue asks which you prefer. - **Interfaces on types.** §9.4.1 and §12.1 ask for alarm and condition types that implement `IRootCauseIndicationType` and `IMaintenanceEventType`. The server creates subtypes of the DI alarm types in its own type namespace (`urn:opcua-netstandard:amb:types`); `UseServerDefinedAlarmTypes = false` uses `HasInterface` on the instance instead, and then does not report the units that need the type-level interfaces. - **Conformance unit names** follow Tables 54 to 56; the profile database has no AMB entries yet. The QualifiedNames are in namespace 0, as DI and Machinery publish theirs (question 5 in the issue). - **Generator gap.** Children a NodeSet gives to a top-level instance (here the `NodeVersion` properties of the alias categories) get no identifier constants. The fix changes the identifiers of other generated models, the reference server's among them, so it is not part of this PR; `AmbModelTests.NodeVersionHasNoIdentifierConstant` records it and fails once the generator emits the constants. - **net48.** The code that the SDK so far only built for .NET 8 and later uses the `NET5_0_OR_GREATER` pattern of `JsonEncoder` for `Enum.IsDefined` and API forms that .NET Framework has (`string.Format`, `Guid.ToString(format, provider)`); the tests use a small async file helper instead of `File.WriteAllTextAsync`. - **User links.** The `UserLink` Property is metadata of this server, not an AMB name: commit 6 explains why and how a client uses it. Changed lines of that commit covered: 69/70 (the one line left is the loop continuation for a link that has other Properties but no marker). - **Known limitations** are listed in the guide: the `DeviceHealth` SourceTimestamp across a restart, removing optional maintenance members once set, `PatchIdentifiers`, locations only at the deepest level, and the history units. ## Related Issues - Part of #4649 - Stacked on #4650 ## Checklist - [x] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [ ] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. (The AMB suite on net10.0 and net8.0 locally on macOS; .NET Framework is covered by the fork CI's Windows net48 legs.) - [ ] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [ ] I have addressed **all** PR feedback received. --------- Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>
235 lines
11 KiB
C#
235 lines
11 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.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.AMB.Server.Assets;
|
|
using Opc.Ua.AMB.Server.Maintenance;
|
|
using Opc.Ua.Client;
|
|
using Opc.Ua.Client.TestFramework;
|
|
using Opc.Ua.Di;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.AMB.Tests
|
|
{
|
|
/// <summary>
|
|
/// Follows a maintenance activity over a real client session: every
|
|
/// transition arrives as an event that carries the state of the
|
|
/// <c>MaintenanceEventStateMachineType</c> and the planned date
|
|
/// (OPC 10000-110 §12).
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("AMB")]
|
|
[Category("Hosting")]
|
|
[NonParallelizable]
|
|
public sealed class AmbMaintenanceSessionTests
|
|
{
|
|
[Test]
|
|
public async Task AClientFollowsAMaintenanceActivityAsync()
|
|
{
|
|
string root = Path.Combine(
|
|
TestContext.CurrentContext.WorkDirectory,
|
|
nameof(AmbMaintenanceSessionTests),
|
|
Guid.NewGuid().ToString("N"));
|
|
var planned = new DateTimeUtc(new DateTime(2027, 3, 1, 8, 0, 0, DateTimeKind.Utc));
|
|
(AmbHostedServer server, IAssetHandle asset, _) = await HealthAlarmTests
|
|
.StartWithSensorAsync(
|
|
nameof(AmbMaintenanceSessionTests),
|
|
builder => builder.WithMaintenance(
|
|
"AnnualInspection",
|
|
AmbConditionClass.Inspection,
|
|
details =>
|
|
{
|
|
details.Description = new LocalizedText("Annual inspection.");
|
|
details.PlannedDate = planned;
|
|
}))
|
|
.ConfigureAwait(false);
|
|
await using AmbHostedServer serverScope = server;
|
|
IMaintenanceActivity inspection = asset.Maintenance!.Activities[0];
|
|
var typeId = new NodeId("AssetMaintenanceActivityConditionType", server.Manager.TypeNamespaceIndex);
|
|
|
|
using var clientFixture = new ClientFixture(NUnitTelemetryContext.Create());
|
|
await clientFixture.LoadClientConfigurationAsync(Path.Combine(root, "client-pki")).ConfigureAwait(false);
|
|
using ISession session = await clientFixture.ConnectAsync(
|
|
new Uri(server.EndpointUrl),
|
|
SecurityPolicies.None).ConfigureAwait(false);
|
|
NodeId diMaintenance = ExpandedNodeId.ToNodeId(
|
|
Opc.Ua.Di.ObjectTypeIds.MaintenanceRequiredAlarmType,
|
|
session.NamespaceUris);
|
|
ushort amb = (ushort)session.NamespaceUris.GetIndex(Namespaces.AMB);
|
|
|
|
CreateSubscriptionResponse subscription = await session.CreateSubscriptionAsync(
|
|
null, 50, 1000, 100, 0, true, 0, CancellationToken.None).ConfigureAwait(false);
|
|
CreateMonitoredItemsResponse items = await session.CreateMonitoredItemsAsync(
|
|
null,
|
|
subscription.SubscriptionId,
|
|
TimestampsToReturn.Neither,
|
|
[
|
|
new MonitoredItemCreateRequest
|
|
{
|
|
ItemToMonitor = new ReadValueId
|
|
{
|
|
NodeId = Ua.ObjectIds.Server,
|
|
AttributeId = Attributes.EventNotifier
|
|
},
|
|
MonitoringMode = MonitoringMode.Reporting,
|
|
RequestedParameters = new MonitoringParameters
|
|
{
|
|
ClientHandle = 1,
|
|
QueueSize = 100,
|
|
DiscardOldest = true,
|
|
Filter = new ExtensionObject(new EventFilter
|
|
{
|
|
SelectClauses =
|
|
[
|
|
Field(Ua.ObjectTypeIds.BaseEventType, new QualifiedName("EventType")),
|
|
Field(
|
|
diMaintenance,
|
|
new QualifiedName("MaintenanceState", amb),
|
|
new QualifiedName("CurrentState"),
|
|
new QualifiedName("Id")),
|
|
Field(Ua.ObjectTypeIds.BaseEventType, new QualifiedName("Message")),
|
|
Field(diMaintenance, new QualifiedName("PlannedDate", amb)),
|
|
Field(Ua.ObjectTypeIds.BaseEventType, new QualifiedName("ConditionClassId"))
|
|
],
|
|
WhereClause = new ContentFilter()
|
|
})
|
|
}
|
|
}
|
|
],
|
|
CancellationToken.None).ConfigureAwait(false);
|
|
Assert.That(
|
|
StatusCode.IsGood(items.Results[0].StatusCode),
|
|
Is.True,
|
|
items.Results[0].StatusCode.ToString());
|
|
|
|
await inspection.StartAsync().ConfigureAwait(false);
|
|
await inspection.FinishAsync(new LocalizedText("Inspection done, no findings.")).ConfigureAwait(false);
|
|
List<EventFieldList> events = await CollectEventsAsync(session, subscription.SubscriptionId, typeId, 2)
|
|
.ConfigureAwait(false);
|
|
|
|
Assert.That(events, Has.Count.EqualTo(2), "one event per transition");
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(
|
|
State(events[0]),
|
|
Is.EqualTo(new NodeId(MaintenanceEventStateMachineTypeIds.StateIds.Executing, amb)));
|
|
Assert.That(events[0].EventFields[2].TryGetValue(out LocalizedText started), Is.True);
|
|
Assert.That(started.Text, Is.EqualTo("Annual inspection."));
|
|
Assert.That(events[0].EventFields[3].TryGetValue(out DateTimeUtc date), Is.True);
|
|
Assert.That(date, Is.EqualTo(planned));
|
|
Assert.That(
|
|
events[0].EventFields[4].TryGetValue(out NodeId conditionClass) ? conditionClass : NodeId.Null,
|
|
Is.EqualTo(AmbConditionClass.Inspection.GetTypeId(session.NamespaceUris)));
|
|
Assert.That(
|
|
State(events[1]),
|
|
Is.EqualTo(new NodeId(MaintenanceEventStateMachineTypeIds.StateIds.Finished, amb)));
|
|
Assert.That(events[1].EventFields[2].TryGetValue(out LocalizedText finished), Is.True);
|
|
Assert.That(finished.Text, Is.EqualTo("Inspection done, no findings."));
|
|
});
|
|
}
|
|
|
|
private static NodeId State(EventFieldList fields)
|
|
{
|
|
return fields.EventFields[1].TryGetValue(out NodeId state) ? state : NodeId.Null;
|
|
}
|
|
|
|
private static SimpleAttributeOperand Field(NodeId typeDefinitionId, params QualifiedName[] browsePath)
|
|
{
|
|
return new SimpleAttributeOperand
|
|
{
|
|
TypeDefinitionId = typeDefinitionId,
|
|
BrowsePath = browsePath.ToArrayOf(),
|
|
AttributeId = Attributes.Value
|
|
};
|
|
}
|
|
|
|
private static async Task<List<EventFieldList>> CollectEventsAsync(
|
|
ISession session,
|
|
uint subscriptionId,
|
|
NodeId eventType,
|
|
int expected)
|
|
{
|
|
var collected = new List<EventFieldList>();
|
|
DateTime deadline = DateTime.UtcNow + TimeSpan.FromSeconds(30);
|
|
ArrayOf<SubscriptionAcknowledgement> acknowledgements = [];
|
|
while (collected.Count < expected && DateTime.UtcNow < deadline)
|
|
{
|
|
PublishResponse publish;
|
|
try
|
|
{
|
|
publish = acknowledgements.Count == 0
|
|
? await session.PublishWithTimeoutAsync(1000).ConfigureAwait(false)
|
|
: await session.PublishWithTimeoutAsync(acknowledgements, 1000).ConfigureAwait(false);
|
|
acknowledgements = [];
|
|
}
|
|
catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadRequestTimeout)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (publish.SubscriptionId == subscriptionId &&
|
|
publish.NotificationMessage.SequenceNumber != 0)
|
|
{
|
|
acknowledgements =
|
|
[
|
|
new SubscriptionAcknowledgement
|
|
{
|
|
SubscriptionId = publish.SubscriptionId,
|
|
SequenceNumber = publish.NotificationMessage.SequenceNumber
|
|
}
|
|
];
|
|
}
|
|
|
|
foreach (ExtensionObject notification in publish.NotificationMessage.NotificationData)
|
|
{
|
|
if (!notification.TryGetValue(out EventNotificationList? events) || events == null)
|
|
{
|
|
continue;
|
|
}
|
|
foreach (EventFieldList fields in events.Events)
|
|
{
|
|
if (fields.EventFields.Count > 0 &&
|
|
fields.EventFields[0].TryGetValue(out NodeId type) &&
|
|
type == eventType)
|
|
{
|
|
collected.Add(fields);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return collected;
|
|
}
|
|
}
|
|
}
|