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;
28 import javax.xml.bind.annotation.XmlAccessType;
29 import javax.xml.bind.annotation.XmlAccessorType;
30 import javax.xml.bind.annotation.XmlElement;
31 import javax.xml.bind.annotation.XmlRootElement;
32 import javax.xml.bind.annotation.XmlType;
34 import org.onap.policy.common.utils.validation.Assertions;
35 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
38 * An artifact key uniquely identifies every first order entity in the system. Every first order
39 * concept in the system must have an {@link PfConceptKey} to identify it. Concepts that are wholly
40 * contained in another concept are identified using a {@link AxReferenceKey} key.
42 * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
43 * regular expressions respectively.
46 @XmlAccessorType(XmlAccessType.FIELD)
47 @XmlRootElement(name = "pfConceptKey", namespace = "http://www.onap.org/policy/models")
49 @XmlType(name = "PfConceptKey", namespace = "http://www.onap.org/policy/models", propOrder = {"name", "version"})
51 public class PfConceptKey extends PfKey {
52 private static final long serialVersionUID = 8932717618579392561L;
54 private static final String NAME_TOKEN = "name";
55 private static final String VERSION_TOKEN = "version";
57 @Column(name = NAME_TOKEN)
58 @XmlElement(required = true)
61 @Column(name = VERSION_TOKEN)
62 @XmlElement(required = true)
63 private String version;
66 * The default constructor creates a null artifact key.
68 public PfConceptKey() {
69 this(NULL_KEY_NAME, NULL_KEY_VERSION);
75 * @param copyConcept the concept to copy from
77 public PfConceptKey(final PfConceptKey copyConcept) {
82 * Constructor to create a key with the specified name and version.
84 * @param name the key name
85 * @param version the key version
87 public PfConceptKey(final String name, final String version) {
89 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
90 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
94 * Constructor to create a key using the key and version from the specified key ID.
96 * @param id the key ID in a format that respects the KEY_ID_REGEXP
98 public PfConceptKey(final String id) {
99 Assertions.argumentNotNull(id, "id may not be null");
101 // Check the incoming ID is valid
102 Assertions.validateStringParameter("id", id, KEY_ID_REGEXP);
104 // Split on colon, if the id passes the regular expression test above
105 // it'll have just one colon separating the name and version
106 // No need for range checks or size checks on the array
107 final String[] nameVersionArray = id.split(":");
109 // Return the new key
110 name = Assertions.validateStringParameter(NAME_TOKEN, nameVersionArray[0], NAME_REGEXP);
111 version = Assertions.validateStringParameter(VERSION_TOKEN, nameVersionArray[1], VERSION_REGEXP);
115 * Get a null artifact key.
117 * @return a null artifact key
119 public static final PfConceptKey getNullKey() {
120 return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
124 public PfConceptKey getKey() {
129 public List<PfKey> getKeys() {
130 final List<PfKey> keyList = new ArrayList<>();
131 keyList.add(getKey());
136 public String getId() {
137 return name + ':' + version;
143 * @return the key name
145 public String getName() {
152 * @param name the key name
154 public void setName(final String name) {
155 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
159 * Gets the key version.
161 * @return the key version
163 public String getVersion() {
168 * Sets the key version.
170 * @param version the key version
172 public void setVersion(final String version) {
173 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
177 public PfKey.Compatibility getCompatibility(final PfKey otherKey) {
178 if (!(otherKey instanceof PfConceptKey)) {
179 return Compatibility.DIFFERENT;
181 final PfConceptKey otherArtifactKey = (PfConceptKey) otherKey;
183 if (this.equals(otherArtifactKey)) {
184 return Compatibility.IDENTICAL;
186 if (!this.getName().equals(otherArtifactKey.getName())) {
187 return Compatibility.DIFFERENT;
190 final String[] thisVersionArray = getVersion().split("\\.");
191 final String[] otherVersionArray = otherArtifactKey.getVersion().split("\\.");
193 // There must always be at least one element in each version
194 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
195 return Compatibility.MAJOR;
198 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
199 && !thisVersionArray[1].equals(otherVersionArray[1])) {
200 return Compatibility.MINOR;
203 return Compatibility.PATCH;
207 public boolean isCompatible(final PfKey otherKey) {
208 if (!(otherKey instanceof PfConceptKey)) {
211 final PfConceptKey otherArtifactKey = (PfConceptKey) otherKey;
213 final Compatibility compatibility = this.getCompatibility(otherArtifactKey);
215 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
219 public PfValidationResult validate(final PfValidationResult result) {
220 final String nameValidationErrorMessage =
221 Assertions.getStringParameterValidationMessage(NAME_TOKEN, name, NAME_REGEXP);
222 if (nameValidationErrorMessage != null) {
223 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
224 "name invalid-" + nameValidationErrorMessage));
227 final String versionValidationErrorMessage =
228 Assertions.getStringParameterValidationMessage(VERSION_TOKEN, version, VERSION_REGEXP);
229 if (versionValidationErrorMessage != null) {
230 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
231 "version invalid-" + versionValidationErrorMessage));
238 public void clean() {
239 name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
240 version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
244 public String toString() {
245 final StringBuilder builder = new StringBuilder();
246 builder.append(this.getClass().getSimpleName());
247 builder.append(":(");
248 builder.append("name=");
249 builder.append(name);
250 builder.append(",version=");
251 builder.append(version);
253 return builder.toString();
257 public PfConcept copyTo(final PfConcept target) {
258 Assertions.argumentNotNull(target, "target may not be null");
260 final PfConcept copyObject = target;
261 Assertions.instanceOf(copyObject, PfConceptKey.class);
263 final PfConceptKey copy = ((PfConceptKey) copyObject);
265 copy.setVersion(version);
271 public int hashCode() {
272 final int prime = 31;
274 result = prime * result + name.hashCode();
275 result = prime * result + version.hashCode();
280 public boolean equals(final Object obj) {
287 if (getClass() != obj.getClass()) {
291 final PfConceptKey other = (PfConceptKey) obj;
293 if (!name.equals(other.name)) {
296 return version.equals(other.version);
300 public int compareTo(final PfConcept otherObj) {
301 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
303 if (this == otherObj) {
306 if (getClass() != otherObj.getClass()) {
307 return this.hashCode() - otherObj.hashCode();
310 final PfConceptKey other = (PfConceptKey) otherObj;
312 if (!name.equals(other.name)) {
313 return name.compareTo(other.name);
315 return version.compareTo(other.version);