2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.simple.concepts;
26 import com.google.gson.annotations.SerializedName;
27 import java.time.Duration;
28 import java.util.List;
29 import javax.persistence.Column;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.base.PfValidationMessage;
44 import org.onap.policy.models.base.PfValidationResult;
45 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
48 * Class to represent the trigger of policy type in TOSCA definition.
50 * @author Chenfei Gao (cgao@research.att.com)
51 * @author Liam Fallon (liam.fallon@est.tech)
54 @Table(name = "ToscaTrigger")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaToscaTrigger extends PfConcept {
59 private static final long serialVersionUID = -6515211640208986971L;
62 private PfReferenceKey key;
65 private String description;
68 @SerializedName("event_type")
69 private String eventType;
72 @SerializedName("schedule")
73 private JpaToscaTimeInterval schedule;
76 @SerializedName("target_filter")
77 private JpaToscaEventFilter targetFilter;
80 private JpaToscaConstraint condition;
83 private JpaToscaConstraint constraint;
86 @SerializedName("period")
87 private Duration period;
90 private int evaluations = 0;
93 private String method;
96 private String action;
99 * The Default Constructor creates a {@link JpaToscaTrigger} object with a null key.
101 public JpaToscaTrigger() {
102 this(new PfReferenceKey());
106 * The Key Constructor creates a {@link JpaToscaTrigger} object with the given concept key.
110 public JpaToscaTrigger(@NonNull final PfReferenceKey key) {
115 * The full Constructor creates a {@link JpaToscaTrigger} object with all mandatory objects.
118 * @param eventType the event type
119 * @param action the trigger action
121 public JpaToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType,
122 @NonNull final String action) {
124 this.eventType = eventType;
125 this.action = action;
131 * @param copyConcept the concept to copy from
133 public JpaToscaTrigger(final JpaToscaTrigger copyConcept) {
135 this.key = new PfReferenceKey(copyConcept.key);
136 this.description = copyConcept.description;
137 this.eventType = copyConcept.eventType;
138 this.schedule = (copyConcept.schedule != null ? new JpaToscaTimeInterval(copyConcept.schedule) : null);
140 (copyConcept.targetFilter != null ? new JpaToscaEventFilter(copyConcept.targetFilter) : null);
141 this.condition = copyConcept.condition;
142 this.constraint = copyConcept.constraint;
143 this.period = copyConcept.period;
144 this.evaluations = copyConcept.evaluations;
145 this.method = copyConcept.method;
146 this.action = copyConcept.action;
150 public List<PfKey> getKeys() {
151 final List<PfKey> keyList = getKey().getKeys();
152 if (schedule != null) {
153 keyList.addAll(schedule.getKeys());
155 if (targetFilter != null) {
156 keyList.addAll(targetFilter.getKeys());
162 public void clean() {
165 description = (description != null ? description.trim() : description);
166 eventType = eventType.trim();
168 if (schedule != null) {
171 if (targetFilter != null) {
172 targetFilter.clean();
175 method = (method != null ? method.trim() : method);
176 action = action.trim();
180 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
181 PfValidationResult result = resultIn;
183 if (key.isNullKey()) {
184 result.addValidationMessage(
185 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
188 result = key.validate(result);
190 if (description != null && description.trim().length() == 0) {
191 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
192 "trigger description may not be blank"));
195 if (!ParameterValidationUtils.validateStringParameter(eventType)) {
196 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
197 "event type on trigger must be defined"));
200 result = validateOptionalFields(result);
202 if (evaluations < 0) {
203 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
204 "evaluations on trigger must be zero or a positive integer"));
207 if (method != null && method.trim().length() == 0) {
208 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
209 "method on trigger may not be blank"));
212 if (!ParameterValidationUtils.validateStringParameter(action)) {
213 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214 "action on trigger must be defined"));
221 * Validate optional fields.
223 * @param resultIn the validation result so far
224 * @return the validation resutls including these fields
226 private PfValidationResult validateOptionalFields(final PfValidationResult resultIn) {
227 PfValidationResult result = resultIn;
229 result = (schedule != null ? schedule.validate(result) : result);
230 result = (targetFilter != null ? targetFilter.validate(result) : result);
236 public int compareTo(final PfConcept otherConcept) {
237 if (otherConcept == null) {
240 if (this == otherConcept) {
243 if (getClass() != otherConcept.getClass()) {
244 return getClass().getName().compareTo(otherConcept.getClass().getName());
247 final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
248 if (!key.equals(other.key)) {
249 return key.compareTo(other.key);
252 return compareFields(other);
256 * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty
259 * @param other the other ToscaTrigger object
261 private int compareFields(final JpaToscaTrigger other) {
262 int result = ObjectUtils.compare(description, other.description);
267 result = ObjectUtils.compare(eventType, other.eventType);
272 result = ObjectUtils.compare(schedule, other.schedule);
277 result = ObjectUtils.compare(targetFilter, other.targetFilter);
282 result = ObjectUtils.compare(condition, other.condition);
287 result = ObjectUtils.compare(constraint, other.constraint);
292 result = ObjectUtils.compare(period, other.period);
297 if (evaluations != other.evaluations) {
298 return evaluations - other.evaluations;
301 result = ObjectUtils.compare(method, other.method);
306 return ObjectUtils.compare(action, other.action);