Use ValidationResult for models v2.0
[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-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
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.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;
43
44 /**
45  * Class to represent the trigger of policy type in TOSCA definition.
46  *
47  * @author Chenfei Gao (cgao@research.att.com)
48  * @author Liam Fallon (liam.fallon@est.tech)
49  */
50 @Entity
51 @Table(name = "ToscaTrigger")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
53 @Data
54 @EqualsAndHashCode(callSuper = false)
55 public class JpaToscaTrigger extends PfConcept {
56     private static final long serialVersionUID = -6515211640208986971L;
57
58     @EmbeddedId
59     private PfReferenceKey key;
60
61     @Column
62     private String description;
63
64     @Column
65     @SerializedName("event_type")
66     private String eventType;
67
68     @Column
69     @SerializedName("schedule")
70     private JpaToscaTimeInterval schedule;
71
72     @Column
73     @SerializedName("target_filter")
74     private JpaToscaEventFilter targetFilter;
75
76     @Column
77     private JpaToscaConstraint condition;
78
79     @Column
80     private JpaToscaConstraint constraint;
81
82     @Column
83     @SerializedName("period")
84     private Duration period;
85
86     @Column
87     private int evaluations = 0;
88
89     @Column
90     private String method;
91
92     @Column
93     private String action;
94
95     /**
96      * The Default Constructor creates a {@link JpaToscaTrigger} object with a null key.
97      */
98     public JpaToscaTrigger() {
99         this(new PfReferenceKey());
100     }
101
102     /**
103      * The Key Constructor creates a {@link JpaToscaTrigger} object with the given concept key.
104      *
105      * @param key the key
106      */
107     public JpaToscaTrigger(@NonNull final PfReferenceKey key) {
108         this(key, "", "");
109     }
110
111     /**
112      * The full Constructor creates a {@link JpaToscaTrigger} object with all mandatory objects.
113      *
114      * @param key the key
115      * @param eventType the event type
116      * @param action the trigger action
117      */
118     public JpaToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType,
119             @NonNull final String action) {
120         this.key = key;
121         this.eventType = eventType;
122         this.action = action;
123     }
124
125     /**
126      * Copy constructor.
127      *
128      * @param copyConcept the concept to copy from
129      */
130     public JpaToscaTrigger(final JpaToscaTrigger copyConcept) {
131         super(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);
136         this.targetFilter =
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;
144     }
145
146     @Override
147     public List<PfKey> getKeys() {
148         final List<PfKey> keyList = getKey().getKeys();
149         if (schedule != null) {
150             keyList.addAll(schedule.getKeys());
151         }
152         if (targetFilter != null) {
153             keyList.addAll(targetFilter.getKeys());
154         }
155         return keyList;
156     }
157
158     @Override
159     public void clean() {
160         key.clean();
161
162         description = (description != null ? description.trim() : description);
163         eventType = eventType.trim();
164
165         if (schedule != null) {
166             schedule.clean();
167         }
168         if (targetFilter != null) {
169             targetFilter.clean();
170         }
171
172         method = (method != null ? method.trim() : method);
173         action = action.trim();
174     }
175
176     @Override
177     public BeanValidationResult validate(@NonNull String fieldName) {
178         BeanValidationResult result = new BeanValidationResult(fieldName, this);
179
180         result.addResult(validateKeyNotNull("key", key));
181
182         result.addResult(validateNotBlank("description", description, false));
183         result.addResult(validateNotBlank("eventType", eventType, true));
184
185         validateOptional(result, "schedule", schedule);
186         validateOptional(result, "targetFilter", targetFilter);
187
188         if (evaluations < 0) {
189             addResult(result, "evaluations", evaluations, "is negative");
190         }
191
192         result.addResult(validateNotBlank("method", method, false));
193         result.addResult(validateNotBlank("action", action, true));
194
195         return result;
196     }
197
198     @Override
199     public int compareTo(final PfConcept otherConcept) {
200         if (otherConcept == null) {
201             return -1;
202         }
203         if (this == otherConcept) {
204             return 0;
205         }
206         if (getClass() != otherConcept.getClass()) {
207             return getClass().getName().compareTo(otherConcept.getClass().getName());
208         }
209
210         final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
211         int result = key.compareTo(other.key);
212         if (result != 0) {
213             return result;
214         }
215
216         return compareFields(other);
217     }
218
219     /**
220      * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty object.
221      *
222      * @param other the other ToscaTrigger object
223      */
224     private int compareFields(final JpaToscaTrigger other) {
225         int result = ObjectUtils.compare(description, other.description);
226         if (result != 0) {
227             return result;
228         }
229
230         result = ObjectUtils.compare(eventType, other.eventType);
231         if (result != 0) {
232             return result;
233         }
234
235         result = ObjectUtils.compare(schedule, other.schedule);
236         if (result != 0) {
237             return result;
238         }
239
240         result = ObjectUtils.compare(targetFilter, other.targetFilter);
241         if (result != 0) {
242             return result;
243         }
244
245         result = ObjectUtils.compare(condition, other.condition);
246         if (result != 0) {
247             return result;
248         }
249
250         result = ObjectUtils.compare(constraint, other.constraint);
251         if (result != 0) {
252             return result;
253         }
254
255         result = ObjectUtils.compare(period, other.period);
256         if (result != 0) {
257             return result;
258         }
259
260         if (evaluations != other.evaluations) {
261             return evaluations - other.evaluations;
262         }
263
264         result = ObjectUtils.compare(method, other.method);
265         if (result != 0) {
266             return result;
267         }
268
269         return ObjectUtils.compare(action, other.action);
270     }
271 }