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