2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (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 lombok.NonNull;
28 import lombok.ToString;
29 import org.onap.policy.common.utils.validation.Assertions;
30 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
33 * A key, upon which other key subclasses can be built, providing implementations of the methods.
37 public abstract class PfKeyImpl extends PfKey {
38 private static final long serialVersionUID = 8932717618579392561L;
40 public static final String NAME_TOKEN = "name";
41 public static final String VERSION_TOKEN = "version";
44 * The default constructor creates a null concept key.
47 this(NULL_KEY_NAME, NULL_KEY_VERSION);
53 * @param copyConcept the concept to copy from
55 public PfKeyImpl(final PfKeyImpl copyConcept) {
57 setName(copyConcept.getName());
58 setVersion(copyConcept.getVersion());
62 * Constructor to create a key with the specified name and version.
64 * @param name the key name
65 * @param version the key version
67 public PfKeyImpl(@NonNull final String name, @NonNull final String version) {
74 * Constructor to create a key using the key and version from the specified key ID.
76 * @param id the key ID in a format that respects the KEY_ID_REGEXP
78 public PfKeyImpl(@NonNull final String id) {
79 // Check the incoming ID is valid
80 Assertions.validateStringParameter("id", id, getKeyIdRegEx());
82 // Split on colon, if the id passes the regular expression test above
83 // it'll have just one colon separating the name and version
84 // No need for range checks or size checks on the array
85 final String[] nameVersionArray = id.split(":");
88 setName(nameVersionArray[0]);
89 setVersion(nameVersionArray[1]);
92 public abstract void setName(@NonNull final String name);
94 public abstract void setVersion(@NonNull final String version);
97 public PfKeyImpl getKey() {
102 public List<PfKey> getKeys() {
103 final List<PfKey> keyList = new ArrayList<>();
104 keyList.add(getKey());
109 public String getId() {
110 return getName() + ':' + getVersion();
114 public boolean isNullKey() {
115 return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
119 * Determines if the name is "null".
121 * @return {@code true} if the name is null, {@code false} otherwise
123 public boolean isNullName() {
124 return PfKey.NULL_KEY_NAME.equals(getName());
128 * Determines if the version is "null".
130 * @return {@code true} if the version is null, {@code false} otherwise
132 public boolean isNullVersion() {
133 return PfKey.NULL_KEY_VERSION.equals(getVersion());
137 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
138 if (!(otherKey instanceof PfKeyImpl)) {
139 return Compatibility.DIFFERENT;
141 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
143 if (this.equals(otherConceptKey)) {
144 return Compatibility.IDENTICAL;
146 if (!this.getName().equals(otherConceptKey.getName())) {
147 return Compatibility.DIFFERENT;
150 final String[] thisVersionArray = getVersion().split("\\.");
151 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
153 // There must always be at least one element in each version
154 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
155 return Compatibility.MAJOR;
158 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
159 && !thisVersionArray[1].equals(otherVersionArray[1])) {
160 return Compatibility.MINOR;
163 return Compatibility.PATCH;
167 public boolean isCompatible(@NonNull final PfKey otherKey) {
168 if (!(otherKey instanceof PfKeyImpl)) {
171 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
173 final Compatibility compatibility = this.getCompatibility(otherConceptKey);
175 return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
179 public boolean isNewerThan(@NonNull final PfKey otherKey) {
180 Assertions.instanceOf(otherKey, PfKeyImpl.class);
182 final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
184 if (this.equals(otherConceptKey)) {
188 if (!this.getName().equals(otherConceptKey.getName())) {
189 return this.getName().compareTo(otherConceptKey.getName()) > 0;
192 final String[] thisVersionArray = getVersion().split("\\.");
193 final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
195 // There must always be at least one element in each version
196 if (!thisVersionArray[0].equals(otherVersionArray[0])) {
197 return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
200 if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
201 && !thisVersionArray[1].equals(otherVersionArray[1])) {
202 return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
205 if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
206 && !thisVersionArray[2].equals(otherVersionArray[2])) {
207 return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
214 public int getMajorVersion() {
215 final String[] versionArray = getVersion().split("\\.");
217 // There must always be at least one element in each version
218 return Integer.parseInt(versionArray[0]);
222 public int getMinorVersion() {
223 final String[] versionArray = getVersion().split("\\.");
225 if (versionArray.length >= 2) {
226 return Integer.parseInt(versionArray[1]);
233 public int getPatchVersion() {
234 final String[] versionArray = getVersion().split("\\.");
236 if (versionArray.length >= 3) {
237 return Integer.parseInt(versionArray[2]);
244 public PfValidationResult validate(final PfValidationResult result) {
245 final String nameValidationErrorMessage =
246 Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(), getNameRegEx());
247 if (nameValidationErrorMessage != null) {
248 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
249 "name invalid-" + nameValidationErrorMessage));
252 final String versionValidationErrorMessage =
253 Assertions.getStringParameterValidationMessage(VERSION_TOKEN, getVersion(), getVersionRegEx());
254 if (versionValidationErrorMessage != null) {
255 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
256 "version invalid-" + versionValidationErrorMessage));
263 public void clean() {
265 setVersion(getVersion());
269 public int compareTo(@NonNull final PfConcept otherObj) {
270 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
272 if (this == otherObj) {
275 if (getClass() != otherObj.getClass()) {
276 return getClass().getName().compareTo(otherObj.getClass().getName());
279 final PfKeyImpl other = (PfKeyImpl) otherObj;
281 if (!getName().equals(other.getName())) {
282 return getName().compareTo(other.getName());
284 return getVersion().compareTo(other.getVersion());
288 * Gets the regular expression used to validate a name.
290 * @return the regular expression used to validate a name
292 protected String getNameRegEx() {
297 * Gets the regular expression used to validate a version.
299 * @return the regular expression used to validate a version
301 protected String getVersionRegEx() {
302 return VERSION_REGEXP;
306 * Gets the regular expression used to validate a key id.
308 * @return the regular expression used to validate a key id
310 protected String getKeyIdRegEx() {
311 return KEY_ID_REGEXP;