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.List;
26 import java.util.TreeSet;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
38 import org.onap.policy.common.utils.validation.Assertions;
39 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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 IS_A_NULL_KEY = " is a null key";
64 private static final long serialVersionUID = -771659065637205430L;
67 private PfConceptKey key;
70 * The Default Constructor creates this concept with a NULL artifact key.
73 this(new PfConceptKey());
77 * Constructor to create this concept with the specified key.
79 * @param key the key of this concept
81 public PfModel(@NonNull final PfConceptKey key) {
83 Assertions.argumentNotNull(key, "key may not be null");
91 * @param copyConcept the concept to copy from
93 public PfModel(@NonNull final PfModel copyConcept) {
95 this.key = new PfConceptKey(copyConcept.key);
99 * Registers this model with the {@link PfModelService}. All models are registered with the
100 * model service so that models can be references from anywhere in the Policy Framework system
101 * without being passed as references through deep call chains.
103 public abstract void register();
106 public List<PfKey> getKeys() {
107 return key.getKeys();
111 public void clean() {
116 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
117 PfValidationResult result = resultIn;
119 if (key.isNullKey()) {
120 result.addValidationMessage(
121 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
124 result = key.validate(result);
126 // Key consistency check
127 final Set<PfConceptKey> artifactKeySet = new TreeSet<>();
128 final Set<PfReferenceKey> referenceKeySet = new TreeSet<>();
129 final Set<PfKeyUse> usedKeySet = new TreeSet<>();
131 for (final PfKey pfKey : this.getKeys()) {
132 // Check for the two type of keys we have
133 if (pfKey instanceof PfConceptKey) {
134 result = validateArtifactKeyInModel((PfConceptKey) pfKey, artifactKeySet, result);
135 } else if (pfKey instanceof PfReferenceKey) {
136 result = validateReferenceKeyInModel((PfReferenceKey) pfKey, referenceKeySet, result);
138 // It must be a PfKeyUse, nothing else is legal
140 usedKeySet.add((PfKeyUse) pfKey);
144 // Check all reference keys have correct parent keys
145 for (final PfReferenceKey referenceKey : referenceKeySet) {
146 if (!artifactKeySet.contains(referenceKey.getParentConceptKey())) {
147 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
148 "parent artifact key not found for reference key " + referenceKey));
152 result = validateKeyUses(usedKeySet, artifactKeySet, referenceKeySet, result);
158 * Check for consistent usage of an artifact key in the model.
160 * @param artifactKey The artifact key to check
161 * @param artifactKeySet The set of artifact keys encountered so far, this key is appended to
163 * @param result The validation result to append to
164 * @return the result of the validation
166 private PfValidationResult validateArtifactKeyInModel(final PfConceptKey artifactKey,
167 final Set<PfConceptKey> artifactKeySet, final PfValidationResult result) {
169 if (artifactKey.isNullKey()) {
170 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
171 "key " + artifactKey + IS_A_NULL_KEY));
174 // Null key name start check
175 if (artifactKey.getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
176 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
177 "key " + artifactKey + " name starts with keyword " + PfKey.NULL_KEY_NAME));
181 if (artifactKeySet.contains(artifactKey)) {
182 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
183 "duplicate key " + artifactKey + " found"));
185 artifactKeySet.add(artifactKey);
192 * Check for consistent usage of a reference key in the model.
194 * @param artifactKey The reference key to check
195 * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
197 * @param result The validation result to append to
198 * @return the result of the validation
200 private PfValidationResult validateReferenceKeyInModel(final PfReferenceKey referenceKey,
201 final Set<PfReferenceKey> referenceKeySet, final PfValidationResult result) {
203 if (referenceKey.isNullKey()) {
204 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
205 "key " + referenceKey + IS_A_NULL_KEY));
208 // Null parent key check
209 if (referenceKey.getParentConceptKey().isNullKey()) {
210 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
211 "parent artifact key of key " + referenceKey + IS_A_NULL_KEY));
214 // Null local name check
215 if (referenceKey.getLocalName().equals(PfKey.NULL_KEY_NAME)) {
216 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
217 "key " + referenceKey + " has a null local name"));
220 // Null key name start check
221 if (referenceKey.getParentConceptKey().getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
222 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
223 "key " + referenceKey + " parent name starts with keyword " + PfKey.NULL_KEY_NAME));
227 if (referenceKeySet.contains(referenceKey)) {
228 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
229 "duplicate key " + referenceKey + " found"));
231 referenceKeySet.add(referenceKey);
238 * Check for consistent usage of cross-key references in the model.
240 * @param usedKeySet The set of all keys used in the model
241 * @param artifactKeySet The set of artifact keys encountered so far, this key is appended to
243 * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
245 * @param result The validation result to append to
246 * @return the result of the validation
248 private PfValidationResult validateKeyUses(final Set<PfKeyUse> usedKeySet, final Set<PfConceptKey> artifactKeySet,
249 final Set<PfReferenceKey> referenceKeySet, final PfValidationResult result) {
250 // Check all key uses
251 for (final PfKeyUse usedKey : usedKeySet) {
252 if (usedKey.getKey() instanceof PfConceptKey) {
253 // PfConceptKey usage, check the key exists
254 if (!artifactKeySet.contains(usedKey.getKey())) {
255 result.addValidationMessage(new PfValidationMessage(usedKey.getKey(), this.getClass(),
256 ValidationResult.INVALID, "an artifact key used in the model is not defined"));
259 // PfReferenceKey usage, check the key exists
260 if (!referenceKeySet.contains(usedKey.getKey())) {
261 result.addValidationMessage(new PfValidationMessage(usedKey.getKey(), this.getClass(),
262 ValidationResult.INVALID, "a reference key used in the model is not defined"));
271 public int compareTo(final PfConcept otherObj) {
272 if (otherObj == null) {
275 if (this == otherObj) {
278 if (getClass() != otherObj.getClass()) {
279 return getClass().getName().compareTo(otherObj.getClass().getName());
282 final PfModel other = (PfModel) otherObj;
284 return key.compareTo(other.key);