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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import java.time.Instant;
24 import javax.persistence.Column;
25 import javax.persistence.Embeddable;
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;
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.
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;
46 @Column(name = TIMESTAMP_TOKEN)
48 private Instant timeStamp;
52 * The default constructor creates a null reference timestamp key.
54 public PfReferenceTimestampKey() {
56 this.timeStamp = DEFAULT_TIMESTAMP;
60 * The Copy Constructor creates a key by copying another key.
63 * the reference key to copy from
65 public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceKey) {
67 this.timeStamp = referenceKey.getTimeStamp();
71 * Constructor to create a null reference key for the specified parent concept key.
74 * the parent concept key of this reference key
76 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
78 this.timeStamp = DEFAULT_TIMESTAMP;
82 * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
85 * the parent concept key of this reference key
87 * the local name of this reference key
89 * the timestamp for this reference key
91 public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant timeStamp) {
92 super(pfConceptKey, localName);
93 this.timeStamp = timeStamp;
97 * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
99 * @param parentReferenceKey
100 * the parent reference key of this reference key
102 * the local name of this reference key
104 * the timestamp for this reference key
106 public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
107 final Instant timeStamp) {
108 super(parentReferenceKey, localName);
109 this.timeStamp = timeStamp;
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.
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
121 * the local name of this reference key
123 * the timestamp for this reference key
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;
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.
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
140 * the local name of this reference key
142 * the timestamp for this reference key
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;
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.
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
161 * the local name of this reference key
163 * the timestamp for this reference key
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;
173 * Constructor to create a key using the key and version from the specified key ID.
175 * @param id the key ID in a format that respects the KEY_ID_REGEXP
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)));
184 * Get a null reference timestamp key.
186 * @return a null reference key
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);
195 public PfReferenceTimestampKey getKey() {
200 public String getId() {
201 return super.getId() + ':' + getTimeStamp().getEpochSecond();
205 public boolean isNullKey() {
206 return super.isNullKey() && getTimeStamp().getEpochSecond() == 0;
210 public boolean isNewerThan(@NonNull PfKey otherKey) {
211 Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
212 final PfReferenceTimestampKey otherConceptKey = (PfReferenceTimestampKey) otherKey;
214 if (this.equals(otherConceptKey)) {
217 if (!getTimeStamp().equals(otherConceptKey.timeStamp)) {
218 return timeStamp.isAfter(otherConceptKey.timeStamp);
220 return super.isNewerThan(otherKey);