78 lines
3.8 KiB
C#
78 lines
3.8 KiB
C#
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
/// <summary>Origin-qualified public identity; namespace indexes are never portable identities.</summary>
|
|
public sealed record DataAddress(string OriginClusterId, string NamespaceUri, string IdentifierKind, string Identifier)
|
|
{
|
|
public void Validate()
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(OriginClusterId);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(NamespaceUri);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(IdentifierKind);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(Identifier);
|
|
}
|
|
}
|
|
public enum DataRealm { Unspecified = 0, Raw = 1, Uns = 2 }
|
|
public enum DataTargetMode { Unspecified = 0, Node = 1, Cluster = 2 }
|
|
public enum DataConsistency { Unspecified = 0, Primary = 1, ReadyReplica = 2 }
|
|
|
|
/// <summary>A request is bound to a cluster and, for node mode, an expected node.</summary>
|
|
public sealed record DataTarget(DataTargetMode Mode, string ExpectedClusterId, string? ExpectedNodeId,
|
|
DataConsistency Consistency = DataConsistency.Primary)
|
|
{
|
|
public void Validate()
|
|
{
|
|
DataContractValidation.Required(Mode);
|
|
DataContractValidation.Required(Consistency);
|
|
if (Mode == DataTargetMode.Cluster && Consistency != DataConsistency.Primary)
|
|
throw new ArgumentException("Cluster targets require Primary consistency.", nameof(Consistency));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(ExpectedClusterId);
|
|
if (Mode == DataTargetMode.Node) ArgumentException.ThrowIfNullOrWhiteSpace(ExpectedNodeId);
|
|
if (Mode == DataTargetMode.Cluster && ExpectedNodeId is not null)
|
|
throw new ArgumentException("Cluster targets cannot pin a node.", nameof(ExpectedNodeId));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The requested view, independent of backing-source identity. Subject is an untrusted claim until
|
|
/// resolved against the authoritative catalog; it must not independently grant access.
|
|
/// </summary>
|
|
public sealed record DataView(DataAddress Address, DataRealm Realm, string? MappingId, long MappingRevision, string Subject)
|
|
{
|
|
public void Validate()
|
|
{
|
|
ArgumentNullException.ThrowIfNull(Address); Address.Validate();
|
|
DataContractValidation.Required(Realm);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(Subject);
|
|
if (MappingRevision < 0 || (MappingId is null && MappingRevision != 0) ||
|
|
(MappingId is not null && (string.IsNullOrWhiteSpace(MappingId) || MappingRevision == 0)))
|
|
throw new ArgumentException("Mapped views require an ID and positive revision.");
|
|
}
|
|
}
|
|
/// <summary>Actual serving replica provenance, distinct from origin ownership.</summary>
|
|
public sealed record ServingIdentity(string NodeId, string Incarnation, string Role, long CatalogEpoch, long AuthorityEpoch,
|
|
long ServingGeneration = 0)
|
|
{
|
|
public void Validate(bool requireEpochs = false)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(NodeId);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(Incarnation);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(Role);
|
|
if (ServingGeneration < 0 || CatalogEpoch < 0 || AuthorityEpoch < 0 || (requireEpochs && (CatalogEpoch == 0 || AuthorityEpoch == 0)))
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(CatalogEpoch));
|
|
}
|
|
}
|
|
}
|
|
public sealed record DataSourceIdentity(DataAddress Address, string StableSourceId, ServingIdentity Serving, DataTargetMode TargetMode,
|
|
DataConsistency Consistency, string PrincipalId, string ExportScope);
|
|
|
|
public static class DataContractValidation
|
|
{
|
|
/// <summary>Reject zero and unrecognized values for a required enum decision.</summary>
|
|
public static void Required<T>(T value) where T : struct, Enum
|
|
{
|
|
if (Convert.ToInt64(value) == 0 || !Enum.IsDefined(value))
|
|
throw new ArgumentOutOfRangeException(nameof(value), value, "A defined nonzero value is required.");
|
|
}
|
|
}
|