Safe Template Condition Object
This object is used by training and prediction
templates for specifying the training conditions.
Change-Id: I31c9f2c6426b6c05798ca9619ad8fa455e5fd746
diff --git a/apps/aiplugin/api/src/main/java/org/onosproject/aiplugin/Condition.java b/apps/aiplugin/api/src/main/java/org/onosproject/aiplugin/Condition.java
new file mode 100644
index 0000000..e734e1f
--- /dev/null
+++ b/apps/aiplugin/api/src/main/java/org/onosproject/aiplugin/Condition.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2024-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package aiplugin.api;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+
+/**
+ * Immutable class representing a condition with name, field, match, value, action, and aggregation.
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+@JsonDeserialize(builder = Condition.Builder.class)
+public final class Condition {
+ private String name;
+ private String field;
+ private String match;
+ private String value;
+ private String action;
+ private String aggregation;
+
+ /**
+ * Private constructor for the Condition class.
+ *
+ * @param builder The Builder object providing field values.
+ */
+ private Condition(Builder builder) {
+ this.name = builder.name;
+ this.field = builder.field;
+ this.match = builder.match;
+ this.value = builder.value;
+ this.action = builder.action;
+ this.aggregation = builder.aggregation;
+ }
+
+ /**
+ * Returns the name of the condition.
+ *
+ * @return The name as a string.
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Returns the field used in the condition.
+ *
+ * @return The field as a string.
+ */
+ public String getField() {
+ return field;
+ }
+
+ /**
+ * Returns the match operator used in the condition.
+ *
+ * @return The match operator as a string.
+ */
+ public String getMatch() {
+ return match;
+ }
+
+ /**
+ * Returns the value used in the condition.
+ *
+ * @return The value as a string.
+ */
+ public String getValue() {
+ return value;
+ }
+
+ /**
+ * Returns the action performed by the condition.
+ *
+ * @return The action as a string.
+ */
+ public String getAction() {
+ return action;
+ }
+
+ /**
+ * Returns the aggregation performed by the condition.
+ *
+ * @return The aggregation as a string.
+ */
+ public String getAggregation() {
+ return aggregation;
+ }
+
+ /**
+ * Static method to create a new Builder instance.
+ *
+ * @return A new Builder for creating a Condition instance.
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Builder class for Condition.
+ */
+ @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build")
+ @JsonIgnoreProperties(ignoreUnknown = true)
+ public static class Builder {
+ private String name;
+ private String field;
+ private String match;
+ private String value;
+ private String action;
+ private String aggregation;
+
+ /**
+ * Sets the name of the condition.
+ *
+ * @param name The name as a string.
+ * @return The Builder instance.
+ */
+ public Builder name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ /**
+ * Sets the field used in the condition.
+ *
+ * @param field The field as a string.
+ * @return The Builder instance.
+ */
+ public Builder field(String field) {
+ this.field = field;
+ return this;
+ }
+
+ /**
+ * Sets the match operator used in the condition.
+ *
+ * @param match The match operator as a string.
+ * @return The Builder instance.
+ */
+ public Builder match(String match) {
+ this.match = match;
+ return this;
+ }
+
+ /**
+ * Sets the value used in the condition.
+ *
+ * @param value The value as a string.
+ * @return The Builder instance.
+ */
+ public Builder value(String value) {
+ this.value = value;
+ return this;
+ }
+
+ /**
+ * Sets the action performed by the condition.
+ *
+ * @param action The action as a string.
+ * @return The Builder instance.
+ */
+ public Builder action(String action) {
+ this.action = action;
+ return this;
+ }
+
+ /**
+ * Sets the aggregation performed by the condition.
+ *
+ * @param aggregation The aggregation as a string.
+ * @return The Builder instance.
+ */
+ public Builder aggregation(String aggregation) {
+ this.aggregation = aggregation;
+ return this;
+ }
+
+ /**
+ * Builds the Condition instance.
+ * Before creating the Condition, it checks that name, field, match, and value are not null.
+ *
+ * @return A new Condition instance.
+ */
+ public Condition build() {
+ Preconditions.checkNotNull(name, "Name cannot be null");
+ Preconditions.checkNotNull(field, "Field cannot be null");
+ Preconditions.checkNotNull(match, "Match cannot be null");
+ Preconditions.checkNotNull(value, "Value cannot be null");
+ return new Condition(this);
+ }
+ }
+
+ /**
+ * Generates a string representation of the Condition.
+ *
+ * @return A string representation of the Condition.
+ */
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("name", name)
+ .add("field", field)
+ .add("match", match)
+ .add("value", value)
+ .add("action", action)
+ .add("aggregation", aggregation)
+ .toString();
+ }
+}