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