417e83fe0200e5b6f8e5f297e1aa81666ce3c040
[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 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;
35 import lombok.Data;
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;
46
47 /**
48  * Class to represent the trigger of policy type in TOSCA definition.
49  *
50  * @author Chenfei Gao (cgao@research.att.com)
51  * @author Liam Fallon (liam.fallon@est.tech)
52  */
53 @Entity
54 @Table(name = "ToscaTrigger")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @Data
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaToscaTrigger extends PfConcept {
59     private static final long serialVersionUID = -6515211640208986971L;
60
61     @EmbeddedId
62     private PfReferenceKey key;
63
64     @Column
65     private String description;
66
67     @Column
68     @SerializedName("event_type")
69     private String eventType;
70
71     @Column
72     @SerializedName("schedule")
73     private JpaToscaTimeInterval schedule;
74
75     @Column
76     @SerializedName("target_filter")
77     private JpaToscaEventFilter targetFilter;
78
79     @Column
80     private JpaToscaConstraint condition;
81
82     @Column
83     private JpaToscaConstraint constraint;
84
85     @Column
86     @SerializedName("period")
87     private Duration period;
88
89     @Column
90     private int evaluations = 0;
91
92     @Column
93     private String method;
94
95     @Column
96     private String action;
97
98     /**
99      * The Default Constructor creates a {@link JpaToscaTrigger} object with a null key.
100      */
101     public JpaToscaTrigger() {
102         this(new PfReferenceKey());
103     }
104
105     /**
106      * The Key Constructor creates a {@link JpaToscaTrigger} object with the given concept key.
107      *
108      * @param key the key
109      */
110     public JpaToscaTrigger(@NonNull final PfReferenceKey key) {
111         this(key, "", "");
112     }
113
114     /**
115      * The full Constructor creates a {@link JpaToscaTrigger} object with all mandatory objects.
116      *
117      * @param key the key
118      * @param eventType the event type
119      * @param action the trigger action
120      */
121     public JpaToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType,
122             @NonNull final String action) {
123         this.key = key;
124         this.eventType = eventType;
125         this.action = action;
126     }
127
128     /**
129      * Copy constructor.
130      *
131      * @param copyConcept the concept to copy from
132      */
133     public JpaToscaTrigger(final JpaToscaTrigger copyConcept) {
134         super(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);
139         this.targetFilter =
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;
147     }
148
149     @Override
150     public List<PfKey> getKeys() {
151         final List<PfKey> keyList = getKey().getKeys();
152         if (schedule != null) {
153             keyList.addAll(schedule.getKeys());
154         }
155         if (targetFilter != null) {
156             keyList.addAll(targetFilter.getKeys());
157         }
158         return keyList;
159     }
160
161     @Override
162     public void clean() {
163         key.clean();
164
165         description = (description != null ? description.trim() : description);
166         eventType = eventType.trim();
167
168         if (schedule != null) {
169             schedule.clean();
170         }
171         if (targetFilter != null) {
172             targetFilter.clean();
173         }
174
175         method = (method != null ? method.trim() : method);
176         action = action.trim();
177     }
178
179     @Override
180     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
181         PfValidationResult result = resultIn;
182
183         if (key.isNullKey()) {
184             result.addValidationMessage(
185                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
186         }
187
188         result = key.validate(result);
189
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"));
193         }
194
195         if (!ParameterValidationUtils.validateStringParameter(eventType)) {
196             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
197                     "event type on trigger must be defined"));
198         }
199
200         result = validateOptionalFields(result);
201
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"));
205         }
206
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"));
210         }
211
212         if (!ParameterValidationUtils.validateStringParameter(action)) {
213             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214                     "action on trigger must be defined"));
215         }
216
217         return result;
218     }
219
220     /**
221      * Validate optional fields.
222      *
223      * @param resultIn the validation result so far
224      * @return the validation resutls including these fields
225      */
226     private PfValidationResult validateOptionalFields(final PfValidationResult resultIn) {
227         PfValidationResult result = resultIn;
228
229         result = (schedule != null ? schedule.validate(result) : result);
230         result = (targetFilter != null ? targetFilter.validate(result) : result);
231
232         return result;
233     }
234
235     @Override
236     public int compareTo(final PfConcept otherConcept) {
237         if (otherConcept == null) {
238             return -1;
239         }
240         if (this == otherConcept) {
241             return 0;
242         }
243         if (getClass() != otherConcept.getClass()) {
244             return getClass().getName().compareTo(otherConcept.getClass().getName());
245         }
246
247         final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
248         if (!key.equals(other.key)) {
249             return key.compareTo(other.key);
250         }
251
252         return compareFields(other);
253     }
254
255     /**
256      * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty
257      * object.
258      *
259      * @param other the other ToscaTrigger object
260      */
261     private int compareFields(final JpaToscaTrigger other) {
262         int result = ObjectUtils.compare(description, other.description);
263         if (result != 0) {
264             return result;
265         }
266
267         result = ObjectUtils.compare(eventType, other.eventType);
268         if (result != 0) {
269             return result;
270         }
271
272         result = ObjectUtils.compare(schedule, other.schedule);
273         if (result != 0) {
274             return result;
275         }
276
277         result = ObjectUtils.compare(targetFilter, other.targetFilter);
278         if (result != 0) {
279             return result;
280         }
281
282         result = ObjectUtils.compare(condition, other.condition);
283         if (result != 0) {
284             return result;
285         }
286
287         result = ObjectUtils.compare(constraint, other.constraint);
288         if (result != 0) {
289             return result;
290         }
291
292         result = ObjectUtils.compare(period, other.period);
293         if (result != 0) {
294             return result;
295         }
296
297         if (evaluations != other.evaluations) {
298             return evaluations - other.evaluations;
299         }
300
301         result = ObjectUtils.compare(method, other.method);
302         if (result != 0) {
303             return result;
304         }
305
306         return ObjectUtils.compare(action, other.action);
307     }
308 }