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());
151 if (condition != null) {
152 keyList.addAll(condition.getKeys());
154 if (constraint != null) {
155 keyList.addAll(constraint.getKeys());
161 public void clean() {
164 description = (description != null ? description.trim() : description);
165 eventType = eventType.trim();
167 if (schedule != null) {
170 if (targetFilter != null) {
171 targetFilter.clean();
173 if (condition != null) {
176 if (constraint != null) {
180 method = (method != null ? method.trim() : method);
181 action = action.trim();
185 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
186 PfValidationResult result = resultIn;
188 if (key.isNullKey()) {
189 result.addValidationMessage(
190 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
193 result = key.validate(result);
195 if (description != null && description.trim().length() == 0) {
196 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
197 "trigger description may not be blank"));
200 if (!ParameterValidationUtils.validateStringParameter(eventType)) {
201 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
202 "event type on trigger must be defined"));
205 result = validateOptionalFields(result);
207 if (evaluations < 0) {
208 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
209 "evaluations on trigger must be zero or a positive integer"));
212 if (method != null && method.trim().length() == 0) {
213 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214 "method on trigger may not be blank"));
217 if (!ParameterValidationUtils.validateStringParameter(action)) {
218 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
219 "action on trigger must be defined"));
226 * Validate optional fields.
228 * @param resultIn the validation result so far
229 * @return the validation resutls including these fields
231 private PfValidationResult validateOptionalFields(final PfValidationResult resultIn) {
232 PfValidationResult result = resultIn;
234 result = (schedule != null ? schedule.validate(result) : result);
235 result = (targetFilter != null ? targetFilter.validate(result) : result);
236 result = (condition != null ? condition.validate(result) : result);
237 result = (constraint != null ? constraint.validate(result) : result);
243 public int compareTo(final PfConcept otherConcept) {
244 if (otherConcept == null) {
247 if (this == otherConcept) {
250 if (getClass() != otherConcept.getClass()) {
251 return this.hashCode() - otherConcept.hashCode();
254 final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
255 if (!key.equals(other.key)) {
256 return key.compareTo(other.key);
259 return compareFields(other);
263 * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty
266 * @param other the other ToscaTrigger object
268 private int compareFields(final JpaToscaTrigger other) {
269 int result = ObjectUtils.compare(description, other.description);
274 result = ObjectUtils.compare(eventType, other.eventType);
279 result = ObjectUtils.compare(schedule, other.schedule);
284 result = ObjectUtils.compare(targetFilter, other.targetFilter);
289 result = ObjectUtils.compare(condition, other.condition);
294 result = ObjectUtils.compare(constraint, other.constraint);
299 result = ObjectUtils.compare(period, other.period);
304 if (evaluations != other.evaluations) {
305 return evaluations - other.evaluations;
308 result = ObjectUtils.compare(method, other.method);
313 return ObjectUtils.compare(action, other.action);
317 public PfConcept copyTo(@NonNull final PfConcept target) {
318 Assertions.instanceOf(target, JpaToscaTrigger.class);
320 final JpaToscaTrigger copy = ((JpaToscaTrigger) target);
321 copy.setKey(new PfReferenceKey(key));
322 copy.setDescription(description);
323 copy.setEventType(eventType);
324 copy.setSchedule(schedule != null ? new JpaToscaTimeInterval(schedule) : null);
325 copy.setTargetFilter(targetFilter != null ? new JpaToscaEventFilter(targetFilter) : null);
326 copy.setCondition(condition);
327 copy.setConstraint(constraint);
328 copy.setPeriod(period);
329 copy.setEvaluations(evaluations);
330 copy.setMethod(method);
331 copy.setAction(action);