2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.Embeddable;
29 import lombok.EqualsAndHashCode;
30 import lombok.NonNull;
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
35 * A reference key identifies entities in the system that are contained in other entities. Every contained concept in
36 * the system must have an {@link PfReferenceKey} to identify it. Non-contained first order concepts are identified
37 * using an {@link PfConceptKey} key.
39 * <p>An {@link PfReferenceKey} contains an {@link PfConceptKey} key reference to the first order entity that contains
40 * it. The local name of the reference key must uniquely identify the referenced concept among those concepts contained
41 * in the reference key's parent. In other words, if a parent concept has more than one child, the local name in the key
42 * of all its children must be unique.
44 * <p>If a reference key's parent is itself a reference key, then the parent's local name must be set in the reference
45 * key. If the parent is a first order concept, then the parent's local name in the key will be set to NULL.
47 * <p>Key validation checks that the parent name and parent version fields match the NAME_REGEXP and
48 * VERSION_REGEXP regular expressions respectively and that the local name fields match the
49 * LOCAL_NAME_REGEXP regular expression.
53 @EqualsAndHashCode(callSuper = false)
54 public class PfReferenceKey extends PfKey {
55 private static final String PARENT_KEY_NAME = "parentKeyName";
56 private static final String PARENT_KEY_VERSION = "parentKeyVersion";
57 private static final String PARENT_LOCAL_NAME = "parentLocalName";
58 private static final String LOCAL_NAME = "localName";
60 private static final long serialVersionUID = 8932717618579392561L;
62 /** Regular expression to specify the structure of local names in reference keys. */
63 public static final String LOCAL_NAME_REGEXP = "[A-Za-z0-9\\-_\\.]+|^$";
65 /** Regular expression to specify the structure of IDs in reference keys. */
66 public static final String REFERENCE_KEY_ID_REGEXP =
67 "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+";
69 private static final int PARENT_NAME_FIELD = 0;
70 private static final int PARENT_VERSION_FIELD = 1;
71 private static final int PARENT_LOCAL_NAME_FIELD = 2;
72 private static final int LOCAL_NAME_FIELD = 3;
74 @Column(name = PARENT_KEY_NAME, length = 120)
75 private String parentKeyName;
77 @Column(name = PARENT_KEY_VERSION, length = 15)
78 private String parentKeyVersion;
80 @Column(name = PARENT_LOCAL_NAME, length = 120)
81 private String parentLocalName;
83 @Column(name = LOCAL_NAME, length = 120)
84 private String localName;
87 * The default constructor creates a null reference key.
89 public PfReferenceKey() {
90 this(NULL_KEY_NAME, NULL_KEY_VERSION, NULL_KEY_NAME, NULL_KEY_NAME);
94 * The Copy Constructor creates a key by copying another key.
97 * the reference key to copy from
99 public PfReferenceKey(final PfReferenceKey referenceKey) {
100 this(referenceKey.getParentKeyName(), referenceKey.getParentKeyVersion(), referenceKey.getParentLocalName(),
101 referenceKey.getLocalName());
105 * Constructor to create a null reference key for the specified parent concept key.
107 * @param pfConceptKey
108 * the parent concept key of this reference key
110 public PfReferenceKey(final PfConceptKey pfConceptKey) {
111 this(pfConceptKey.getName(), pfConceptKey.getVersion(), NULL_KEY_NAME, NULL_KEY_NAME);
115 * Constructor to create a reference key for the given parent concept key with the given local name.
117 * @param pfConceptKey
118 * the parent concept key of this reference key
120 * the local name of this reference key
122 public PfReferenceKey(final PfConceptKey pfConceptKey, final String localName) {
123 this(pfConceptKey, NULL_KEY_NAME, localName);
127 * Constructor to create a reference key for the given parent reference key with the given local name.
129 * @param parentReferenceKey
130 * the parent reference key of this reference key
132 * the local name of this reference key
134 public PfReferenceKey(final PfReferenceKey parentReferenceKey, final String localName) {
135 this(parentReferenceKey.getParentConceptKey(), parentReferenceKey.getLocalName(), localName);
139 * Constructor to create a reference key for the given parent reference key (specified by the parent reference key's
140 * concept key and local name) with the given local name.
142 * @param pfConceptKey
143 * the concept key of the parent reference key of this reference key
144 * @param parentLocalName
145 * the local name of the parent reference key of this reference key
147 * the local name of this reference key
149 public PfReferenceKey(final PfConceptKey pfConceptKey, final String parentLocalName, final String localName) {
150 this(pfConceptKey.getName(), pfConceptKey.getVersion(), parentLocalName, localName);
154 * Constructor to create a reference key for the given parent concept key (specified by the parent concept key's
155 * name and version) with the given local name.
157 * @param parentKeyName
158 * the name of the parent concept key of this reference key
159 * @param parentKeyVersion
160 * the version of the parent concept key of this reference key
162 * the local name of this reference key
164 public PfReferenceKey(final String parentKeyName, final String parentKeyVersion, final String localName) {
165 this(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName);
169 * Constructor to create a reference key for the given parent key (specified by the parent key's name, version nad
170 * local name) with the given local name.
172 * @param parentKeyName
173 * the parent key name of this reference key
174 * @param parentKeyVersion
175 * the parent key version of this reference key
176 * @param parentLocalName
177 * the parent local name of this reference key
179 * the local name of this reference key
181 public PfReferenceKey(final String parentKeyName, final String parentKeyVersion, final String parentLocalName,
182 final String localName) {
184 this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP);
185 this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion,
187 this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName,
189 this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP);
193 * Constructor to create a key from the specified key ID.
196 * the key ID in a format that respects the KEY_ID_REGEXP
198 public PfReferenceKey(final String id) {
199 final String conditionedId = Assertions.validateStringParameter("id", id, REFERENCE_KEY_ID_REGEXP);
201 // Split on colon, if the id passes the regular expression test above
202 // it'll have just three colons separating the parent name,
203 // parent version, parent local name, and and local name
204 // No need for range checks or size checks on the array
205 final String[] nameVersionNameArray = conditionedId.split(":");
207 // Initiate the new key
208 parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, nameVersionNameArray[PARENT_NAME_FIELD],
210 parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION,
211 nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP);
212 parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME,
213 nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP);
214 localName = Assertions.validateStringParameter(LOCAL_NAME, nameVersionNameArray[LOCAL_NAME_FIELD],
219 * Get a null reference key.
221 * @return a null reference key
223 public static PfReferenceKey getNullKey() {
224 return new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
225 PfKey.NULL_KEY_NAME);
229 public PfReferenceKey getKey() {
234 public List<PfKey> getKeys() {
235 final List<PfKey> keyList = new ArrayList<>();
236 keyList.add(getKey());
241 public String getId() {
242 return parentKeyName + ':' + parentKeyVersion + ':' + parentLocalName + ':' + localName;
246 public boolean isNullKey() {
247 return this.equals(PfReferenceKey.getNullKey());
251 * Gets the parent concept key of this reference key.
253 * @return the parent concept key of this reference key
255 public PfConceptKey getParentConceptKey() {
256 return new PfConceptKey(parentKeyName, parentKeyVersion);
260 * Gets the parent reference key of this reference key.
262 * @return the parent reference key of this reference key
264 public PfReferenceKey getParentReferenceKey() {
265 return new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName);
269 * Sets the parent concept key of this reference key.
272 * the parent concept key of this reference key
274 public void setParentConceptKey(final PfConceptKey parentKey) {
275 Assertions.argumentNotNull(parentKey, "parentKey may not be null");
277 parentKeyName = parentKey.getName();
278 parentKeyVersion = parentKey.getVersion();
279 parentLocalName = NULL_KEY_NAME;
283 * Sets the parent reference key of this reference key.
286 * the parent reference key of this reference key
288 public void setParentReferenceKey(final PfReferenceKey parentKey) {
289 Assertions.argumentNotNull(parentKey, "parentKey may not be null");
291 parentKeyName = parentKey.getParentKeyName();
292 parentKeyVersion = parentKey.getParentKeyVersion();
293 parentLocalName = parentKey.getLocalName();
297 public PfKey.Compatibility getCompatibility(final PfKey otherKey) {
298 if (!(otherKey instanceof PfReferenceKey)) {
299 return Compatibility.DIFFERENT;
301 final PfReferenceKey otherReferenceKey = (PfReferenceKey) otherKey;
303 return this.getParentConceptKey().getCompatibility(otherReferenceKey.getParentConceptKey());
307 public boolean isCompatible(@NonNull final PfKey otherKey) {
308 if (!(otherKey instanceof PfReferenceKey)) {
311 final PfReferenceKey otherReferenceKey = (PfReferenceKey) otherKey;
313 return this.getParentConceptKey().isCompatible(otherReferenceKey.getParentConceptKey());
317 public int getMajorVersion() {
318 return this.getParentConceptKey().getMajorVersion();
322 public int getMinorVersion() {
323 return this.getParentConceptKey().getMinorVersion();
327 public int getPatchVersion() {
328 return this.getParentConceptKey().getPatchVersion();
333 public boolean isNewerThan(@NonNull final PfKey otherKey) {
334 Assertions.instanceOf(otherKey, PfReferenceKey.class);
336 final PfReferenceKey otherReferenceKey = (PfReferenceKey) otherKey;
338 return this.getParentConceptKey().isNewerThan(otherReferenceKey.getParentConceptKey());
342 public PfValidationResult validate(final PfValidationResult result) {
343 final String parentNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(PARENT_KEY_NAME,
344 parentKeyName, NAME_REGEXP);
345 if (parentNameValidationErrorMessage != null) {
346 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
347 "parentKeyName invalid-" + parentNameValidationErrorMessage));
350 final String parentKeyVersionValidationErrorMessage = Assertions
351 .getStringParameterValidationMessage(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP);
352 if (parentKeyVersionValidationErrorMessage != null) {
353 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
354 "parentKeyVersion invalid-" + parentKeyVersionValidationErrorMessage));
357 final String parentLocalNameValidationErrorMessage = Assertions
358 .getStringParameterValidationMessage(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP);
359 if (parentLocalNameValidationErrorMessage != null) {
360 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
361 "parentLocalName invalid-" + parentLocalNameValidationErrorMessage));
364 final String localNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(LOCAL_NAME,
365 localName, LOCAL_NAME_REGEXP);
366 if (localNameValidationErrorMessage != null) {
367 result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
368 "localName invalid-" + localNameValidationErrorMessage));
375 public void clean() {
376 parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP);
377 parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP);
378 parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP);
379 localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP);
383 public int compareTo(final PfConcept otherObj) {
384 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
386 if (this == otherObj) {
389 if (getClass() != otherObj.getClass()) {
390 return getClass().getName().compareTo(otherObj.getClass().getName());
393 final PfReferenceKey other = (PfReferenceKey) otherObj;
394 if (!parentKeyName.equals(other.parentKeyName)) {
395 return parentKeyName.compareTo(other.parentKeyName);
397 if (!parentKeyVersion.equals(other.parentKeyVersion)) {
398 return parentKeyVersion.compareTo(other.parentKeyVersion);
400 if (!parentLocalName.equals(other.parentLocalName)) {
401 return parentLocalName.compareTo(other.parentLocalName);
403 return localName.compareTo(other.localName);