Refactor to authorative TOSCA serializtion
[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         return keyList;
152     }
153
154     @Override
155     public void clean() {
156         key.clean();
157
158         description = (description != null ? description.trim() : description);
159         eventType = eventType.trim();
160
161         if (schedule != null) {
162             schedule.clean();
163         }
164         if (targetFilter != null) {
165             targetFilter.clean();
166         }
167
168         method = (method != null ? method.trim() : method);
169         action = action.trim();
170     }
171
172     @Override
173     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
174         PfValidationResult result = resultIn;
175
176         if (key.isNullKey()) {
177             result.addValidationMessage(
178                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
179         }
180
181         result = key.validate(result);
182
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"));
186         }
187
188         if (!ParameterValidationUtils.validateStringParameter(eventType)) {
189             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
190                     "event type on trigger must be defined"));
191         }
192
193         result = validateOptionalFields(result);
194
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"));
198         }
199
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"));
203         }
204
205         if (!ParameterValidationUtils.validateStringParameter(action)) {
206             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
207                     "action on trigger must be defined"));
208         }
209
210         return result;
211     }
212
213     /**
214      * Validate optional fields.
215      *
216      * @param resultIn the validation result so far
217      * @return the validation resutls including these fields
218      */
219     private PfValidationResult validateOptionalFields(final PfValidationResult resultIn) {
220         PfValidationResult result = resultIn;
221
222         result = (schedule != null ? schedule.validate(result) : result);
223         result = (targetFilter != null ? targetFilter.validate(result) : result);
224
225         return result;
226     }
227
228     @Override
229     public int compareTo(final PfConcept otherConcept) {
230         if (otherConcept == null) {
231             return -1;
232         }
233         if (this == otherConcept) {
234             return 0;
235         }
236         if (getClass() != otherConcept.getClass()) {
237             return this.hashCode() - otherConcept.hashCode();
238         }
239
240         final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
241         if (!key.equals(other.key)) {
242             return key.compareTo(other.key);
243         }
244
245         return compareFields(other);
246     }
247
248     /**
249      * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty
250      * object.
251      *
252      * @param other the other ToscaTrigger object
253      */
254     private int compareFields(final JpaToscaTrigger other) {
255         int result = ObjectUtils.compare(description, other.description);
256         if (result != 0) {
257             return result;
258         }
259
260         result = ObjectUtils.compare(eventType, other.eventType);
261         if (result != 0) {
262             return result;
263         }
264
265         result = ObjectUtils.compare(schedule, other.schedule);
266         if (result != 0) {
267             return result;
268         }
269
270         result = ObjectUtils.compare(targetFilter, other.targetFilter);
271         if (result != 0) {
272             return result;
273         }
274
275         result = ObjectUtils.compare(condition, other.condition);
276         if (result != 0) {
277             return result;
278         }
279
280         result = ObjectUtils.compare(constraint, other.constraint);
281         if (result != 0) {
282             return result;
283         }
284
285         result = ObjectUtils.compare(period, other.period);
286         if (result != 0) {
287             return result;
288         }
289
290         if (evaluations != other.evaluations) {
291             return evaluations - other.evaluations;
292         }
293
294         result = ObjectUtils.compare(method, other.method);
295         if (result != 0) {
296             return result;
297         }
298
299         return ObjectUtils.compare(action, other.action);
300     }
301
302     @Override
303     public PfConcept copyTo(@NonNull final PfConcept target) {
304         Assertions.instanceOf(target, JpaToscaTrigger.class);
305
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);
318
319         return copy;
320     }
321 }