2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (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;
30 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
33 import lombok.ToString;
35 import org.onap.policy.common.utils.validation.Assertions;
36 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
39 * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system
40 * must have an {@link PfConceptKey} to identify it. Concepts that are wholly contained in another concept are
41 * identified using a {@link PfReferenceKey} key.
43 * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
44 * regular expressions respectively.
49 @EqualsAndHashCode(callSuper = false)
50 public class PfConceptKey extends PfKey {
51 private static final long serialVersionUID = 8932717618579392561L;
53 private static final String NAME_TOKEN = "name";
54 private static final String VERSION_TOKEN = "version";
56 @Column(name = NAME_TOKEN, length = 120)
59 @Column(name = VERSION_TOKEN, length = 20)
60 private String version;
63 * The default constructor creates a null concept key.
65 public PfConceptKey() {
66 this(NULL_KEY_NAME, NULL_KEY_VERSION);
72 * @param copyConcept the concept to copy from
74 public PfConceptKey(@NonNull final PfConceptKey copyConcept) {
76 this.name = copyConcept.name;
77 this.version = copyConcept.version;
81 * Constructor to create a key with the specified name and version.
83 * @param name the key name
84 * @param version the key version
86 public PfConceptKey(@NonNull final String name, @NonNull final String version) {
88 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
89 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
93 * Constructor to create a key using the key and version from the specified key ID.
95 * @param id the key ID in a format that respects the KEY_ID_REGEXP
97 public PfConceptKey(@NonNull final String id) {
98 // Check the incoming ID is valid
99 Assertions.validateStringParameter("id", id, KEY_ID_REGEXP);
101 // Split on colon, if the id passes the regular expression test above
102 // it'll have just one colon separating the name and version
103 // No need for range checks or size checks on the array
104 final String[] nameVersionArray = id.split(":");
106 // Return the new key
107 name = Assertions.validateStringParameter(NAME_TOKEN, nameVersionArray[0], NAME_REGEXP);
108 version = Assertions.validateStringParameter(VERSION_TOKEN, nameVersionArray[1], VERSION_REGEXP);
112 * Get a null concept key.
114 * @return a null concept key
116 public static final PfConceptKey getNullKey() {
117 return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
121 public PfConceptKey getKey() {
125 public void setName(@NonNull final String name) {
126 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
129 public void setVersion(@NonNull final String version) {
130 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
134 public List<PfKey> getKeys() {
135 final List<PfKey> keyList = new ArrayList<>();
136 keyList.add(getKey());
141 public String getId() {
142 return name + ':' + version;
146 public boolean isNullKey() {
147 return this.equals(PfConceptKey.getNullKey());
151 * Determines if the version is "null".
153 * @return {@code true} if the version is null, {@code false} otherwise
155 public boolean isNullVersion() {
156 return PfKey.NULL_KEY_VERSION.equals(getVersion());
160 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
161 if (!(otherKey instanceof PfConceptKey)) {
162 return Compatibility.DIFFERENT;
164 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
166 if (this.equals(otherConceptKey)) {
167 return Compatibility.IDENTICAL;
169 if (!this.getName().equals(otherConceptKey.getName())) {
170 return Compatibility.DIFFERENT;
173 final String[] thisVersionArray = getVersion().split("\\.");
174 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
176 // There must always be at least one element in each version
177 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
178 return Compatibility.MAJOR;
181 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
182 && !thisVersionArray[1].equals(otherVersionArray[1])) {
183 return Compatibility.MINOR;
186 return Compatibility.PATCH;
190 public boolean isCompatible(@NonNull final PfKey otherKey) {
191 if (!(otherKey instanceof PfConceptKey)) {
194 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
196 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
198 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
202 public boolean isNewerThan(@NonNull final PfKey otherKey) {
203 Assertions.instanceOf(otherKey, PfConceptKey.class);
205 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
207 if (this.equals(otherConceptKey)) {
211 if (!this.getName().equals(otherConceptKey.getName())) {
212 return this.getName().compareTo(otherConceptKey.getName()) > 0;
215 final String[] thisVersionArray = getVersion().split("\\.");
216 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
218 // There must always be at least one element in each version
219 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
220 return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
223 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
224 && !thisVersionArray[1].equals(otherVersionArray[1])) {
225 return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
228 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
229 && !thisVersionArray[2].equals(otherVersionArray[2])) {
230 return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
237 public int getMajorVersion() {
238 final String[] versionArray = getVersion().split("\\.");
240 // There must always be at least one element in each version
241 return Integer.parseInt(versionArray[0]);
245 public int getMinorVersion() {
246 final String[] versionArray = getVersion().split("\\.");
248 if (versionArray.length >= 2) {
249 return Integer.parseInt(versionArray[1]);
257 public int getPatchVersion() {
258 final String[] versionArray = getVersion().split("\\.");
260 if (versionArray.length >= 3) {
261 return Integer.parseInt(versionArray[2]);
269 public PfValidationResult validate(final PfValidationResult result) {
270 final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
272 if (nameValidationErrorMessage != null) {
273 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
274 "name invalid-" + nameValidationErrorMessage));
277 final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
278 version, VERSION_REGEXP);
279 if (versionValidationErrorMessage != null) {
280 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
281 "version invalid-" + versionValidationErrorMessage));
288 public void clean() {
289 name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
290 version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
294 public int compareTo(@NonNull final PfConcept otherObj) {
295 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
297 if (this == otherObj) {
300 if (getClass() != otherObj.getClass()) {
301 return getClass().getName().compareTo(otherObj.getClass().getName());
304 final PfConceptKey other = (PfConceptKey) otherObj;
306 if (!name.equals(other.name)) {
307 return name.compareTo(other.name);
309 return version.compareTo(other.version);