fix(dcl): apply native-alarm conditionFilter (client-side gate + OPC UA WhereClause) (#8)
conditionFilter was plumbed end-to-end but applied nowhere — a filtered source silently mirrored all conditions. Define the filter as a comma-separated, case-insensitive list of condition type names (blank = all); enforce it authoritatively client-side in DataConnectionActor routing (uniform across OPC UA + MxGateway) and, for OPC UA, additionally build a server-side EventFilter WhereClause as a bandwidth optimization.
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
||||
using Opc.Ua;
|
||||
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer;
|
||||
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Adapters;
|
||||
|
||||
/// <summary>
|
||||
/// M2.4 (#8): the OPC UA EventFilter gains a server-side <see cref="ContentFilter"/>
|
||||
/// WhereClause as a bandwidth optimisation when a condition-type filter is present.
|
||||
/// The client-side gate in DataConnectionActor remains authoritative; these tests
|
||||
/// only pin the filter-shaping. No live server required — pure SDK object building.
|
||||
/// </summary>
|
||||
public class RealOpcUaClientAlarmFilterTests
|
||||
{
|
||||
[Fact]
|
||||
public void BuildAlarmEventFilter_NoFilter_HasNoWhereClause()
|
||||
{
|
||||
var filter = RealOpcUaClient.BuildAlarmEventFilter(AlarmConditionFilter.AllowAll);
|
||||
Assert.NotEmpty(filter.SelectClauses);
|
||||
Assert.Empty(filter.WhereClause.Elements);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildAlarmEventFilter_WithKnownTypes_BuildsNonEmptyWhereClause()
|
||||
{
|
||||
var parsed = AlarmConditionFilter.Parse("LimitAlarmType,DiscreteAlarmType");
|
||||
var filter = RealOpcUaClient.BuildAlarmEventFilter(parsed);
|
||||
|
||||
Assert.NotEmpty(filter.WhereClause.Elements);
|
||||
// Two known types → two OfType operands (OR'd when more than one).
|
||||
var ofTypeCount = filter.WhereClause.Elements.Count(e => e.FilterOperator == FilterOperator.OfType);
|
||||
Assert.Equal(2, ofTypeCount);
|
||||
Assert.Contains(filter.WhereClause.Elements, e => e.FilterOperator == FilterOperator.Or);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildAlarmEventFilter_SingleKnownType_BuildsSingleOfType_NoOr()
|
||||
{
|
||||
var parsed = AlarmConditionFilter.Parse("AlarmConditionType");
|
||||
var filter = RealOpcUaClient.BuildAlarmEventFilter(parsed);
|
||||
|
||||
Assert.Single(filter.WhereClause.Elements);
|
||||
Assert.Equal(FilterOperator.OfType, filter.WhereClause.Elements[0].FilterOperator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildAlarmEventFilter_TypeMatchingIsCaseInsensitive()
|
||||
{
|
||||
var parsed = AlarmConditionFilter.Parse("limitalarmtype");
|
||||
var filter = RealOpcUaClient.BuildAlarmEventFilter(parsed);
|
||||
Assert.Single(filter.WhereClause.Elements, e => e.FilterOperator == FilterOperator.OfType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildAlarmEventFilter_AllUnknownTypes_OmitsWhereClause()
|
||||
{
|
||||
// Custom/vendor type names we cannot map to standard NodeIds are skipped
|
||||
// server-side; the client-side gate still enforces them. Omitting the
|
||||
// WhereClause is the safe choice — a partial WhereClause would drop the
|
||||
// unmapped types at the server and break correctness.
|
||||
var parsed = AlarmConditionFilter.Parse("MyVendorCustomAlarm,AnotherCustomThing");
|
||||
var filter = RealOpcUaClient.BuildAlarmEventFilter(parsed);
|
||||
Assert.Empty(filter.WhereClause.Elements);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildAlarmEventFilter_MixedKnownAndUnknown_OmitsWhereClause()
|
||||
{
|
||||
// If ANY requested type can't be mapped, a server-side WhereClause would
|
||||
// silently drop that type's events — so we omit the optimisation entirely
|
||||
// and let the (authoritative) client gate do the filtering.
|
||||
var parsed = AlarmConditionFilter.Parse("LimitAlarmType,MyVendorCustomAlarm");
|
||||
var filter = RealOpcUaClient.BuildAlarmEventFilter(parsed);
|
||||
Assert.Empty(filter.WhereClause.Elements);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Alarms;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// M2.4 (#8): the alarm conditionFilter is a comma-separated, case-insensitive
|
||||
/// list of condition type names. Blank = allow all. These tests pin the
|
||||
/// parse-once / IsAllowed predicate that the DataConnectionActor uses as the
|
||||
/// authoritative client-side gate.
|
||||
/// </summary>
|
||||
public class AlarmConditionFilterTests
|
||||
{
|
||||
private static NativeAlarmTransition Tx(string typeName,
|
||||
AlarmTransitionKind kind = AlarmTransitionKind.Raise) =>
|
||||
new("ref", "obj", typeName, kind,
|
||||
new AlarmConditionState(true, false, null, AlarmShelveState.Unshelved, false, 500),
|
||||
"cat", "desc", "msg", "", "", null, DateTimeOffset.UtcNow, "1", "0");
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData(",")]
|
||||
[InlineData(" , , ")]
|
||||
public void NullOrBlankFilter_IsEmpty_AllowsEverything(string? filter)
|
||||
{
|
||||
var f = AlarmConditionFilter.Parse(filter);
|
||||
Assert.True(f.IsEmpty);
|
||||
Assert.True(f.IsAllowed(Tx("AnalogLimit.Hi")));
|
||||
Assert.True(f.IsAllowed(Tx("anything-at-all")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_SplitsCommaSeparatedList()
|
||||
{
|
||||
var f = AlarmConditionFilter.Parse("AnalogLimit.Hi,DiscreteAlarm,AnalogLimit.Lo");
|
||||
Assert.False(f.IsEmpty);
|
||||
Assert.True(f.IsAllowed(Tx("AnalogLimit.Hi")));
|
||||
Assert.True(f.IsAllowed(Tx("DiscreteAlarm")));
|
||||
Assert.True(f.IsAllowed(Tx("AnalogLimit.Lo")));
|
||||
Assert.False(f.IsAllowed(Tx("AnalogLimit.HiHi")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAllowed_IsCaseInsensitive()
|
||||
{
|
||||
var f = AlarmConditionFilter.Parse("AnalogLimit.Hi");
|
||||
Assert.True(f.IsAllowed(Tx("analoglimit.hi")));
|
||||
Assert.True(f.IsAllowed(Tx("ANALOGLIMIT.HI")));
|
||||
Assert.False(f.IsAllowed(Tx("DiscreteAlarm")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_TrimsWhitespaceAroundEachName()
|
||||
{
|
||||
var f = AlarmConditionFilter.Parse(" AnalogLimit.Hi ,\tDiscreteAlarm ");
|
||||
Assert.True(f.IsAllowed(Tx("AnalogLimit.Hi")));
|
||||
Assert.True(f.IsAllowed(Tx("DiscreteAlarm")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_DropsEmptyEntries_KeepsNonEmpty()
|
||||
{
|
||||
var f = AlarmConditionFilter.Parse("AnalogLimit.Hi,, ,DiscreteAlarm");
|
||||
Assert.False(f.IsEmpty);
|
||||
Assert.True(f.IsAllowed(Tx("AnalogLimit.Hi")));
|
||||
Assert.True(f.IsAllowed(Tx("DiscreteAlarm")));
|
||||
Assert.False(f.IsAllowed(Tx("")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAllowed_NeverDropsSnapshotCompleteFramingSentinel()
|
||||
{
|
||||
// SnapshotComplete is a pure framing sentinel (empty AlarmTypeName) that
|
||||
// drives the NativeAlarmActor's atomic snapshot swap. A type filter must
|
||||
// never swallow it or the snapshot replay never completes.
|
||||
var f = AlarmConditionFilter.Parse("AnalogLimit.Hi");
|
||||
Assert.True(f.IsAllowed(Tx("", AlarmTransitionKind.SnapshotComplete)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAllowed_FiltersReplayedSnapshotConditionsByType()
|
||||
{
|
||||
// Snapshot-kind transitions carry real conditions and ARE filtered.
|
||||
var f = AlarmConditionFilter.Parse("AnalogLimit.Hi");
|
||||
Assert.True(f.IsAllowed(Tx("AnalogLimit.Hi", AlarmTransitionKind.Snapshot)));
|
||||
Assert.False(f.IsAllowed(Tx("DiscreteAlarm", AlarmTransitionKind.Snapshot)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Names_ExposesNormalizedSet_ForServerSideOptimization()
|
||||
{
|
||||
var f = AlarmConditionFilter.Parse(" AnalogLimit.Hi , DiscreteAlarm ");
|
||||
Assert.Equal(new[] { "AnalogLimit.Hi", "DiscreteAlarm" }, f.Names.OrderBy(n => n).ToArray());
|
||||
Assert.Empty(AlarmConditionFilter.Parse(null).Names);
|
||||
}
|
||||
}
|
||||
+133
-1
@@ -23,10 +23,27 @@ public class DataConnectionActorAlarmTests : TestKit
|
||||
};
|
||||
|
||||
private static NativeAlarmTransition Raise(string sourceRef, string sourceObj) =>
|
||||
new(sourceRef, sourceObj, "AnalogLimit.Hi", AlarmTransitionKind.Raise,
|
||||
Raise(sourceRef, sourceObj, "AnalogLimit.Hi");
|
||||
|
||||
private static NativeAlarmTransition Raise(string sourceRef, string sourceObj, string typeName,
|
||||
AlarmTransitionKind kind = AlarmTransitionKind.Raise) =>
|
||||
new(sourceRef, sourceObj, typeName, kind,
|
||||
new AlarmConditionState(true, false, null, AlarmShelveState.Unshelved, false, 500),
|
||||
"Process", "hi", "hi", "", "", null, DateTimeOffset.UtcNow, "92", "90");
|
||||
|
||||
private static (IDataConnection Adapter, Func<AlarmTransitionCallback?> Cb) BuildAlarmAdapter()
|
||||
{
|
||||
AlarmTransitionCallback? cb = null;
|
||||
var adapter = Substitute.For<IDataConnection, IAlarmSubscribableConnection>();
|
||||
adapter.ConnectAsync(Arg.Any<IDictionary<string, string>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Task.CompletedTask);
|
||||
((IAlarmSubscribableConnection)adapter)
|
||||
.SubscribeAlarmsAsync(Arg.Any<string>(), Arg.Any<string?>(),
|
||||
Arg.Do<AlarmTransitionCallback>(c => cb = c), Arg.Any<CancellationToken>())
|
||||
.Returns(Task.FromResult("alarm-sub-1"));
|
||||
return (adapter, () => cb);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeAlarms_RoutesTransitionToInstanceSubscriber()
|
||||
{
|
||||
@@ -63,4 +80,119 @@ public class DataConnectionActorAlarmTests : TestKit
|
||||
actor.Tell(new SubscribeAlarmsRequest("c", "inst", "conn", "Tank01", null, DateTimeOffset.UtcNow));
|
||||
ExpectMsg<SubscribeAlarmsResponse>(m => !m.Success && m.ErrorMessage != null);
|
||||
}
|
||||
|
||||
// ── M2.4 (#8): conditionFilter is now applied client-side in the actor ──
|
||||
|
||||
[Fact]
|
||||
public void SubscribeAlarms_WithTypeFilter_DeliversOnlyMatchingTypes()
|
||||
{
|
||||
var (adapter, getCb) = BuildAlarmAdapter();
|
||||
var actor = Sys.ActorOf(Props.Create(() => new DataConnectionActor(
|
||||
"conn", adapter, _options, _health, _factory, "OpcUa")));
|
||||
|
||||
actor.Tell(new SubscribeAlarmsRequest("c", "inst", "conn", "Tank01",
|
||||
"AnalogLimit.Hi,AnalogLimit.Lo", DateTimeOffset.UtcNow));
|
||||
ExpectMsg<SubscribeAlarmsResponse>(m => m.Success);
|
||||
var cb = getCb();
|
||||
Assert.NotNull(cb);
|
||||
|
||||
// Non-matching type is dropped (no message delivered).
|
||||
cb!(Raise("Tank01.HiHi", "Tank01", "AnalogLimit.HiHi"));
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(250));
|
||||
|
||||
// Matching type is delivered.
|
||||
cb!(Raise("Tank01.Hi", "Tank01", "AnalogLimit.Hi"));
|
||||
ExpectMsg<NativeAlarmTransitionUpdate>(u => u.Transition.AlarmTypeName == "AnalogLimit.Hi");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeAlarms_WithNullFilter_DeliversAllTypes()
|
||||
{
|
||||
var (adapter, getCb) = BuildAlarmAdapter();
|
||||
var actor = Sys.ActorOf(Props.Create(() => new DataConnectionActor(
|
||||
"conn", adapter, _options, _health, _factory, "OpcUa")));
|
||||
|
||||
actor.Tell(new SubscribeAlarmsRequest("c", "inst", "conn", "Tank01", null, DateTimeOffset.UtcNow));
|
||||
ExpectMsg<SubscribeAlarmsResponse>(m => m.Success);
|
||||
var cb = getCb();
|
||||
Assert.NotNull(cb);
|
||||
|
||||
cb!(Raise("Tank01.HiHi", "Tank01", "AnalogLimit.HiHi"));
|
||||
ExpectMsg<NativeAlarmTransitionUpdate>(u => u.Transition.AlarmTypeName == "AnalogLimit.HiHi");
|
||||
cb!(Raise("Tank01.Lo", "Tank01", "DiscreteAlarm"));
|
||||
ExpectMsg<NativeAlarmTransitionUpdate>(u => u.Transition.AlarmTypeName == "DiscreteAlarm");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeAlarms_FilterMatch_IgnoresCaseAndWhitespace()
|
||||
{
|
||||
var (adapter, getCb) = BuildAlarmAdapter();
|
||||
var actor = Sys.ActorOf(Props.Create(() => new DataConnectionActor(
|
||||
"conn", adapter, _options, _health, _factory, "OpcUa")));
|
||||
|
||||
actor.Tell(new SubscribeAlarmsRequest("c", "inst", "conn", "Tank01",
|
||||
" analoglimit.hi ,\tDISCRETEALARM ", DateTimeOffset.UtcNow));
|
||||
ExpectMsg<SubscribeAlarmsResponse>(m => m.Success);
|
||||
var cb = getCb();
|
||||
Assert.NotNull(cb);
|
||||
|
||||
cb!(Raise("Tank01.Hi", "Tank01", "AnalogLimit.Hi")); // case differs from filter
|
||||
ExpectMsg<NativeAlarmTransitionUpdate>(u => u.Transition.AlarmTypeName == "AnalogLimit.Hi");
|
||||
cb!(Raise("Tank01.Disc", "Tank01", "DiscreteAlarm"));
|
||||
ExpectMsg<NativeAlarmTransitionUpdate>(u => u.Transition.AlarmTypeName == "DiscreteAlarm");
|
||||
cb!(Raise("Tank01.HiHi", "Tank01", "AnalogLimit.HiHi")); // not listed
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(250));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeAlarms_GatewayWideFeed_IsFilteredClientSide()
|
||||
{
|
||||
// MxGateway has no server-side filter: its adapter opens ONE gateway-wide
|
||||
// feed and the actor is the authoritative gate. A filtered source must
|
||||
// only see its own matching types even though the feed carries everything.
|
||||
var (adapter, getCb) = BuildAlarmAdapter();
|
||||
var actor = Sys.ActorOf(Props.Create(() => new DataConnectionActor(
|
||||
"conn", adapter, _options, _health, _factory, "MxGateway")));
|
||||
|
||||
actor.Tell(new SubscribeAlarmsRequest("c", "inst", "conn", "Reactor",
|
||||
"HighTemp", DateTimeOffset.UtcNow));
|
||||
ExpectMsg<SubscribeAlarmsResponse>(m => m.Success);
|
||||
var cb = getCb();
|
||||
Assert.NotNull(cb);
|
||||
|
||||
// Gateway-wide feed delivers a transition for a different source object —
|
||||
// dropped by source routing.
|
||||
cb!(Raise("Pump.Fault", "Pump", "HighTemp"));
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(200));
|
||||
// Right source, wrong type — dropped by the client-side type gate.
|
||||
cb!(Raise("Reactor.LowTemp", "Reactor", "LowTemp"));
|
||||
ExpectNoMsg(TimeSpan.FromMilliseconds(200));
|
||||
// Right source, right type — delivered.
|
||||
cb!(Raise("Reactor.HighTemp", "Reactor", "HighTemp"));
|
||||
ExpectMsg<NativeAlarmTransitionUpdate>(u =>
|
||||
u.Transition.SourceObjectReference == "Reactor" && u.Transition.AlarmTypeName == "HighTemp");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SubscribeAlarms_WithFilter_StillForwardsSnapshotCompleteSentinel()
|
||||
{
|
||||
// The SnapshotComplete framing sentinel (empty AlarmTypeName) must survive
|
||||
// the type gate so the NativeAlarmActor's snapshot swap can complete.
|
||||
var (adapter, getCb) = BuildAlarmAdapter();
|
||||
var actor = Sys.ActorOf(Props.Create(() => new DataConnectionActor(
|
||||
"conn", adapter, _options, _health, _factory, "OpcUa")));
|
||||
|
||||
actor.Tell(new SubscribeAlarmsRequest("c", "inst", "conn", "Tank01",
|
||||
"AnalogLimit.Hi", DateTimeOffset.UtcNow));
|
||||
ExpectMsg<SubscribeAlarmsResponse>(m => m.Success);
|
||||
var cb = getCb();
|
||||
Assert.NotNull(cb);
|
||||
|
||||
// Snapshot-complete sentinel: empty source refs (the framing marker) but
|
||||
// routed because every subscriber receives it; never type-filtered.
|
||||
cb!(new NativeAlarmTransition("Tank01", "Tank01", "", AlarmTransitionKind.SnapshotComplete,
|
||||
new AlarmConditionState(false, true, null, AlarmShelveState.Unshelved, false, 0),
|
||||
"", "", "", "", "", null, DateTimeOffset.UtcNow, "", ""));
|
||||
ExpectMsg<NativeAlarmTransitionUpdate>(u => u.Transition.Kind == AlarmTransitionKind.SnapshotComplete);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user