8be1126affc82207b81523999a98aa62aca5baaf
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfReferenceTimestampKey.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import java.time.Instant;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
28 import javax.persistence.Column;
29 import javax.persistence.Embeddable;
30 import javax.persistence.Embedded;
31 import javax.persistence.Temporal;
32 import javax.persistence.TemporalType;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.onap.policy.common.parameters.annotations.NotNull;
37 import org.onap.policy.common.utils.validation.Assertions;
38
39 /**
40  * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
41  * additional option to have timestamp as a parameter.
42  *
43  */
44
45 @Embeddable
46 @Data
47 @EqualsAndHashCode(callSuper = false)
48 public class PfReferenceTimestampKey extends PfKey {
49     private static final long serialVersionUID = 1130918285832617215L;
50
51     private static final String TIMESTAMP_TOKEN = "timeStamp";
52
53     @Column(name = TIMESTAMP_TOKEN, precision = 3)
54     @Temporal(TemporalType.TIMESTAMP)
55     @NotNull
56     private Date timeStamp;
57
58     @Embedded
59     @Column
60     private PfReferenceKey referenceKey;
61
62     /**
63      * The default constructor creates a null reference timestamp key.
64      */
65     public PfReferenceTimestampKey() {
66         this.referenceKey = new PfReferenceKey();
67         this.timeStamp = new Date(0);
68     }
69
70     /**
71      * The Copy Constructor creates a key by copying another key.
72      *
73      * @param referenceTimestampKey
74      *        the reference key to copy from
75      */
76     public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) {
77         this.referenceKey = referenceTimestampKey.getReferenceKey();
78         this.timeStamp = referenceTimestampKey.getTimeStamp();
79     }
80
81     /**
82      * Constructor to create a null reference key for the specified parent concept key.
83      *
84      * @param pfConceptKey
85      *        the parent concept key of this reference key
86      */
87     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
88         this.referenceKey = new PfReferenceKey(pfConceptKey);
89         this.timeStamp = new Date(0);
90     }
91
92     /**
93      * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
94      *
95      * @param pfConceptKey
96      *        the parent concept key of this reference key
97      * @param localName
98      *        the local name of this reference key
99      * @param instant
100      *        the time stamp for this reference key
101      */
102     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
103         this.referenceKey = new PfReferenceKey(pfConceptKey, localName);
104         this.timeStamp = Date.from(instant);
105     }
106
107     /**
108      * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
109      *
110      * @param parentReferenceKey
111      *        the parent reference key of this reference key
112      * @param localName
113      *        the local name of this reference key
114      * @param instant
115      *        the time stamp for this reference key
116      */
117     public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
118                                    final Instant instant) {
119         this.referenceKey = new PfReferenceKey(parentReferenceKey, localName);
120         this.timeStamp = Date.from(instant);
121     }
122
123     /**
124      * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
125      * reference key's concept key and local name) with the given local name.
126      *
127      * @param pfConceptKey
128      *        the concept key of the parent reference key of this reference key
129      * @param parentLocalName
130      *        the local name of the parent reference key of this reference key
131      * @param localName
132      *        the local name of this reference key
133      * @param instant
134      *        the time stamp for this reference key
135      */
136     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
137                                    final String localName, final Instant instant) {
138         this.referenceKey = new PfReferenceKey(pfConceptKey, parentLocalName, localName);
139         this.timeStamp = Date.from(instant);
140     }
141
142     /**
143      * Constructor to create a reference timestamp key for the given parent concept key (specified by the
144      * parent concept key's name and version) with the given local name.
145      *
146      * @param parentKeyName
147      *        the name of the parent concept key of this reference key
148      * @param parentKeyVersion
149      *        the version of the parent concept key of this reference key
150      * @param localName
151      *        the local name of this reference key
152      * @param instant
153      *        the time stamp for this reference key
154      */
155     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
156                                    final Instant instant) {
157         this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, PfKey.NULL_KEY_NAME, localName);
158         this.timeStamp = Date.from(instant);
159     }
160
161     /**
162      * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
163      * version and local name) with the given local name.
164      *
165      * @param parentKeyName
166      *        the parent key name of this reference key
167      * @param parentKeyVersion
168      *        the parent key version of this reference key
169      * @param parentLocalName
170      *        the parent local name of this reference key
171      * @param localName
172      *        the local name of this reference key
173      * @param instant
174      *        the instant for this reference key
175      */
176     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
177                                    final String parentLocalName, final String localName, final Instant instant) {
178         this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName, localName);
179         this.timeStamp = Date.from(instant);
180     }
181
182
183     /**
184      * Constructor to create a key using the key and version from the specified key ID.
185      *
186      * @param id the key ID in a format that respects the KEY_ID_REGEXP
187      */
188     public PfReferenceTimestampKey(final String id) {
189         this.referenceKey = new PfReferenceKey(id.substring(0, id.lastIndexOf(':')));
190         this.timeStamp = new Date(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
191     }
192
193
194     /**
195      * Get a null reference timestamp key.
196      *
197      * @return a null reference key
198      */
199     public static PfReferenceTimestampKey getNullKey() {
200         return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
201             PfKey.NULL_KEY_NAME, Instant.EPOCH);
202     }
203
204     public Instant getInstant() {
205         return timeStamp.toInstant();
206     }
207
208     public void setInstant(final Instant instant) {
209         setTimeStamp(Date.from(instant));
210     }
211
212     /**
213      * Get the key of this reference.
214      *
215      * @return the pfReferenceTimestamp key
216      */
217     @Override
218     public PfReferenceTimestampKey getKey() {
219         return this;
220     }
221
222     /**
223      * Get the key as a string.
224      * @return pfReferenceTimestamp key.
225      */
226     @Override
227     public String getId() {
228         return getReferenceKey().getId() + ':' + getTimeStamp().getTime();
229     }
230
231
232     /**
233      * Check if this key is a newer version than the other key.
234      *
235      * @param otherKey the key to check against
236      * @return true, if this key is newer than the other key
237      */
238     @Override
239     public boolean isNewerThan(@NonNull PfKey otherKey) {
240         Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
241         final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
242         if (!getTimeStamp().equals(otherReferenceKey.timeStamp)) {
243             return timeStamp.after(otherReferenceKey.timeStamp);
244         }
245         return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey());
246     }
247
248     @Override
249     public boolean isNullKey() {
250         return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0;
251     }
252
253     @Override
254     public int getMajorVersion() {
255         return getReferenceKey().getMajorVersion();
256     }
257
258     @Override
259     public int getMinorVersion() {
260         return getReferenceKey().getMinorVersion();
261     }
262
263     @Override
264     public int getPatchVersion() {
265         return getReferenceKey().getPatchVersion();
266     }
267
268
269     @Override
270     public int compareTo(@NonNull final PfConcept otherObj) {
271         if (this == otherObj) {
272             return 0;
273         }
274         if (getClass() != otherObj.getClass()) {
275             return getClass().getName().compareTo(otherObj.getClass().getName());
276         }
277         int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey());
278         if (0 == result) {
279             return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp);
280         }
281         return result;
282     }
283
284     @Override
285     public List<PfKey> getKeys() {
286         return Collections.singletonList(getKey());
287     }
288
289     @Override
290     public void clean() {
291         getReferenceKey().clean();
292     }
293
294     @Override
295     public Compatibility getCompatibility(@NonNull PfKey otherKey) {
296         return getReferenceKey().getCompatibility(otherKey);
297     }
298
299     @Override
300     public boolean isCompatible(@NonNull PfKey otherKey) {
301         if (!(otherKey instanceof PfReferenceTimestampKey)) {
302             return false;
303         }
304         final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey;
305
306         return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey()
307             .getParentConceptKey());
308     }
309 }