Add DAO module for Models
[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
32 import org.onap.policy.common.utils.validation.Assertions;
33 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
34
35 /**
36  * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system
37  * must have an {@link PfConceptKey} to identify it. Concepts that are wholly contained in another concept are
38  * identified using a {@link PfReferenceKey} key.
39  *
40  * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
41  * regular expressions respectively.
42  */
43 @Embeddable
44 @Data
45 @EqualsAndHashCode(callSuper = false)
46 public class PfConceptKey extends PfKey {
47     private static final long serialVersionUID = 8932717618579392561L;
48
49     private static final String NAME_TOKEN = "name";
50     private static final String VERSION_TOKEN = "version";
51
52     @Column(name = NAME_TOKEN)
53     private String name;
54
55     @Column(name = VERSION_TOKEN)
56     private String version;
57
58     /**
59      * The default constructor creates a null concept key.
60      */
61     public PfConceptKey() {
62         this(NULL_KEY_NAME, NULL_KEY_VERSION);
63     }
64
65     /**
66      * Copy constructor.
67      *
68      * @param copyConcept the concept to copy from
69      */
70     public PfConceptKey(final PfConceptKey copyConcept) {
71         super(copyConcept);
72     }
73
74     /**
75      * Constructor to create a key with the specified name and version.
76      *
77      * @param name the key name
78      * @param version the key version
79      */
80     public PfConceptKey(final String name, final String version) {
81         super();
82         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
83         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
84     }
85
86     /**
87      * Constructor to create a key using the key and version from the specified key ID.
88      *
89      * @param id the key ID in a format that respects the KEY_ID_REGEXP
90      */
91     public PfConceptKey(final String id) {
92         Assertions.argumentNotNull(id, "id may not be null");
93
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 PfKey.Compatibility getCompatibility(final PfKey otherKey) {
135         if (!(otherKey instanceof PfConceptKey)) {
136             return Compatibility.DIFFERENT;
137         }
138         final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
139
140         if (this.equals(otherConceptKey)) {
141             return Compatibility.IDENTICAL;
142         }
143         if (!this.getName().equals(otherConceptKey.getName())) {
144             return Compatibility.DIFFERENT;
145         }
146
147         final String[] thisVersionArray = getVersion().split("\\.");
148         final String[] otherVersionArray = otherConceptKey.getVersion().split("\\.");
149
150         // There must always be at least one element in each version
151         if (!thisVersionArray[0].equals(otherVersionArray[0])) {
152             return Compatibility.MAJOR;
153         }
154
155         if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2
156                         && !thisVersionArray[1].equals(otherVersionArray[1])) {
157             return Compatibility.MINOR;
158         }
159
160         return Compatibility.PATCH;
161     }
162
163     @Override
164     public boolean isCompatible(final PfKey otherKey) {
165         if (!(otherKey instanceof PfConceptKey)) {
166             return false;
167         }
168         final PfConceptKey otherConceptKey = (PfConceptKey) otherKey;
169
170         final Compatibility compatibility = this.getCompatibility(otherConceptKey);
171
172         return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR);
173     }
174
175     @Override
176     public PfValidationResult validate(final PfValidationResult result) {
177         final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
178                         NAME_REGEXP);
179         if (nameValidationErrorMessage != null) {
180             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
181                             "name invalid-" + nameValidationErrorMessage));
182         }
183
184         final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
185                         version, VERSION_REGEXP);
186         if (versionValidationErrorMessage != null) {
187             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
188                             "version invalid-" + versionValidationErrorMessage));
189         }
190
191         return result;
192     }
193
194     @Override
195     public void clean() {
196         name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
197         version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
198     }
199
200     @Override
201     public PfConcept copyTo(final PfConcept target) {
202         Assertions.argumentNotNull(target, "target may not be null");
203
204         final PfConcept copyObject = target;
205         Assertions.instanceOf(copyObject, PfConceptKey.class);
206
207         final PfConceptKey copy = ((PfConceptKey) copyObject);
208         copy.setName(name);
209         copy.setVersion(version);
210
211         return copyObject;
212     }
213
214     @Override
215     public int compareTo(final PfConcept otherObj) {
216         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
217
218         if (this == otherObj) {
219             return 0;
220         }
221         if (getClass() != otherObj.getClass()) {
222             return this.hashCode() - otherObj.hashCode();
223         }
224
225         final PfConceptKey other = (PfConceptKey) otherObj;
226
227         if (!name.equals(other.name)) {
228             return name.compareTo(other.name);
229         }
230         return version.compareTo(other.version);
231     }
232 }