61028f9276c4b3e8db64cd40a040efb428fc9370
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKeyImpl.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import lombok.Getter;
28 import lombok.NonNull;
29 import lombok.ToString;
30
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
33
34 /**
35  * A key, upon which other key subclasses can be built, providing implementations of the methods.
36  */
37 @Getter
38 @ToString
39 public abstract class PfKeyImpl extends PfKey {
40     private static final long serialVersionUID = 8932717618579392561L;
41
42     public static final String NAME_TOKEN = "name";
43     public static final String VERSION_TOKEN = "version";
44
45     /**
46      * The default constructor creates a null concept key.
47      */
48     public PfKeyImpl() {
49         this(NULL_KEY_NAME, NULL_KEY_VERSION);
50     }
51
52     /**
53      * Copy constructor.
54      *
55      * @param copyConcept the concept to copy from
56      */
57     public PfKeyImpl(final PfKeyImpl copyConcept) {
58         super(copyConcept);
59         setName(copyConcept.getName());
60         setVersion(copyConcept.getVersion());
61     }
62
63     /**
64      * Constructor to create a key with the specified name and version.
65      *
66      * @param name the key name
67      * @param version the key version
68      */
69     public PfKeyImpl(@NonNull final String name, @NonNull final String version) {
70         super();
71         setName(name);
72         setVersion(version);
73     }
74
75     /**
76      * Constructor to create a key using the key and version from the specified key ID.
77      *
78      * @param id the key ID in a format that respects the KEY_ID_REGEXP
79      */
80     public PfKeyImpl(@NonNull final String id) {
81         // Check the incoming ID is valid
82         Assertions.validateStringParameter("id", id, getKeyIdRegEx());
83
84         // Split on colon, if the id passes the regular expression test above
85         // it'll have just one colon separating the name and version
86         // No need for range checks or size checks on the array
87         final String[] nameVersionArray = id.split(":");
88
89         // Return the new key
90         setName(nameVersionArray[0]);
91         setVersion(nameVersionArray[1]);
92     }
93
94     public abstract void setName(@NonNull final String name);
95
96     public abstract void setVersion(@NonNull final String version);
97
98     @Override
99     public PfKeyImpl getKey() {
100         return this;
101     }
102
103     @Override
104     public List<PfKey> getKeys() {
105         final List<PfKey> keyList = new ArrayList<>();
106         keyList.add(getKey());
107         return keyList;
108     }
109
110     @Override
111     public String getId() {
112         return getName() + ':' + getVersion();
113     }
114
115     @Override
116     public boolean isNullKey() {
117         return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion()));
118     }
119
120     /**
121      * Determines if the name is "null".
122      *
123      * @return {@code true} if the name is null, {@code false} otherwise
124      */
125     public boolean isNullName() {
126         return PfKey.NULL_KEY_NAME.equals(getName());
127     }
128
129     /**
130      * Determines if the version is "null".
131      *
132      * @return {@code true} if the version is null, {@code false} otherwise
133      */
134     public boolean isNullVersion() {
135         return PfKey.NULL_KEY_VERSION.equals(getVersion());
136     }
137
138     @Override
139     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
140         if (!(otherKey instanceof PfKeyImpl)) {
141             return Compatibility.DIFFERENT;
142         }
143         final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
144
145         if (this.equals(otherConceptKey)) {
146             return Compatibility.IDENTICAL;
147         }
148         if (!this.getName().equals(otherConceptKey.getName())) {
149             return Compatibility.DIFFERENT;
150         }
151
152         final String[] thisVersionArray = getVersion().split("\\.");
153         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
154
155         // There must always be at least one element in each version
156         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
157             return Compatibility.MAJOR;
158         }
159
160         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
161             && !thisVersionArray[1].equals(otherVersionArray[1])) {
162             return Compatibility.MINOR;
163         }
164
165         return Compatibility.PATCH;
166     }
167
168     @Override
169     public boolean isCompatible(@NonNull final PfKey otherKey) {
170         if (!(otherKey instanceof PfKeyImpl)) {
171             return false;
172         }
173         final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
174
175         final Compatibility compatibility = this.getCompatibility(otherConceptKey);
176
177         return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
178     }
179
180     @Override
181     public boolean isNewerThan(@NonNull final PfKey otherKey) {
182         Assertions.instanceOf(otherKey, PfKeyImpl.class);
183
184         final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey;
185
186         if (this.equals(otherConceptKey)) {
187             return false;
188         }
189
190         if (!this.getName().equals(otherConceptKey.getName())) {
191             return this.getName().compareTo(otherConceptKey.getName()) > 0;
192         }
193
194         final String[] thisVersionArray = getVersion().split("\\.");
195         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
196
197         // There must always be at least one element in each version
198         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
199             return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]);
200         }
201
202         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
203             && !thisVersionArray[1].equals(otherVersionArray[1])) {
204             return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]);
205         }
206
207         if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3
208             && !thisVersionArray[2].equals(otherVersionArray[2])) {
209             return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]);
210         }
211
212         return false;
213     }
214
215     @Override
216     public int getMajorVersion() {
217         final String[] versionArray = getVersion().split("\\.");
218
219         // There must always be at least one element in each version
220         return Integer.parseInt(versionArray[0]);
221     }
222
223     @Override
224     public int getMinorVersion() {
225         final String[] versionArray = getVersion().split("\\.");
226
227         if (versionArray.length >= 2) {
228             return Integer.parseInt(versionArray[1]);
229         } else {
230             return 0;
231         }
232     }
233
234     @Override
235     public int getPatchVersion() {
236         final String[] versionArray = getVersion().split("\\.");
237
238         if (versionArray.length >= 3) {
239             return Integer.parseInt(versionArray[2]);
240         } else {
241             return 0;
242         }
243     }
244
245     @Override
246     public PfValidationResult validate(final PfValidationResult result) {
247         final String nameValidationErrorMessage =
248             Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(), getNameRegEx());
249         if (nameValidationErrorMessage != null) {
250             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
251                 "name invalid-" + nameValidationErrorMessage));
252         }
253
254         final String versionValidationErrorMessage =
255             Assertions.getStringParameterValidationMessage(VERSION_TOKEN, getVersion(), getVersionRegEx());
256         if (versionValidationErrorMessage != null) {
257             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
258                 "version invalid-" + versionValidationErrorMessage));
259         }
260
261         return result;
262     }
263
264     @Override
265     public void clean() {
266         setName(getName());
267         setVersion(getVersion());
268     }
269
270     @Override
271     public int compareTo(@NonNull final PfConcept otherObj) {
272         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
273
274         if (this == otherObj) {
275             return 0;
276         }
277         if (getClass() != otherObj.getClass()) {
278             return getClass().getName().compareTo(otherObj.getClass().getName());
279         }
280
281         final PfKeyImpl other = (PfKeyImpl) otherObj;
282
283         if (!getName().equals(other.getName())) {
284             return getName().compareTo(other.getName());
285         }
286         return getVersion().compareTo(other.getVersion());
287     }
288
289     /**
290      * Gets the regular expression used to validate a name.
291      *
292      * @return the regular expression used to validate a name
293      */
294     protected String getNameRegEx() {
295         return NAME_REGEXP;
296     }
297
298     /**
299      * Gets the regular expression used to validate a version.
300      *
301      * @return the regular expression used to validate a version
302      */
303     protected String getVersionRegEx() {
304         return VERSION_REGEXP;
305     }
306
307     /**
308      * Gets the regular expression used to validate a key id.
309      *
310      * @return the regular expression used to validate a key id
311      */
312     protected String getKeyIdRegEx() {
313         return KEY_ID_REGEXP;
314     }
315 }