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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import java.util.List;
25 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;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
37 import org.onap.policy.common.utils.validation.Assertions;
38 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
41 * This class is the base class for all models in the Policy Framework. All model classes inherit
42 * from this model so all models must have a key and have key information.
44 * <p>Validation checks that the model key is valid. It goes on to check for null keys and checks
45 * each key for uniqueness in the model. A check is carried out to ensure that an {@link PfKeyInfo}
46 * instance exists for every {@link PfConceptKey} key. For each {@link PfReferenceKey} instance, a
47 * check is made that its parent and local name are nut null and that a {@link PfKeyInfo} entry
48 * exists for its parent. Then a check is made that each used {@link PfConceptKey} and
49 * {@link PfReferenceKey} usage references a key that exists. Finally, a check is made to ensure
50 * that an {@link PfConceptKey} instance exists for every {@link PfKeyInfo} instance.
52 * @param <C> the type of concept on which the interface is applied.
56 @Table(name = "PfModel")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = false)
60 public abstract class PfModel extends PfConcept {
61 private static final String IS_A_NULL_KEY = " is a null key";
63 private static final long serialVersionUID = -771659065637205430L;
66 private PfConceptKey key;
69 * The Default Constructor creates this concept with a NULL artifact key.
72 this(new PfConceptKey());
76 * Constructor to create this concept with the specified key.
78 * @param key the key of this concept
80 public PfModel(@NonNull final PfConceptKey key) {
82 Assertions.argumentNotNull(key, "key may not be null");
90 * @param copyConcept the concept to copy from
92 public PfModel(@NonNull final PfModel copyConcept) {
97 * Registers this model with the {@link PfModelService}. All models are registered with the
98 * model service so that models can be references from anywhere in the Policy Framework system
99 * without being passed as references through deep call chains.
101 public abstract void register();
104 public List<PfKey> getKeys() {
105 return key.getKeys();
109 public void clean() {
114 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
115 PfValidationResult result = resultIn;
117 if (key.isNullKey()) {
118 result.addValidationMessage(
119 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
122 result = key.validate(result);
124 // Key consistency check
125 final Set<PfConceptKey> artifactKeySet = new TreeSet<>();
126 final Set<PfReferenceKey> referenceKeySet = new TreeSet<>();
127 final Set<PfKeyUse> usedKeySet = new TreeSet<>();
129 for (final PfKey pfKey : this.getKeys()) {
130 // Check for the two type of keys we have
131 if (pfKey instanceof PfConceptKey) {
132 result = validateArtifactKeyInModel((PfConceptKey) pfKey, artifactKeySet, result);
133 } else if (pfKey instanceof PfReferenceKey) {
134 result = validateReferenceKeyInModel((PfReferenceKey) pfKey, referenceKeySet, result);
136 // It must be a PfKeyUse, nothing else is legal
138 usedKeySet.add((PfKeyUse) pfKey);
142 // Check all reference keys have correct parent keys
143 for (final PfReferenceKey referenceKey : referenceKeySet) {
144 if (!artifactKeySet.contains(referenceKey.getParentConceptKey())) {
145 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
146 "parent artifact key not found for reference key " + referenceKey));
150 result = validateKeyUses(usedKeySet, artifactKeySet, referenceKeySet, result);
156 * Check for consistent usage of an artifact key in the model.
158 * @param artifactKey The artifact key to check
159 * @param artifactKeySet The set of artifact keys encountered so far, this key is appended to
161 * @param result The validation result to append to
162 * @return the result of the validation
164 private PfValidationResult validateArtifactKeyInModel(final PfConceptKey artifactKey,
165 final Set<PfConceptKey> artifactKeySet, final PfValidationResult result) {
167 if (artifactKey.isNullKey()) {
168 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
169 "key " + artifactKey + IS_A_NULL_KEY));
172 // Null key name start check
173 if (artifactKey.getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
174 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
175 "key " + artifactKey + " name starts with keyword " + PfKey.NULL_KEY_NAME));
179 if (artifactKeySet.contains(artifactKey)) {
180 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
181 "duplicate key " + artifactKey + " found"));
183 artifactKeySet.add(artifactKey);
190 * Check for consistent usage of a reference key in the model.
192 * @param artifactKey The reference key to check
193 * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
195 * @param result The validation result to append to
196 * @return the result of the validation
198 private PfValidationResult validateReferenceKeyInModel(final PfReferenceKey referenceKey,
199 final Set<PfReferenceKey> referenceKeySet, final PfValidationResult result) {
201 if (referenceKey.isNullKey()) {
202 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
203 "key " + referenceKey + IS_A_NULL_KEY));
206 // Null parent key check
207 if (referenceKey.getParentConceptKey().isNullKey()) {
208 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
209 "parent artifact key of key " + referenceKey + IS_A_NULL_KEY));
212 // Null local name check
213 if (referenceKey.getLocalName().equals(PfKey.NULL_KEY_NAME)) {
214 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
215 "key " + referenceKey + " has a null local name"));
218 // Null key name start check
219 if (referenceKey.getParentConceptKey().getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
220 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
221 "key " + referenceKey + " parent name starts with keyword " + PfKey.NULL_KEY_NAME));
225 if (referenceKeySet.contains(referenceKey)) {
226 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
227 "duplicate key " + referenceKey + " found"));
229 referenceKeySet.add(referenceKey);
236 * Check for consistent usage of cross-key references in the model.
238 * @param usedKeySet The set of all keys used in the model
239 * @param artifactKeySet The set of artifact keys encountered so far, this key is appended to
241 * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
243 * @param result The validation result to append to
244 * @return the result of the validation
246 private PfValidationResult validateKeyUses(final Set<PfKeyUse> usedKeySet, final Set<PfConceptKey> artifactKeySet,
247 final Set<PfReferenceKey> referenceKeySet, final PfValidationResult result) {
248 // Check all key uses
249 for (final PfKeyUse usedKey : usedKeySet) {
250 if (usedKey.getKey() instanceof PfConceptKey) {
251 // PfConceptKey usage, check the key exists
252 if (!artifactKeySet.contains(usedKey.getKey())) {
253 result.addValidationMessage(new PfValidationMessage(usedKey.getKey(), this.getClass(),
254 ValidationResult.INVALID, "an artifact key used in the model is not defined"));
257 // PfReferenceKey usage, check the key exists
258 if (!referenceKeySet.contains(usedKey.getKey())) {
259 result.addValidationMessage(new PfValidationMessage(usedKey.getKey(), this.getClass(),
260 ValidationResult.INVALID, "a reference key used in the model is not defined"));
269 public int compareTo(final PfConcept otherObj) {
270 if (otherObj == null) {
273 if (this == otherObj) {
276 if (getClass() != otherObj.getClass()) {
277 return this.hashCode() - otherObj.hashCode();
280 final PfModel other = (PfModel) otherObj;
282 return key.compareTo(other.key);
286 public PfConcept copyTo(@NonNull final PfConcept target) {
287 Assertions.instanceOf(target, PfModel.class);
289 final PfModel copy = ((PfModel) target);
290 copy.setKey(new PfConceptKey(key));