2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ModificationsCopyright (C) 2019 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.persistence.Column;
28 import javax.persistence.Embeddable;
31 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
34 import org.onap.policy.common.utils.validation.Assertions;
35 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
38 * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system
39 * must have an {@link PfConceptKey} to identify it. Concepts that are wholly contained in another concept are
40 * identified using a {@link PfReferenceKey} key.
42 * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
43 * regular expressions respectively.
47 @EqualsAndHashCode(callSuper = false)
48 public class PfConceptKey extends PfKey {
49 private static final long serialVersionUID = 8932717618579392561L;
51 private static final String NAME_TOKEN = "name";
52 private static final String VERSION_TOKEN = "version";
54 @Column(name = NAME_TOKEN, length = 128)
57 @Column(name = VERSION_TOKEN, length = 128)
58 private String version;
61 * The default constructor creates a null concept key.
63 public PfConceptKey() {
64 this(NULL_KEY_NAME, NULL_KEY_VERSION);
70 * @param copyConcept the concept to copy from
72 public PfConceptKey(@NonNull final PfConceptKey copyConcept) {
77 * Constructor to create a key with the specified name and version.
79 * @param name the key name
80 * @param version the key version
82 public PfConceptKey(@NonNull final String name, @NonNull final String version) {
84 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
85 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
89 * Constructor to create a key using the key and version from the specified key ID.
91 * @param id the key ID in a format that respects the KEY_ID_REGEXP
93 public PfConceptKey(@NonNull final String id) {
94 // Check the incoming ID is valid
95 Assertions.validateStringParameter("id", id, KEY_ID_REGEXP);
97 // Split on colon, if the id passes the regular expression test above
98 // it'll have just one colon separating the name and version
99 // No need for range checks or size checks on the array
100 final String[] nameVersionArray = id.split(":");
102 // Return the new key
103 name = Assertions.validateStringParameter(NAME_TOKEN, nameVersionArray[0], NAME_REGEXP);
104 version = Assertions.validateStringParameter(VERSION_TOKEN, nameVersionArray[1], VERSION_REGEXP);
108 * Get a null concept key.
110 * @return a null concept key
112 public static final PfConceptKey getNullKey() {
113 return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
117 public PfConceptKey getKey() {
122 public List<PfKey> getKeys() {
123 final List<PfKey> keyList = new ArrayList<>();
124 keyList.add(getKey());
129 public String getId() {
130 return name + ':' + version;
134 public boolean isNullKey() {
135 return this.equals(PfConceptKey.getNullKey());
139 * Determines if the version is "null".
141 * @return {@code true} if the version is null, {@code false} otherwise
143 public boolean isNullVersion() {
144 return PfKey.NULL_KEY_VERSION.equals(getVersion());
148 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
149 if (!(otherKey instanceof PfConceptKey)) {
150 return Compatibility.DIFFERENT;
152 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
154 if (this.equals(otherConceptKey)) {
155 return Compatibility.IDENTICAL;
157 if (!this.getName().equals(otherConceptKey.getName())) {
158 return Compatibility.DIFFERENT;
161 final String[] thisVersionArray = getVersion().split("\\.");
162 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
164 // There must always be at least one element in each version
165 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
166 return Compatibility.MAJOR;
169 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
170 && !thisVersionArray[1].equals(otherVersionArray[1])) {
171 return Compatibility.MINOR;
174 return Compatibility.PATCH;
178 public boolean isCompatible(@NonNull final PfKey otherKey) {
179 if (!(otherKey instanceof PfConceptKey)) {
182 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
184 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
186 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
190 public boolean isNewerThan(@NonNull final PfKey otherKey) {
191 Assertions.instanceOf(otherKey, PfConceptKey.class);
193 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
195 if (this.equals(otherConceptKey)) {
199 if (!this.getName().equals(otherConceptKey.getName())) {
200 return this.getName().compareTo(otherConceptKey.getName()) > 0;
203 final String[] thisVersionArray = getVersion().split("\\.");
204 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
206 // There must always be at least one element in each version
207 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
208 return thisVersionArray[0].compareTo(otherVersionArray[0]) > 0;
211 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
212 && !thisVersionArray[1].equals(otherVersionArray[1])) {
213 return thisVersionArray[1].compareTo(otherVersionArray[1]) > 0;
216 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
217 && !thisVersionArray[2].equals(otherVersionArray[2])) {
218 return thisVersionArray[2].compareTo(otherVersionArray[2]) > 0;
225 public int getMajorVersion() {
226 final String[] versionArray = getVersion().split("\\.");
228 // There must always be at least one element in each version
229 return Integer.parseInt(versionArray[0]);
233 public int getMinorVersion() {
234 final String[] versionArray = getVersion().split("\\.");
236 if (versionArray.length >= 2) {
237 return Integer.parseInt(versionArray[1]);
245 public int getPatchVersion() {
246 final String[] versionArray = getVersion().split("\\.");
248 if (versionArray.length >= 3) {
249 return Integer.parseInt(versionArray[2]);
257 public PfValidationResult validate(final PfValidationResult result) {
258 final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
260 if (nameValidationErrorMessage != null) {
261 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
262 "name invalid-" + nameValidationErrorMessage));
265 final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
266 version, VERSION_REGEXP);
267 if (versionValidationErrorMessage != null) {
268 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
269 "version invalid-" + versionValidationErrorMessage));
276 public void clean() {
277 name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
278 version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
282 public PfConcept copyTo(final PfConcept target) {
283 Assertions.argumentNotNull(target, "target may not be null");
285 final PfConcept copyObject = target;
286 Assertions.instanceOf(copyObject, PfConceptKey.class);
288 final PfConceptKey copy = ((PfConceptKey) copyObject);
290 copy.setVersion(version);
296 public int compareTo(@NonNull final PfConcept otherObj) {
297 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
299 if (this == otherObj) {
302 if (getClass() != otherObj.getClass()) {
303 return this.hashCode() - otherObj.hashCode();
306 final PfConceptKey other = (PfConceptKey) otherObj;
308 if (!name.equals(other.name)) {
309 return name.compareTo(other.name);
311 return version.compareTo(other.version);