aboutsummaryrefslogtreecommitdiffstats
path: root/models-base/src/main/java/org/onap/policy/models/base/PfModel.java
blob: 1caa6329ec046d45047a5c5def4e4393295e1d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019, 2023 Nordix Foundation.
 *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 *  Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.base;

import jakarta.persistence.EmbeddedId;
import jakarta.persistence.MappedSuperclass;
import java.io.Serial;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import org.onap.policy.common.parameters.BeanValidationResult;
import org.onap.policy.common.parameters.ValidationStatus;
import org.onap.policy.common.parameters.annotations.NotNull;
import org.onap.policy.common.utils.validation.Assertions;
import org.onap.policy.models.base.validation.annotations.VerifyKey;

/**
 * This class is the base class for all models in the Policy Framework. All model classes inherit
 * from this model so all models must have a key and have key information.
 *
 * <p>Validation checks that the model key is valid. It goes on to check for null keys and checks
 * each key for uniqueness in the model. A check is carried out to ensure that an {@link PfKey}
 * instance exists for every {@link PfConceptKey} key. For each {@link PfReferenceKey} instance, a
 * check is made that its parent and local name are not null and that a {@link PfKey} entry
 * exists for its parent. Then a check is made that each used {@link PfConceptKey} and
 * {@link PfReferenceKey} usage references a key that exists. Finally, a check is made to ensure
 * that an {@link PfConceptKey} instance exists for every {@link PfKey} instance.
 *
 * @param <C> the type of concept on which the interface is applied.
 */

@MappedSuperclass
@Data
@EqualsAndHashCode(callSuper = false)
public abstract class PfModel extends PfConcept {
    private static final String KEYS_TOKEN = "keys";

    @Serial
    private static final long serialVersionUID = -771659065637205430L;

    @EmbeddedId
    @VerifyKey
    @NotNull
    private PfConceptKey key;

    /**
     * The Default Constructor creates this concept with a NULL artifact key.
     */
    protected PfModel() {
        this(new PfConceptKey());
    }

    /**
     * Constructor to create this concept with the specified key.
     *
     * @param key the key of this concept
     */
    protected PfModel(@NonNull final PfConceptKey key) {
        super();
        Assertions.argumentNotNull(key, "key may not be null");

        this.key = key;
    }

    /**
     * Copy constructor.
     *
     * @param copyConcept the concept to copy from
     */
    protected PfModel(@NonNull final PfModel copyConcept) {
        super(copyConcept);
        this.key = new PfConceptKey(copyConcept.key);
    }

    /**
     * Registers this model with the {@link PfModelService}. All models are registered with the
     * model service so that models can be references from anywhere in the Policy Framework system
     * without being passed as references through deep call chains.
     */
    public abstract void register();

    @Override
    public List<PfKey> getKeys() {
        return key.getKeys();
    }

    @Override
    public void clean() {
        key.clean();
    }

    @Override
    public BeanValidationResult validate(@NonNull String fieldName) {
        BeanValidationResult result = new PfValidator().validateTop(fieldName, this);

        // Key consistency check
        final Set<PfConceptKey> artifactKeySet = new TreeSet<>();
        final Set<PfReferenceKey> referenceKeySet = new TreeSet<>();
        final Set<PfKeyUse> usedKeySet = new TreeSet<>();

        for (final PfKey pfKey : this.getKeys()) {
            // Check for the two type of keys we have
            if (pfKey instanceof PfConceptKey) {
                validateArtifactKeyInModel((PfConceptKey) pfKey, artifactKeySet, result);
            } else if (pfKey instanceof PfReferenceKey) {
                validateReferenceKeyInModel((PfReferenceKey) pfKey, referenceKeySet, result);
            } else {
                // It must be a PfKeyUse, nothing else is legal
                usedKeySet.add((PfKeyUse) pfKey);
            }
        }

        // Check all reference keys have correct parent keys
        for (final PfReferenceKey referenceKey : referenceKeySet) {
            if (!artifactKeySet.contains(referenceKey.getParentConceptKey())) {
                addResult(result, "reference key", referenceKey, "parent artifact key not found");
            }
        }

        validateKeyUses(usedKeySet, artifactKeySet, referenceKeySet, result);

        return result;
    }

    /**
     * Check for consistent usage of an artifact key in the model.
     *
     * @param artifactKey    The artifact key to check
     * @param artifactKeySet The set of artifact keys encountered so far, this key is appended to
     *                       the set
     * @param result         where to add the results
     */
    private void validateArtifactKeyInModel(final PfConceptKey artifactKey,
                                            final Set<PfConceptKey> artifactKeySet, final BeanValidationResult result) {

        validateKeyNotNull(result, KEYS_TOKEN, artifactKey);

        var result2 = new BeanValidationResult(KEYS_TOKEN, artifactKey);

        // Null key name start check
        if (artifactKey.getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
            addResult(result2, "name of " + artifactKey.getId(), artifactKey.getName(),
                "starts with keyword " + PfKey.NULL_KEY_NAME);
        }

        // Unique key check
        if (artifactKeySet.contains(artifactKey)) {
            addResult(result, KEYS_TOKEN, artifactKey, "duplicate key");
        } else {
            artifactKeySet.add(artifactKey);
        }
    }

    /**
     * Check for consistent usage of a reference key in the model.
     *
     * @param referenceKey    The reference key to check
     * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
     *                        the set
     * @param result          where to add the results
     */
    private void validateReferenceKeyInModel(final PfReferenceKey referenceKey,
                                             final Set<PfReferenceKey> referenceKeySet,
                                             final BeanValidationResult result) {
        // Null key check
        if (referenceKey.isNullKey()) {
            addResult(result, KEYS_TOKEN, referenceKey, IS_A_NULL_KEY);
        }

        var result2 = new BeanValidationResult(KEYS_TOKEN, referenceKey);

        // Null parent key check
        if (referenceKey.getParentConceptKey().isNullKey()) {
            addResult(result2, "parent key of " + referenceKey.getId(), referenceKey.getParentConceptKey().getId(),
                IS_A_NULL_KEY);
        }

        // Null local name check
        if (referenceKey.getLocalName().equals(PfKey.NULL_KEY_NAME)) {
            addResult(result2, "local name of " + referenceKey.getId(), referenceKey.getLocalName(), IS_NULL);
        }

        // Null key name start check
        if (referenceKey.getParentConceptKey().getName().toUpperCase().startsWith(PfKey.NULL_KEY_NAME)) {
            addResult(result2, "parent name of " + referenceKey.getId(), referenceKey.getParentConceptKey().getName(),
                "starts with keyword " + PfKey.NULL_KEY_NAME);
        }

        // Unique key check
        if (referenceKeySet.contains(referenceKey)) {
            addResult(result, KEYS_TOKEN, referenceKey, "duplicate key");
        } else {
            referenceKeySet.add(referenceKey);
        }
    }

    /**
     * Check for consistent usage of cross-key references in the model.
     *
     * @param usedKeySet      The set of all keys used in the model
     * @param artifactKeySet  The set of artifact keys encountered so far, this key is appended to
     *                        the set
     * @param referenceKeySet The set of reference keys encountered so far, this key is appended to
     *                        the set
     * @param result          where to add the results
     */
    private void validateKeyUses(final Set<PfKeyUse> usedKeySet, final Set<PfConceptKey> artifactKeySet,
                                 final Set<PfReferenceKey> referenceKeySet, final BeanValidationResult result) {
        // Check all key uses
        for (final PfKeyUse usedKey : usedKeySet) {
            if (usedKey.getKey() instanceof PfConceptKey) {
                // PfConceptKey usage, check the key exists
                if (!artifactKeySet.contains(usedKey.getKey())) {
                    result.addResult("artifact key", usedKey.getId(), ValidationStatus.INVALID, NOT_DEFINED);
                }
            } else {
                // PfReferenceKey usage, check the key exists
                if (!referenceKeySet.contains(usedKey.getKey())) {
                    result.addResult("reference key", usedKey.getId(), ValidationStatus.INVALID, NOT_DEFINED);
                }
            }
        }
    }

    @Override
    public int compareTo(final PfConcept otherObj) {
        if (otherObj == null) {
            return -1;
        }
        if (this == otherObj) {
            return 0;
        }
        if (getClass() != otherObj.getClass()) {
            return getClass().getName().compareTo(otherObj.getClass().getName());
        }

        final PfModel other = (PfModel) otherObj;

        return key.compareTo(other.key);
    }
}