internal static unsafe NodeDeactivationResult CreateFromNative(
            NativeTypes.FABRIC_NODE_DEACTIVATION_QUERY_RESULT_ITEM nativeResult)
        {
            IList <NodeDeactivationTask> tasks = new List <NodeDeactivationTask>();
            IList <SafetyCheck>          pendingSafetyChecks = new List <SafetyCheck>();

            var nativeTasks     = (NativeTypes.FABRIC_NODE_DEACTIVATION_TASK_LIST *)nativeResult.Tasks;
            var nativeItemArray = (NativeTypes.FABRIC_NODE_DEACTIVATION_TASK *)nativeTasks->Items;

            for (int i = 0; i < nativeTasks->Count; ++i)
            {
                var nativeItem = *(nativeItemArray + i);
                tasks.Add(NodeDeactivationTask.CreateFromNative(nativeItem));
            }

            if (nativeResult.Reserved != IntPtr.Zero)
            {
                var ex1 = (NativeTypes.FABRIC_NODE_DEACTIVATION_QUERY_RESULT_ITEM_EX1 *)nativeResult.Reserved;
                pendingSafetyChecks = SafetyCheck.FromNativeList(
                    (NativeTypes.FABRIC_SAFETY_CHECK_LIST *)ex1->PendingSafetyChecks);
            }

            var nodeDeactivationResult = new NodeDeactivationResult(
                (NodeDeactivationIntent)nativeResult.EffectiveIntent,
                (NodeDeactivationStatus)nativeResult.Status,
                tasks,
                pendingSafetyChecks);

            return(nodeDeactivationResult);
        }
        /// <inheritdoc />
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            ThrowIf.Null(reader, "reader");

            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            SafetyCheck result = null;

            var    wrapperJObject    = JObject.Load(reader);
            JToken safetyCheckJToken = wrapperJObject[JsonSerializerImplConstants.SafetyCheck];

            if (safetyCheckJToken != null)
            {
                JToken          safetyCheckKindJToken = safetyCheckJToken[JsonSerializerImplConstants.Kind];
                SafetyCheckKind kind = (SafetyCheckKind)safetyCheckKindJToken.ToObject(typeof(SafetyCheckKind));

                switch (kind)
                {
                case SafetyCheckKind.EnsureSeedNodeQuorum:
                    result = new SeedNodeSafetyCheck(kind);
                    break;

                case SafetyCheckKind.EnsurePartitionQuorum:
                case SafetyCheckKind.WaitForInBuildReplica:
                case SafetyCheckKind.WaitForPrimaryPlacement:
                case SafetyCheckKind.WaitForPrimarySwap:
                case SafetyCheckKind.WaitForReconfiguration:
                case SafetyCheckKind.EnsureAvailability:
                    JToken partitionIdJToken = safetyCheckJToken[JsonSerializerImplConstants.PartitionId];
                    Guid   partitionId       = partitionIdJToken.ToObject <Guid>(serializer);
                    result = new PartitionSafetyCheck(kind, partitionId);
                    break;

                default:
                    result = new UnknownSafetyCheck(kind);
                    break;
                }
            }

            return(result);
        }
        /// <summary>
        /// Gets the object from Json properties.
        /// </summary>
        /// <param name="reader">The <see cref="T: Newtonsoft.Json.JsonReader" /> to read from.</param>
        /// <returns>The object Value.</returns>
        internal static SafetyCheck GetFromJsonProperties(JsonReader reader)
        {
            SafetyCheck obj      = null;
            var         propName = reader.ReadPropertyName();

            if (!propName.Equals("Kind", StringComparison.Ordinal))
            {
                throw new JsonReaderException($"Incorrect discriminator property name {propName}, Expected discriminator property name is Kind.");
            }

            var propValue = reader.ReadValueAsString();

            if (propValue.Equals("EnsureAvailability", StringComparison.Ordinal))
            {
                obj = EnsureAvailabilitySafetyCheckConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("EnsurePartitionQuorum", StringComparison.Ordinal))
            {
                obj = EnsurePartitionQurumSafetyCheckConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("EnsureSeedNodeQuorum", StringComparison.Ordinal))
            {
                obj = SeedNodeSafetyCheckConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("WaitForInbuildReplica", StringComparison.Ordinal))
            {
                obj = WaitForInbuildReplicaSafetyCheckConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("WaitForPrimaryPlacement", StringComparison.Ordinal))
            {
                obj = WaitForPrimaryPlacementSafetyCheckConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("WaitForPrimarySwap", StringComparison.Ordinal))
            {
                obj = WaitForPrimarySwapSafetyCheckConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("WaitForReconfiguration", StringComparison.Ordinal))
            {
                obj = WaitForReconfigurationSafetyCheckConverter.GetFromJsonProperties(reader);
            }
            else
            {
                throw new InvalidOperationException("Unknown Kind.");
            }

            return(obj);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Serializes the object to JSON.
        /// </summary>
        /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param>
        /// <param name="obj">The object to serialize to JSON.</param>
        internal static void Serialize(JsonWriter writer, SafetyCheck obj)
        {
            var kind = obj.Kind;

            if (kind.Equals(SafetyCheckKind.EnsureAvailability))
            {
                EnsureAvailabilitySafetyCheckConverter.Serialize(writer, (EnsureAvailabilitySafetyCheck)obj);
            }
            else if (kind.Equals(SafetyCheckKind.EnsurePartitionQuorum))
            {
                EnsurePartitionQurumSafetyCheckConverter.Serialize(writer, (EnsurePartitionQurumSafetyCheck)obj);
            }
            else if (kind.Equals(SafetyCheckKind.EnsureSeedNodeQuorum))
            {
                SeedNodeSafetyCheckConverter.Serialize(writer, (SeedNodeSafetyCheck)obj);
            }
            else if (kind.Equals(SafetyCheckKind.WaitForInbuildReplica))
            {
                WaitForInbuildReplicaSafetyCheckConverter.Serialize(writer, (WaitForInbuildReplicaSafetyCheck)obj);
            }
            else if (kind.Equals(SafetyCheckKind.WaitForPrimaryPlacement))
            {
                WaitForPrimaryPlacementSafetyCheckConverter.Serialize(writer, (WaitForPrimaryPlacementSafetyCheck)obj);
            }
            else if (kind.Equals(SafetyCheckKind.WaitForPrimarySwap))
            {
                WaitForPrimarySwapSafetyCheckConverter.Serialize(writer, (WaitForPrimarySwapSafetyCheck)obj);
            }
            else if (kind.Equals(SafetyCheckKind.WaitForReconfiguration))
            {
                WaitForReconfigurationSafetyCheckConverter.Serialize(writer, (WaitForReconfigurationSafetyCheck)obj);
            }
            else
            {
                throw new InvalidOperationException("Unknown Kind.");
            }
        }