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