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