|
| 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 | +package org.elasticsearch.xpack.core.ml.action; |
| 7 | + |
| 8 | +import org.elasticsearch.action.Action; |
| 9 | +import org.elasticsearch.action.ActionRequest; |
| 10 | +import org.elasticsearch.action.ActionRequestBuilder; |
| 11 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 12 | +import org.elasticsearch.client.ElasticsearchClient; |
| 13 | +import org.elasticsearch.common.Nullable; |
| 14 | +import org.elasticsearch.common.ParseField; |
| 15 | +import org.elasticsearch.common.Strings; |
| 16 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 17 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 18 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 19 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 20 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 21 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 22 | +import org.elasticsearch.xpack.core.ml.job.config.MlFilter; |
| 23 | +import org.elasticsearch.xpack.core.ml.job.messages.Messages; |
| 24 | +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.SortedSet; |
| 32 | +import java.util.TreeSet; |
| 33 | + |
| 34 | + |
| 35 | +public class UpdateFilterAction extends Action<UpdateFilterAction.Request, PutFilterAction.Response, UpdateFilterAction.RequestBuilder> { |
| 36 | + |
| 37 | + public static final UpdateFilterAction INSTANCE = new UpdateFilterAction(); |
| 38 | + public static final String NAME = "cluster:admin/xpack/ml/filters/update"; |
| 39 | + |
| 40 | + private UpdateFilterAction() { |
| 41 | + super(NAME); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public RequestBuilder newRequestBuilder(ElasticsearchClient client) { |
| 46 | + return new RequestBuilder(client); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public PutFilterAction.Response newResponse() { |
| 51 | + return new PutFilterAction.Response(); |
| 52 | + } |
| 53 | + |
| 54 | + public static class Request extends ActionRequest implements ToXContentObject { |
| 55 | + |
| 56 | + public static final ParseField ADD_ITEMS = new ParseField("add_items"); |
| 57 | + public static final ParseField REMOVE_ITEMS = new ParseField("remove_items"); |
| 58 | + |
| 59 | + private static final ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new); |
| 60 | + |
| 61 | + static { |
| 62 | + PARSER.declareString((request, filterId) -> request.filterId = filterId, MlFilter.ID); |
| 63 | + PARSER.declareStringOrNull(Request::setDescription, MlFilter.DESCRIPTION); |
| 64 | + PARSER.declareStringArray(Request::setAddItems, ADD_ITEMS); |
| 65 | + PARSER.declareStringArray(Request::setRemoveItems, REMOVE_ITEMS); |
| 66 | + } |
| 67 | + |
| 68 | + public static Request parseRequest(String filterId, XContentParser parser) { |
| 69 | + Request request = PARSER.apply(parser, null); |
| 70 | + if (request.filterId == null) { |
| 71 | + request.filterId = filterId; |
| 72 | + } else if (!Strings.isNullOrEmpty(filterId) && !filterId.equals(request.filterId)) { |
| 73 | + // If we have both URI and body filter ID, they must be identical |
| 74 | + throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, MlFilter.ID.getPreferredName(), |
| 75 | + request.filterId, filterId)); |
| 76 | + } |
| 77 | + return request; |
| 78 | + } |
| 79 | + |
| 80 | + private String filterId; |
| 81 | + @Nullable |
| 82 | + private String description; |
| 83 | + private SortedSet<String> addItems = Collections.emptySortedSet(); |
| 84 | + private SortedSet<String> removeItems = Collections.emptySortedSet(); |
| 85 | + |
| 86 | + public Request() { |
| 87 | + } |
| 88 | + |
| 89 | + public Request(String filterId) { |
| 90 | + this.filterId = ExceptionsHelper.requireNonNull(filterId, MlFilter.ID.getPreferredName()); |
| 91 | + } |
| 92 | + |
| 93 | + public String getFilterId() { |
| 94 | + return filterId; |
| 95 | + } |
| 96 | + |
| 97 | + public String getDescription() { |
| 98 | + return description; |
| 99 | + } |
| 100 | + |
| 101 | + public void setDescription(String description) { |
| 102 | + this.description = description; |
| 103 | + } |
| 104 | + |
| 105 | + public SortedSet<String> getAddItems() { |
| 106 | + return addItems; |
| 107 | + } |
| 108 | + |
| 109 | + public void setAddItems(Collection<String> addItems) { |
| 110 | + this.addItems = new TreeSet<>(ExceptionsHelper.requireNonNull(addItems, ADD_ITEMS.getPreferredName())); |
| 111 | + } |
| 112 | + |
| 113 | + public SortedSet<String> getRemoveItems() { |
| 114 | + return removeItems; |
| 115 | + } |
| 116 | + |
| 117 | + public void setRemoveItems(Collection<String> removeItems) { |
| 118 | + this.removeItems = new TreeSet<>(ExceptionsHelper.requireNonNull(removeItems, REMOVE_ITEMS.getPreferredName())); |
| 119 | + } |
| 120 | + |
| 121 | + public boolean isNoop() { |
| 122 | + return description == null && addItems.isEmpty() && removeItems.isEmpty(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public ActionRequestValidationException validate() { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void readFrom(StreamInput in) throws IOException { |
| 132 | + super.readFrom(in); |
| 133 | + filterId = in.readString(); |
| 134 | + description = in.readOptionalString(); |
| 135 | + addItems = new TreeSet<>(Arrays.asList(in.readStringArray())); |
| 136 | + removeItems = new TreeSet<>(Arrays.asList(in.readStringArray())); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void writeTo(StreamOutput out) throws IOException { |
| 141 | + super.writeTo(out); |
| 142 | + out.writeString(filterId); |
| 143 | + out.writeOptionalString(description); |
| 144 | + out.writeStringArray(addItems.toArray(new String[addItems.size()])); |
| 145 | + out.writeStringArray(removeItems.toArray(new String[removeItems.size()])); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 150 | + builder.startObject(); |
| 151 | + builder.field(MlFilter.ID.getPreferredName(), filterId); |
| 152 | + if (description != null) { |
| 153 | + builder.field(MlFilter.DESCRIPTION.getPreferredName(), description); |
| 154 | + } |
| 155 | + if (addItems.isEmpty() == false) { |
| 156 | + builder.field(ADD_ITEMS.getPreferredName(), addItems); |
| 157 | + } |
| 158 | + if (removeItems.isEmpty() == false) { |
| 159 | + builder.field(REMOVE_ITEMS.getPreferredName(), removeItems); |
| 160 | + } |
| 161 | + builder.endObject(); |
| 162 | + return builder; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public int hashCode() { |
| 167 | + return Objects.hash(filterId, description, addItems, removeItems); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public boolean equals(Object obj) { |
| 172 | + if (obj == null) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + if (getClass() != obj.getClass()) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + Request other = (Request) obj; |
| 179 | + return Objects.equals(filterId, other.filterId) |
| 180 | + && Objects.equals(description, other.description) |
| 181 | + && Objects.equals(addItems, other.addItems) |
| 182 | + && Objects.equals(removeItems, other.removeItems); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + public static class RequestBuilder extends ActionRequestBuilder<Request, PutFilterAction.Response, RequestBuilder> { |
| 187 | + |
| 188 | + public RequestBuilder(ElasticsearchClient client) { |
| 189 | + super(client, INSTANCE, new Request()); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments