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