1e13b225c4656de9c09aa131653d68771dfa31ff
[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-2021 Nordix Foundation.
7  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * SPDX-License-Identifier: Apache-2.0
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.models.tosca.simple.concepts;
26
27 import com.google.gson.annotations.SerializedName;
28 import java.time.Duration;
29 import java.util.List;
30 import javax.persistence.Column;
31 import javax.persistence.EmbeddedId;
32 import lombok.Data;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35 import org.apache.commons.lang3.ObjectUtils;
36 import org.onap.policy.common.parameters.annotations.Min;
37 import org.onap.policy.common.parameters.annotations.NotBlank;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.parameters.annotations.Valid;
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.validation.annotations.VerifyKey;
44
45 /**
46  * Class to represent the trigger of policy type in TOSCA definition.
47  *
48  * @author Chenfei Gao (cgao@research.att.com)
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 @Data
52 @EqualsAndHashCode(callSuper = false)
53 public class JpaToscaTrigger extends PfConcept {
54     private static final long serialVersionUID = -6515211640208986971L;
55
56     @EmbeddedId
57     @VerifyKey
58     @NotNull
59     private PfReferenceKey key;
60
61     @Column
62     @NotBlank
63     private String description;
64
65     @Column
66     @SerializedName("event_type")
67     @NotNull
68     @NotBlank
69     private String eventType;
70
71     @Column
72     @SerializedName("schedule")
73     @Valid
74     private JpaToscaTimeInterval schedule;
75
76     @Column
77     @SerializedName("target_filter")
78     @Valid
79     private JpaToscaEventFilter targetFilter;
80
81     @Column(name = "toscaCondition")
82     @Valid
83     private JpaToscaConstraint condition;
84
85     @Column(name = "toscaConstraint")
86     @Valid
87     private JpaToscaConstraint constraint;
88
89     @Column
90     @SerializedName("period")
91     private Duration period;
92
93     @Column
94     @Min(0)
95     private int evaluations = 0;
96
97     @Column
98     @NotBlank
99     private String method;
100
101     @Column
102     @NotNull
103     @NotBlank
104     private String action;
105
106     /**
107      * The Default Constructor creates a {@link JpaToscaTrigger} object with a null key.
108      */
109     public JpaToscaTrigger() {
110         this(new PfReferenceKey());
111     }
112
113     /**
114      * The Key Constructor creates a {@link JpaToscaTrigger} object with the given concept key.
115      *
116      * @param key the key
117      */
118     public JpaToscaTrigger(@NonNull final PfReferenceKey key) {
119         this(key, "", "");
120     }
121
122     /**
123      * The full Constructor creates a {@link JpaToscaTrigger} object with all mandatory objects.
124      *
125      * @param key the key
126      * @param eventType the event type
127      * @param action the trigger action
128      */
129     public JpaToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType,
130             @NonNull final String action) {
131         this.key = key;
132         this.eventType = eventType;
133         this.action = action;
134     }
135
136     /**
137      * Copy constructor.
138      *
139      * @param copyConcept the concept to copy from
140      */
141     public JpaToscaTrigger(final JpaToscaTrigger copyConcept) {
142         super(copyConcept);
143         this.key = new PfReferenceKey(copyConcept.key);
144         this.description = copyConcept.description;
145         this.eventType = copyConcept.eventType;
146         this.schedule = (copyConcept.schedule != null ? new JpaToscaTimeInterval(copyConcept.schedule) : null);
147         this.targetFilter =
148                 (copyConcept.targetFilter != null ? new JpaToscaEventFilter(copyConcept.targetFilter) : null);
149         this.condition = copyConcept.condition;
150         this.constraint = copyConcept.constraint;
151         this.period = copyConcept.period;
152         this.evaluations = copyConcept.evaluations;
153         this.method = copyConcept.method;
154         this.action = copyConcept.action;
155     }
156
157     @Override
158     public List<PfKey> getKeys() {
159         final List<PfKey> keyList = getKey().getKeys();
160         if (schedule != null) {
161             keyList.addAll(schedule.getKeys());
162         }
163         if (targetFilter != null) {
164             keyList.addAll(targetFilter.getKeys());
165         }
166         return keyList;
167     }
168
169     @Override
170     public void clean() {
171         key.clean();
172
173         description = (description != null ? description.trim() : description);
174         eventType = eventType.trim();
175
176         if (schedule != null) {
177             schedule.clean();
178         }
179         if (targetFilter != null) {
180             targetFilter.clean();
181         }
182
183         method = (method != null ? method.trim() : method);
184         action = action.trim();
185     }
186
187     @Override
188     public int compareTo(final PfConcept otherConcept) {
189         if (otherConcept == null) {
190             return -1;
191         }
192         if (this == otherConcept) {
193             return 0;
194         }
195         if (getClass() != otherConcept.getClass()) {
196             return getClass().getName().compareTo(otherConcept.getClass().getName());
197         }
198
199         final JpaToscaTrigger other = (JpaToscaTrigger) otherConcept;
200         int result = key.compareTo(other.key);
201         if (result != 0) {
202             return result;
203         }
204
205         return compareFields(other);
206     }
207
208     /**
209      * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty object.
210      *
211      * @param other the other ToscaTrigger object
212      */
213     private int compareFields(final JpaToscaTrigger other) {
214         int result = ObjectUtils.compare(description, other.description);
215         if (result != 0) {
216             return result;
217         }
218
219         result = ObjectUtils.compare(eventType, other.eventType);
220         if (result != 0) {
221             return result;
222         }
223
224         result = ObjectUtils.compare(schedule, other.schedule);
225         if (result != 0) {
226             return result;
227         }
228
229         result = ObjectUtils.compare(targetFilter, other.targetFilter);
230         if (result != 0) {
231             return result;
232         }
233
234         result = ObjectUtils.compare(condition, other.condition);
235         if (result != 0) {
236             return result;
237         }
238
239         result = ObjectUtils.compare(constraint, other.constraint);
240         if (result != 0) {
241             return result;
242         }
243
244         result = ObjectUtils.compare(period, other.period);
245         if (result != 0) {
246             return result;
247         }
248
249         if (evaluations != other.evaluations) {
250             return evaluations - other.evaluations;
251         }
252
253         result = ObjectUtils.compare(method, other.method);
254         if (result != 0) {
255             return result;
256         }
257
258         return ObjectUtils.compare(action, other.action);
259     }
260 }