Add DAO Enabled Tosca Model
[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)
54     private String name;
55
56     @Column(name = VERSION_TOKEN)
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 PfValidationResult validate(final PfValidationResult result) {
181         final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name,
182                         NAME_REGEXP);
183         if (nameValidationErrorMessage != null) {
184             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
185                             "name invalid-" + nameValidationErrorMessage));
186         }
187
188         final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN,
189                         version, VERSION_REGEXP);
190         if (versionValidationErrorMessage != null) {
191             result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
192                             "version invalid-" + versionValidationErrorMessage));
193         }
194
195         return result;
196     }
197
198     @Override
199     public void clean() {
200         name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP);
201         version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP);
202     }
203
204     @Override
205     public PfConcept copyTo(final PfConcept target) {
206         Assertions.argumentNotNull(target, "target may not be null");
207
208         final PfConcept copyObject = target;
209         Assertions.instanceOf(copyObject, PfConceptKey.class);
210
211         final PfConceptKey copy = ((PfConceptKey) copyObject);
212         copy.setName(name);
213         copy.setVersion(version);
214
215         return copyObject;
216     }
217
218     @Override
219     public int compareTo(@NonNull final PfConcept otherObj) {
220         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
221
222         if (this == otherObj) {
223             return 0;
224         }
225         if (getClass() != otherObj.getClass()) {
226             return this.hashCode() - otherObj.hashCode();
227         }
228
229         final PfConceptKey other = (PfConceptKey) otherObj;
230
231         if (!name.equals(other.name)) {
232             return name.compareTo(other.name);
233         }
234         return version.compareTo(other.version);
235     }
236 }