2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 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.List;
26 import java.util.TreeSet;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.Table;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.common.parameters.ValidationStatus;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.utils.validation.Assertions;
39 import org.onap.policy.models.base.validation.annotations.VerifyKey;
42 * This class is the base class for all models in the Policy Framework. All model classes inherit
43 * from this model so all models must have a key and have key information.
45 * <p>Validation checks that the model key is valid. It goes on to check for null keys and checks
46 * each key for uniqueness in the model. A check is carried out to ensure that an {@link PfKeyInfo}
47 * instance exists for every {@link PfConceptKey} key. For each {@link PfReferenceKey} instance, a
48 * check is made that its parent and local name are nut null and that a {@link PfKeyInfo} entry
49 * exists for its parent. Then a check is made that each used {@link PfConceptKey} and
50 * {@link PfReferenceKey} usage references a key that exists. Finally, a check is made to ensure
51 * that an {@link PfConceptKey} instance exists for every {@link PfKeyInfo} instance.
53 * @param <C> the type of concept on which the interface is applied.
57 @Table(name = "PfModel")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = false)
61 public abstract class PfModel extends PfConcept {
62 private static final String KEYS_TOKEN = "keys";
64 private static final long serialVersionUID = -771659065637205430L;
69 private PfConceptKey key;
72 * The Default Constructor creates this concept with a NULL artifact key.
75 this(new PfConceptKey());
79 * Constructor to create this concept with the specified key.
81 * @param key the key of this concept
83 protected PfModel(@NonNull final PfConceptKey key) {
85 Assertions.argumentNotNull(key, "key may not be null");
93 * @param copyConcept the concept to copy from
95 protected PfModel(@NonNull final PfModel copyConcept) {
97 this.key = new PfConceptKey(copyConcept.key);
101 * Registers this model with the {@link PfModelService}. All models are registered with the
102 * model service so that models can be references from anywhere in the Policy Framework system
103 * without being passed as references through deep call chains.
105 public abstract void register();
108 public List<PfKey> getKeys() {
109 return key.getKeys();
113 public void clean() {
118 public BeanValidationResult validate(@NonNull String fieldName) {
119 BeanValidationResult result = new PfValidator().validateTop(fieldName, this);
121 // Key consistency check
122 final Set<PfConceptKey> artifactKeySet = new TreeSet<>();
123 final Set<PfReferenceKey> referenceKeySet = new TreeSet<>();
124 final Set<PfKeyUse> usedKeySet = new TreeSet<>();
126 for (final PfKey pfKey : this.getKeys()) {
127 // Check for the two type of keys we have
128 if (pfKey instanceof PfConceptKey) {
129 validateArtifactKeyInModel((PfConceptKey) pfKey, artifactKeySet, result);
130 } else if (pfKey instanceof PfReferenceKey) {
131 validateReferenceKeyInModel((PfReferenceKey) pfKey, referenceKeySet, result);
133 // It must be a PfKeyUse, nothing else is legal
134 usedKeySet.add((PfKeyUse) pfKey);
138 // Check all reference keys have correct parent keys
139 for (final PfReferenceKey referenceKey : referenceKeySet) {
140 if (!artifactKeySet.contains(referenceKey.getParentConceptKey())) {
141 addResult(result, "reference key", referenceKey, "parent artifact key not found");
145 validateKeyUses(usedKeySet, artifactKeySet, referenceKeySet, result);
151 * Check for consistent usage of an artifact key in the model.
153 * @param artifactKey The artifact key to check
154 * @param artifactKeySet The set of artifact keys encountered so far, this key is appended to
156 * @param result where to add the results
158 private void validateArtifactKeyInModel(final PfConceptKey artifactKey,
159 final Set<PfConceptKey> artifactKeySet, final BeanValidationResult result) {
161 validateKeyNotNull(result, KEYS_TOKEN, artifactKey);
163 BeanValidationResult result2 = new BeanValidationResult(KEYS_TOKEN, artifactKey);
165 // Null key name start check
166 if (artifactKey.getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
167 addResult(result2, "name of " + artifactKey.getId(), artifactKey.getName(),
168 "starts with keyword " + PfKey.NULL_KEY_NAME);
172 if (artifactKeySet.contains(artifactKey)) {
173 addResult(result, KEYS_TOKEN, artifactKey, "duplicate key");
175 artifactKeySet.add(artifactKey);
180 * Check for consistent usage of a reference key in the model.
182 * @param referenceKey The reference key to check
183 * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
185 * @param result where to add the results
187 private void validateReferenceKeyInModel(final PfReferenceKey referenceKey,
188 final Set<PfReferenceKey> referenceKeySet, final BeanValidationResult result) {
190 if (referenceKey.isNullKey()) {
191 addResult(result, KEYS_TOKEN, referenceKey, IS_A_NULL_KEY);
194 BeanValidationResult result2 = new BeanValidationResult(KEYS_TOKEN, referenceKey);
196 // Null parent key check
197 if (referenceKey.getParentConceptKey().isNullKey()) {
198 addResult(result2, "parent key of " + referenceKey.getId(), referenceKey.getParentConceptKey().getId(),
202 // Null local name check
203 if (referenceKey.getLocalName().equals(PfKey.NULL_KEY_NAME)) {
204 addResult(result2, "local name of " + referenceKey.getId(), referenceKey.getLocalName(), IS_NULL);
207 // Null key name start check
208 if (referenceKey.getParentConceptKey().getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
209 addResult(result2, "parent name of " + referenceKey.getId(), referenceKey.getParentConceptKey().getName(),
210 "starts with keyword " + PfKey.NULL_KEY_NAME);
214 if (referenceKeySet.contains(referenceKey)) {
215 addResult(result, KEYS_TOKEN, referenceKey, "duplicate key");
217 referenceKeySet.add(referenceKey);
222 * Check for consistent usage of cross-key references in the model.
224 * @param usedKeySet The set of all keys used in the model
225 * @param artifactKeySet The set of artifact keys encountered so far, this key is appended to
227 * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
229 * @param result where to add the results
231 private void validateKeyUses(final Set<PfKeyUse> usedKeySet, final Set<PfConceptKey> artifactKeySet,
232 final Set<PfReferenceKey> referenceKeySet, final BeanValidationResult result) {
233 // Check all key uses
234 for (final PfKeyUse usedKey : usedKeySet) {
235 if (usedKey.getKey() instanceof PfConceptKey) {
236 // PfConceptKey usage, check the key exists
237 if (!artifactKeySet.contains(usedKey.getKey())) {
238 result.addResult("artifact key", usedKey.getId(), ValidationStatus.INVALID, NOT_DEFINED);
241 // PfReferenceKey usage, check the key exists
242 if (!referenceKeySet.contains(usedKey.getKey())) {
243 result.addResult("reference key", usedKey.getId(), ValidationStatus.INVALID, NOT_DEFINED);
250 public int compareTo(final PfConcept otherObj) {
251 if (otherObj == null) {
254 if (this == otherObj) {
257 if (getClass() != otherObj.getClass()) {
258 return getClass().getName().compareTo(otherObj.getClass().getName());
261 final PfModel other = (PfModel) otherObj;
263 return key.compareTo(other.key);