diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs index 47ef5bb04..f474f4704 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs @@ -6567,6 +6567,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage if (projectedNotifier is { } notifierId) { if (!ImportedNotifierAdmits(IdentityOf(context), notifierId)) return StatusCodes.BadUserAccessDenied; + PromoteProjectedEventFolder(notifierId); // A4 F4: admitted ⇒ a notifier, even if it materialised before its authority } else if (SecurityFilteringActive && !unsubscribe && IsHandleInNamespace(sourceId) is { } source diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs index 3daba583f..607766e47 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs @@ -623,6 +623,27 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink if (_importedConditions.Find(id) is { } c) WireImportedFolder(c, folder); } + /// + /// A4 F4: promotes, just before the SDK attaches an ADMITTED event item to it (and refuses a node without + /// SubscribeToEvents, BadNotSupported), a materialised projected folder whose mapping was not yet + /// condition-bearing when it materialised — its authority activates at the consumer's first barrier, after the projected + /// pages were served. Nothing re-promoted it, so a folder-first client on a freshly restarted consumer was refused until an + /// i=2253 item opened demand and a condition got wired there. One dictionary probe; caller holds no lock. + /// + /// The projected notifier the item targets. + private void PromoteProjectedEventFolder(NodeId folderId) + { + if (ProjectedConditionAccess is not { } access) return; + lock (Lock) + { + if (_importedConditions.PromotedFolders.Contains(folderId) || !_importedConditions.FolderKeys.TryGetValue(folderId, out var key)) return; + if (!access.IsConditionBearingMapping(key.Mapping) || FindProjectedFolder(key) is not { } folder) return; + _importedConditions.PromotedFolders.Add(folder.NodeId); + folder.EventNotifier = (byte)(folder.EventNotifier | EventNotifiers.SubscribeToEvents); + folder.ClearChangeMasks(SystemContext, includeChildren: false); + } + } + /// A projected folder is being removed: unwire it symmetrically first (N7) and drop it from the index. Under Lock. private void OnProjectedFolderRemoving(NodeId folderId) { diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs index d279585c3..f2f6c335e 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs @@ -326,6 +326,39 @@ public sealed class ImportedConditionEventWireTests await Events.WaitUntilAsync(() => events.Of(grantedItem).Any(e => Message(e) == "arrived")); } + /// + /// A4 F4: on a freshly restarted consumer the projected folders materialise before the mapping's authority is active, so + /// materialisation does not promote them. A folder-first event item once the authority IS active must still be admitted + /// (it opens the demand that brings the conditions in) — it was refused BadNotSupported by the SDK because nothing + /// re-promoted the folder. + /// + [Fact] + public async Task F4_a_folder_materialised_before_its_mapping_was_condition_bearing_still_admits_a_folder_first_event_item() + { + var ct = TestContext.Current.CancellationToken; + await using var rig = await ServerEventRig.StartAsync("a4-f4-folderfirst", encrypted: false, ct); + var access = new FakeConditionAccess { ConditionBearing = false }; + access.Allowed.Add(Granted); + var f = Fixture.Install(rig, access); + using var granted = await rig.ConnectAsync(anonymous: false, ct); + + // Control (the defect's precondition): while the mapping is not condition-bearing the folder is no notifier — refused. + var early = await SubscribeAsync(granted, new Events(), ct); + var refused = AddItem(early, f.AnchorB); + await early.ApplyChangesAsync(ct); + (refused.Status.Error?.StatusCode ?? StatusCodes.Good).Code.ShouldBe(StatusCodes.BadNotSupported.Code, "not condition-bearing yet"); + + access.ConditionBearing = true; // the authority activates after materialisation (first barrier of a restarted consumer) + var events = new Events(); + var sub = await SubscribeAsync(granted, events, ct); + var item = AddItem(sub, f.AnchorB); + await sub.ApplyChangesAsync(ct); + (item.Status.Error?.StatusCode ?? StatusCodes.Good).Code.ShouldBe(StatusCodes.Good.Code, "folder-first item admitted"); + access.Outstanding.ShouldBe(1, "the folder-only client opened condition demand"); + f.Apply(Update(Key2, version: 3, active: true, acked: false, [f.BPlacement()], message: "arrived-f4")); + await Events.WaitUntilAsync(() => events.Of(item).Any(e => Message(e) == "arrived-f4")); + } + [Fact] public async Task Deviation6_removing_an_imported_condition_sends_RefreshRequired_only_to_the_items_that_could_see_it() { @@ -453,7 +486,9 @@ public sealed class ImportedConditionEventWireTests public long AuthorityGeneration => Interlocked.Read(ref Generation); public bool CanSubscribeFolder(RoleCarryingUserIdentity? caller, string mappingId, string folderLocalSubject) => caller is not null && Allowed.Contains(caller.Principal); - public bool IsConditionBearingMapping(string mappingId) => true; + /// A4 F4: false models a mapping whose authority was not yet active when its folders materialised. + public volatile bool ConditionBearing = true; + public bool IsConditionBearingMapping(string mappingId) => ConditionBearing; /// Leases currently held (acquired minus released). public int Outstanding; public IDisposable? AcquireEventDemand(RoleCarryingUserIdentity? caller) @@ -484,6 +519,11 @@ public sealed class ImportedConditionEventWireTests { var access = new FakeConditionAccess(); access.Allowed.Add(grantedPrincipal); + return Install(rig, access); + } + + public static Fixture Install(ServerEventRig rig, FakeConditionAccess access) + { rig.Nm.ProjectedConditionAccess = access; // bound before projections materialise, as the host does var (anchor, anchorB, device, definition) = Materialise(rig.Nm); return new Fixture { Rig = rig, Access = access, Anchor = anchor, AnchorB = anchorB, DeviceFolder = device, DeviceDefinition = definition };