Add support for legacy guard policies
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTrigger.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import com.google.gson.annotations.SerializedName;
27
28 import java.time.Duration;
29 import java.util.List;
30
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;
37
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41
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;
51
52 /**
53  * Class to represent the trigger of policy type in TOSCA definition.
54  *
55  * @author Chenfei Gao (cgao@research.att.com)
56  * @author Liam Fallon (liam.fallon@est.tech)
57  */
58 @Entity
59 @Table(name = "ToscaTrigger")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @Data
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaToscaTrigger extends PfConcept {
64     private static final long serialVersionUID = -6515211640208986971L;
65
66     @EmbeddedId
67     private PfReferenceKey key;
68
69     @Column
70     private String description;
71
72     @Column
73     @SerializedName("event_type")
74     private String eventType;
75
76     @Column
77     @SerializedName("schedule")
78     private JpaToscaTimeInterval schedule;
79
80     @Column
81     @SerializedName("target_filter")
82     private JpaToscaEventFilter targetFilter;
83
84     @Column
85     private JpaToscaConstraint condition;
86
87     @Column
88     private JpaToscaConstraint constraint;
89
90     @Column
91     @SerializedName("period")
92     private Duration period;
93
94     @Column
95     private int evaluations = 0;
96
97     @Column
98     private String method;
99
100     @Column
101     private String action;
102
103     /**
104      * The Default Constructor creates a {@link JpaToscaTrigger} object with a null key.
105      */
106     public JpaToscaTrigger() {
107         this(new PfReferenceKey());
108     }
109
110     /**
111      * The Key Constructor creates a {@link JpaToscaTrigger} object with the given concept key.
112      *
113      * @param key the key
114      */
115     public JpaToscaTrigger(@NonNull final PfReferenceKey key) {
116         this(key, "", "");
117     }
118
119     /**
120      * The full Constructor creates a {@link JpaToscaTrigger} object with all mandatory objects.
121      *
122      * @param key the key
123      * @param eventType the event type
124      * @param action the trigger action
125      */
126     public JpaToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType,
127             @NonNull final String action) {
128         this.key = key;
129         this.eventType = eventType;
130         this.action = action;
131     }
132
133     /**
134      * Copy constructor.
135      *
136      * @param copyConcept the concept to copy from
137      */
138     public JpaToscaTrigger(final JpaToscaTrigger copyConcept) {
139         super(copyConcept);
140     }
141
142     @Override
143     public List<PfKey> getKeys() {
144         final List<PfKey> keyList = getKey().getKeys();
145         if (schedule != null) {
146             keyList.addAll(schedule.getKeys());
147         }
148         if (targetFilter != null) {
149             keyList.addAll(targetFilter.getKeys());
150         }
151         if (condition != null) {
152             keyList.addAll(condition.getKeys());
153         }
154         if (constraint != null) {
155             keyList.addAll(constraint.getKeys());
156         }
157         return keyList;
158     }
159
160     @Override
161     public void clean() {
162         key.clean();
163
164         description = (description != null ? description.trim() : description);
165         eventType = eventType.trim();
166
167         if (schedule != null) {
168             schedule.clean();
169         }
170         if (targetFilter != null) {
171             targetFilter.clean();
172         }
173         if (condition != null) {
174             condition.clean();
175         }
176         if (constraint != null) {
177             constraint.clean();
178         }
179
180         method = (method != null ? method.trim() : method);
181         action = action.trim();
182     }
183
184     @Override
185     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
186         PfValidationResult result = resultIn;
187
188         if (key.isNullKey()) {
189             result.addValidationMessage(
190                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
191         }
192
193         result = key.validate(result);
194
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"));
198         }
199
200         if (!ParameterValidationUtils.validateStringParameter(eventType)) {
201             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
202                     "event type on trigger must be defined"));
203         }
204
205         result = validateOptionalFields(result);
206
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"));
210         }
211
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"));
215         }
216
217         if (!ParameterValidationUtils.validateStringParameter(action)) {
218             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
219                     "action on trigger must be defined"));
220         }
221
222         return result;
223     }
224
225     /**
226      * Validate optional fields.
227      *
228      * @param resultIn the validation result so far
229      * @return the validation resutls including these fields
230      */
231     private PfValidationResult validateOptionalFields(final PfValidationResult resultIn) {
232         PfValidationResult result = resultIn;
233
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);
238
239         return result;
240     }
241
242     @Override
243     public int compareTo(final PfConcept otherConcept) {
244         if (otherConcept == null) {
245             return -1;
246         }
247         if (this == otherConcept) {
248             return 0;
249         }
250         if (getClass() != otherConcept.getClass()) {
251             return this.hashCode() - otherConcept.hashCode();
252         }
253
254         final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
255         if (!key.equals(other.key)) {
256             return key.compareTo(other.key);
257         }
258
259         return compareFields(other);
260     }
261
262     /**
263      * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty
264      * object.
265      *
266      * @param other the other ToscaTrigger object
267      */
268     private int compareFields(final JpaToscaTrigger other) {
269         int result = ObjectUtils.compare(description, other.description);
270         if (result != 0) {
271             return result;
272         }
273
274         result = ObjectUtils.compare(eventType, other.eventType);
275         if (result != 0) {
276             return result;
277         }
278
279         result = ObjectUtils.compare(schedule, other.schedule);
280         if (result != 0) {
281             return result;
282         }
283
284         result = ObjectUtils.compare(targetFilter, other.targetFilter);
285         if (result != 0) {
286             return result;
287         }
288
289         result = ObjectUtils.compare(condition, other.condition);
290         if (result != 0) {
291             return result;
292         }
293
294         result = ObjectUtils.compare(constraint, other.constraint);
295         if (result != 0) {
296             return result;
297         }
298
299         result = ObjectUtils.compare(period, other.period);
300         if (result != 0) {
301             return result;
302         }
303
304         if (evaluations != other.evaluations) {
305             return evaluations - other.evaluations;
306         }
307
308         result = ObjectUtils.compare(method, other.method);
309         if (result != 0) {
310             return result;
311         }
312
313         return ObjectUtils.compare(action, other.action);
314     }
315
316     @Override
317     public PfConcept copyTo(@NonNull final PfConcept target) {
318         Assertions.instanceOf(target, JpaToscaTrigger.class);
319
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);
332
333         return copy;
334     }
335 }