| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Actors; |
| | | 3 | | using AsiBackbone.Core.Serialization; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Core.Handshakes; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a framework-neutral acknowledgment response for a liability or responsibility handshake. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class LiabilityHandshakeAcknowledgment |
| | | 11 | | { |
| | 3 | 12 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 3 | 13 | | new ReadOnlyDictionary<string, string>( |
| | 3 | 14 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 15 | | |
| | 26 | 16 | | private LiabilityHandshakeAcknowledgment( |
| | 26 | 17 | | string acknowledgmentId, |
| | 26 | 18 | | string? schemaVersion, |
| | 26 | 19 | | string handshakeId, |
| | 26 | 20 | | string actorId, |
| | 26 | 21 | | AsiBackboneActorType actorType, |
| | 26 | 22 | | string? actorDisplayName, |
| | 26 | 23 | | string acknowledgmentCode, |
| | 26 | 24 | | bool acknowledged, |
| | 26 | 25 | | DateTimeOffset occurredUtc, |
| | 26 | 26 | | string? correlationId, |
| | 26 | 27 | | string? traceId, |
| | 26 | 28 | | IReadOnlyDictionary<string, string> metadata) |
| | | 29 | | { |
| | 26 | 30 | | ArgumentException.ThrowIfNullOrWhiteSpace(acknowledgmentId); |
| | 26 | 31 | | ArgumentException.ThrowIfNullOrWhiteSpace(handshakeId); |
| | 26 | 32 | | ArgumentException.ThrowIfNullOrWhiteSpace(actorId); |
| | 26 | 33 | | ArgumentException.ThrowIfNullOrWhiteSpace(acknowledgmentCode); |
| | | 34 | | |
| | 26 | 35 | | AcknowledgmentId = acknowledgmentId.Trim(); |
| | 26 | 36 | | SchemaVersion = AsiBackboneSchemaVersions.Normalize(schemaVersion); |
| | 26 | 37 | | HandshakeId = handshakeId.Trim(); |
| | 26 | 38 | | ActorId = actorId.Trim(); |
| | 26 | 39 | | ActorType = actorType; |
| | 26 | 40 | | ActorDisplayName = NormalizeOptional(actorDisplayName); |
| | 26 | 41 | | AcknowledgmentCode = acknowledgmentCode.Trim(); |
| | 26 | 42 | | Acknowledged = acknowledged; |
| | 26 | 43 | | OccurredUtc = occurredUtc.ToUniversalTime(); |
| | 26 | 44 | | CorrelationId = NormalizeOptional(correlationId); |
| | 26 | 45 | | TraceId = NormalizeOptional(traceId); |
| | 26 | 46 | | Metadata = metadata; |
| | 26 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the stable acknowledgment identifier. |
| | | 51 | | /// </summary> |
| | 7 | 52 | | public string AcknowledgmentId { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the schema version for the serialized handshake acknowledgment shape. |
| | | 56 | | /// </summary> |
| | 4 | 57 | | public string SchemaVersion { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the handshake identifier associated with the acknowledgment. |
| | | 61 | | /// </summary> |
| | 8 | 62 | | public string HandshakeId { get; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the stable actor identifier associated with the acknowledgment. |
| | | 66 | | /// </summary> |
| | 6 | 67 | | public string ActorId { get; } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets the actor type associated with the acknowledgment. |
| | | 71 | | /// </summary> |
| | 6 | 72 | | public AsiBackboneActorType ActorType { get; } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets the optional display name or label associated with the actor. |
| | | 76 | | /// </summary> |
| | 7 | 77 | | public string? ActorDisplayName { get; } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the acknowledgment code accepted or rejected by the actor. |
| | | 81 | | /// </summary> |
| | 9 | 82 | | public string AcknowledgmentCode { get; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets a value indicating whether the required acknowledgment was accepted. |
| | | 86 | | /// </summary> |
| | 24 | 87 | | public bool Acknowledged { get; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets a value indicating whether the required acknowledgment was rejected. |
| | | 91 | | /// </summary> |
| | 13 | 92 | | public bool Rejected => !Acknowledged; |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets the UTC timestamp when the acknowledgment response occurred. |
| | | 96 | | /// </summary> |
| | 11 | 97 | | public DateTimeOffset OccurredUtc { get; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets the correlation identifier associated with the acknowledgment, when supplied by the host. |
| | | 101 | | /// </summary> |
| | 9 | 102 | | public string? CorrelationId { get; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets the trace identifier associated with the acknowledgment, when supplied by the host. |
| | | 106 | | /// </summary> |
| | 9 | 107 | | public string? TraceId { get; } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets additional framework-neutral acknowledgment metadata supplied by the host. |
| | | 111 | | /// </summary> |
| | 31 | 112 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Gets a value indicating whether this acknowledgment contains metadata. |
| | | 116 | | /// </summary> |
| | 9 | 117 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Creates an acknowledgment response for a liability or responsibility handshake. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="request">The handshake request being acknowledged.</param> |
| | | 123 | | /// <param name="actor">The actor responding to the handshake.</param> |
| | | 124 | | /// <param name="acknowledged">Whether the actor accepted the required acknowledgment.</param> |
| | | 125 | | /// <param name="acknowledgmentId">Optional acknowledgment identifier. When omitted, a new identifier is generated.< |
| | | 126 | | /// <param name="occurredUtc">Optional response timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 127 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 128 | | /// <param name="schemaVersion">Optional schema version for serialized or persisted acknowledgment records.</param> |
| | | 129 | | /// <returns>A liability handshake acknowledgment response.</returns> |
| | | 130 | | public static LiabilityHandshakeAcknowledgment Create( |
| | | 131 | | LiabilityHandshakeRequest request, |
| | | 132 | | IAsiBackboneActorContext actor, |
| | | 133 | | bool acknowledged, |
| | | 134 | | string? acknowledgmentId = null, |
| | | 135 | | DateTimeOffset? occurredUtc = null, |
| | | 136 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 137 | | string? schemaVersion = null) |
| | | 138 | | { |
| | 28 | 139 | | ArgumentNullException.ThrowIfNull(request); |
| | 27 | 140 | | ArgumentNullException.ThrowIfNull(actor); |
| | | 141 | | |
| | 26 | 142 | | return new LiabilityHandshakeAcknowledgment( |
| | 26 | 143 | | NormalizeIdentifier(acknowledgmentId), |
| | 26 | 144 | | schemaVersion, |
| | 26 | 145 | | request.HandshakeId, |
| | 26 | 146 | | actor.ActorId, |
| | 26 | 147 | | actor.ActorType, |
| | 26 | 148 | | actor.DisplayName, |
| | 26 | 149 | | request.RequiredAcknowledgmentCode, |
| | 26 | 150 | | acknowledged, |
| | 26 | 151 | | occurredUtc ?? DateTimeOffset.UtcNow, |
| | 26 | 152 | | request.CorrelationId, |
| | 26 | 153 | | request.TraceId, |
| | 26 | 154 | | NormalizeMetadata(metadata)); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Creates an accepted acknowledgment response for a liability or responsibility handshake. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="request">The handshake request being acknowledged.</param> |
| | | 161 | | /// <param name="actor">The actor accepting the handshake.</param> |
| | | 162 | | /// <param name="acknowledgmentId">Optional acknowledgment identifier. When omitted, a new identifier is generated.< |
| | | 163 | | /// <param name="occurredUtc">Optional response timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 164 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 165 | | /// <param name="schemaVersion">Optional schema version for serialized or persisted acknowledgment records.</param> |
| | | 166 | | /// <returns>An accepted liability handshake acknowledgment response.</returns> |
| | | 167 | | public static LiabilityHandshakeAcknowledgment Accept( |
| | | 168 | | LiabilityHandshakeRequest request, |
| | | 169 | | IAsiBackboneActorContext actor, |
| | | 170 | | string? acknowledgmentId = null, |
| | | 171 | | DateTimeOffset? occurredUtc = null, |
| | | 172 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 173 | | string? schemaVersion = null) |
| | | 174 | | { |
| | 21 | 175 | | return Create(request, actor, acknowledged: true, acknowledgmentId, occurredUtc, metadata, schemaVersion); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Creates a rejected acknowledgment response for a liability or responsibility handshake. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <param name="request">The handshake request being rejected.</param> |
| | | 182 | | /// <param name="actor">The actor rejecting the handshake.</param> |
| | | 183 | | /// <param name="acknowledgmentId">Optional acknowledgment identifier. When omitted, a new identifier is generated.< |
| | | 184 | | /// <param name="occurredUtc">Optional response timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 185 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 186 | | /// <param name="schemaVersion">Optional schema version for serialized or persisted acknowledgment records.</param> |
| | | 187 | | /// <returns>A rejected liability handshake acknowledgment response.</returns> |
| | | 188 | | public static LiabilityHandshakeAcknowledgment Reject( |
| | | 189 | | LiabilityHandshakeRequest request, |
| | | 190 | | IAsiBackboneActorContext actor, |
| | | 191 | | string? acknowledgmentId = null, |
| | | 192 | | DateTimeOffset? occurredUtc = null, |
| | | 193 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 194 | | string? schemaVersion = null) |
| | | 195 | | { |
| | 1 | 196 | | return Create(request, actor, acknowledged: false, acknowledgmentId, occurredUtc, metadata, schemaVersion); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | private static string NormalizeIdentifier(string? identifier) |
| | | 200 | | { |
| | 26 | 201 | | return string.IsNullOrWhiteSpace(identifier) |
| | 26 | 202 | | ? Guid.NewGuid().ToString("N") |
| | 26 | 203 | | : identifier.Trim(); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | private static string? NormalizeOptional(string? value) |
| | | 207 | | { |
| | 78 | 208 | | return string.IsNullOrWhiteSpace(value) |
| | 78 | 209 | | ? null |
| | 78 | 210 | | : value.Trim(); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 214 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 215 | | { |
| | 26 | 216 | | if (metadata is null || metadata.Count == 0) |
| | | 217 | | { |
| | 16 | 218 | | return EmptyMetadata; |
| | | 219 | | } |
| | | 220 | | |
| | 10 | 221 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 222 | | |
| | 46 | 223 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 224 | | { |
| | 13 | 225 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 226 | | { |
| | | 227 | | continue; |
| | | 228 | | } |
| | | 229 | | |
| | 10 | 230 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 231 | | } |
| | | 232 | | |
| | 10 | 233 | | return normalizedMetadata.Count == 0 |
| | 10 | 234 | | ? EmptyMetadata |
| | 10 | 235 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 236 | | } |
| | | 237 | | } |