2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.util.ArrayList;
24 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.Embeddable;
30 import lombok.EqualsAndHashCode;
31 import lombok.NonNull;
33 import org.onap.policy.common.utils.validation.Assertions;
34 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
37 * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system
38 * must have an {@link PfConceptKey} to identify it. Concepts that are wholly contained in another concept are
39 * identified using a {@link PfReferenceKey} key.
41 * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
42 * regular expressions respectively.
46 @EqualsAndHashCode(callSuper = false)
47 public class PfConceptKey extends PfKey {
48 private static final long serialVersionUID = 8932717618579392561L;
50 private static final String NAME_TOKEN = "name";
51 private static final String VERSION_TOKEN = "version";
53 @Column(name = NAME_TOKEN, length = 128)
56 @Column(name = VERSION_TOKEN, length = 128)
57 private String version;
60 * The default constructor creates a null concept key.
62 public PfConceptKey() {
63 this(NULL_KEY_NAME, NULL_KEY_VERSION);
69 * @param copyConcept the concept to copy from
71 public PfConceptKey(@NonNull final PfConceptKey copyConcept) {
76 * Constructor to create a key with the specified name and version.
78 * @param name the key name
79 * @param version the key version
81 public PfConceptKey(@NonNull final String name, @NonNull final String version) {
83 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
84 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
88 * Constructor to create a key using the key and version from the specified key ID.
90 * @param id the key ID in a format that respects the KEY_ID_REGEXP
92 public PfConceptKey(@NonNull final String id) {
93 // Check the incoming ID is valid
94 Assertions.validateStringParameter("id", id, KEY_ID_REGEXP);
96 // Split on colon, if the id passes the regular expression test above
97 // it'll have just one colon separating the name and version
98 // No need for range checks or size checks on the array
99 final String[] nameVersionArray = id.split(":");
101 // Return the new key
102 name = Assertions.validateStringParameter(NAME_TOKEN, nameVersionArray[0], NAME_REGEXP);
103 version = Assertions.validateStringParameter(VERSION_TOKEN, nameVersionArray[1], VERSION_REGEXP);
107 * Get a null concept key.
109 * @return a null concept key
111 public static final PfConceptKey getNullKey() {
112 return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
116 public PfConceptKey getKey() {
121 public List<PfKey> getKeys() {
122 final List<PfKey> keyList = new ArrayList<>();
123 keyList.add(getKey());
128 public String getId() {
129 return name + ':' + version;
133 public boolean isNullKey() {
134 return this.equals(PfConceptKey.getNullKey());
138 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
139 if (!(otherKey instanceof PfConceptKey)) {
140 return Compatibility.DIFFERENT;
142 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
144 if (this.equals(otherConceptKey)) {
145 return Compatibility.IDENTICAL;
147 if (!this.getName().equals(otherConceptKey.getName())) {
148 return Compatibility.DIFFERENT;
151 final String[] thisVersionArray = getVersion().split("\\.");
152 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
154 // There must always be at least one element in each version
155 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
156 return Compatibility.MAJOR;
159 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
160 && !thisVersionArray[1].equals(otherVersionArray[1])) {
161 return Compatibility.MINOR;
164 return Compatibility.PATCH;
168 public boolean isCompatible(@NonNull final PfKey otherKey) {
169 if (!(otherKey instanceof PfConceptKey)) {
172 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
174 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
176 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
180 public boolean isNewerThan(@NonNull final PfKey otherKey) {
181 Assertions.instanceOf(otherKey, PfConceptKey.class);
183 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
185 if (this.equals(otherConceptKey)) {
189 if (!this.getName().equals(otherConceptKey.getName())) {
190 return this.getName().compareTo(otherConceptKey.getName()) > 0;
193 final String[] thisVersionArray = getVersion().split("\\.");
194 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
196 // There must always be at least one element in each version
197 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
198 return thisVersionArray[0].compareTo(otherVersionArray[0]) > 0;
201 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
202 && !thisVersionArray[1].equals(otherVersionArray[1])) {
203 return thisVersionArray[1].compareTo(otherVersionArray[1]) > 0;
206 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
207 && !thisVersionArray[2].equals(otherVersionArray[2])) {
208 return thisVersionArray[2].compareTo(otherVersionArray[2]) > 0;
215 public int getMajorVersion() {
216 final String[] versionArray = getVersion().split("\\.");
218 // There must always be at least one element in each version
219 return Integer.parseInt(versionArray[0]);
223 public int getMinorVersion() {
224 final String[] versionArray = getVersion().split("\\.");
226 if (versionArray.length >= 2) {
227 return Integer.parseInt(versionArray[1]);
235 public int getPatchVersion() {
236 final String[] versionArray = getVersion().split("\\.");
238 if (versionArray.length >= 3) {
239 return Integer.parseInt(versionArray[2]);
247 public PfValidationResult validate(final PfValidationResult result) {
248 final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
250 if (nameValidationErrorMessage != null) {
251 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
252 "name invalid-" + nameValidationErrorMessage));
255 final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
256 version, VERSION_REGEXP);
257 if (versionValidationErrorMessage != null) {
258 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
259 "version invalid-" + versionValidationErrorMessage));
266 public void clean() {
267 name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
268 version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
272 public PfConcept copyTo(final PfConcept target) {
273 Assertions.argumentNotNull(target, "target may not be null");
275 final PfConcept copyObject = target;
276 Assertions.instanceOf(copyObject, PfConceptKey.class);
278 final PfConceptKey copy = ((PfConceptKey) copyObject);
280 copy.setVersion(version);
286 public int compareTo(@NonNull final PfConcept otherObj) {
287 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
289 if (this == otherObj) {
292 if (getClass() != otherObj.getClass()) {
293 return this.hashCode() - otherObj.hashCode();
296 final PfConceptKey other = (PfConceptKey) otherObj;
298 if (!name.equals(other.name)) {
299 return name.compareTo(other.name);
301 return version.compareTo(other.version);