|
| 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 | +package org.elasticsearch.protocol.xpack.ml.job.config; |
| 20 | + |
| 21 | +import org.elasticsearch.common.ParseField; |
| 22 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 23 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.EnumSet; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +public class DetectionRule implements ToXContentObject { |
| 34 | + |
| 35 | + public static final ParseField DETECTION_RULE_FIELD = new ParseField("detection_rule"); |
| 36 | + public static final ParseField ACTIONS_FIELD = new ParseField("actions"); |
| 37 | + public static final ParseField SCOPE_FIELD = new ParseField("scope"); |
| 38 | + public static final ParseField CONDITIONS_FIELD = new ParseField("conditions"); |
| 39 | + |
| 40 | + public static final ObjectParser<Builder, Void> PARSER = |
| 41 | + new ObjectParser<>(DETECTION_RULE_FIELD.getPreferredName(), true, Builder::new);; |
| 42 | + |
| 43 | + static { |
| 44 | + PARSER.declareStringArray(Builder::setActions, ACTIONS_FIELD); |
| 45 | + PARSER.declareObject(Builder::setScope, RuleScope.parser(), SCOPE_FIELD); |
| 46 | + PARSER.declareObjectArray(Builder::setConditions, RuleCondition.PARSER, CONDITIONS_FIELD); |
| 47 | + } |
| 48 | + |
| 49 | + private final EnumSet<RuleAction> actions; |
| 50 | + private final RuleScope scope; |
| 51 | + private final List<RuleCondition> conditions; |
| 52 | + |
| 53 | + private DetectionRule(EnumSet<RuleAction> actions, RuleScope scope, List<RuleCondition> conditions) { |
| 54 | + this.actions = Objects.requireNonNull(actions); |
| 55 | + this.scope = Objects.requireNonNull(scope); |
| 56 | + this.conditions = Collections.unmodifiableList(conditions); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 61 | + builder.startObject(); |
| 62 | + builder.field(ACTIONS_FIELD.getPreferredName(), actions); |
| 63 | + if (scope.isEmpty() == false) { |
| 64 | + builder.field(SCOPE_FIELD.getPreferredName(), scope); |
| 65 | + } |
| 66 | + if (conditions.isEmpty() == false) { |
| 67 | + builder.field(CONDITIONS_FIELD.getPreferredName(), conditions); |
| 68 | + } |
| 69 | + builder.endObject(); |
| 70 | + return builder; |
| 71 | + } |
| 72 | + |
| 73 | + public EnumSet<RuleAction> getActions() { |
| 74 | + return actions; |
| 75 | + } |
| 76 | + |
| 77 | + public RuleScope getScope() { |
| 78 | + return scope; |
| 79 | + } |
| 80 | + |
| 81 | + public List<RuleCondition> getConditions() { |
| 82 | + return conditions; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean equals(Object obj) { |
| 87 | + if (this == obj) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + if (obj instanceof DetectionRule == false) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + DetectionRule other = (DetectionRule) obj; |
| 96 | + return Objects.equals(actions, other.actions) |
| 97 | + && Objects.equals(scope, other.scope) |
| 98 | + && Objects.equals(conditions, other.conditions); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public int hashCode() { |
| 103 | + return Objects.hash(actions, scope, conditions); |
| 104 | + } |
| 105 | + |
| 106 | + public static class Builder { |
| 107 | + private EnumSet<RuleAction> actions = EnumSet.of(RuleAction.SKIP_RESULT); |
| 108 | + private RuleScope scope = new RuleScope(); |
| 109 | + private List<RuleCondition> conditions = Collections.emptyList(); |
| 110 | + |
| 111 | + public Builder(RuleScope.Builder scope) { |
| 112 | + this.scope = scope.build(); |
| 113 | + } |
| 114 | + |
| 115 | + public Builder(List<RuleCondition> conditions) { |
| 116 | + this.conditions = Objects.requireNonNull(conditions); |
| 117 | + } |
| 118 | + |
| 119 | + Builder() { |
| 120 | + } |
| 121 | + |
| 122 | + public Builder setActions(List<String> actions) { |
| 123 | + this.actions.clear(); |
| 124 | + actions.stream().map(RuleAction::fromString).forEach(this.actions::add); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + public Builder setActions(EnumSet<RuleAction> actions) { |
| 129 | + this.actions = Objects.requireNonNull(actions, ACTIONS_FIELD.getPreferredName()); |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public Builder setActions(RuleAction... actions) { |
| 134 | + this.actions.clear(); |
| 135 | + Arrays.stream(actions).forEach(this.actions::add); |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + public Builder setScope(RuleScope scope) { |
| 140 | + this.scope = Objects.requireNonNull(scope); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public Builder setConditions(List<RuleCondition> conditions) { |
| 145 | + this.conditions = Objects.requireNonNull(conditions); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + public DetectionRule build() { |
| 150 | + return new DetectionRule(actions, scope, conditions); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments