|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core.indexlifecycle; |
| 8 | + |
| 9 | +import org.elasticsearch.action.ActionResponse; |
| 10 | +import org.elasticsearch.common.ParseField; |
| 11 | +import org.elasticsearch.common.Strings; |
| 12 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 14 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 15 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 16 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 17 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +/** |
| 28 | + * The response object returned by the Explain Lifecycle API. |
| 29 | + * |
| 30 | + * Since the API can be run over multiple indices the response provides a map of |
| 31 | + * index to the explanation of the lifecycle status for that index. |
| 32 | + */ |
| 33 | +public class ExplainLifecycleResponse extends ActionResponse implements ToXContentObject { |
| 34 | + |
| 35 | + public static final ParseField INDICES_FIELD = new ParseField("indices"); |
| 36 | + |
| 37 | + private Map<String, IndexLifecycleExplainResponse> indexResponses; |
| 38 | + |
| 39 | + @SuppressWarnings("unchecked") |
| 40 | + private static final ConstructingObjectParser<ExplainLifecycleResponse, Void> PARSER = new ConstructingObjectParser<>( |
| 41 | + "explain_lifecycle_response", a -> new ExplainLifecycleResponse(((List<IndexLifecycleExplainResponse>) a[0]).stream() |
| 42 | + .collect(Collectors.toMap(IndexLifecycleExplainResponse::getIndex, Function.identity())))); |
| 43 | + static { |
| 44 | + PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> IndexLifecycleExplainResponse.PARSER.apply(p, c), |
| 45 | + INDICES_FIELD); |
| 46 | + } |
| 47 | + |
| 48 | + public static ExplainLifecycleResponse fromXContent(XContentParser parser) { |
| 49 | + return PARSER.apply(parser, null); |
| 50 | + } |
| 51 | + |
| 52 | + public ExplainLifecycleResponse() { |
| 53 | + } |
| 54 | + |
| 55 | + public ExplainLifecycleResponse(Map<String, IndexLifecycleExplainResponse> indexResponses) { |
| 56 | + this.indexResponses = indexResponses; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return a map of the responses from each requested index. The maps key is |
| 61 | + * the index name and the value is the |
| 62 | + * {@link IndexLifecycleExplainResponse} describing the current |
| 63 | + * lifecycle status of that index |
| 64 | + */ |
| 65 | + public Map<String, IndexLifecycleExplainResponse> getIndexResponses() { |
| 66 | + return indexResponses; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 71 | + builder.startObject(); |
| 72 | + builder.startObject(INDICES_FIELD.getPreferredName()); |
| 73 | + for (IndexLifecycleExplainResponse indexResponse : indexResponses.values()) { |
| 74 | + builder.field(indexResponse.getIndex(), indexResponse); |
| 75 | + } |
| 76 | + builder.endObject(); |
| 77 | + builder.endObject(); |
| 78 | + return builder; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void readFrom(StreamInput in) throws IOException { |
| 83 | + int size = in.readVInt(); |
| 84 | + Map<String, IndexLifecycleExplainResponse> indexResponses = new HashMap<>(size); |
| 85 | + for (int i = 0; i < size; i++) { |
| 86 | + IndexLifecycleExplainResponse indexResponse = new IndexLifecycleExplainResponse(in); |
| 87 | + indexResponses.put(indexResponse.getIndex(), indexResponse); |
| 88 | + } |
| 89 | + this.indexResponses = indexResponses; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void writeTo(StreamOutput out) throws IOException { |
| 94 | + out.writeVInt(indexResponses.size()); |
| 95 | + for (IndexLifecycleExplainResponse e : indexResponses.values()) { |
| 96 | + e.writeTo(out); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int hashCode() { |
| 102 | + return Objects.hash(indexResponses); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean equals(Object obj) { |
| 107 | + if (obj == null) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + if (obj.getClass() != getClass()) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + ExplainLifecycleResponse other = (ExplainLifecycleResponse) obj; |
| 114 | + return Objects.equals(indexResponses, other.indexResponses); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String toString() { |
| 119 | + return Strings.toString(this, true, true); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments