|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.watcher; |
| 21 | + |
| 22 | +import org.elasticsearch.ElasticsearchParseException; |
| 23 | +import org.elasticsearch.common.Nullable; |
| 24 | +import org.elasticsearch.common.ParseField; |
| 25 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.joda.time.DateTime; |
| 27 | +import org.joda.time.DateTimeZone; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Locale; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +public class ActionStatus { |
| 34 | + |
| 35 | + private final AckStatus ackStatus; |
| 36 | + @Nullable private final Execution lastExecution; |
| 37 | + @Nullable private final Execution lastSuccessfulExecution; |
| 38 | + @Nullable private final Throttle lastThrottle; |
| 39 | + |
| 40 | + public ActionStatus(AckStatus ackStatus, |
| 41 | + @Nullable Execution lastExecution, |
| 42 | + @Nullable Execution lastSuccessfulExecution, |
| 43 | + @Nullable Throttle lastThrottle) { |
| 44 | + this.ackStatus = ackStatus; |
| 45 | + this.lastExecution = lastExecution; |
| 46 | + this.lastSuccessfulExecution = lastSuccessfulExecution; |
| 47 | + this.lastThrottle = lastThrottle; |
| 48 | + } |
| 49 | + |
| 50 | + public AckStatus ackStatus() { |
| 51 | + return ackStatus; |
| 52 | + } |
| 53 | + |
| 54 | + public Execution lastExecution() { |
| 55 | + return lastExecution; |
| 56 | + } |
| 57 | + |
| 58 | + public Execution lastSuccessfulExecution() { |
| 59 | + return lastSuccessfulExecution; |
| 60 | + } |
| 61 | + |
| 62 | + public Throttle lastThrottle() { |
| 63 | + return lastThrottle; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean equals(Object o) { |
| 68 | + if (this == o) return true; |
| 69 | + if (o == null || getClass() != o.getClass()) return false; |
| 70 | + |
| 71 | + ActionStatus that = (ActionStatus) o; |
| 72 | + |
| 73 | + return Objects.equals(ackStatus, that.ackStatus) && |
| 74 | + Objects.equals(lastExecution, that.lastExecution) && |
| 75 | + Objects.equals(lastSuccessfulExecution, that.lastSuccessfulExecution) && |
| 76 | + Objects.equals(lastThrottle, that.lastThrottle); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public int hashCode() { |
| 81 | + return Objects.hash(ackStatus, lastExecution, lastSuccessfulExecution, lastThrottle); |
| 82 | + } |
| 83 | + |
| 84 | + public static ActionStatus parse(String actionId, XContentParser parser) throws IOException { |
| 85 | + AckStatus ackStatus = null; |
| 86 | + Execution lastExecution = null; |
| 87 | + Execution lastSuccessfulExecution = null; |
| 88 | + Throttle lastThrottle = null; |
| 89 | + |
| 90 | + String currentFieldName = null; |
| 91 | + XContentParser.Token token; |
| 92 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 93 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 94 | + currentFieldName = parser.currentName(); |
| 95 | + } else if (Field.ACK_STATUS.match(currentFieldName, parser.getDeprecationHandler())) { |
| 96 | + ackStatus = AckStatus.parse(actionId, parser); |
| 97 | + } else if (Field.LAST_EXECUTION.match(currentFieldName, parser.getDeprecationHandler())) { |
| 98 | + lastExecution = Execution.parse(actionId, parser); |
| 99 | + } else if (Field.LAST_SUCCESSFUL_EXECUTION.match(currentFieldName, parser.getDeprecationHandler())) { |
| 100 | + lastSuccessfulExecution = Execution.parse(actionId, parser); |
| 101 | + } else if (Field.LAST_THROTTLE.match(currentFieldName, parser.getDeprecationHandler())) { |
| 102 | + lastThrottle = Throttle.parse(actionId, parser); |
| 103 | + } else { |
| 104 | + parser.skipChildren(); |
| 105 | + } |
| 106 | + } |
| 107 | + if (ackStatus == null) { |
| 108 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}]", |
| 109 | + actionId, Field.ACK_STATUS.getPreferredName()); |
| 110 | + } |
| 111 | + return new ActionStatus(ackStatus, lastExecution, lastSuccessfulExecution, lastThrottle); |
| 112 | + } |
| 113 | + |
| 114 | + public static class AckStatus { |
| 115 | + |
| 116 | + public enum State { |
| 117 | + AWAITS_SUCCESSFUL_EXECUTION, |
| 118 | + ACKABLE, |
| 119 | + ACKED; |
| 120 | + } |
| 121 | + |
| 122 | + private final DateTime timestamp; |
| 123 | + private final State state; |
| 124 | + |
| 125 | + public AckStatus(DateTime timestamp, State state) { |
| 126 | + this.timestamp = timestamp.toDateTime(DateTimeZone.UTC); |
| 127 | + this.state = state; |
| 128 | + } |
| 129 | + |
| 130 | + public DateTime timestamp() { |
| 131 | + return timestamp; |
| 132 | + } |
| 133 | + |
| 134 | + public State state() { |
| 135 | + return state; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public boolean equals(Object o) { |
| 140 | + if (this == o) return true; |
| 141 | + if (o == null || getClass() != o.getClass()) return false; |
| 142 | + |
| 143 | + AckStatus ackStatus = (AckStatus) o; |
| 144 | + |
| 145 | + return Objects.equals(timestamp, ackStatus.timestamp) && Objects.equals(state, ackStatus.state); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int hashCode() { |
| 150 | + return Objects.hash(timestamp, state); |
| 151 | + } |
| 152 | + |
| 153 | + public static AckStatus parse(String actionId, XContentParser parser) throws IOException { |
| 154 | + DateTime timestamp = null; |
| 155 | + State state = null; |
| 156 | + |
| 157 | + String currentFieldName = null; |
| 158 | + XContentParser.Token token; |
| 159 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 160 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 161 | + currentFieldName = parser.currentName(); |
| 162 | + } else if (Field.TIMESTAMP.match(currentFieldName, parser.getDeprecationHandler())) { |
| 163 | + timestamp = WatchStatusDateParser.parseDate(parser.text()); |
| 164 | + } else if (Field.ACK_STATUS_STATE.match(currentFieldName, parser.getDeprecationHandler())) { |
| 165 | + state = State.valueOf(parser.text().toUpperCase(Locale.ROOT)); |
| 166 | + } else { |
| 167 | + parser.skipChildren(); |
| 168 | + } |
| 169 | + } |
| 170 | + if (timestamp == null) { |
| 171 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", |
| 172 | + actionId, Field.ACK_STATUS.getPreferredName(), Field.TIMESTAMP.getPreferredName()); |
| 173 | + } |
| 174 | + if (state == null) { |
| 175 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", |
| 176 | + actionId, Field.ACK_STATUS.getPreferredName(), Field.ACK_STATUS_STATE.getPreferredName()); |
| 177 | + } |
| 178 | + return new AckStatus(timestamp, state); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public static class Execution { |
| 183 | + |
| 184 | + public static Execution successful(DateTime timestamp) { |
| 185 | + return new Execution(timestamp, true, null); |
| 186 | + } |
| 187 | + |
| 188 | + public static Execution failure(DateTime timestamp, String reason) { |
| 189 | + return new Execution(timestamp, false, reason); |
| 190 | + } |
| 191 | + |
| 192 | + private final DateTime timestamp; |
| 193 | + private final boolean successful; |
| 194 | + private final String reason; |
| 195 | + |
| 196 | + private Execution(DateTime timestamp, boolean successful, String reason) { |
| 197 | + this.timestamp = timestamp.toDateTime(DateTimeZone.UTC); |
| 198 | + this.successful = successful; |
| 199 | + this.reason = reason; |
| 200 | + } |
| 201 | + |
| 202 | + public DateTime timestamp() { |
| 203 | + return timestamp; |
| 204 | + } |
| 205 | + |
| 206 | + public boolean successful() { |
| 207 | + return successful; |
| 208 | + } |
| 209 | + |
| 210 | + public String reason() { |
| 211 | + return reason; |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public boolean equals(Object o) { |
| 216 | + if (this == o) return true; |
| 217 | + if (o == null || getClass() != o.getClass()) return false; |
| 218 | + |
| 219 | + Execution execution = (Execution) o; |
| 220 | + |
| 221 | + return Objects.equals(successful, execution.successful) && |
| 222 | + Objects.equals(timestamp, execution.timestamp) && |
| 223 | + Objects.equals(reason, execution.reason); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public int hashCode() { |
| 228 | + return Objects.hash(timestamp, successful, reason); |
| 229 | + } |
| 230 | + |
| 231 | + public static Execution parse(String actionId, XContentParser parser) throws IOException { |
| 232 | + DateTime timestamp = null; |
| 233 | + Boolean successful = null; |
| 234 | + String reason = null; |
| 235 | + |
| 236 | + String currentFieldName = null; |
| 237 | + XContentParser.Token token; |
| 238 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 239 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 240 | + currentFieldName = parser.currentName(); |
| 241 | + } else if (Field.TIMESTAMP.match(currentFieldName, parser.getDeprecationHandler())) { |
| 242 | + timestamp = WatchStatusDateParser.parseDate(parser.text()); |
| 243 | + } else if (Field.EXECUTION_SUCCESSFUL.match(currentFieldName, parser.getDeprecationHandler())) { |
| 244 | + successful = parser.booleanValue(); |
| 245 | + } else if (Field.REASON.match(currentFieldName, parser.getDeprecationHandler())) { |
| 246 | + reason = parser.text(); |
| 247 | + } else { |
| 248 | + parser.skipChildren(); |
| 249 | + } |
| 250 | + } |
| 251 | + if (timestamp == null) { |
| 252 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", |
| 253 | + actionId, Field.LAST_EXECUTION.getPreferredName(), Field.TIMESTAMP.getPreferredName()); |
| 254 | + } |
| 255 | + if (successful == null) { |
| 256 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", |
| 257 | + actionId, Field.LAST_EXECUTION.getPreferredName(), Field.EXECUTION_SUCCESSFUL.getPreferredName()); |
| 258 | + } |
| 259 | + if (successful) { |
| 260 | + return successful(timestamp); |
| 261 | + } |
| 262 | + if (reason == null) { |
| 263 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field for unsuccessful" + |
| 264 | + " execution [{}.{}]", actionId, Field.LAST_EXECUTION.getPreferredName(), Field.REASON.getPreferredName()); |
| 265 | + } |
| 266 | + return failure(timestamp, reason); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + public static class Throttle { |
| 271 | + |
| 272 | + private final DateTime timestamp; |
| 273 | + private final String reason; |
| 274 | + |
| 275 | + public Throttle(DateTime timestamp, String reason) { |
| 276 | + this.timestamp = timestamp.toDateTime(DateTimeZone.UTC); |
| 277 | + this.reason = reason; |
| 278 | + } |
| 279 | + |
| 280 | + public DateTime timestamp() { |
| 281 | + return timestamp; |
| 282 | + } |
| 283 | + |
| 284 | + public String reason() { |
| 285 | + return reason; |
| 286 | + } |
| 287 | + |
| 288 | + @Override |
| 289 | + public boolean equals(Object o) { |
| 290 | + if (this == o) return true; |
| 291 | + if (o == null || getClass() != o.getClass()) return false; |
| 292 | + |
| 293 | + Throttle throttle = (Throttle) o; |
| 294 | + return Objects.equals(timestamp, throttle.timestamp) && Objects.equals(reason, throttle.reason); |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public int hashCode() { |
| 299 | + return Objects.hash(timestamp, reason); |
| 300 | + } |
| 301 | + |
| 302 | + public static Throttle parse(String actionId, XContentParser parser) throws IOException { |
| 303 | + DateTime timestamp = null; |
| 304 | + String reason = null; |
| 305 | + |
| 306 | + String currentFieldName = null; |
| 307 | + XContentParser.Token token; |
| 308 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 309 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 310 | + currentFieldName = parser.currentName(); |
| 311 | + } else if (Field.TIMESTAMP.match(currentFieldName, parser.getDeprecationHandler())) { |
| 312 | + timestamp = WatchStatusDateParser.parseDate(parser.text()); |
| 313 | + } else if (Field.REASON.match(currentFieldName, parser.getDeprecationHandler())) { |
| 314 | + reason = parser.text(); |
| 315 | + } else { |
| 316 | + parser.skipChildren(); |
| 317 | + } |
| 318 | + } |
| 319 | + if (timestamp == null) { |
| 320 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", |
| 321 | + actionId, Field.LAST_THROTTLE.getPreferredName(), Field.TIMESTAMP.getPreferredName()); |
| 322 | + } |
| 323 | + if (reason == null) { |
| 324 | + throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", |
| 325 | + actionId, Field.LAST_THROTTLE.getPreferredName(), Field.REASON.getPreferredName()); |
| 326 | + } |
| 327 | + return new Throttle(timestamp, reason); |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + private interface Field { |
| 332 | + ParseField ACK_STATUS = new ParseField("ack"); |
| 333 | + ParseField ACK_STATUS_STATE = new ParseField("state"); |
| 334 | + ParseField LAST_EXECUTION = new ParseField("last_execution"); |
| 335 | + ParseField LAST_SUCCESSFUL_EXECUTION = new ParseField("last_successful_execution"); |
| 336 | + ParseField EXECUTION_SUCCESSFUL = new ParseField("successful"); |
| 337 | + ParseField LAST_THROTTLE = new ParseField("last_throttle"); |
| 338 | + ParseField TIMESTAMP = new ParseField("timestamp"); |
| 339 | + ParseField REASON = new ParseField("reason"); |
| 340 | + } |
| 341 | +} |
0 commit comments