|
| 1 | +namespace Sigtran.NET.Core.Utilities; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Describes a configured maintained external peer lab binding. |
| 5 | +/// </summary> |
| 6 | +public sealed class SigtranMaintainedPeerLabBinding |
| 7 | +{ |
| 8 | + private readonly Dictionary<string, string> _environmentVariables; |
| 9 | + private readonly string[] _satisfiedCriterionIds; |
| 10 | + |
| 11 | + /// <summary>Creates a maintained external peer lab binding.</summary> |
| 12 | + /// <param name="id">The stable binding id.</param> |
| 13 | + /// <param name="peerProfile">The peer profile selected for the binding.</param> |
| 14 | + /// <param name="packageId">The lab package id or package-neutral placeholder.</param> |
| 15 | + /// <param name="packageVersion">The lab package version or package-neutral placeholder.</param> |
| 16 | + /// <param name="artifactRoot">The artifact root used by the binding.</param> |
| 17 | + /// <param name="environmentVariables">The environment variables required by lab scripts.</param> |
| 18 | + /// <param name="satisfiedCriterionIds">The maintained peer selection criteria satisfied by the binding.</param> |
| 19 | + public SigtranMaintainedPeerLabBinding( |
| 20 | + string id, |
| 21 | + SigtranInteropPeerProfile peerProfile, |
| 22 | + string packageId, |
| 23 | + string packageVersion, |
| 24 | + string artifactRoot, |
| 25 | + IReadOnlyDictionary<string, string> environmentVariables, |
| 26 | + IReadOnlyList<string> satisfiedCriterionIds) |
| 27 | + { |
| 28 | + ArgumentNullException.ThrowIfNull(peerProfile); |
| 29 | + ArgumentNullException.ThrowIfNull(environmentVariables); |
| 30 | + ArgumentNullException.ThrowIfNull(satisfiedCriterionIds); |
| 31 | + |
| 32 | + Id = string.IsNullOrWhiteSpace(id) ? throw new ArgumentException("Binding id is required.", nameof(id)) : id; |
| 33 | + PeerProfile = peerProfile; |
| 34 | + PackageId = string.IsNullOrWhiteSpace(packageId) ? throw new ArgumentException("Package id is required.", nameof(packageId)) : packageId; |
| 35 | + PackageVersion = string.IsNullOrWhiteSpace(packageVersion) ? throw new ArgumentException("Package version is required.", nameof(packageVersion)) : packageVersion; |
| 36 | + ArtifactRoot = string.IsNullOrWhiteSpace(artifactRoot) ? throw new ArgumentException("Artifact root is required.", nameof(artifactRoot)) : artifactRoot; |
| 37 | + _environmentVariables = environmentVariables.Count == 0 |
| 38 | + ? throw new ArgumentException("At least one environment variable is required.", nameof(environmentVariables)) |
| 39 | + : new Dictionary<string, string>(environmentVariables, StringComparer.OrdinalIgnoreCase); |
| 40 | + _satisfiedCriterionIds = satisfiedCriterionIds.Count == 0 |
| 41 | + ? throw new ArgumentException("At least one satisfied criterion id is required.", nameof(satisfiedCriterionIds)) |
| 42 | + : satisfiedCriterionIds.ToArray(); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary>The stable binding id.</summary> |
| 46 | + public string Id { get; } |
| 47 | + |
| 48 | + /// <summary>The peer profile selected for the binding.</summary> |
| 49 | + public SigtranInteropPeerProfile PeerProfile { get; } |
| 50 | + |
| 51 | + /// <summary>The lab package id or package-neutral placeholder.</summary> |
| 52 | + public string PackageId { get; } |
| 53 | + |
| 54 | + /// <summary>The lab package version or package-neutral placeholder.</summary> |
| 55 | + public string PackageVersion { get; } |
| 56 | + |
| 57 | + /// <summary>The artifact root used by the binding.</summary> |
| 58 | + public string ArtifactRoot { get; } |
| 59 | + |
| 60 | + /// <summary>The environment variables required by lab scripts.</summary> |
| 61 | + public IReadOnlyDictionary<string, string> EnvironmentVariables => new Dictionary<string, string>(_environmentVariables, StringComparer.OrdinalIgnoreCase); |
| 62 | + |
| 63 | + /// <summary>The maintained peer selection criteria satisfied by the binding.</summary> |
| 64 | + public IReadOnlyList<string> SatisfiedCriterionIds => _satisfiedCriterionIds.ToArray(); |
| 65 | + |
| 66 | + /// <summary>Evaluates the binding against a maintained peer selection policy.</summary> |
| 67 | + /// <param name="policy">The maintained peer selection policy.</param> |
| 68 | + /// <returns>The maintained peer selection report.</returns> |
| 69 | + public SigtranMaintainedPeerSelectionReport EvaluateSelection(SigtranMaintainedPeerSelectionPolicy policy) |
| 70 | + { |
| 71 | + ArgumentNullException.ThrowIfNull(policy); |
| 72 | + return policy.Evaluate(PeerProfile, _satisfiedCriterionIds); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary>Formats a compact package-neutral binding summary.</summary> |
| 76 | + /// <returns>The binding summary.</returns> |
| 77 | + public string Describe() |
| 78 | + { |
| 79 | + return $"binding={Id} peer={PeerProfile.Id} packageConfigured={PackageId.Length > 0} versionConfigured={PackageVersion.Length > 0} artifacts={ArtifactRoot} criteria={_satisfiedCriterionIds.Length}"; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// <summary> |
| 84 | +/// Provides package-neutral maintained external peer lab bindings. |
| 85 | +/// </summary> |
| 86 | +public static class SigtranMaintainedPeerLabBindings |
| 87 | +{ |
| 88 | + /// <summary>The environment variable that identifies the external peer profile.</summary> |
| 89 | + public const string PeerIdEnvironmentVariable = "SIGTRAN_EXTERNAL_PEER_ID"; |
| 90 | + |
| 91 | + /// <summary>The environment variable that identifies the external peer package.</summary> |
| 92 | + public const string PackageEnvironmentVariable = "SIGTRAN_EXTERNAL_PEER_PACKAGE"; |
| 93 | + |
| 94 | + /// <summary>The environment variable that identifies the external peer package version.</summary> |
| 95 | + public const string PackageVersionEnvironmentVariable = "SIGTRAN_EXTERNAL_PEER_PACKAGE_VERSION"; |
| 96 | + |
| 97 | + /// <summary>The environment variable that identifies the external peer artifact root.</summary> |
| 98 | + public const string ArtifactRootEnvironmentVariable = "SIGTRAN_EXTERNAL_PEER_ARTIFACT_ROOT"; |
| 99 | + |
| 100 | + /// <summary>Creates the default maintained external peer lab binding.</summary> |
| 101 | + /// <returns>The default maintained external peer lab binding.</returns> |
| 102 | + public static SigtranMaintainedPeerLabBinding CreateDefault() |
| 103 | + { |
| 104 | + SigtranInteropPeerProfile profile = SigtranInteropPeerProfiles.CreateExternalPeerSignallingGateway(); |
| 105 | + IReadOnlyList<string> criteria = SigtranMaintainedPeerSelectionPolicy.CreateDefault() |
| 106 | + .GetCriteria() |
| 107 | + .Select(static criterion => criterion.Id) |
| 108 | + .ToArray(); |
| 109 | + |
| 110 | + Dictionary<string, string> environment = new(StringComparer.OrdinalIgnoreCase) |
| 111 | + { |
| 112 | + [PeerIdEnvironmentVariable] = profile.Id, |
| 113 | + [PackageEnvironmentVariable] = "external-peer-package", |
| 114 | + [PackageVersionEnvironmentVariable] = "configured-by-lab", |
| 115 | + [ArtifactRootEnvironmentVariable] = "artifacts/external-peer/maintained" |
| 116 | + }; |
| 117 | + |
| 118 | + return new( |
| 119 | + "maintained-external-peer-lab", |
| 120 | + profile, |
| 121 | + environment[PackageEnvironmentVariable], |
| 122 | + environment[PackageVersionEnvironmentVariable], |
| 123 | + environment[ArtifactRootEnvironmentVariable], |
| 124 | + environment, |
| 125 | + criteria); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary>Creates the maintained external peer lab binding catalog.</summary> |
| 129 | + /// <returns>The maintained external peer lab binding catalog.</returns> |
| 130 | + public static IReadOnlyList<SigtranMaintainedPeerLabBinding> CreateCatalog() |
| 131 | + { |
| 132 | + return [CreateDefault()]; |
| 133 | + } |
| 134 | +} |
0 commit comments