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;
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) {
79 * Constructor to create a key with the specified name and version.
81 * @param name the key name
82 * @param version the key version
84 public PfConceptKey(@NonNull final String name, @NonNull final String version) {
86 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
87 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
91 * Constructor to create a key using the key and version from the specified key ID.
93 * @param id the key ID in a format that respects the KEY_ID_REGEXP
95 public PfConceptKey(@NonNull final String id) {
96 // Check the incoming ID is valid
97 Assertions.validateStringParameter("id", id, KEY_ID_REGEXP);
99 // Split on colon, if the id passes the regular expression test above
100 // it'll have just one colon separating the name and version
101 // No need for range checks or size checks on the array
102 final String[] nameVersionArray = id.split(":");
104 // Return the new key
105 name = Assertions.validateStringParameter(NAME_TOKEN, nameVersionArray[0], NAME_REGEXP);
106 version = Assertions.validateStringParameter(VERSION_TOKEN, nameVersionArray[1], VERSION_REGEXP);
110 * Get a null concept key.
112 * @return a null concept key
114 public static final PfConceptKey getNullKey() {
115 return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
119 public PfConceptKey getKey() {
123 public void setName(@NonNull final String name) {
124 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
127 public void setVersion(@NonNull final String version) {
128 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
132 public List<PfKey> getKeys() {
133 final List<PfKey> keyList = new ArrayList<>();
134 keyList.add(getKey());
139 public String getId() {
140 return name + ':' + version;
144 public boolean isNullKey() {
145 return this.equals(PfConceptKey.getNullKey());
149 * Determines if the version is "null".
151 * @return {@code true} if the version is null, {@code false} otherwise
153 public boolean isNullVersion() {
154 return PfKey.NULL_KEY_VERSION.equals(getVersion());
158 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
159 if (!(otherKey instanceof PfConceptKey)) {
160 return Compatibility.DIFFERENT;
162 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
164 if (this.equals(otherConceptKey)) {
165 return Compatibility.IDENTICAL;
167 if (!this.getName().equals(otherConceptKey.getName())) {
168 return Compatibility.DIFFERENT;
171 final String[] thisVersionArray = getVersion().split("\\.");
172 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
174 // There must always be at least one element in each version
175 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
176 return Compatibility.MAJOR;
179 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
180 && !thisVersionArray[1].equals(otherVersionArray[1])) {
181 return Compatibility.MINOR;
184 return Compatibility.PATCH;
188 public boolean isCompatible(@NonNull final PfKey otherKey) {
189 if (!(otherKey instanceof PfConceptKey)) {
192 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
194 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
196 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
200 public boolean isNewerThan(@NonNull final PfKey otherKey) {
201 Assertions.instanceOf(otherKey, PfConceptKey.class);
203 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
205 if (this.equals(otherConceptKey)) {
209 if (!this.getName().equals(otherConceptKey.getName())) {
210 return this.getName().compareTo(otherConceptKey.getName()) > 0;
213 final String[] thisVersionArray = getVersion().split("\\.");
214 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
216 // There must always be at least one element in each version
217 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
218 return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
221 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
222 && !thisVersionArray[1].equals(otherVersionArray[1])) {
223 return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
226 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
227 && !thisVersionArray[2].equals(otherVersionArray[2])) {
228 return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
235 public int getMajorVersion() {
236 final String[] versionArray = getVersion().split("\\.");
238 // There must always be at least one element in each version
239 return Integer.parseInt(versionArray[0]);
243 public int getMinorVersion() {
244 final String[] versionArray = getVersion().split("\\.");
246 if (versionArray.length >= 2) {
247 return Integer.parseInt(versionArray[1]);
255 public int getPatchVersion() {
256 final String[] versionArray = getVersion().split("\\.");
258 if (versionArray.length >= 3) {
259 return Integer.parseInt(versionArray[2]);
267 public PfValidationResult validate(final PfValidationResult result) {
268 final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
270 if (nameValidationErrorMessage != null) {
271 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
272 "name invalid-" + nameValidationErrorMessage));
275 final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
276 version, VERSION_REGEXP);
277 if (versionValidationErrorMessage != null) {
278 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
279 "version invalid-" + versionValidationErrorMessage));
286 public void clean() {
287 name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
288 version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
292 public PfConcept copyTo(final PfConcept target) {
293 Assertions.argumentNotNull(target, "target may not be null");
295 final PfConcept copyObject = target;
296 Assertions.instanceOf(copyObject, PfConceptKey.class);
298 final PfConceptKey copy = ((PfConceptKey) copyObject);
300 copy.setVersion(version);
306 public int compareTo(@NonNull final PfConcept otherObj) {
307 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
309 if (this == otherObj) {
312 if (getClass() != otherObj.getClass()) {
313 return this.hashCode() - otherObj.hashCode();
316 final PfConceptKey other = (PfConceptKey) otherObj;
318 if (!name.equals(other.name)) {
319 return name.compareTo(other.name);
321 return version.compareTo(other.version);