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;
32 import org.onap.policy.common.utils.validation.Assertions;
33 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
36 * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system
37 * must have an {@link PfConceptKey} to identify it. Concepts that are wholly contained in another concept are
38 * identified using a {@link PfReferenceKey} key.
40 * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
41 * regular expressions respectively.
45 @EqualsAndHashCode(callSuper = false)
46 public class PfConceptKey extends PfKey {
47 private static final long serialVersionUID = 8932717618579392561L;
49 private static final String NAME_TOKEN = "name";
50 private static final String VERSION_TOKEN = "version";
52 @Column(name = NAME_TOKEN)
55 @Column(name = VERSION_TOKEN)
56 private String version;
59 * The default constructor creates a null concept key.
61 public PfConceptKey() {
62 this(NULL_KEY_NAME, NULL_KEY_VERSION);
68 * @param copyConcept the concept to copy from
70 public PfConceptKey(final PfConceptKey copyConcept) {
75 * Constructor to create a key with the specified name and version.
77 * @param name the key name
78 * @param version the key version
80 public PfConceptKey(final String name, final String version) {
82 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
83 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
87 * Constructor to create a key using the key and version from the specified key ID.
89 * @param id the key ID in a format that respects the KEY_ID_REGEXP
91 public PfConceptKey(final String id) {
92 Assertions.argumentNotNull(id, "id may not be null");
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 PfKey.Compatibility getCompatibility(final PfKey otherKey) {
135 if (!(otherKey instanceof PfConceptKey)) {
136 return Compatibility.DIFFERENT;
138 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
140 if (this.equals(otherConceptKey)) {
141 return Compatibility.IDENTICAL;
143 if (!this.getName().equals(otherConceptKey.getName())) {
144 return Compatibility.DIFFERENT;
147 final String[] thisVersionArray = getVersion().split("\\.");
148 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
150 // There must always be at least one element in each version
151 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
152 return Compatibility.MAJOR;
155 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
156 && !thisVersionArray[1].equals(otherVersionArray[1])) {
157 return Compatibility.MINOR;
160 return Compatibility.PATCH;
164 public boolean isCompatible(final PfKey otherKey) {
165 if (!(otherKey instanceof PfConceptKey)) {
168 final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
170 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
172 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
176 public PfValidationResult validate(final PfValidationResult result) {
177 final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
179 if (nameValidationErrorMessage != null) {
180 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
181 "name invalid-" + nameValidationErrorMessage));
184 final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
185 version, VERSION_REGEXP);
186 if (versionValidationErrorMessage != null) {
187 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
188 "version invalid-" + versionValidationErrorMessage));
195 public void clean() {
196 name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
197 version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
201 public PfConcept copyTo(final PfConcept target) {
202 Assertions.argumentNotNull(target, "target may not be null");
204 final PfConcept copyObject = target;
205 Assertions.instanceOf(copyObject, PfConceptKey.class);
207 final PfConceptKey copy = ((PfConceptKey) copyObject);
209 copy.setVersion(version);
215 public int compareTo(final PfConcept otherObj) {
216 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
218 if (this == otherObj) {
221 if (getClass() != otherObj.getClass()) {
222 return this.hashCode() - otherObj.hashCode();
225 final PfConceptKey other = (PfConceptKey) otherObj;
227 if (!name.equals(other.name)) {
228 return name.compareTo(other.name);
230 return version.compareTo(other.version);