using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;
///
/// The deploy gate's rule pass over one — everything AdminOperationsActor checks
/// before the script compile gate (), stated once so the upgrade preflight
/// (Preflight.UpgradePreflight, lmxopcua#1013/#1015/#1021/#1058) reports exactly what the next deploy would
/// refuse instead of a re-implementation that drifts.
///
public static class DeployGateRules
{
/// The deploy-gate error code a TagConfig inspection finding carries in Error mode.
public const string TagConfigInvalidCode = "TagConfigInvalid";
/// What the rule pass found.
/// Blocking: , the per-cluster topology check, and — in
/// — the TagConfig inspection.
/// The TagConfig inspection in mode
/// (non-blocking); empty in Error mode, where they are in .
/// Always non-blocking (lmxopcua#781 item 3).
/// Always non-blocking (data-connection alarm-mapping budget).
public sealed record Result(
IReadOnlyList Errors,
IReadOnlyList TagConfigWarnings,
IReadOnlyList SqlWritableWarnings,
IReadOnlyList DataConnectionWarnings);
/// Runs every rule over the snapshot.
/// The whole-DB snapshot ().
/// The configured Deployment:TagConfigValidationMode.
/// The findings.
public static Result Evaluate(DraftSnapshot draft, TagConfigValidationMode tagConfigMode)
{
ArgumentNullException.ThrowIfNull(draft);
// Full pre-seal gate: the complete DraftValidator (one pass, all rules).
var errors = DraftValidator.Validate(draft).ToList();
// Cluster-topology guard. The SQL CK_ServerCluster_RedundancyMode_NodeCount CHECK enforces the
// (NodeCount, RedundancyMode) pair on the row itself, but it cannot see the per-node ClusterNode.Enabled
// flag — an operator can disable a node (effective enabled-count = 1) while leaving RedundancyMode at
// Hot/Warm, which would boot the runtime into an InvalidTopology band. Not part of DraftValidator.Validate,
// so it runs here over the snapshot's cluster/node rows, ClusterId-ordered for a deterministic summary.
// R03:3.2 — both come off the SAME snapshot read as every other table.
var nodesByCluster = draft.ClusterNodes
.GroupBy(n => n.ClusterId, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.ToList(), StringComparer.Ordinal);
foreach (var cluster in draft.ServerClusters.OrderBy(c => c.ClusterId, StringComparer.Ordinal))
{
var nodes = nodesByCluster.TryGetValue(cluster.ClusterId, out var ns) ? (IReadOnlyList)ns : [];
errors.AddRange(DraftValidator.ValidateClusterTopology(cluster, nodes));
}
// Deploy-time TagConfig strictness inspection. The driver-typed EquipmentTagConfigInspector surfaces every
// blob the driver's own reader would REJECT — each of which makes the driver skip the tag so it reads
// BadNodeIdUnknown forever. Warn mode (default): non-blocking; Error mode: blocking. A raw Tag's driver is
// resolved via its Device.
var driverTypeById = draft.DriverInstances
.GroupBy(d => d.DriverInstanceId, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.First().DriverType, StringComparer.Ordinal);
var deviceById = draft.Devices
.GroupBy(d => d.DeviceId, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal);
var tagConfigFindings = new List();
foreach (var t in draft.Tags)
{
if (!deviceById.TryGetValue(t.DeviceId, out var device)) continue;
if (!driverTypeById.TryGetValue(device.DriverInstanceId, out var driverType)) continue;
foreach (var w in EquipmentTagConfigInspector.Inspect(driverType, t.TagConfig, device.DeviceConfig))
tagConfigFindings.Add(new ValidationError(TagConfigInvalidCode, $"tag '{t.TagId}': {w}", t.TagId));
}
IReadOnlyList tagConfigWarnings = [];
if (tagConfigMode == TagConfigValidationMode.Error) errors.AddRange(tagConfigFindings);
else tagConfigWarnings = tagConfigFindings;
return new Result(
errors,
tagConfigWarnings,
SqlTagDeployChecks.WritableMismatchWarnings(draft),
DataConnectionValidator.DeployWarnings(draft));
}
}