feat(opcua): surgical RemoveVariableNode with NodeDeleted model-change (R2-07 T8)
This commit is contained in:
@@ -71,7 +71,7 @@
|
||||
{
|
||||
"id": "T8",
|
||||
"subject": "Phase 2: OtOpcUaNodeManager.RemoveVariableNode + BuildNodesRemovedModelChange (NodeDeleted) (high-risk)",
|
||||
"status": "pending",
|
||||
"status": "completed",
|
||||
"blockedBy": [
|
||||
"T7"
|
||||
]
|
||||
|
||||
@@ -1664,6 +1664,59 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
|
||||
return e;
|
||||
}
|
||||
|
||||
/// <summary>Build (but do not report) the Part 3 <c>GeneralModelChangeEvent</c> announcing that the node
|
||||
/// at <paramref name="affectedNodeId"/> was DELETED. Mirrors <see cref="BuildNodesAddedModelChange"/>
|
||||
/// exactly — the only differences are <c>Verb = NodeDeleted</c> and that the affected node has already
|
||||
/// been dropped from the maps, so <c>AffectedType</c> resolves to <see cref="NodeId.Null"/> (a valid Part 3
|
||||
/// "type not applicable"; clients re-browse the parent regardless). <c>internal</c> so a node-manager test
|
||||
/// can assert the populated Changes structure at the nearest deterministic seam.</summary>
|
||||
/// <param name="affectedNodeId">The node id of the deleted node.</param>
|
||||
/// <returns>A populated, unreported <see cref="GeneralModelChangeEventState"/>.</returns>
|
||||
internal GeneralModelChangeEventState BuildNodesRemovedModelChange(string affectedNodeId)
|
||||
{
|
||||
var affected = new NodeId(affectedNodeId, NamespaceIndex);
|
||||
var e = new GeneralModelChangeEventState(null);
|
||||
e.Initialize(
|
||||
SystemContext,
|
||||
source: null,
|
||||
severity: EventSeverity.Medium,
|
||||
message: new LocalizedText($"Node deleted: {affected}"));
|
||||
// Part 3 §8.7.4: emitted by the Server object — set SourceNode/SourceName to Server explicitly
|
||||
// (mirrors BuildNodesAddedModelChange).
|
||||
e.SetChildValue(SystemContext, BrowseNames.SourceNode, ObjectIds.Server, false);
|
||||
e.SetChildValue(SystemContext, BrowseNames.SourceName, "Server", false);
|
||||
var change = new ModelChangeStructureDataType
|
||||
{
|
||||
Affected = affected,
|
||||
// The node is already gone from the maps, so its TypeDefinition is not applicable (Null).
|
||||
AffectedType = ResolveAffectedTypeDefinition(affectedNodeId),
|
||||
Verb = (byte)ModelChangeStructureVerbMask.NodeDeleted,
|
||||
};
|
||||
e.SetChildValue(SystemContext, BrowseNames.Changes, new[] { change }, false);
|
||||
return e;
|
||||
}
|
||||
|
||||
/// <summary>Report a pre-built <see cref="GeneralModelChangeEventState"/> OUTSIDE <c>Lock</c> —
|
||||
/// <c>Server.ReportEvent</c> re-enters the server's own subscription/event path, so holding <c>Lock</c>
|
||||
/// across it risks a lock-order inversion (mirrors <see cref="RaiseNodesAddedModelChange"/> /
|
||||
/// <c>ReportNodeShapeChangedEvent</c>). Tolerant: swallow-and-log when eventing is disabled / there are no
|
||||
/// monitored items / the server is shutting down — the node mutation already stands.</summary>
|
||||
/// <param name="e">The pre-built event to report.</param>
|
||||
/// <param name="affectedNodeId">The affected node id (for the diagnostic only).</param>
|
||||
private void ReportModelChangeOutsideLock(GeneralModelChangeEventState e, string affectedNodeId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Server.ReportEvent(SystemContext, e);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
#pragma warning disable CS0618 // Utils.LogError is [Obsolete] in favour of an ITelemetryContext this manager doesn't carry.
|
||||
Utils.LogError(ex, "OtOpcUaNodeManager: failed to report GeneralModelChangeEvent for {0}", affectedNodeId);
|
||||
#pragma warning restore CS0618
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Resolve the TypeDefinition of a materialised node id from the live folder/variable maps for a
|
||||
/// model-change event's <c>AffectedType</c>; <see cref="NodeId.Null"/> when the id is not registered.</summary>
|
||||
/// <param name="nodeId">The folder-scoped node id whose TypeDefinition is wanted.</param>
|
||||
@@ -1746,12 +1799,28 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
|
||||
}
|
||||
}
|
||||
|
||||
// R2-07 Phase 2 (T7 placeholder) — surgical single-node / subtree removal. These return false (the
|
||||
// rebuild-fallback contract) so the tree stays shippable at T7 while T8/T9/T10 implement the real
|
||||
// in-place teardown (mirroring the per-node loops inside RebuildAddressSpace, scoped to the id/subtree,
|
||||
// with a NodeDeleted model-change reported outside Lock). Implemented in T8/T9/T10.
|
||||
/// <inheritdoc cref="ISurgicalAddressSpaceSink.RemoveVariableNode"/>
|
||||
public bool RemoveVariableNode(string variableNodeId) => false;
|
||||
public bool RemoveVariableNode(string variableNodeId)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(variableNodeId);
|
||||
EnsureAddressSpaceCreated();
|
||||
|
||||
GeneralModelChangeEventState e;
|
||||
lock (Lock)
|
||||
{
|
||||
// Unknown id ⇒ the node-manager maps drifted from what the planner believes; return false so the
|
||||
// caller (AddressSpaceApplier) falls back to a full rebuild (resync). Mirrors the per-node
|
||||
// teardown inside RebuildAddressSpace, scoped to this one id.
|
||||
if (!_variables.TryRemove(variableNodeId, out var variable)) return false;
|
||||
variable.Parent?.RemoveChild(variable);
|
||||
PredefinedNodes?.Remove(variable.NodeId);
|
||||
// Drop the historized-tagname registration alongside the variable it maps (Phase C parity).
|
||||
_historizedTagnames.TryRemove(variableNodeId, out _);
|
||||
e = BuildNodesRemovedModelChange(variableNodeId);
|
||||
}
|
||||
ReportModelChangeOutsideLock(e, variableNodeId);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ISurgicalAddressSpaceSink.RemoveAlarmConditionNode"/>
|
||||
public bool RemoveAlarmConditionNode(string alarmNodeId) => false;
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// R2-07 Phase 2 (T8/T9/T10) — surgical IN-PLACE removal on the node manager: a single value variable
|
||||
/// (<see cref="OtOpcUaNodeManager.RemoveVariableNode"/>), a single Part 9 alarm condition
|
||||
/// (<see cref="OtOpcUaNodeManager.RemoveAlarmConditionNode"/>), and an equipment folder + its whole
|
||||
/// descendant subtree with notifier demotion (<see cref="OtOpcUaNodeManager.RemoveEquipmentSubtree"/>).
|
||||
/// Each removal detaches only the scoped node(s), cleans the matching maps, and raises a Part 3
|
||||
/// NodeDeleted model-change; an unknown id returns false (the caller falls back to a full rebuild).
|
||||
/// </summary>
|
||||
public sealed class NodeManagerSurgicalRemoveTests : IDisposable
|
||||
{
|
||||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||||
|
||||
private readonly string _pkiRoot = Path.Combine(
|
||||
Path.GetTempPath(),
|
||||
$"otopcua-surgical-remove-{Guid.NewGuid():N}");
|
||||
|
||||
// ---------- T8: RemoveVariableNode ----------
|
||||
|
||||
/// <summary>Ensure a variable then remove it: it disappears from the maps (TryGetVariable null,
|
||||
/// VariableCount decremented), its historized-tagname registration is dropped, and the call returns
|
||||
/// true.</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Fact]
|
||||
public async Task RemoveVariableNode_drops_variable_and_historian_registration()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
var nm = server.NodeManager!;
|
||||
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1");
|
||||
nm.EnsureVariable("eq-1/A", "eq-1", "A", "Float", writable: false, historianTagname: "Hist.A");
|
||||
nm.EnsureVariable("eq-1/B", "eq-1", "B", "Float", writable: false);
|
||||
var countBefore = nm.VariableCount;
|
||||
nm.TryGetHistorizedTagname("eq-1/A", out _).ShouldBeTrue();
|
||||
|
||||
nm.RemoveVariableNode("eq-1/A").ShouldBeTrue();
|
||||
|
||||
nm.TryGetVariable("eq-1/A").ShouldBeNull();
|
||||
nm.VariableCount.ShouldBe(countBefore - 1);
|
||||
nm.TryGetHistorizedTagname("eq-1/A", out _).ShouldBeFalse();
|
||||
// The sibling variable is untouched.
|
||||
nm.TryGetVariable("eq-1/B").ShouldNotBeNull();
|
||||
|
||||
await host.DisposeAsync();
|
||||
}
|
||||
|
||||
/// <summary>Removing an unknown variable id returns false (map drift ⇒ caller rebuilds).</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Fact]
|
||||
public async Task RemoveVariableNode_unknown_id_returns_false()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
var nm = server.NodeManager!;
|
||||
|
||||
nm.RemoveVariableNode("eq-1/nope").ShouldBeFalse();
|
||||
|
||||
await host.DisposeAsync();
|
||||
}
|
||||
|
||||
/// <summary>The built removed-node event announces the deleted node with verb NodeDeleted.</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Fact]
|
||||
public async Task Built_removed_event_announces_the_deleted_node_with_NodeDeleted_verb()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
var nm = server.NodeManager!;
|
||||
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1");
|
||||
nm.EnsureVariable("eq-1/A", "eq-1", "A", "Float", writable: false);
|
||||
|
||||
var e = nm.BuildNodesRemovedModelChange("eq-1/A");
|
||||
|
||||
e.ShouldNotBeNull();
|
||||
e.Changes.ShouldNotBeNull();
|
||||
var changes = e.Changes.Value;
|
||||
changes.Length.ShouldBe(1);
|
||||
changes[0].Verb.ShouldBe((byte)ModelChangeStructureVerbMask.NodeDeleted);
|
||||
|
||||
await host.DisposeAsync();
|
||||
}
|
||||
|
||||
// ---------- T9: RemoveAlarmConditionNode ----------
|
||||
|
||||
/// <summary>Materialise a native alarm condition then remove it: it disappears from the condition map and
|
||||
/// the native flag is cleared; unknown id returns false.</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Fact]
|
||||
public async Task RemoveAlarmConditionNode_drops_condition_and_native_flag()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
var nm = server.NodeManager!;
|
||||
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1");
|
||||
nm.MaterialiseAlarmCondition("eq-1/OverTemp", "eq-1", "OverTemp", "OffNormalAlarm", 700, isNative: true);
|
||||
nm.TryGetAlarmCondition("eq-1/OverTemp").ShouldNotBeNull();
|
||||
nm.IsNativeAlarmNode("eq-1/OverTemp").ShouldBeTrue();
|
||||
|
||||
nm.RemoveAlarmConditionNode("eq-1/OverTemp").ShouldBeTrue();
|
||||
|
||||
nm.TryGetAlarmCondition("eq-1/OverTemp").ShouldBeNull();
|
||||
nm.IsNativeAlarmNode("eq-1/OverTemp").ShouldBeFalse();
|
||||
|
||||
nm.RemoveAlarmConditionNode("eq-1/OverTemp").ShouldBeFalse(); // already gone ⇒ false
|
||||
|
||||
await host.DisposeAsync();
|
||||
}
|
||||
|
||||
/// <summary>A scripted condition removes cleanly too (no native flag was set).</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Fact]
|
||||
public async Task RemoveAlarmConditionNode_removes_scripted_condition()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
var nm = server.NodeManager!;
|
||||
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1");
|
||||
nm.MaterialiseAlarmCondition("alm-1", "eq-1", "HighTemp", "OffNormalAlarm", 500, isNative: false);
|
||||
|
||||
nm.RemoveAlarmConditionNode("alm-1").ShouldBeTrue();
|
||||
nm.TryGetAlarmCondition("alm-1").ShouldBeNull();
|
||||
|
||||
await host.DisposeAsync();
|
||||
}
|
||||
|
||||
// ---------- T10: RemoveEquipmentSubtree ----------
|
||||
|
||||
/// <summary>Remove an equipment folder carrying a sub-folder, variables (one historized), and a condition:
|
||||
/// every descendant disappears from every map, the notifier registration is demoted, and a SIBLING
|
||||
/// equipment is fully intact.</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Fact]
|
||||
public async Task RemoveEquipmentSubtree_removes_all_descendants_and_leaves_siblings_intact()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
var nm = server.NodeManager!;
|
||||
|
||||
// Target equipment eq-1 with a sub-folder, two variables (one historized), and a native condition.
|
||||
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1");
|
||||
nm.EnsureFolder("eq-1/Diag", parentNodeId: "eq-1", displayName: "Diag");
|
||||
nm.EnsureVariable("eq-1/A", "eq-1", "A", "Float", writable: false, historianTagname: "Hist.A");
|
||||
nm.EnsureVariable("eq-1/Diag/T", "eq-1/Diag", "T", "Float", writable: false);
|
||||
nm.MaterialiseAlarmCondition("eq-1/OverTemp", "eq-1", "OverTemp", "OffNormalAlarm", 700, isNative: true);
|
||||
|
||||
// Sibling equipment eq-2 that must survive untouched.
|
||||
nm.EnsureFolder("eq-2", parentNodeId: null, displayName: "Equipment 2");
|
||||
nm.EnsureVariable("eq-2/S", "eq-2", "S", "Float", writable: false);
|
||||
|
||||
nm.RemoveEquipmentSubtree("eq-1").ShouldBeTrue();
|
||||
|
||||
// Every eq-1 descendant is gone from every map.
|
||||
nm.TryGetFolder("eq-1").ShouldBeNull();
|
||||
nm.TryGetFolder("eq-1/Diag").ShouldBeNull();
|
||||
nm.TryGetVariable("eq-1/A").ShouldBeNull();
|
||||
nm.TryGetVariable("eq-1/Diag/T").ShouldBeNull();
|
||||
nm.TryGetAlarmCondition("eq-1/OverTemp").ShouldBeNull();
|
||||
nm.TryGetHistorizedTagname("eq-1/A", out _).ShouldBeFalse();
|
||||
nm.IsNativeAlarmNode("eq-1/OverTemp").ShouldBeFalse();
|
||||
|
||||
// Sibling eq-2 is fully intact.
|
||||
nm.TryGetFolder("eq-2").ShouldNotBeNull();
|
||||
nm.TryGetVariable("eq-2/S").ShouldNotBeNull();
|
||||
|
||||
// Re-materialising an alarm under eq-2 still works (the notifier machinery was not corrupted by the
|
||||
// eq-1 demotion) — proves no orphaned root-notifier ref broke the event path.
|
||||
Should.NotThrow(() => nm.MaterialiseAlarmCondition("eq-2/Alm", "eq-2", "Alm", "OffNormalAlarm", 300, isNative: false));
|
||||
|
||||
await host.DisposeAsync();
|
||||
}
|
||||
|
||||
/// <summary>Removing an unknown equipment id returns false (map drift ⇒ caller rebuilds).</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
[Fact]
|
||||
public async Task RemoveEquipmentSubtree_unknown_id_returns_false()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
var nm = server.NodeManager!;
|
||||
|
||||
nm.RemoveEquipmentSubtree("eq-nope").ShouldBeFalse();
|
||||
|
||||
await host.DisposeAsync();
|
||||
}
|
||||
|
||||
private async Task<(OpcUaApplicationHost Host, OtOpcUaSdkServer Server)> BootAsync()
|
||||
{
|
||||
var host = new OpcUaApplicationHost(
|
||||
new OpcUaApplicationHostOptions
|
||||
{
|
||||
ApplicationName = "OtOpcUa.SurgicalRemoveTest",
|
||||
ApplicationUri = $"urn:OtOpcUa.SurgicalRemoveTest:{Guid.NewGuid():N}",
|
||||
OpcUaPort = AllocateFreePort(),
|
||||
PublicHostname = "localhost",
|
||||
PkiStoreRoot = _pkiRoot,
|
||||
},
|
||||
Microsoft.Extensions.Logging.Abstractions.NullLogger<OpcUaApplicationHost>.Instance);
|
||||
|
||||
var server = new OtOpcUaSdkServer();
|
||||
await host.StartAsync(server, Ct);
|
||||
return (host, server);
|
||||
}
|
||||
|
||||
private static int AllocateFreePort()
|
||||
{
|
||||
using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
var port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
|
||||
listener.Stop();
|
||||
return port;
|
||||
}
|
||||
|
||||
/// <summary>Cleans up the PKI root directory.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (Directory.Exists(_pkiRoot))
|
||||
{
|
||||
try { Directory.Delete(_pkiRoot, recursive: true); }
|
||||
catch { /* best-effort cleanup */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user