-
Notifications
You must be signed in to change notification settings - Fork 0
Unify live and PCAP Sampled Values observation pipeline #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e16553f
Add unified per-stream SV observation manager
masarray d35ad78
Add unified live and PCAP observation pipeline tests
masarray d466d30
Add temporary P3B integration workflow
masarray 83d9428
Integrate live and PCAP paths with shared observation pipeline
github-actions[bot] 13760a4
Add temporary P3B finalization workflow
masarray 81bfb36
Run temporary P3B finalization on pull request updates
masarray 36867f0
Make temporary P3B finalization replacements robust
masarray b1cca76
Apply tolerant P3B documentation finalization
masarray 6236c9c
Raise P3B coverage regression floors
masarray 48d2fe1
Enforce raised P3B coverage floors in CI
masarray 3e6fc38
Document unified observation pipeline and raise coverage gates
github-actions[bot] a0f0ad5
Remove temporary P3B finalization workflow
masarray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
src/ARSVIN.Engine/AR.Iec61850/SampledValues/Profiles/SvStreamObservationManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| using System.Collections.Concurrent; | ||
| using AR.Iec61850.Scl; | ||
|
|
||
| namespace AR.Iec61850.SampledValues.Profiles; | ||
|
|
||
| public enum SvObservationInputKind | ||
| { | ||
| Unknown, | ||
| LiveCapture, | ||
| PcapReplay | ||
| } | ||
|
|
||
| public sealed record SvObservedStreamKey | ||
| { | ||
| public string SourceMac { get; init; } = string.Empty; | ||
| public string DestinationMac { get; init; } = string.Empty; | ||
| public ushort? VlanId { get; init; } | ||
| public ushort AppId { get; init; } | ||
| public string SvId { get; init; } = string.Empty; | ||
| public string DataSetReference { get; init; } = string.Empty; | ||
|
|
||
| public string Id | ||
| { | ||
| get | ||
| { | ||
| var vlan = VlanId?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "-"; | ||
| return $"SV|{AppId:X4}|{SourceMac}|{DestinationMac}|{vlan}|{SvId}|{DataSetReference}"; | ||
| } | ||
| } | ||
|
|
||
| public static SvObservedStreamKey FromFrame(SampledValuesFrame frame) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(frame); | ||
| var first = frame.Pdu.Asdus.FirstOrDefault() | ||
| ?? throw new ArgumentException("An observed SV frame requires at least one ASDU.", nameof(frame)); | ||
|
|
||
| return new SvObservedStreamKey | ||
| { | ||
| SourceMac = frame.Source.ToString(), | ||
| DestinationMac = frame.Destination.ToString(), | ||
| VlanId = frame.Vlan?.VlanId, | ||
| AppId = frame.AppId, | ||
| SvId = first.SvId, | ||
| DataSetReference = first.DataSetReference | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| public sealed record SvStreamObservationSnapshot | ||
| { | ||
| public SvObservedStreamKey Key { get; init; } = new(); | ||
| public SvObservedStreamFacts Facts { get; init; } = new(); | ||
| public IReadOnlyList<SvObservationInputKind> InputKinds { get; init; } | ||
| = Array.Empty<SvObservationInputKind>(); | ||
| public SvObservationInputKind LastInputKind { get; init; } | ||
| public bool IsBoundToScl { get; init; } | ||
| public string ControlBlockReference { get; init; } = string.Empty; | ||
| public IReadOnlyList<string> Diagnostics { get; init; } = Array.Empty<string>(); | ||
| } | ||
|
|
||
| public sealed class SvStreamObservationManager | ||
| { | ||
| private sealed class StreamState | ||
| { | ||
| private readonly object _gate = new(); | ||
| private readonly HashSet<SvObservationInputKind> _inputKinds = []; | ||
| private readonly Queue<string> _diagnostics = new(); | ||
|
|
||
| public StreamState(int maximumObservations, TimeSpan maximumAge) | ||
| { | ||
| Accumulator = new SvObservationAccumulator(maximumObservations, maximumAge); | ||
| } | ||
|
|
||
| public SvObservationAccumulator Accumulator { get; } | ||
| public SvObservationInputKind LastInputKind { get; private set; } | ||
| public bool IsBoundToScl { get; private set; } | ||
| public string ControlBlockReference { get; private set; } = string.Empty; | ||
|
|
||
| public void Add( | ||
| SvFrameObservation observation, | ||
| SvObservationInputKind inputKind, | ||
| SampledValuesPublisherProfile? profile, | ||
| IEnumerable<string> diagnostics) | ||
| { | ||
| Accumulator.Add(observation); | ||
| lock (_gate) | ||
| { | ||
| LastInputKind = inputKind; | ||
| _inputKinds.Add(inputKind); | ||
| if (profile is not null) | ||
| { | ||
| IsBoundToScl = true; | ||
| ControlBlockReference = profile.Stream.ControlBlockReference; | ||
| } | ||
|
|
||
| foreach (var diagnostic in diagnostics.Where(item => !string.IsNullOrWhiteSpace(item))) | ||
| { | ||
| if (_diagnostics.Contains(diagnostic, StringComparer.Ordinal)) | ||
| continue; | ||
|
|
||
| _diagnostics.Enqueue(diagnostic); | ||
| while (_diagnostics.Count > 16) | ||
| _diagnostics.Dequeue(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public SvStreamObservationSnapshot Snapshot(SvObservedStreamKey key) | ||
| { | ||
| var facts = Accumulator.BuildFacts(); | ||
| lock (_gate) | ||
| { | ||
| return new SvStreamObservationSnapshot | ||
| { | ||
| Key = key, | ||
| Facts = facts, | ||
| InputKinds = _inputKinds.OrderBy(item => item).ToArray(), | ||
| LastInputKind = LastInputKind, | ||
| IsBoundToScl = IsBoundToScl, | ||
| ControlBlockReference = ControlBlockReference, | ||
| Diagnostics = facts.Diagnostics.Concat(_diagnostics).Distinct(StringComparer.Ordinal).ToArray() | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private readonly ConcurrentDictionary<SvObservedStreamKey, StreamState> _streams = new(); | ||
| private readonly int _maximumObservations; | ||
| private readonly TimeSpan _maximumAge; | ||
|
|
||
| public SvStreamObservationManager( | ||
| int maximumObservations = SvObservationAccumulator.DefaultMaximumObservations, | ||
| TimeSpan? maximumAge = null) | ||
| { | ||
| if (maximumObservations < 2) | ||
| throw new ArgumentOutOfRangeException(nameof(maximumObservations)); | ||
|
|
||
| _maximumAge = maximumAge ?? SvObservationAccumulator.DefaultMaximumAge; | ||
| if (_maximumAge <= TimeSpan.Zero) | ||
| throw new ArgumentOutOfRangeException(nameof(maximumAge)); | ||
|
|
||
| _maximumObservations = maximumObservations; | ||
| } | ||
|
|
||
| public int Count => _streams.Count; | ||
|
|
||
| public bool TryObserve( | ||
| DateTimeOffset timestamp, | ||
| SampledValuesFrame frame, | ||
| SvObservationInputKind inputKind, | ||
| SampledValuesPublisherProfile? profile, | ||
| out SvStreamObservationSnapshot snapshot, | ||
| double? nominalFrequencyHz = null) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(frame); | ||
| snapshot = new SvStreamObservationSnapshot(); | ||
|
|
||
| var asdus = frame.Pdu.Asdus; | ||
| var first = asdus.FirstOrDefault(); | ||
| if (first is null || first.SamplePayload.Length <= 0) | ||
| return false; | ||
|
|
||
| var key = SvObservedStreamKey.FromFrame(frame); | ||
| var diagnostics = ValidateFrameConsistency(asdus); | ||
| var signature = profile?.Entries.Select(ToSignature).ToArray() | ||
| ?? Array.Empty<SvDatasetElementSignature>(); | ||
| var payloadLengths = asdus.Select(item => item.SamplePayload.Length).Distinct().ToArray(); | ||
| var payloadLength = payloadLengths.Length == 1 ? payloadLengths[0] : first.SamplePayload.Length; | ||
|
|
||
| var observation = new SvFrameObservation | ||
| { | ||
| Timestamp = timestamp, | ||
| EtherType = 0x88BA, | ||
| AppId = frame.AppId, | ||
| DestinationMac = frame.Destination.ToString(), | ||
| VlanId = frame.Vlan?.VlanId, | ||
| VlanPriority = frame.Vlan?.PriorityCodePoint, | ||
| SvId = first.SvId, | ||
| DataSetReference = first.DataSetReference, | ||
| ConfigurationRevision = first.ConfigurationRevision, | ||
| PayloadBytesPerAsdu = payloadLength, | ||
| SampleCounts = asdus.Select(item => item.SampleCount).ToArray(), | ||
| DeclaredSampleRate = StableNullable(asdus.Select(item => item.SampleRate)), | ||
| DeclaredSampleMode = StableNullable(asdus.Select(item => item.SampleMode)), | ||
| NominalFrequencyHz = nominalFrequencyHz, | ||
| DataSetSignature = signature | ||
| }; | ||
|
|
||
| var state = _streams.GetOrAdd( | ||
| key, | ||
| _ => new StreamState(_maximumObservations, _maximumAge)); | ||
| state.Add(observation, inputKind, profile, diagnostics); | ||
| snapshot = state.Snapshot(key); | ||
| return true; | ||
| } | ||
|
|
||
| public bool TryGetSnapshot(SvObservedStreamKey key, out SvStreamObservationSnapshot snapshot) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(key); | ||
| if (_streams.TryGetValue(key, out var state)) | ||
| { | ||
| snapshot = state.Snapshot(key); | ||
| return true; | ||
| } | ||
|
|
||
| snapshot = new SvStreamObservationSnapshot(); | ||
| return false; | ||
| } | ||
|
|
||
| public IReadOnlyList<SvStreamObservationSnapshot> SnapshotAll() | ||
| => _streams.Select(pair => pair.Value.Snapshot(pair.Key)) | ||
| .OrderBy(item => item.Key.AppId) | ||
| .ThenBy(item => item.Key.SvId, StringComparer.Ordinal) | ||
| .ThenBy(item => item.Key.DataSetReference, StringComparer.Ordinal) | ||
| .ToArray(); | ||
|
|
||
| public void Clear() => _streams.Clear(); | ||
|
|
||
| private static IReadOnlyList<string> ValidateFrameConsistency(IReadOnlyList<SampledValueAsdu> asdus) | ||
| { | ||
| var diagnostics = new List<string>(); | ||
| var first = asdus[0]; | ||
|
|
||
| if (asdus.Any(item => !string.Equals(item.SvId, first.SvId, StringComparison.Ordinal))) | ||
| diagnostics.Add("ASDUs inside one Ethernet frame expose different svID values."); | ||
| if (asdus.Any(item => !string.Equals(item.DataSetReference, first.DataSetReference, StringComparison.Ordinal))) | ||
| diagnostics.Add("ASDUs inside one Ethernet frame expose different dataset references."); | ||
| if (asdus.Any(item => item.ConfigurationRevision != first.ConfigurationRevision)) | ||
| diagnostics.Add("ASDUs inside one Ethernet frame expose different confRev values."); | ||
| if (asdus.Select(item => item.SamplePayload.Length).Distinct().Count() > 1) | ||
| diagnostics.Add("ASDUs inside one Ethernet frame expose different payload lengths."); | ||
|
|
||
| return diagnostics; | ||
| } | ||
|
|
||
| private static SvDatasetElementSignature ToSignature(SclDataSetEntry entry) | ||
| => new() | ||
| { | ||
| BType = entry.BType, | ||
| Cdc = entry.Cdc, | ||
| IsQuality = entry.IsQuality, | ||
| IsTimestamp = entry.IsTimestamp | ||
| }; | ||
|
|
||
| private static ushort? StableNullable(IEnumerable<ushort?> values) | ||
| { | ||
| var distinct = values.Distinct().ToArray(); | ||
| return distinct.Length == 1 ? distinct[0] : null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the Subscriber's
FindProfilereturns its APPID-only fallback for a frame whose dataset reference does not match that SCL stream, this code still converts the profile entries into the observation'sDataSetSignatureand the state is marked SCL-bound. Since the existing SCL validation does not checkDataSetReference, an unexpected dataset can be reported with SCL-derived facts instead of unknown/conflicting facts; require the wiresvIDand dataset reference to match before setting the signature/bound state.Useful? React with 👍 / 👎.