|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.admin.cluster.decommission.awareness.get; |
| 10 | + |
| 11 | +import org.opensearch.OpenSearchParseException; |
| 12 | +import org.opensearch.action.ActionResponse; |
| 13 | +import org.opensearch.cluster.decommission.DecommissionAttribute; |
| 14 | +import org.opensearch.cluster.decommission.DecommissionStatus; |
| 15 | +import org.opensearch.common.io.stream.StreamInput; |
| 16 | +import org.opensearch.common.io.stream.StreamOutput; |
| 17 | +import org.opensearch.common.xcontent.ToXContentObject; |
| 18 | +import org.opensearch.common.xcontent.XContentBuilder; |
| 19 | +import org.opensearch.common.xcontent.XContentParser; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Locale; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; |
| 26 | + |
| 27 | +/** |
| 28 | + * Response for decommission status |
| 29 | + * |
| 30 | + * @opensearch.internal |
| 31 | + */ |
| 32 | +public class GetDecommissionStateResponse extends ActionResponse implements ToXContentObject { |
| 33 | + |
| 34 | + private DecommissionAttribute decommissionedAttribute; |
| 35 | + private DecommissionStatus status; |
| 36 | + |
| 37 | + GetDecommissionStateResponse() { |
| 38 | + this(null, null); |
| 39 | + } |
| 40 | + |
| 41 | + GetDecommissionStateResponse(DecommissionAttribute decommissionedAttribute, DecommissionStatus status) { |
| 42 | + this.decommissionedAttribute = decommissionedAttribute; |
| 43 | + this.status = status; |
| 44 | + } |
| 45 | + |
| 46 | + GetDecommissionStateResponse(StreamInput in) throws IOException { |
| 47 | + // read decommissioned attribute and status only if it is present |
| 48 | + if (in.readBoolean()) { |
| 49 | + this.decommissionedAttribute = new DecommissionAttribute(in); |
| 50 | + } |
| 51 | + if (in.readBoolean()) { |
| 52 | + this.status = DecommissionStatus.fromString(in.readString()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void writeTo(StreamOutput out) throws IOException { |
| 58 | + // if decommissioned attribute is null, mark absence of decommissioned attribute |
| 59 | + if (decommissionedAttribute == null) { |
| 60 | + out.writeBoolean(false); |
| 61 | + } else { |
| 62 | + out.writeBoolean(true); |
| 63 | + decommissionedAttribute.writeTo(out); |
| 64 | + } |
| 65 | + |
| 66 | + // if status is null, mark absence of status |
| 67 | + if (status == null) { |
| 68 | + out.writeBoolean(false); |
| 69 | + } else { |
| 70 | + out.writeBoolean(true); |
| 71 | + out.writeString(status.status()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public DecommissionAttribute getDecommissionedAttribute() { |
| 76 | + return decommissionedAttribute; |
| 77 | + } |
| 78 | + |
| 79 | + public DecommissionStatus getDecommissionStatus() { |
| 80 | + return status; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 85 | + builder.startObject(); |
| 86 | + builder.startObject("awareness"); |
| 87 | + if (decommissionedAttribute != null) { |
| 88 | + builder.field(decommissionedAttribute.attributeName(), decommissionedAttribute.attributeValue()); |
| 89 | + } |
| 90 | + builder.endObject(); |
| 91 | + if (status != null) { |
| 92 | + builder.field("status", status); |
| 93 | + } |
| 94 | + builder.endObject(); |
| 95 | + return builder; |
| 96 | + } |
| 97 | + |
| 98 | + public static GetDecommissionStateResponse fromXContent(XContentParser parser) throws IOException { |
| 99 | + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); |
| 100 | + String attributeType = "awareness"; |
| 101 | + XContentParser.Token token; |
| 102 | + DecommissionAttribute decommissionAttribute = null; |
| 103 | + DecommissionStatus status = null; |
| 104 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 105 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 106 | + String currentFieldName = parser.currentName(); |
| 107 | + if (attributeType.equals(currentFieldName)) { |
| 108 | + if (parser.nextToken() != XContentParser.Token.START_OBJECT) { |
| 109 | + throw new OpenSearchParseException( |
| 110 | + "failed to parse decommission attribute type [{}], expected object", |
| 111 | + attributeType |
| 112 | + ); |
| 113 | + } |
| 114 | + token = parser.nextToken(); |
| 115 | + if (token != XContentParser.Token.END_OBJECT) { |
| 116 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 117 | + String fieldName = parser.currentName(); |
| 118 | + String value; |
| 119 | + token = parser.nextToken(); |
| 120 | + if (token == XContentParser.Token.VALUE_STRING) { |
| 121 | + value = parser.text(); |
| 122 | + } else { |
| 123 | + throw new OpenSearchParseException( |
| 124 | + "failed to parse attribute [{}], expected string for attribute value", |
| 125 | + fieldName |
| 126 | + ); |
| 127 | + } |
| 128 | + decommissionAttribute = new DecommissionAttribute(fieldName, value); |
| 129 | + parser.nextToken(); |
| 130 | + } else { |
| 131 | + throw new OpenSearchParseException("failed to parse attribute type [{}], unexpected type", attributeType); |
| 132 | + } |
| 133 | + } else { |
| 134 | + throw new OpenSearchParseException("failed to parse attribute type [{}]", attributeType); |
| 135 | + } |
| 136 | + } else if ("status".equals(currentFieldName)) { |
| 137 | + if (parser.nextToken() != XContentParser.Token.VALUE_STRING) { |
| 138 | + throw new OpenSearchParseException( |
| 139 | + "failed to parse status of decommissioning, expected string but found unknown type" |
| 140 | + ); |
| 141 | + } |
| 142 | + status = DecommissionStatus.fromString(parser.text().toLowerCase(Locale.ROOT)); |
| 143 | + } else { |
| 144 | + throw new OpenSearchParseException( |
| 145 | + "unknown field found [{}], failed to parse the decommission attribute", |
| 146 | + currentFieldName |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + return new GetDecommissionStateResponse(decommissionAttribute, status); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean equals(Object o) { |
| 156 | + if (this == o) return true; |
| 157 | + if (o == null || getClass() != o.getClass()) return false; |
| 158 | + GetDecommissionStateResponse that = (GetDecommissionStateResponse) o; |
| 159 | + return decommissionedAttribute.equals(that.decommissionedAttribute) && status == that.status; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public int hashCode() { |
| 164 | + return Objects.hash(decommissionedAttribute, status); |
| 165 | + } |
| 166 | +} |
0 commit comments