819b7d8121b8ca7c00763633a0684a1d65b10f9b
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTimeInterval.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.sql.Timestamp;
28 import java.time.Instant;
29 import java.util.List;
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.onap.policy.common.parameters.BeanValidationResult;
39 import org.onap.policy.common.parameters.annotations.NotNull;
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.PfUtils;
44 import org.onap.policy.models.base.validation.annotations.VerifyKey;
45
46 /**
47  * Class to represent the TimeInterval in TOSCA definition.
48  *
49  * @author Chenfei Gao (cgao@research.att.com)
50  * @author Liam Fallon (liam.fallon@est.tech)
51  *
52  */
53 @Entity
54 @Table(name = "ToscaTimeInterval")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @Data
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaToscaTimeInterval extends PfConcept {
59     private static final long serialVersionUID = 9151467029611969980L;
60
61     @EmbeddedId
62     @VerifyKey
63     @NotNull
64     private PfReferenceKey key;
65
66     @SerializedName("start_time")
67     private Timestamp startTime;
68
69     @SerializedName("end_time")
70     private Timestamp endTime;
71
72     /**
73      * The Default Constructor creates a {@link JpaToscaTimeInterval} object with a null key.
74      */
75     public JpaToscaTimeInterval() {
76         this(new PfReferenceKey());
77     }
78
79     /**
80      * The Key Constructor creates a {@link JpaToscaTimeInterval} object with the given concept key.
81      *
82      * @param key the key
83      */
84     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key) {
85         this(key, Instant.EPOCH, Instant.EPOCH);
86     }
87
88     /**
89      * The full constructor creates a {@link JpaToscaTimeInterval} object with all fields.
90      *
91      * @param key the key
92      */
93     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Instant startTime,
94             @NonNull final Instant endTime) {
95         this.key = key;
96         this.startTime = Timestamp.from(startTime);
97         this.endTime = Timestamp.from(endTime);
98     }
99
100     /**
101      * Copy constructor.
102      *
103      * @param copyConcept the concept to copy from
104      */
105     public JpaToscaTimeInterval(final JpaToscaTimeInterval copyConcept) {
106         super(copyConcept);
107         this.key = new PfReferenceKey(copyConcept.key);
108         this.startTime = copyConcept.startTime;
109         this.endTime = copyConcept.endTime;
110     }
111
112     @Override
113     public List<PfKey> getKeys() {
114         return getKey().getKeys();
115     }
116
117     @Override
118     public void clean() {
119         key.clean();
120     }
121
122     @Override
123     public BeanValidationResult validate(@NonNull String fieldName) {
124         BeanValidationResult result = super.validate(fieldName);
125
126         if (startTime == null || startTime.getTime() == 0) {
127             addResult(result, "startTime", startTime, "is null or zero");
128         }
129
130         if (endTime == null || endTime.getTime() == 0) {
131             addResult(result, "endTime", endTime, "is null or zero");
132         }
133
134         if (startTime != null && endTime != null && endTime.before(startTime)) {
135             addResult(result, "endTime", endTime, "is before startTime");
136         }
137
138         return result;
139     }
140
141     @Override
142     public int compareTo(final PfConcept otherConcept) {
143         if (otherConcept == null) {
144             return -1;
145         }
146         if (this == otherConcept) {
147             return 0;
148         }
149         if (getClass() != otherConcept.getClass()) {
150             return getClass().getName().compareTo(otherConcept.getClass().getName());
151         }
152
153         final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
154         int result = key.compareTo(other.key);
155         if (result != 0) {
156             return result;
157         }
158
159         result = PfUtils.compareObjects(startTime, other.startTime);
160         if (result != 0) {
161             return result;
162         }
163
164         return PfUtils.compareObjects(endTime, other.endTime);
165     }
166 }