2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 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.parameters.BeanValidationResult;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
45 * Class to represent the trigger of policy type in TOSCA definition.
47 * @author Chenfei Gao (cgao@research.att.com)
48 * @author Liam Fallon (liam.fallon@est.tech)
51 @Table(name = "ToscaTrigger")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
54 @EqualsAndHashCode(callSuper = false)
55 public class JpaToscaTrigger extends PfConcept {
56 private static final long serialVersionUID = -6515211640208986971L;
59 private PfReferenceKey key;
62 private String description;
65 @SerializedName("event_type")
66 private String eventType;
69 @SerializedName("schedule")
70 private JpaToscaTimeInterval schedule;
73 @SerializedName("target_filter")
74 private JpaToscaEventFilter targetFilter;
77 private JpaToscaConstraint condition;
80 private JpaToscaConstraint constraint;
83 @SerializedName("period")
84 private Duration period;
87 private int evaluations = 0;
90 private String method;
93 private String action;
96 * The Default Constructor creates a {@link JpaToscaTrigger} object with a null key.
98 public JpaToscaTrigger() {
99 this(new PfReferenceKey());
103 * The Key Constructor creates a {@link JpaToscaTrigger} object with the given concept key.
107 public JpaToscaTrigger(@NonNull final PfReferenceKey key) {
112 * The full Constructor creates a {@link JpaToscaTrigger} object with all mandatory objects.
115 * @param eventType the event type
116 * @param action the trigger action
118 public JpaToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType,
119 @NonNull final String action) {
121 this.eventType = eventType;
122 this.action = action;
128 * @param copyConcept the concept to copy from
130 public JpaToscaTrigger(final JpaToscaTrigger copyConcept) {
132 this.key = new PfReferenceKey(copyConcept.key);
133 this.description = copyConcept.description;
134 this.eventType = copyConcept.eventType;
135 this.schedule = (copyConcept.schedule != null ? new JpaToscaTimeInterval(copyConcept.schedule) : null);
137 (copyConcept.targetFilter != null ? new JpaToscaEventFilter(copyConcept.targetFilter) : null);
138 this.condition = copyConcept.condition;
139 this.constraint = copyConcept.constraint;
140 this.period = copyConcept.period;
141 this.evaluations = copyConcept.evaluations;
142 this.method = copyConcept.method;
143 this.action = copyConcept.action;
147 public List<PfKey> getKeys() {
148 final List<PfKey> keyList = getKey().getKeys();
149 if (schedule != null) {
150 keyList.addAll(schedule.getKeys());
152 if (targetFilter != null) {
153 keyList.addAll(targetFilter.getKeys());
159 public void clean() {
162 description = (description != null ? description.trim() : description);
163 eventType = eventType.trim();
165 if (schedule != null) {
168 if (targetFilter != null) {
169 targetFilter.clean();
172 method = (method != null ? method.trim() : method);
173 action = action.trim();
177 public BeanValidationResult validate(@NonNull String fieldName) {
178 BeanValidationResult result = new BeanValidationResult(fieldName, this);
180 result.addResult(validateKeyNotNull("key", key));
182 result.addResult(validateNotBlank("description", description, false));
183 result.addResult(validateNotBlank("eventType", eventType, true));
185 validateOptional(result, "schedule", schedule);
186 validateOptional(result, "targetFilter", targetFilter);
188 if (evaluations < 0) {
189 addResult(result, "evaluations", evaluations, "is negative");
192 result.addResult(validateNotBlank("method", method, false));
193 result.addResult(validateNotBlank("action", action, true));
199 public int compareTo(final PfConcept otherConcept) {
200 if (otherConcept == null) {
203 if (this == otherConcept) {
206 if (getClass() != otherConcept.getClass()) {
207 return getClass().getName().compareTo(otherConcept.getClass().getName());
210 final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
211 int result = key.compareTo(other.key);
216 return compareFields(other);
220 * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty object.
222 * @param other the other ToscaTrigger object
224 private int compareFields(final JpaToscaTrigger other) {
225 int result = ObjectUtils.compare(description, other.description);
230 result = ObjectUtils.compare(eventType, other.eventType);
235 result = ObjectUtils.compare(schedule, other.schedule);
240 result = ObjectUtils.compare(targetFilter, other.targetFilter);
245 result = ObjectUtils.compare(condition, other.condition);
250 result = ObjectUtils.compare(constraint, other.constraint);
255 result = ObjectUtils.compare(period, other.period);
260 if (evaluations != other.evaluations) {
261 return evaluations - other.evaluations;
264 result = ObjectUtils.compare(method, other.method);
269 return ObjectUtils.compare(action, other.action);