2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
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.apex.model.basicmodel.concepts;
24 import java.util.ArrayList;
25 import java.util.List;
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 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
34 import org.onap.policy.common.utils.validation.Assertions;
37 * A reference key identifies entities in the system that are contained in other entities. Every contained concept in
38 * the system must have an {@link AxReferenceKey} to identify it. Non-contained first order concepts are identified
39 * using an {@link AxArtifactKey} key.
41 * <p>An {@link AxReferenceKey} contains an {@link AxArtifactKey} key reference to the first order entity that contains
42 * it. The local name of the reference key must uniquely identify the referenced concept among those concepts contained
43 * in the reference key's parent. In other words, if a parent concept has more than one child, the local name in the key
44 * of all its children must be unique.
46 * <p>If a reference key's parent is itself a reference key, then the parent's local name must be set in the reference
47 * key. If the parent is a first order concept, then the parent's local name in the key will be set to NULL.
49 * <p>Key validation checks that the parent name and parent version fields match the NAME_REGEXP and
50 * VERSION_REGEXP regular expressions respectively and that the local name fields match the
51 * LOCAL_NAME_REGEXP regular expression.
54 @XmlAccessorType(XmlAccessType.FIELD)
55 @XmlRootElement(name = "apexReferenceKey", namespace = "http://www.onap.org/policy/apex-pdp")
56 @XmlType(name = "AxReferenceKey", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = { "parentKeyName",
57 "parentKeyVersion", "parentLocalName", "localName" })
59 public class AxReferenceKey extends AxKey {
60 private static final String PARENT_KEY_NAME = "parentKeyName";
61 private static final String PARENT_KEY_VERSION = "parentKeyVersion";
62 private static final String PARENT_LOCAL_NAME = "parentLocalName";
63 private static final String LOCAL_NAME = "localName";
65 private static final long serialVersionUID = 8932717618579392561L;
67 /** Regular expression to specify the structure of local names in reference keys. */
68 public static final String LOCAL_NAME_REGEXP = "[A-Za-z0-9\\-_\\.]+|^$";
70 /** Regular expression to specify the structure of IDs in reference keys. */
71 public static final String REFERENCE_KEY_ID_REGEXP =
72 "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+";
74 private static final int PARENT_NAME_FIELD = 0;
75 private static final int PARENT_VERSION_FIELD = 1;
76 private static final int PARENT_LOCAL_NAME_FIELD = 2;
77 private static final int LOCAL_NAME_FIELD = 3;
79 @Column(name = PARENT_KEY_NAME)
80 @XmlElement(required = true)
81 private String parentKeyName;
83 @Column(name = PARENT_KEY_VERSION)
84 @XmlElement(required = true)
85 private String parentKeyVersion;
87 @Column(name = PARENT_LOCAL_NAME)
88 @XmlElement(required = true)
89 private String parentLocalName;
91 @Column(name = LOCAL_NAME)
92 @XmlElement(required = true)
93 private String localName;
96 * The default constructor creates a null reference key.
98 public AxReferenceKey() {
99 this(NULL_KEY_NAME, NULL_KEY_VERSION, NULL_KEY_NAME, NULL_KEY_NAME);
103 * The Copy Constructor creates a key by copying another key.
105 * @param referenceKey
106 * the reference key to copy from
108 public AxReferenceKey(final AxReferenceKey referenceKey) {
109 this(referenceKey.getParentKeyName(), referenceKey.getParentKeyVersion(), referenceKey.getParentLocalName(),
110 referenceKey.getLocalName());
114 * Constructor to create a null reference key for the specified parent artifact key.
116 * @param axArtifactKey
117 * the parent artifact key of this reference key
119 public AxReferenceKey(final AxArtifactKey axArtifactKey) {
120 this(axArtifactKey.getName(), axArtifactKey.getVersion(), NULL_KEY_NAME, NULL_KEY_NAME);
124 * Constructor to create a reference key for the given parent artifact key with the given local name.
126 * @param axArtifactKey
127 * the parent artifact key of this reference key
129 * the local name of this reference key
131 public AxReferenceKey(final AxArtifactKey axArtifactKey, final String localName) {
132 this(axArtifactKey, NULL_KEY_NAME, localName);
136 * Constructor to create a reference key for the given parent reference key with the given local name.
138 * @param parentReferenceKey
139 * the parent reference key of this reference key
141 * the local name of this reference key
143 public AxReferenceKey(final AxReferenceKey parentReferenceKey, final String localName) {
144 this(parentReferenceKey.getParentArtifactKey(), parentReferenceKey.getLocalName(), localName);
148 * Constructor to create a reference key for the given parent reference key (specified by the parent reference key's
149 * artifact key and local name) with the given local name.
151 * @param axArtifactKey
152 * the artifact key of the parent reference key of this reference key
153 * @param parentLocalName
154 * the local name of the parent reference key of this reference key
156 * the local name of this reference key
158 public AxReferenceKey(final AxArtifactKey axArtifactKey, final String parentLocalName, final String localName) {
159 this(axArtifactKey.getName(), axArtifactKey.getVersion(), parentLocalName, localName);
163 * Constructor to create a reference key for the given parent artifact key (specified by the parent artifact key's
164 * name and version) with the given local name.
166 * @param parentKeyName
167 * the name of the parent artifact key of this reference key
168 * @param parentKeyVersion
169 * the version of the parent artifact key of this reference key
171 * the local name of this reference key
173 public AxReferenceKey(final String parentKeyName, final String parentKeyVersion, final String localName) {
174 this(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName);
178 * Constructor to create a reference key for the given parent key (specified by the parent key's name, version nad
179 * local name) with the given local name.
181 * @param parentKeyName
182 * the parent key name of this reference key
183 * @param parentKeyVersion
184 * the parent key version of this reference key
185 * @param parentLocalName
186 * the parent local name of this reference key
188 * the local name of this reference key
190 public AxReferenceKey(final String parentKeyName, final String parentKeyVersion, final String parentLocalName,
191 final String localName) {
193 this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP);
194 this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion,
196 this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName,
198 this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP);
202 * Constructor to create a key from the specified key ID.
205 * the key ID in a format that respects the KEY_ID_REGEXP
207 public AxReferenceKey(final String id) {
208 final String conditionedId = Assertions.validateStringParameter("id", id, REFERENCE_KEY_ID_REGEXP);
210 // Split on colon, if the id passes the regular expression test above
211 // it'll have just three colons separating the parent name,
212 // parent version, parent local name, and and local name
213 // No need for range checks or size checks on the array
214 final String[] nameVersionNameArray = conditionedId.split(":");
216 // Initiate the new key
217 parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, nameVersionNameArray[PARENT_NAME_FIELD],
219 parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION,
220 nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP);
221 parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME,
222 nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP);
223 localName = Assertions.validateStringParameter(LOCAL_NAME, nameVersionNameArray[LOCAL_NAME_FIELD],
228 * Get a null reference key.
230 * @return a null reference key
232 public static AxReferenceKey getNullKey() {
233 return new AxReferenceKey(AxKey.NULL_KEY_NAME, AxKey.NULL_KEY_VERSION, AxKey.NULL_KEY_NAME,
234 AxKey.NULL_KEY_NAME);
241 public AxReferenceKey getKey() {
249 public List<AxKey> getKeys() {
250 final List<AxKey> keyList = new ArrayList<>();
251 keyList.add(getKey());
259 public String getId() {
260 return parentKeyName + ':' + parentKeyVersion + ':' + parentLocalName + ':' + localName;
264 * Gets the parent artifact key of this reference key.
266 * @return the parent artifact key of this reference key
268 public AxArtifactKey getParentArtifactKey() {
269 return new AxArtifactKey(parentKeyName, parentKeyVersion);
273 * Gets the parent reference key of this reference key.
275 * @return the parent reference key of this reference key
277 public AxReferenceKey getParentReferenceKey() {
278 return new AxReferenceKey(parentKeyName, parentKeyVersion, parentLocalName);
282 * Sets the parent artifact key of this reference key.
285 * the parent artifact key of this reference key
287 public void setParentArtifactKey(final AxArtifactKey parentKey) {
288 Assertions.argumentNotNull(parentKey, "parentKey may not be null");
290 parentKeyName = parentKey.getName();
291 parentKeyVersion = parentKey.getVersion();
292 parentLocalName = NULL_KEY_NAME;
296 * Sets the parent reference key of this reference key.
299 * the parent reference key of this reference key
301 public void setParentReferenceKey(final AxReferenceKey parentKey) {
302 Assertions.argumentNotNull(parentKey, "parentKey may not be null");
304 parentKeyName = parentKey.getParentKeyName();
305 parentKeyVersion = parentKey.getParentKeyVersion();
306 parentLocalName = parentKey.getLocalName();
310 * Gets the parent key name of this reference key.
312 * @return the parent key name of this reference key
314 public String getParentKeyName() {
315 return parentKeyName;
319 * Sets the parent key name of this reference key.
321 * @param parentKeyName
322 * the parent key name of this reference key
324 public void setParentKeyName(final String parentKeyName) {
325 this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP);
329 * Gets the parent key version of this reference key.
331 * @return the parent key version of this reference key
333 public String getParentKeyVersion() {
334 return parentKeyVersion;
338 * Sets the parent key version of this reference key.
340 * @param parentKeyVersion
341 * the parent key version of this reference key
343 public void setParentKeyVersion(final String parentKeyVersion) {
344 this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion,
349 * Gets the parent local name of this reference key.
351 * @return the parent local name of this reference key
353 public String getParentLocalName() {
354 return parentLocalName;
358 * Sets the parent local name of this reference key.
360 * @param parentLocalName
361 * the parent local name of this reference key
363 public void setParentLocalName(final String parentLocalName) {
364 this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName,
369 * Gets the local name of this reference key.
371 * @return the local name of this reference key
373 public String getLocalName() {
378 * Sets the local name of this reference key.
381 * the local name of this reference key
383 public void setLocalName(final String localName) {
384 this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP);
391 public AxKey.Compatibility getCompatibility(final AxKey otherKey) {
392 if (!(otherKey instanceof AxReferenceKey)) {
393 return Compatibility.DIFFERENT;
395 final AxReferenceKey otherReferenceKey = (AxReferenceKey) otherKey;
397 return this.getParentArtifactKey().getCompatibility(otherReferenceKey.getParentArtifactKey());
404 public boolean isCompatible(final AxKey otherKey) {
405 if (!(otherKey instanceof AxReferenceKey)) {
408 final AxReferenceKey otherReferenceKey = (AxReferenceKey) otherKey;
410 return this.getParentArtifactKey().isCompatible(otherReferenceKey.getParentArtifactKey());
417 public AxValidationResult validate(final AxValidationResult result) {
418 final String parentNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(PARENT_KEY_NAME,
419 parentKeyName, NAME_REGEXP);
420 if (parentNameValidationErrorMessage != null) {
421 result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID,
422 "parentKeyName invalid-" + parentNameValidationErrorMessage));
425 final String parentKeyVersionValidationErrorMessage = Assertions
426 .getStringParameterValidationMessage(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP);
427 if (parentKeyVersionValidationErrorMessage != null) {
428 result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID,
429 "parentKeyVersion invalid-" + parentKeyVersionValidationErrorMessage));
432 final String parentLocalNameValidationErrorMessage = Assertions
433 .getStringParameterValidationMessage(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP);
434 if (parentLocalNameValidationErrorMessage != null) {
435 result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID,
436 "parentLocalName invalid-" + parentLocalNameValidationErrorMessage));
439 final String localNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(LOCAL_NAME,
440 localName, LOCAL_NAME_REGEXP);
441 if (localNameValidationErrorMessage != null) {
442 result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID,
443 "localName invalid-" + localNameValidationErrorMessage));
453 public void clean() {
454 parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP);
455 parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP);
456 parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP);
457 localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP);
464 public String toString() {
465 final StringBuilder builder = new StringBuilder();
466 builder.append(this.getClass().getSimpleName());
467 builder.append(":(");
468 builder.append("parentKeyName=");
469 builder.append(parentKeyName);
470 builder.append(",parentKeyVersion=");
471 builder.append(parentKeyVersion);
472 builder.append(",parentLocalName=");
473 builder.append(parentLocalName);
474 builder.append(",localName=");
475 builder.append(localName);
477 return builder.toString();
484 public AxConcept copyTo(final AxConcept target) {
485 Assertions.argumentNotNull(target, "target may not be null");
487 final Object copyObject = target;
488 Assertions.instanceOf(copyObject, AxReferenceKey.class);
490 final AxReferenceKey copy = ((AxReferenceKey) copyObject);
491 copy.setParentKeyName(parentKeyName);
492 copy.setParentKeyVersion(parentKeyVersion);
493 copy.setLocalName(localName);
494 copy.setParentLocalName(parentLocalName);
503 public int hashCode() {
504 final int prime = 31;
506 result = prime * result + parentKeyName.hashCode();
507 result = prime * result + parentKeyVersion.hashCode();
508 result = prime * result + parentLocalName.hashCode();
509 result = prime * result + localName.hashCode();
517 public boolean equals(final Object obj) {
519 throw new IllegalArgumentException("comparison object may not be null");
526 if (getClass() != obj.getClass()) {
530 final AxReferenceKey other = (AxReferenceKey) obj;
532 if (!parentKeyName.equals(other.parentKeyName)) {
535 if (!parentKeyVersion.equals(other.parentKeyVersion)) {
538 if (!parentLocalName.equals(other.parentLocalName)) {
541 return localName.equals(other.localName);
548 public int compareTo(final AxConcept otherObj) {
549 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
551 if (this == otherObj) {
554 if (getClass() != otherObj.getClass()) {
555 return this.hashCode() - otherObj.hashCode();
558 final AxReferenceKey other = (AxReferenceKey) otherObj;
559 if (!parentKeyName.equals(other.parentKeyName)) {
560 return parentKeyName.compareTo(other.parentKeyName);
562 if (!parentKeyVersion.equals(other.parentKeyVersion)) {
563 return parentKeyVersion.compareTo(other.parentKeyVersion);
565 if (!parentLocalName.equals(other.parentLocalName)) {
566 return parentLocalName.compareTo(other.parentLocalName);
568 return localName.compareTo(other.localName);