feat(grpc-data): A2a — condition contract: ConditionCapabilities (17), condition_scope (4), ConditionStates arm 9, codecs
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
using W = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Commons.DataConnections;
|
||||
|
||||
/// <summary>
|
||||
/// A2 condition codecs (plan §4.2, §5.5): <c>CapabilitiesResponse.conditions</c>, <c>SubscriptionItem.condition_scope</c>
|
||||
/// and the <c>condition_states</c> frame arm (9). Only ever exercised on a call that negotiated <c>conditions</c>: the
|
||||
/// host strips every one of these shapes from a header-less call, so a v1 client never sees them.
|
||||
/// </summary>
|
||||
public static class DataConditionProtoMapper
|
||||
{
|
||||
/// <summary>The most condition records one decoded frame may carry.</summary>
|
||||
public const int MaxRecordsPerFrame = 5000;
|
||||
|
||||
/// <summary>Encodes the capabilities field.</summary>
|
||||
public static W.ConditionCapabilities EncodeCapabilities(DataConditionCapabilities x) =>
|
||||
new() { RootScopeItems = x.RootScopeItems, Methods = x.Methods, MaxConditions = x.MaxConditions };
|
||||
|
||||
/// <summary>Decodes the capabilities field (null when absent).</summary>
|
||||
public static DataConditionCapabilities? DecodeCapabilities(W.ConditionCapabilities? x)
|
||||
{
|
||||
if (x is null) return null;
|
||||
var decoded = new DataConditionCapabilities(x.RootScopeItems, x.Methods, x.MaxConditions);
|
||||
decoded.Validate();
|
||||
return decoded;
|
||||
}
|
||||
|
||||
/// <summary>Encodes one condition record.</summary>
|
||||
public static W.ConditionState EncodeState(DataConditionState x)
|
||||
{
|
||||
var v = new W.ConditionState
|
||||
{
|
||||
ConditionKey = x.ConditionKey, Version = x.Version, Removed = x.Removed, StateKnown = x.StateKnown,
|
||||
Active = x.Active, Acknowledged = x.Acknowledged, Confirmed = x.Confirmed, Enabled = x.Enabled,
|
||||
Shelving = x.Shelving, Severity = x.Severity, Message = x.Message ?? "", Quality = x.Quality,
|
||||
ConditionClass = x.ConditionClass, Retain = x.Retain,
|
||||
};
|
||||
if (x.Info is { } info)
|
||||
{
|
||||
v.Info = new W.ConditionInfo
|
||||
{
|
||||
ConditionType = info.ConditionType, ConditionName = info.ConditionName, SourceKind = (int)info.SourceKind,
|
||||
SupportedMethods = info.SupportedMethods,
|
||||
};
|
||||
v.Info.Placements.AddRange(info.Placements.Select(DataProtoMapper.EncodeView));
|
||||
}
|
||||
if (x.UnshelveAtUtc is { } unshelve) v.UnshelveAt = Timestamp.FromDateTimeOffset(unshelve);
|
||||
if (x.TimeUtc is { } time) v.Time = Timestamp.FromDateTimeOffset(time);
|
||||
if (x.Activation is { } a) v.Activation = new W.ActivationMarker { Incarnation = a.Incarnation, Sequence = a.Sequence };
|
||||
if (x.Comment is not null) v.Comment = x.Comment;
|
||||
if (x.ClientUserId is not null) v.ClientUserId = x.ClientUserId;
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>Decodes one condition record (a malformed key or negative version is an <see cref="ArgumentException"/>).</summary>
|
||||
public static DataConditionState DecodeState(W.ConditionState x)
|
||||
{
|
||||
if (!ImportedConditionIds.IsWellFormedConditionKey(x.ConditionKey) || x.Version < 0)
|
||||
throw new ArgumentException("Invalid condition record.");
|
||||
DataConditionInfo? info = null;
|
||||
if (x.Info is { } i)
|
||||
{
|
||||
var kind = (DataConditionSourceKind)i.SourceKind;
|
||||
if (!System.Enum.IsDefined(kind)) kind = DataConditionSourceKind.Unspecified;
|
||||
info = new DataConditionInfo(i.ConditionType, i.ConditionName, kind,
|
||||
i.Placements.Select(DataProtoMapper.DecodeView).ToArray(), i.SupportedMethods);
|
||||
}
|
||||
return new DataConditionState(x.ConditionKey, x.Version, x.Removed, x.StateKnown, info, x.Active, x.Acknowledged,
|
||||
x.Confirmed, x.Enabled, x.Shelving, x.Severity, x.Message, x.Quality, x.UnshelveAt?.ToDateTimeOffset(),
|
||||
x.ConditionClass, x.Time?.ToDateTimeOffset(), x.Retain,
|
||||
x.Activation is { } m ? new DataActivationMarker(m.Incarnation, m.Sequence) : null,
|
||||
x.HasComment ? x.Comment : null, x.HasClientUserId ? x.ClientUserId : null);
|
||||
}
|
||||
|
||||
/// <summary>Encodes a condition frame (arm 9). The host calls this only on a call that negotiated <c>conditions</c>.</summary>
|
||||
public static W.SubscriptionFrame EncodeFrame(DataSubscriptionConditions frame)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(frame);
|
||||
var states = new W.ConditionStates
|
||||
{
|
||||
Handle = DataProtoMapper.EncodeHandle(frame.Handle), IsSnapshot = frame.IsSnapshot, SnapshotComplete = frame.SnapshotComplete,
|
||||
};
|
||||
if (frame.Total is { } total) states.Total = total;
|
||||
states.Items.AddRange(frame.Items.Select(EncodeState));
|
||||
return new W.SubscriptionFrame
|
||||
{
|
||||
SubscriptionId = frame.SubscriptionId, StreamEpoch = frame.StreamEpoch, Sequence = frame.Sequence, ConditionStates = states,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Decodes a condition frame; the handle must belong to the frame's stream.</summary>
|
||||
public static DataSubscriptionConditions DecodeFrame(W.SubscriptionFrame frame)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(frame);
|
||||
var c = frame.ConditionStates ?? throw new ArgumentException("Expected condition states.");
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(frame.SubscriptionId);
|
||||
if (frame.StreamEpoch <= 0 || frame.Sequence <= 0 || c.Handle is null || c.Items.Count > MaxRecordsPerFrame
|
||||
|| c.HasTotal && c.Total < 0)
|
||||
throw new ArgumentException("Invalid condition frame.");
|
||||
var handle = DataProtoMapper.DecodeHandle(c.Handle);
|
||||
if (handle.SubscriptionId != frame.SubscriptionId || handle.StreamEpoch != frame.StreamEpoch)
|
||||
throw new ArgumentException("Condition handle belongs to another stream.");
|
||||
return new DataSubscriptionConditions(frame.SubscriptionId, frame.StreamEpoch, frame.Sequence, handle, c.IsSnapshot,
|
||||
c.HasTotal ? c.Total : null, c.Items.Select(DecodeState).ToArray(), c.SnapshotComplete);
|
||||
}
|
||||
}
|
||||
@@ -304,10 +304,13 @@ public static class DataProtoMapper
|
||||
ClientItemId = x.ClientItemId,
|
||||
View = EncodeView(x.View),
|
||||
Settings = EncodeSettings(x.Settings),
|
||||
// A2: only a caller that negotiated `conditions` ever sets this (the host strips it otherwise).
|
||||
ConditionScope = x.ConditionScope is { } scope ? new W.ConditionScope { IncludeInfo = scope.IncludeInfo } : null,
|
||||
};
|
||||
|
||||
public static DataSubscriptionItem DecodeSubscriptionItem(W.SubscriptionItem x) =>
|
||||
new(x.ClientItemId, DecodeView(x.View), DecodeSettings(x.Settings));
|
||||
new(x.ClientItemId, DecodeView(x.View), DecodeSettings(x.Settings),
|
||||
x.ConditionScope is { } scope ? new DataConditionScope(scope.IncludeInfo) : null);
|
||||
|
||||
public static W.SubscriptionCommand EncodeCommand(DataSubscriptionCommand x)
|
||||
{
|
||||
@@ -1037,6 +1040,9 @@ public static class DataProtoMapper
|
||||
// H2: the service strips this from a call that did not negotiate history.
|
||||
if (x.History is { } history)
|
||||
v.History = DataHistoryProtoMapper.EncodeCapabilities(history);
|
||||
// A2: the service strips this (and the AlarmSubscribe bit) from a call that did not negotiate conditions.
|
||||
if (x.Conditions is { } conditions)
|
||||
v.Conditions = DataConditionProtoMapper.EncodeCapabilities(conditions);
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -1062,7 +1068,9 @@ public static class DataProtoMapper
|
||||
r,
|
||||
// H2: HistoryRead without the history capabilities field is no grant (the two only ever travel together).
|
||||
(DataOperations)(x.Operations & (uint)DataOperationSets.Understood
|
||||
& (DataHistoryProtoMapper.DecodeCapabilities(x.History) is null ? ~(uint)DataOperations.HistoryRead : uint.MaxValue)),
|
||||
& (DataHistoryProtoMapper.DecodeCapabilities(x.History) is null ? ~(uint)DataOperations.HistoryRead : uint.MaxValue)
|
||||
// A2: likewise AlarmSubscribe without the conditions field.
|
||||
& (x.Conditions is null ? ~(uint)DataOperations.AlarmSubscribe : uint.MaxValue)),
|
||||
kinds,
|
||||
new DataLimits(
|
||||
l.MaxPageItems,
|
||||
@@ -1084,7 +1092,8 @@ public static class DataProtoMapper
|
||||
x.WriteServingGenerationEnforced,
|
||||
x.SubscriptionFreshnessChallenges,
|
||||
x.MetadataItemLeases,
|
||||
DataHistoryProtoMapper.DecodeCapabilities(x.History)
|
||||
DataHistoryProtoMapper.DecodeCapabilities(x.History),
|
||||
DataConditionProtoMapper.DecodeCapabilities(x.Conditions)
|
||||
);
|
||||
result.Validate();
|
||||
return result;
|
||||
|
||||
@@ -123,8 +123,17 @@ message CapabilitiesResponse {
|
||||
bool metadata_item_leases = 15;
|
||||
// H2: only on a call that negotiated `history`, and only when this origin has a historian backend.
|
||||
HistoryCapabilities history = 16;
|
||||
// X0 RESERVATION (not protobuf `reserved`: the number WILL be used): 17 = ConditionCapabilities conditions (A2).
|
||||
// Pinned by DataConnectionProtoContract + GrpcDataProtoReservationTests.
|
||||
// A2: only on a call that negotiated `conditions`, and only when this origin can serve them (a condition-key secret
|
||||
// is provisioned and the condition-state hub is wired). Pinned by DataConnectionProtoContract + GrpcDataProtoReservationTests.
|
||||
ConditionCapabilities conditions = 17;
|
||||
}
|
||||
// A2 (plan §4.2/§5.5): what the origin's condition streams offer this caller.
|
||||
message ConditionCapabilities {
|
||||
bool root_scope_items = 1;
|
||||
// Bitmask of supported condition methods (A2b); 0 in A2a.
|
||||
uint32 methods = 2;
|
||||
// The most conditions one root-scoped item admits (the export's MaxConditions); 0 = undisclosed (no grant).
|
||||
int32 max_conditions = 3;
|
||||
}
|
||||
message HistoryCapabilities {
|
||||
bool raw = 1;
|
||||
@@ -245,7 +254,46 @@ message SubscriptionItem {
|
||||
string client_item_id = 1;
|
||||
View view = 2;
|
||||
Settings settings = 3;
|
||||
// X0 RESERVATION: 4 = ConditionScope condition_scope (A2, header-gated). Pinned by GrpcDataProtoReservationTests.
|
||||
// A2 (header-gated): one root-scoped item = "all conditions under this export root"; view = the mapping's remote root.
|
||||
ConditionScope condition_scope = 4;
|
||||
}
|
||||
message ConditionScope { bool include_info = 1; }
|
||||
message ConditionInfo {
|
||||
string condition_type = 1; // namespace-URI qualified
|
||||
string condition_name = 2;
|
||||
int32 source_kind = 3; // 1 = NATIVE, 2 = SCRIPTED
|
||||
repeated View placements = 4; // realm-local placement addresses inside THIS export (§5.2), never cross-realm
|
||||
uint32 supported_methods = 5;
|
||||
}
|
||||
message ActivationMarker { string incarnation = 1; uint64 sequence = 2; }
|
||||
message ConditionState {
|
||||
string condition_key = 1; // opaque keyed hash (§5.2)
|
||||
int64 version = 2;
|
||||
bool removed = 3;
|
||||
bool state_known = 4;
|
||||
ConditionInfo info = 5; // first appearance in an epoch and in every snapshot record
|
||||
bool active = 6;
|
||||
bool acknowledged = 7;
|
||||
bool confirmed = 8;
|
||||
bool enabled = 9;
|
||||
int32 shelving = 10;
|
||||
uint32 severity = 11;
|
||||
string message = 12;
|
||||
uint32 quality = 13;
|
||||
google.protobuf.Timestamp unshelve_at = 14;
|
||||
int32 condition_class = 15;
|
||||
google.protobuf.Timestamp time = 16;
|
||||
bool retain = 17;
|
||||
ActivationMarker activation = 18;
|
||||
optional string comment = 19;
|
||||
optional string client_user_id = 20;
|
||||
}
|
||||
message ConditionStates {
|
||||
Handle handle = 1; // the root-scoped item's handle
|
||||
bool is_snapshot = 2;
|
||||
optional int32 total = 3; // on the snapshot-begin chunk, before any record (admission before materialising)
|
||||
repeated ConditionState items = 4;
|
||||
bool snapshot_complete = 5;
|
||||
}
|
||||
message SubscriptionCommand {
|
||||
string subscription_id = 1;
|
||||
@@ -340,8 +388,9 @@ message SubscriptionFrame {
|
||||
Control control = 5;
|
||||
Values values = 6;
|
||||
CompactValues compact_values = 7;
|
||||
// X0 RESERVATION: oneof arm 9 = ConditionStates condition_states (A2). Only ever sent on a call that
|
||||
// negotiated `conditions` through x-otopcua-data-features: v1 clients fault on an unknown frame arm.
|
||||
// A2: only ever sent on a call that negotiated `conditions` through x-otopcua-data-features (v1 clients fault on
|
||||
// an unknown frame arm), and only for a root-scoped condition item that call opened.
|
||||
ConditionStates condition_states = 9;
|
||||
}
|
||||
}
|
||||
// H2 remote history (plan §4.2). An unset start/end is ABSENT, never MinValue: the origin's end-only rules depend on it.
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// A2 condition streaming (grpc-data history/alarms plan §4.2, §5.5). Root-scoped condition items on Subscribe: one item
|
||||
// per mapping = "all conditions under this export root". Negotiated per call through x-otopcua-data-features
|
||||
// `conditions`; a call without it never sees any of these shapes.
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/// <summary>What an origin's condition streams offer (<c>CapabilitiesResponse.conditions</c>); null when not negotiated
|
||||
/// or not servable (no condition-key secret, no condition-state hub).</summary>
|
||||
/// <param name="RootScopeItems">Root-scoped condition items are accepted.</param>
|
||||
/// <param name="Methods">Supported condition methods (bitmask; A2b). 0 in A2a.</param>
|
||||
/// <param name="MaxConditions">The most conditions one root-scoped item admits for this caller (the largest
|
||||
/// <c>MaxConditions</c> of its AlarmSubscribe exports); 0 = undisclosed (no grant).</param>
|
||||
public sealed record DataConditionCapabilities(bool RootScopeItems, uint Methods, int MaxConditions)
|
||||
{
|
||||
public void Validate()
|
||||
{
|
||||
if (MaxConditions < 0) throw new ArgumentOutOfRangeException(nameof(MaxConditions));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A root-scoped condition item (<c>SubscriptionItem.condition_scope</c>); the item's view is the export root.</summary>
|
||||
/// <param name="IncludeInfo">Carry <see cref="DataConditionInfo"/> on first appearance and in every snapshot record.</param>
|
||||
public sealed record DataConditionScope(bool IncludeInfo);
|
||||
|
||||
/// <summary>Where an origin condition comes from (wire <c>ConditionInfo.source_kind</c>).</summary>
|
||||
public enum DataConditionSourceKind { Unspecified = 0, Native = 1, Scripted = 2 }
|
||||
|
||||
/// <summary>Static facts about one condition, carried on first appearance in an epoch and in every snapshot record.</summary>
|
||||
/// <param name="ConditionType">The condition type, namespace-URI qualified.</param>
|
||||
/// <param name="ConditionName">The Part 9 ConditionName.</param>
|
||||
/// <param name="SourceKind">Native (driver-fed) or scripted.</param>
|
||||
/// <param name="Placements">Realm-local placement addresses inside the listing export (§5.2), never cross-realm.</param>
|
||||
/// <param name="SupportedMethods">The methods this condition supports (A2b).</param>
|
||||
public sealed record DataConditionInfo(string ConditionType, string ConditionName, DataConditionSourceKind SourceKind,
|
||||
IReadOnlyList<DataView> Placements, uint SupportedMethods);
|
||||
|
||||
/// <summary>The origin-owned activation marker (T3): (serving incarnation, per-condition occurrence sequence).</summary>
|
||||
public sealed record DataActivationMarker(string Incarnation, ulong Sequence);
|
||||
|
||||
/// <summary>One condition record of a root-scoped item (<c>ConditionState</c>).</summary>
|
||||
/// <remarks>A record with <see cref="StateKnown"/> = false never changes a consumer's last state (§5.1 T2).</remarks>
|
||||
public sealed record DataConditionState(string ConditionKey, long Version, bool Removed, bool StateKnown, DataConditionInfo? Info,
|
||||
bool Active, bool Acknowledged, bool Confirmed, bool Enabled, int Shelving, uint Severity, string Message, uint Quality,
|
||||
DateTimeOffset? UnshelveAtUtc, int ConditionClass, DateTimeOffset? TimeUtc, bool Retain, DataActivationMarker? Activation,
|
||||
string? Comment = null, string? ClientUserId = null);
|
||||
|
||||
/// <summary>
|
||||
/// A condition frame (<c>SubscriptionFrame.condition_states</c>, oneof arm 9) for one root-scoped item. A snapshot begins
|
||||
/// with <see cref="IsSnapshot"/> and <see cref="Total"/> set and no records (so capacity is decided before anything is
|
||||
/// materialised); records follow; <see cref="SnapshotComplete"/> ends it. Later frames are delta records.
|
||||
/// </summary>
|
||||
public sealed record DataSubscriptionConditions(string SubscriptionId, long StreamEpoch, long Sequence, DataItemHandle Handle,
|
||||
bool IsSnapshot, int? Total, IReadOnlyList<DataConditionState> Items, bool SnapshotComplete)
|
||||
: DataSubscriptionFrame(SubscriptionId, StreamEpoch, Sequence);
|
||||
@@ -318,7 +318,7 @@ public sealed record DataCapabilities(int ProtocolMajor, int ProtocolMinor, Serv
|
||||
DataOperations Operations, IReadOnlySet<DataValueKind> ValueKinds, DataLimits Limits, bool CompactBatches,
|
||||
int CompactCodecVersion, string ClusterId, bool SubscriptionAgeReadiness = false, bool ResponseServingIdentity = false,
|
||||
bool WriteServingGenerationEnforced = false, bool SubscriptionFreshnessChallenges = false,
|
||||
bool MetadataItemLeases = false, DataHistoryCapabilities? History = null)
|
||||
bool MetadataItemLeases = false, DataHistoryCapabilities? History = null, DataConditionCapabilities? Conditions = null)
|
||||
{
|
||||
private readonly DataOperations _operations = Operations & DataOperationSets.Understood;
|
||||
/// <summary>Masked to <see cref="DataOperationSets.Understood"/> on construction and on every <c>with</c>.</summary>
|
||||
@@ -344,6 +344,7 @@ public sealed record DataCapabilities(int ProtocolMajor, int ProtocolMinor, Serv
|
||||
}
|
||||
// Operations is masked by its accessor, so unknown bits from a newer peer never reach here (X0).
|
||||
History?.Validate();
|
||||
Conditions?.Validate();
|
||||
}
|
||||
}
|
||||
public sealed record DataMetadata(DataView View, string BrowseName, string NodeKind, DataValueKind ValueKind, int ValueRank,
|
||||
|
||||
@@ -85,7 +85,9 @@ public sealed record DataSubscriptionSettings(TimeSpan SamplingInterval, TimeSpa
|
||||
}
|
||||
}
|
||||
}
|
||||
public sealed record DataSubscriptionItem(string ClientItemId, DataView View, DataSubscriptionSettings Settings);
|
||||
/// <summary>A subscription item. <paramref name="ConditionScope"/> (A2, header-gated) makes it a root-scoped condition item.</summary>
|
||||
public sealed record DataSubscriptionItem(string ClientItemId, DataView View, DataSubscriptionSettings Settings,
|
||||
DataConditionScope? ConditionScope = null);
|
||||
public abstract record DataSubscriptionCommand(string SubscriptionId, long StreamEpoch, string CommandId, TimeSpan AcknowledgementTimeout);
|
||||
public sealed record OpenSubscription(string SubscriptionId, long StreamEpoch, string CommandId, TimeSpan AcknowledgementTimeout,
|
||||
IReadOnlyList<DataSubscriptionItem> Items, bool WatchCatalogOnly, TimeSpan SessionLifetime, DataBudget SnapshotBudget)
|
||||
|
||||
Reference in New Issue
Block a user