Files
lmxopcua/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ProjectedDataIds.cs
T

79 lines
3.8 KiB
C#

using System.Text.Json;
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
/// <summary>Disjoint local identifiers for mapped nodes. Decoding never establishes authorization.</summary>
public static class ProjectedDataIds
{
public const string RawNamespaceUri = "https://zb.com/otopcua/mapped/raw";
public const string UnsNamespaceUri = "https://zb.com/otopcua/mapped/uns";
private const string Prefix = "p1.";
private const int MaximumIdentifierLength = 16_384;
private sealed record Identity(string MappingId, DataAddress Origin);
public static string NamespaceUri(DataRealm realm) => realm switch
{
DataRealm.Raw => RawNamespaceUri,
DataRealm.Uns => UnsNamespaceUri,
_ => throw new ArgumentOutOfRangeException(nameof(realm))
};
public static bool IsMappedNamespace(string namespaceUri) =>
namespaceUri is RawNamespaceUri or UnsNamespaceUri;
/// <summary>A portable dependency key, distinct from ordinary unqualified local paths.</summary>
public static string DependencyReference(DataRealm realm, string localIdentifier)
{
if (!TryDecodeIdentifier(localIdentifier, out _, out _)) throw new ArgumentException("Invalid projected identifier.", nameof(localIdentifier));
return "nsu=" + NamespaceUri(realm) + ";s=" + localIdentifier;
}
public static bool TryParseDependencyReference(string reference, out DataRealm realm, out string localIdentifier)
{
realm = DataRealm.Unspecified; localIdentifier = string.Empty;
if (reference is null) return false;
foreach (var candidate in new[] { DataRealm.Raw, DataRealm.Uns })
{
var prefix = "nsu=" + NamespaceUri(candidate) + ";s=";
if (!reference.StartsWith(prefix, StringComparison.Ordinal)) continue;
var identifier = reference[prefix.Length..];
if (!TryDecodeIdentifier(identifier, out _, out _)) return false;
realm = candidate; localIdentifier = identifier; return true;
}
return false;
}
public static string Identifier(string mappingId, DataAddress origin)
{
ArgumentException.ThrowIfNullOrWhiteSpace(mappingId);
if (mappingId.Length > 256) throw new ArgumentOutOfRangeException(nameof(mappingId));
ArgumentNullException.ThrowIfNull(origin);
origin.Validate();
var payload = JsonSerializer.SerializeToUtf8Bytes(new Identity(mappingId, origin));
if (payload.Length > (MaximumIdentifierLength - Prefix.Length) * 3 / 4)
throw new ArgumentOutOfRangeException(nameof(origin));
return Prefix + Convert.ToBase64String(payload).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
public static bool TryDecodeIdentifier(string identifier, out string mappingId, out DataAddress? origin)
{
mappingId = string.Empty; origin = null;
if (identifier is null || identifier.Length > MaximumIdentifierLength || !identifier.StartsWith(Prefix, StringComparison.Ordinal))
return false;
var encoded = identifier[Prefix.Length..];
if (encoded.Length == 0 || encoded.Any(c => !char.IsAsciiLetterOrDigit(c) && c is not '-' and not '_')) return false;
try
{
var padded = encoded.Replace('-', '+').Replace('_', '/');
padded = padded.PadRight((padded.Length + 3) / 4 * 4, '=');
var parsed = JsonSerializer.Deserialize<Identity>(Convert.FromBase64String(padded));
if (parsed?.Origin is null || !string.Equals(Identifier(parsed.MappingId, parsed.Origin), identifier, StringComparison.Ordinal))
return false;
mappingId = parsed.MappingId; origin = parsed.Origin;
return true;
}
catch (Exception error) when (error is JsonException or FormatException or ArgumentException)
{ return false; }
}
}