5cb639ed54cc04cb94c338acef6f764b685ea14a
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import java.time.Instant;
24 import javax.persistence.Column;
25 import javax.persistence.Embeddable;
26 import lombok.Data;
27 import lombok.EqualsAndHashCode;
28 import lombok.NonNull;
29 import lombok.ToString;
30 import org.onap.policy.common.parameters.annotations.NotNull;
31 import org.onap.policy.common.utils.validation.Assertions;
32
33 /**
34  * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
35  * additional option to have timestamp as a parameter.
36  *
37  */
38 @Embeddable
39 @Data
40 @EqualsAndHashCode
41 @ToString(callSuper = true)
42 public class PfReferenceTimestampKey extends PfReferenceKey {
43     private static final String TIMESTAMP_TOKEN = "timeStamp";
44     private static final Instant DEFAULT_TIMESTAMP = Instant.EPOCH;
45
46     @Column(name = TIMESTAMP_TOKEN)
47     @NotNull
48     private Instant timeStamp;
49
50
51     /**
52      * The default constructor creates a null reference timestamp key.
53      */
54     public PfReferenceTimestampKey() {
55         super();
56         this.timeStamp = DEFAULT_TIMESTAMP;
57     }
58
59     /**
60      * The Copy Constructor creates a key by copying another key.
61      *
62      * @param referenceKey
63      *        the reference key to copy from
64      */
65     public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceKey) {
66         super(referenceKey);
67         this.timeStamp = referenceKey.getTimeStamp();
68     }
69
70     /**
71      * Constructor to create a null reference key for the specified parent concept key.
72      *
73      * @param pfConceptKey
74      *        the parent concept key of this reference key
75      */
76     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
77         super(pfConceptKey);
78         this.timeStamp = DEFAULT_TIMESTAMP;
79     }
80
81     /**
82      * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
83      *
84      * @param pfConceptKey
85      *        the parent concept key of this reference key
86      * @param localName
87      *        the local name of this reference key
88      * @param timeStamp
89      *        the timestamp for this reference key
90      */
91     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant timeStamp) {
92         super(pfConceptKey, localName);
93         this.timeStamp = timeStamp;
94     }
95
96     /**
97      * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
98      *
99      * @param parentReferenceKey
100      *        the parent reference key of this reference key
101      * @param localName
102      *        the local name of this reference key
103      * @param timeStamp
104      *        the timestamp for this reference key
105      */
106     public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
107                                    final Instant timeStamp) {
108         super(parentReferenceKey, localName);
109         this.timeStamp = timeStamp;
110     }
111
112     /**
113      * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
114      * reference key's concept key and local name) with the given local name.
115      *
116      * @param pfConceptKey
117      *        the concept key of the parent reference key of this reference key
118      * @param parentLocalName
119      *        the local name of the parent reference key of this reference key
120      * @param localName
121      *        the local name of this reference key
122      * @param timeStamp
123      *        the timestamp for this reference key
124      */
125     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
126                                    final String localName, final Instant timeStamp) {
127         super(pfConceptKey, parentLocalName, localName);
128         this.timeStamp = timeStamp;
129     }
130
131     /**
132      * Constructor to create a reference timestamp key for the given parent concept key (specified by the
133      * parent concept key's name and version) with the given local name.
134      *
135      * @param parentKeyName
136      *        the name of the parent concept key of this reference key
137      * @param parentKeyVersion
138      *        the version of the parent concept key of this reference key
139      * @param localName
140      *        the local name of this reference key
141      * @param timeStamp
142      *        the timestamp for this reference key
143      */
144     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
145                                    final Instant timeStamp) {
146         super(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName);
147         this.timeStamp = timeStamp;
148     }
149
150     /**
151      * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
152      * version and local name) with the given local name.
153      *
154      * @param parentKeyName
155      *        the parent key name of this reference key
156      * @param parentKeyVersion
157      *        the parent key version of this reference key
158      * @param parentLocalName
159      *        the parent local name of this reference key
160      * @param localName
161      *        the local name of this reference key
162      * @param timeStamp
163      *        the timestamp for this reference key
164      */
165     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
166                                    final String parentLocalName, final String localName, final Instant timeStamp) {
167         super(parentKeyName, parentKeyVersion, parentLocalName, localName);
168         this.timeStamp = timeStamp;
169     }
170
171
172     /**
173      * Constructor to create a key using the key and version from the specified key ID.
174      *
175      * @param id the key ID in a format that respects the KEY_ID_REGEXP
176      */
177     public PfReferenceTimestampKey(final String id) {
178         super(id.substring(0, id.lastIndexOf(':')));
179         this.timeStamp = Instant.ofEpochSecond(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
180     }
181
182
183     /**
184      * Get a null reference timestamp key.
185      *
186      * @return a null reference key
187      */
188     public static PfReferenceTimestampKey getNullKey() {
189         return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
190             PfKey.NULL_KEY_NAME, DEFAULT_TIMESTAMP);
191     }
192
193
194     @Override
195     public PfReferenceTimestampKey getKey() {
196         return this;
197     }
198
199     @Override
200     public String getId() {
201         return super.getId() + ':' + getTimeStamp().getEpochSecond();
202     }
203
204     @Override
205     public boolean isNullKey() {
206         return super.isNullKey() && getTimeStamp().getEpochSecond() == 0;
207     }
208
209     @Override
210     public boolean isNewerThan(@NonNull PfKey otherKey) {
211         Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
212         final PfReferenceTimestampKey otherConceptKey = (PfReferenceTimestampKey) otherKey;
213
214         if (this.equals(otherConceptKey)) {
215             return false;
216         }
217         if (!getTimeStamp().equals(otherConceptKey.timeStamp)) {
218             return timeStamp.isAfter(otherConceptKey.timeStamp);
219         }
220         return super.isNewerThan(otherKey);
221     }
222 }