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)
56 @Column(name = VERSION_TOKEN)
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 PfValidationResult validate(final PfValidationResult result) {
181 final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
183 if (nameValidationErrorMessage != null) {
184 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
185 "name invalid-" + nameValidationErrorMessage));
188 final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
189 version, VERSION_REGEXP);
190 if (versionValidationErrorMessage != null) {
191 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
192 "version invalid-" + versionValidationErrorMessage));
199 public void clean() {
200 name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
201 version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
205 public PfConcept copyTo(final PfConcept target) {
206 Assertions.argumentNotNull(target, "target may not be null");
208 final PfConcept copyObject = target;
209 Assertions.instanceOf(copyObject, PfConceptKey.class);
211 final PfConceptKey copy = ((PfConceptKey) copyObject);
213 copy.setVersion(version);
219 public int compareTo(@NonNull final PfConcept otherObj) {
220 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
222 if (this == otherObj) {
225 if (getClass() != otherObj.getClass()) {
226 return this.hashCode() - otherObj.hashCode();
229 final PfConceptKey other = (PfConceptKey) otherObj;
231 if (!name.equals(other.name)) {
232 return name.compareTo(other.name);
234 return version.compareTo(other.version);