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;
28 import java.time.Duration;
29 import java.util.List;
31 import javax.persistence.Column;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Table;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
42 import org.apache.commons.lang3.ObjectUtils;
43 import org.onap.policy.common.utils.validation.Assertions;
44 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfReferenceKey;
48 import org.onap.policy.models.base.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
53 * Class to represent the trigger of policy type in TOSCA definition.
55 * @author Chenfei Gao (cgao@research.att.com)
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaTrigger")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaToscaTrigger extends PfConcept {
64 private static final long serialVersionUID = -6515211640208986971L;
67 private PfReferenceKey key;
70 private String description;
73 @SerializedName("event_type")
74 private String eventType;
77 @SerializedName("schedule")
78 private JpaToscaTimeInterval schedule;
81 @SerializedName("target_filter")
82 private JpaToscaEventFilter targetFilter;
85 private JpaToscaConstraint condition;
88 private JpaToscaConstraint constraint;
91 @SerializedName("period")
92 private Duration period;
95 private int evaluations = 0;
98 private String method;
101 private String action;
104 * The Default Constructor creates a {@link JpaToscaTrigger} object with a null key.
106 public JpaToscaTrigger() {
107 this(new PfReferenceKey());
111 * The Key Constructor creates a {@link JpaToscaTrigger} object with the given concept key.
115 public JpaToscaTrigger(@NonNull final PfReferenceKey key) {
120 * The full Constructor creates a {@link JpaToscaTrigger} object with all mandatory objects.
123 * @param eventType the event type
124 * @param action the trigger action
126 public JpaToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType,
127 @NonNull final String action) {
129 this.eventType = eventType;
130 this.action = action;
136 * @param copyConcept the concept to copy from
138 public JpaToscaTrigger(final JpaToscaTrigger copyConcept) {
143 public List<PfKey> getKeys() {
144 final List<PfKey> keyList = getKey().getKeys();
145 if (schedule != null) {
146 keyList.addAll(schedule.getKeys());
148 if (targetFilter != null) {
149 keyList.addAll(targetFilter.getKeys());
155 public void clean() {
158 description = (description != null ? description.trim() : description);
159 eventType = eventType.trim();
161 if (schedule != null) {
164 if (targetFilter != null) {
165 targetFilter.clean();
168 method = (method != null ? method.trim() : method);
169 action = action.trim();
173 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
174 PfValidationResult result = resultIn;
176 if (key.isNullKey()) {
177 result.addValidationMessage(
178 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
181 result = key.validate(result);
183 if (description != null && description.trim().length() == 0) {
184 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
185 "trigger description may not be blank"));
188 if (!ParameterValidationUtils.validateStringParameter(eventType)) {
189 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
190 "event type on trigger must be defined"));
193 result = validateOptionalFields(result);
195 if (evaluations < 0) {
196 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
197 "evaluations on trigger must be zero or a positive integer"));
200 if (method != null && method.trim().length() == 0) {
201 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
202 "method on trigger may not be blank"));
205 if (!ParameterValidationUtils.validateStringParameter(action)) {
206 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
207 "action on trigger must be defined"));
214 * Validate optional fields.
216 * @param resultIn the validation result so far
217 * @return the validation resutls including these fields
219 private PfValidationResult validateOptionalFields(final PfValidationResult resultIn) {
220 PfValidationResult result = resultIn;
222 result = (schedule != null ? schedule.validate(result) : result);
223 result = (targetFilter != null ? targetFilter.validate(result) : result);
229 public int compareTo(final PfConcept otherConcept) {
230 if (otherConcept == null) {
233 if (this == otherConcept) {
236 if (getClass() != otherConcept.getClass()) {
237 return this.hashCode() - otherConcept.hashCode();
240 final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
241 if (!key.equals(other.key)) {
242 return key.compareTo(other.key);
245 return compareFields(other);
249 * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty
252 * @param other the other ToscaTrigger object
254 private int compareFields(final JpaToscaTrigger other) {
255 int result = ObjectUtils.compare(description, other.description);
260 result = ObjectUtils.compare(eventType, other.eventType);
265 result = ObjectUtils.compare(schedule, other.schedule);
270 result = ObjectUtils.compare(targetFilter, other.targetFilter);
275 result = ObjectUtils.compare(condition, other.condition);
280 result = ObjectUtils.compare(constraint, other.constraint);
285 result = ObjectUtils.compare(period, other.period);
290 if (evaluations != other.evaluations) {
291 return evaluations - other.evaluations;
294 result = ObjectUtils.compare(method, other.method);
299 return ObjectUtils.compare(action, other.action);
303 public PfConcept copyTo(@NonNull final PfConcept target) {
304 Assertions.instanceOf(target, JpaToscaTrigger.class);
306 final JpaToscaTrigger copy = ((JpaToscaTrigger) target);
307 copy.setKey(new PfReferenceKey(key));
308 copy.setDescription(description);
309 copy.setEventType(eventType);
310 copy.setSchedule(schedule != null ? new JpaToscaTimeInterval(schedule) : null);
311 copy.setTargetFilter(targetFilter != null ? new JpaToscaEventFilter(targetFilter) : null);
312 copy.setCondition(condition);
313 copy.setConstraint(constraint);
314 copy.setPeriod(period);
315 copy.setEvaluations(evaluations);
316 copy.setMethod(method);
317 copy.setAction(action);