Files
lmxopcua/scripts/validation/grpc-data/node-probe/ProjectionDiagnostics.cs
T

82 lines
5.3 KiB
C#

using GrpcDataProbe;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
namespace GrpcDataNodeProbe;
public sealed partial class NodeProbeWorker
{
// Fixture-only, read-only inspection. Retained validity and item observations
// are bounded diagnostics, not an atomic authorization decision.
private ProbeResult CaptureProjectionDiagnostics(ProbeCommand command)
{
if (!Enum.TryParse<DataRealm>(command.Realm, true, out var realm) ||
realm is not (DataRealm.Raw or DataRealm.Uns) ||
string.IsNullOrWhiteSpace(command.OriginCluster) || command.OriginCluster.Length > 128 ||
string.IsNullOrWhiteSpace(command.NamespaceUri) || command.NamespaceUri.Length > 1024 ||
string.IsNullOrWhiteSpace(command.Path) || command.Path.Length > 4096)
throw new ArgumentException("Invalid fixture projection target.");
var mapping = command.OperationId ?? (realm == DataRealm.Raw ? "map-raw" : "map-uns");
if (string.IsNullOrWhiteSpace(mapping) || mapping.Length > 128)
throw new ArgumentException("Invalid fixture mapping.");
var identifier = ProjectedDataIds.Identifier(mapping,
new DataAddress(command.OriginCluster, command.NamespaceUri, "string", command.Path));
var nodes = services.GetRequiredService<IProjectedNodeLookup>();
var catalog = services.GetRequiredService<IFederatedProjectionCatalog>();
var mappings = services.GetRequiredService<ProjectedMappingDeploymentRuntime>();
var access = services.GetService<ProjectedDataAccess>();
var accessPresent = access is not null;
var nodeIdResolvable = nodes.TryGetNodeId(ProjectedDataIds.NamespaceUri(realm), identifier, out var nodeId);
ProjectedVariableDefinition? definition = null;
var definitionResolvable = nodeIdResolvable && nodeId is { } actual && nodes.TryResolve(actual, out definition);
var proofResolvable = catalog.TryResolveProjected(realm, identifier, out var proof);
var connectionPresent = mappings.TryGetConnectionId(mapping, out var connectionId);
var cache = services.GetRequiredService<ProjectedCatalogCache>();
var capture = typeof(ProjectedCatalogCache).GetMethod("CaptureRetainedDiagnostics",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var retained = capture?.Invoke(cache, [realm, identifier]);
var retainedFlags = new Dictionary<string, object?>();
foreach (var name in new[] { "Retained", "Retired", "Generation", "RouteAlive", "LocalAuthorityActive",
"RemoteLeaseValid", "MappingBindingCurrent", "CacheGenerationCurrent" })
retainedFlags[name] = retained?.GetType().GetProperty(name)?.GetValue(retained);
var metadataMatches = definition is not null && proof is not null &&
proof.Metadata.Dimensions.SequenceEqual(definition.Metadata.Dimensions) &&
(proof.Metadata with { Dimensions = definition.Metadata.Dimensions }) == definition.Metadata;
var deferred = services.GetService<DeferredAddressSpaceSink>();
var sdk = deferred?.GetCapability<SdkAddressSpaceSink>();
var manager = sdk?.GetType().GetField("_nodeManager",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)?.GetValue(sdk);
var sdkItems = SdkItemDiagnostics.Capture(manager, nodeId);
var observations = new Dictionary<string, object?>
{
["nodeIdResolvable"] = nodeIdResolvable,
["definitionResolvable"] = definitionResolvable,
["definitionAuthorityCurrent"] = definition?.AuthorityIsCurrent(),
["definitionLifetimeActive"] = definition?.Lifetime.IsActive,
["definitionGeneration"] = definition?.ProjectionGeneration,
["proofResolvable"] = proofResolvable,
["proofGeneration"] = proof?.Generation,
["generationMatches"] = definition is not null && proof is not null && definition.ProjectionGeneration == proof.Generation,
["metadataMatches"] = metadataMatches,
["leaseValid"] = proof?.Lease.IsValid,
["leaseRemainingMilliseconds"] = null, // IDataAuthorizationLease does not expose a remaining duration.
["retainedValidity"] = retainedFlags,
["maximumSampleAgeMilliseconds"] = proof?.MaximumSampleAge.TotalMilliseconds,
["servingGeneration"] = proof?.Lease.Serving.ServingGeneration,
["allowedOperations"] = proof?.Subject.AllowedOperations.ToString(),
["connectionMappingPresent"] = connectionPresent && !string.IsNullOrWhiteSpace(connectionId),
["projectedAccessPresent"] = accessPresent,
["streamCounters"] = ProjectedStreamDiagnostics.Capture(),
["deliveryCounters"] = ProjectedDeliveryDiagnostics.Capture(),
["sdkItems"] = sdkItems,
["runtimeCohorts"] = RuntimeCohortDiagnostics.Capture(access, catalog, realm, identifier, proof),
["callerAuthorization"] = "unknown: no caller identity is supplied to this fixture diagnostic"
};
return new(command.Id, command.Operation, true, 1, observations);
}
}