ea976ffa81
C-6: DeploymentQueries projects Deployment without ArtifactBlob (DbHealthProbe, readiness, ClusterOverview, DriverHost boot). C-11/A-5: DraftSnapshotFactory.FromConfigDbForDataConnectionsAsync + one-parse-per-artifact LoadAppliedAsync + capped pickers. H-6: ConfigPublishCoordinator persists acks off the actor thread (AckPersisted); deadline backlog kicked in parallel. H-3: Restart/Reconnect reply = dispatch; ConfigEdit audit written off-mailbox, failure logged not surfaced. U-6: ServerHistorian:MaxEventsPerRead (default 10000) pages HistoryReadEvents; advertised as MaxReturnEventValues. A-8: /reservations active-only by default, projected, capped at 200. Co-Authored-By: Claude <noreply@anthropic.com>
361 lines
14 KiB
C#
361 lines
14 KiB
C#
using System.Text.Json;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
|
|
|
public static partial class DeploymentArtifact
|
|
{
|
|
/// <summary>Reads the durable central scope generation independently of whether this node still has imports.</summary>
|
|
public static long ParseDataConnectionScopeVersion(ReadOnlySpan<byte> blob)
|
|
{
|
|
using var parsed = Open(blob.ToArray());
|
|
return parsed.ParseDataConnectionScopeVersion();
|
|
}
|
|
/// <summary>Read the node-effective data connections from an artifact.</summary>
|
|
public static IReadOnlyList<DataConnection> ParseDataConnections(
|
|
ReadOnlySpan<byte> blob,
|
|
string nodeId
|
|
)
|
|
{
|
|
using var parsed = Open(blob.ToArray());
|
|
return parsed.ParseDataConnections(nodeId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read the node-effective data connections of SEVERAL nodes from one artifact, inflating and opening the blob
|
|
/// once (lmxopcua#996 / A-5: two nodes on one deployment used to inflate the same blob twice).
|
|
/// </summary>
|
|
/// <param name="blob">The artifact blob.</param>
|
|
/// <param name="nodeIds">The nodes to project for.</param>
|
|
/// <returns>One entry per distinct node id (case-insensitive).</returns>
|
|
public static IReadOnlyDictionary<string, IReadOnlyList<DataConnection>> ParseDataConnections(
|
|
ReadOnlySpan<byte> blob,
|
|
IEnumerable<string> nodeIds
|
|
)
|
|
{
|
|
using var parsed = Open(blob.ToArray());
|
|
var result = new Dictionary<string, IReadOnlyList<DataConnection>>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (var node in nodeIds)
|
|
if (!result.ContainsKey(node))
|
|
result[node] = parsed.ParseDataConnections(node);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>Read the node-effective data exports from an artifact.</summary>
|
|
public static IReadOnlyList<DataExport> ParseDataExports(ReadOnlySpan<byte> blob, string nodeId)
|
|
{
|
|
using var parsed = Open(blob.ToArray());
|
|
return parsed.ParseDataExports(nodeId);
|
|
}
|
|
|
|
/// <summary>Read the node-effective remote namespace mappings from an artifact.</summary>
|
|
public static IReadOnlyList<RemoteNamespaceMapping> ParseRemoteNamespaceMappings(
|
|
ReadOnlySpan<byte> blob,
|
|
string nodeId
|
|
)
|
|
{
|
|
using var parsed = Open(blob.ToArray());
|
|
return parsed.ParseRemoteNamespaceMappings(nodeId);
|
|
}
|
|
}
|
|
|
|
internal sealed partial class ParsedDeploymentArtifact
|
|
{
|
|
internal long ParseDataConnectionScopeVersion()
|
|
{
|
|
if (!Root.TryGetProperty("DataConnectionScopeVersion", out var value)) return 0;
|
|
if (!value.TryGetInt64(out var version) || version <= 0)
|
|
throw new InvalidOperationException("Data connection scope generation is invalid.");
|
|
return version;
|
|
}
|
|
/// <summary>Materialize this node's eligible catalog inputs while the artifact document is alive.</summary>
|
|
internal DraftSnapshot? ParseLocalCatalogSnapshot(string nodeId) => ReadDataProjection(nodeId)?.Snapshot;
|
|
|
|
/// <summary>Legacy artifacts and empty data sections do not require an activated data catalog.</summary>
|
|
internal bool ExpectsLocalDataCatalog()
|
|
{
|
|
var root = Root;
|
|
if (root.ValueKind != JsonValueKind.Object) return false;
|
|
return new[] { "DataConnections", "DataExports", "RemoteNamespaceMappings" }
|
|
.Any(name => root.TryGetProperty(name, out var value)
|
|
&& (value.ValueKind != JsonValueKind.Array || value.GetArrayLength() != 0));
|
|
}
|
|
|
|
private static readonly JsonSerializerOptions DataJsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
};
|
|
private static readonly StringComparer DataIds = StringComparer.OrdinalIgnoreCase;
|
|
|
|
public IReadOnlyList<DataConnection> ParseDataConnections(string nodeId)
|
|
{
|
|
var projection = ReadDataProjection(nodeId);
|
|
if (projection is null)
|
|
return [];
|
|
ValidateRows(
|
|
projection.Value.Snapshot,
|
|
projection.Value.Snapshot.DataConnections.Select(x => x.DataConnectionId)
|
|
);
|
|
return projection
|
|
.Value.Snapshot.DataConnections.Where(x => x.ConsumerNodeIds.Contains(nodeId, DataIds))
|
|
.ToArray();
|
|
}
|
|
|
|
public IReadOnlyList<DataExport> ParseDataExports(string nodeId)
|
|
{
|
|
var projection = ReadDataProjection(nodeId);
|
|
if (projection is null)
|
|
return [];
|
|
ValidateRows(
|
|
projection.Value.Snapshot,
|
|
projection.Value.Snapshot.DataExports.Select(x => x.DataExportId)
|
|
);
|
|
return projection.Value.Snapshot.DataExports.ToArray();
|
|
}
|
|
|
|
public IReadOnlyList<RemoteNamespaceMapping> ParseRemoteNamespaceMappings(string nodeId)
|
|
{
|
|
var projection = ReadDataProjection(nodeId);
|
|
if (projection is null)
|
|
return [];
|
|
var snapshot = projection.Value.Snapshot;
|
|
ValidateRows(
|
|
snapshot,
|
|
snapshot
|
|
.RemoteNamespaceMappings.Where(x =>
|
|
DataIds.Equals(x.ClusterId, projection.Value.ClusterId)
|
|
)
|
|
.Select(x => x.RemoteNamespaceMappingId)
|
|
.Concat(snapshot.DataConnections.Select(x => x.DataConnectionId))
|
|
);
|
|
return snapshot
|
|
.RemoteNamespaceMappings.Where(x =>
|
|
DataIds.Equals(x.ClusterId, projection.Value.ClusterId)
|
|
&& x.ConsumerNodeIds.Contains(nodeId, DataIds)
|
|
)
|
|
.ToArray();
|
|
}
|
|
|
|
private (DraftSnapshot Snapshot, string ClusterId)? ReadDataProjection(string nodeId)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(nodeId);
|
|
var root = Root;
|
|
if (root.ValueKind != JsonValueKind.Object)
|
|
return null;
|
|
if (
|
|
!root.TryGetProperty("DataConnections", out _)
|
|
&& !root.TryGetProperty("DataExports", out _)
|
|
&& !root.TryGetProperty("RemoteNamespaceMappings", out _)
|
|
)
|
|
return null;
|
|
var node = EnumerateArray(root, "Nodes")
|
|
.Where(x => DataIds.Equals(ReadDataString(x, "NodeId"), nodeId))
|
|
.ToArray();
|
|
if (
|
|
node.Length != 1
|
|
|| !IsExplicitTrue(node[0], "Enabled")
|
|
|| IsExplicitTrue(node[0], "MaintenanceMode")
|
|
)
|
|
return null;
|
|
var clusterId = ReadDataString(node[0], "ClusterId");
|
|
if (string.IsNullOrWhiteSpace(clusterId))
|
|
return null;
|
|
var cluster = EnumerateArray(root, "Clusters")
|
|
.Where(x => DataIds.Equals(ReadDataString(x, "ClusterId"), clusterId))
|
|
.ToArray();
|
|
if (cluster.Length != 1 || !IsExplicitTrue(cluster[0], "Enabled"))
|
|
return null;
|
|
|
|
var connections = ReadDataRows<DataConnection>(
|
|
root,
|
|
"DataConnections",
|
|
"DataConnectionId",
|
|
clusterId
|
|
);
|
|
var exports = ReadDataRows<DataExport>(root, "DataExports", "DataExportId", clusterId);
|
|
var mappings = ReadDataRows<RemoteNamespaceMapping>(
|
|
root,
|
|
"RemoteNamespaceMappings",
|
|
"RemoteNamespaceMappingId",
|
|
clusterId
|
|
);
|
|
// Only foreign mapping identity is needed to detect a projected root. Do not import
|
|
// foreign configuration or validate it as a local execution graph.
|
|
foreach (var foreign in EnumerateArray(root, "RemoteNamespaceMappings"))
|
|
{
|
|
var owner = ReadDataString(foreign, "ClusterId");
|
|
var id = ReadDataString(foreign, "RemoteNamespaceMappingId");
|
|
if (
|
|
!string.IsNullOrWhiteSpace(owner)
|
|
&& !DataIds.Equals(owner, clusterId)
|
|
&& !string.IsNullOrWhiteSpace(id)
|
|
)
|
|
mappings.Add(
|
|
new RemoteNamespaceMapping { ClusterId = owner, RemoteNamespaceMappingId = id }
|
|
);
|
|
}
|
|
foreach (var c in connections)
|
|
if (c.ConsumerNodeIds is null || c.EligibleTargetNodeIds is null || c.Endpoints is null)
|
|
throw InvalidRow(c.DataConnectionId);
|
|
foreach (var m in mappings)
|
|
if (m.ConsumerNodeIds is null)
|
|
throw InvalidRow(m.RemoteNamespaceMappingId);
|
|
var snapshot = new DraftSnapshot
|
|
{
|
|
GenerationId = 0,
|
|
ClusterId = clusterId,
|
|
ServerClusters = ReadTopology<ServerCluster>(root, "Clusters"),
|
|
ClusterNodes = ReadTopology<ClusterNode>(root, "Nodes"),
|
|
RawFolders = ReadTopology<RawFolder>(root, "RawFolders"),
|
|
DriverInstances = ReadTopology<DriverInstance>(root, "DriverInstances"),
|
|
Devices = ReadTopology<Device>(root, "Devices"),
|
|
TagGroups = ReadTopology<TagGroup>(root, "TagGroups"),
|
|
Tags = ReadTopology<Tag>(root, "Tags"),
|
|
UnsAreas = ReadTopology<UnsArea>(root, "UnsAreas"),
|
|
UnsLines = ReadTopology<UnsLine>(root, "UnsLines"),
|
|
Equipment = ReadTopology<Equipment>(root, "Equipment"),
|
|
UnsTagReferences = ReadTopology<UnsTagReference>(root, "UnsTagReferences"),
|
|
VirtualTags = ReadTopology<VirtualTag>(root, "VirtualTags"),
|
|
ScriptedAlarms = ReadTopology<ScriptedAlarm>(root, "ScriptedAlarms"),
|
|
Scripts = ReadTopology<Script>(root, "Scripts"),
|
|
DataConnections = connections,
|
|
DataExports = exports,
|
|
RemoteNamespaceMappings = mappings,
|
|
};
|
|
return (snapshot, clusterId);
|
|
}
|
|
|
|
private static JsonElement? DataProperty(JsonElement element, string property)
|
|
{
|
|
JsonElement? found = null;
|
|
foreach (var entry in element.EnumerateObject())
|
|
{
|
|
if (!DataIds.Equals(entry.Name, property))
|
|
continue;
|
|
if (found is not null)
|
|
throw InvalidRow(null);
|
|
found = entry.Value;
|
|
}
|
|
return found;
|
|
}
|
|
|
|
private static string? ReadDataString(JsonElement element, string property)
|
|
{
|
|
var value = DataProperty(element, property);
|
|
return value is { ValueKind: JsonValueKind.String } ? value.Value.GetString() : null;
|
|
}
|
|
|
|
private static bool IsExplicitTrue(JsonElement element, string property) =>
|
|
DataProperty(element, property) is { ValueKind: JsonValueKind.True };
|
|
|
|
private static void RejectDuplicateDataProperties(JsonElement element)
|
|
{
|
|
var names = new HashSet<string>(DataIds);
|
|
foreach (var property in element.EnumerateObject())
|
|
if (!names.Add(property.Name))
|
|
throw InvalidRow(null);
|
|
}
|
|
|
|
private static InvalidDataException InvalidRow(string? id) =>
|
|
new(
|
|
$"Invalid data configuration row '{(string.IsNullOrWhiteSpace(id) ? "<unknown>" : id)}'."
|
|
);
|
|
|
|
private static List<T> ReadDataRows<T>(
|
|
JsonElement root,
|
|
string name,
|
|
string idName,
|
|
string clusterId
|
|
)
|
|
{
|
|
if (!root.TryGetProperty(name, out var array))
|
|
return [];
|
|
if (array.ValueKind != JsonValueKind.Array)
|
|
throw new InvalidDataException($"Invalid {name} collection.");
|
|
var result = new List<T>();
|
|
foreach (var element in array.EnumerateArray())
|
|
{
|
|
if (element.ValueKind != JsonValueKind.Object)
|
|
throw InvalidRow(null);
|
|
RejectDuplicateDataProperties(element);
|
|
var owner = ReadDataString(element, "ClusterId");
|
|
var id = ReadDataString(element, idName);
|
|
if (string.IsNullOrWhiteSpace(owner))
|
|
{
|
|
if (IsExplicitTrue(element, "Enabled"))
|
|
throw InvalidRow(id);
|
|
continue;
|
|
}
|
|
if (!DataIds.Equals(owner, clusterId))
|
|
continue;
|
|
if (IsExplicitTrue(element, "Enabled") && string.IsNullOrWhiteSpace(id))
|
|
throw InvalidRow(null);
|
|
try
|
|
{
|
|
var row = element.Deserialize<T>(DataJsonOptions);
|
|
if (row is null)
|
|
throw InvalidRow(id);
|
|
var identity = row switch
|
|
{
|
|
DataConnection c => (c.Enabled, c.DataConnectionId, c.ClusterId),
|
|
DataExport e => (e.Enabled, e.DataExportId, e.ClusterId),
|
|
RemoteNamespaceMapping m => (
|
|
m.Enabled,
|
|
m.RemoteNamespaceMappingId,
|
|
m.ClusterId
|
|
),
|
|
_ => throw new InvalidOperationException("Unsupported data row type."),
|
|
};
|
|
if (
|
|
!DataIds.Equals(identity.Item3, clusterId)
|
|
|| (identity.Item1 && string.IsNullOrWhiteSpace(identity.Item2))
|
|
)
|
|
throw InvalidRow(identity.Item2);
|
|
result.Add(row);
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
throw InvalidRow(id);
|
|
}
|
|
catch (NotSupportedException)
|
|
{
|
|
throw InvalidRow(id);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static List<T> ReadTopology<T>(JsonElement root, string name)
|
|
{
|
|
if (!root.TryGetProperty(name, out var array))
|
|
return [];
|
|
if (array.ValueKind != JsonValueKind.Array)
|
|
throw new InvalidDataException($"Invalid {name} topology.");
|
|
try
|
|
{
|
|
if (array.EnumerateArray().Any(element => element.ValueKind != JsonValueKind.Object))
|
|
throw new InvalidDataException($"Invalid {name} topology.");
|
|
var rows = array.Deserialize<List<T>>(DataJsonOptions)
|
|
?? throw new InvalidDataException($"Invalid {name} topology.");
|
|
if (rows.Any(row => row is null))
|
|
throw new InvalidDataException($"Invalid {name} topology.");
|
|
return rows;
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
throw new InvalidDataException($"Invalid {name} topology.");
|
|
}
|
|
}
|
|
|
|
private static void ValidateRows(DraftSnapshot snapshot, IEnumerable<string> ids)
|
|
{
|
|
var requested = ids.ToHashSet(DataIds);
|
|
var error = DataConnectionValidator
|
|
.ValidateDeployed(snapshot)
|
|
.FirstOrDefault(x => x.Context is not null && requested.Contains(x.Context));
|
|
if (error is not null)
|
|
throw InvalidRow(error.Context);
|
|
}
|
|
}
|