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.lang.reflect.ParameterizedType;
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.NavigableMap;
31 import java.util.TreeMap;
33 import javax.persistence.CascadeType;
34 import javax.persistence.EmbeddedId;
35 import javax.persistence.Entity;
36 import javax.persistence.FetchType;
37 import javax.persistence.ManyToMany;
38 import javax.persistence.Table;
39 import javax.ws.rs.core.Response;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
45 import org.onap.policy.common.utils.validation.Assertions;
46 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
50 * This class is a concept container and holds a map of concepts. The {@link PfConceptContainer} class implements the
51 * helper methods of the {@link PfConceptGetter} interface to allow {@link PfConceptContainer} instances to be retrieved
52 * by calling methods directly on this class without referencing the contained map.
54 * <p>Validation checks that a container key is not null. An error is issued if no concepts are defined in a container.
55 * Each concept entry is checked to ensure that its key and value are not null and that the key matches the key in the
56 * map value. Each concept entry is then validated individually.
58 * @param C the concept being contained
62 @Table(name = "PfConceptContainer")
64 @EqualsAndHashCode(callSuper = false)
66 public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> extends PfConcept
67 implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> {
68 private static final long serialVersionUID = -324211738823208318L;
71 private PfConceptKey key;
73 @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
74 private Map<PfConceptKey, C> conceptMap;
77 * The Default Constructor creates a {@link PfConceptContainer} object with a null artifact key and creates an empty
80 public PfConceptContainer() {
81 this(new PfConceptKey());
85 * The Key Constructor creates a {@link PfConceptContainer} object with the given artifact key and creates an empty
88 * @param key the concept key
90 public PfConceptContainer(@NonNull final PfConceptKey key) {
91 this(key, new TreeMap<PfConceptKey, C>());
95 * This Constructor creates an concept container with all of its fields defined.
97 * @param key the concept container key
98 * @param conceptMap the concepts to be stored in the concept container
100 public PfConceptContainer(@NonNull final PfConceptKey key, @NonNull final Map<PfConceptKey, C> conceptMap) {
104 this.conceptMap = new TreeMap<>(conceptMap);
110 * @param copyConcept the concept to copy from
112 public PfConceptContainer(@NonNull final PfConceptContainer<C, A> copyConcept) {
117 public List<PfKey> getKeys() {
118 final List<PfKey> keyList = key.getKeys();
120 for (final C concept : conceptMap.values()) {
121 keyList.addAll(concept.getKeys());
128 public List<Map<String, A>> toAuthorative() {
129 Map<String, A> toscaPolicyMap = new LinkedHashMap<>();
131 for (Entry<PfConceptKey, C> conceptEntry : getConceptMap().entrySet()) {
132 @SuppressWarnings("unchecked")
133 PfAuthorative<A> authoritiveImpl = (PfAuthorative<A>) conceptEntry.getValue();
134 toscaPolicyMap.put(conceptEntry.getKey().getName(), authoritiveImpl.toAuthorative());
137 List<Map<String, A>> toscaPolicyMapList = new ArrayList<>();
138 toscaPolicyMapList.add(toscaPolicyMap);
140 return toscaPolicyMapList;
144 public void fromAuthorative(List<Map<String, A>> authorativeList) {
145 // Clear any existing map entries
148 // Concepts are in lists of maps
149 for (Map<String, A> incomingConceptMap : authorativeList) {
150 // Add the map entries one by one
151 for (Entry<String, A> incomingConceptEntry : incomingConceptMap.entrySet()) {
152 C jpaConcept = getConceptNewInstance();
154 // This cast allows us to call the fromAuthorative method
155 @SuppressWarnings("unchecked")
156 PfAuthorative<A> authoritiveImpl = (PfAuthorative<A>) jpaConcept;
158 if (incomingConceptEntry.getValue().getName() == null) {
159 incomingConceptEntry.getValue().setName(incomingConceptEntry.getKey());
162 // Set the key name and the rest of the values on the concept
163 authoritiveImpl.fromAuthorative(incomingConceptEntry.getValue());
165 // This cast gets the key of the concept
166 PfConceptKey conceptKey = (PfConceptKey) jpaConcept.getKey();
168 // Set the concept key of the concept
169 conceptKey.setName(incomingConceptEntry.getValue().getName());
171 if (incomingConceptEntry.getValue().getVersion() != null) {
172 conceptKey.setVersion(incomingConceptEntry.getValue().getVersion());
175 conceptKey.setVersion(PfKey.NULL_KEY_VERSION);
178 // After all that, save the map entry
179 conceptMap.put(conceptKey, jpaConcept);
183 if (conceptMap.isEmpty()) {
184 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
185 "An incoming list of concepts must have at least one entry");
190 public void clean() {
192 for (final Entry<PfConceptKey, C> conceptEntry : conceptMap.entrySet()) {
193 conceptEntry.getKey().clean();
194 conceptEntry.getValue().clean();
199 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
200 PfValidationResult result = resultIn;
202 if (key.equals(PfConceptKey.getNullKey())) {
203 result.addValidationMessage(
204 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
207 result = key.validate(result);
209 if (conceptMap.isEmpty()) {
210 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
211 "conceptMap may not be empty"));
213 result = validateConceptMap(result);
220 * Validate the concept map of the container.
222 * @param resultIn the incoming validation results so far
223 * @return the validation results with the results of this validation added
225 private PfValidationResult validateConceptMap(final PfValidationResult resultIn) {
226 PfValidationResult result = resultIn;
228 for (final Entry<PfConceptKey, C> conceptEntry : conceptMap.entrySet()) {
229 if (conceptEntry.getKey().equals(PfConceptKey.getNullKey())) {
230 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
231 "key on concept entry " + conceptEntry.getKey() + " may not be the null key"));
233 if (conceptEntry.getValue() == null) {
234 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
235 "value on concept entry " + conceptEntry.getKey() + " may not be null"));
237 if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) {
238 result.addValidationMessage(new PfValidationMessage(key, this.getClass(),
239 ValidationResult.INVALID, "key on concept entry key " + conceptEntry.getKey()
240 + " does not equal concept value key " + conceptEntry.getValue().getKey()));
241 result = conceptEntry.getValue().validate(result);
243 result = conceptEntry.getValue().validate(result);
250 public int compareTo(final PfConcept otherConcept) {
251 if (otherConcept == null) {
254 if (this == otherConcept) {
257 if (getClass() != otherConcept.getClass()) {
258 return this.hashCode() - otherConcept.hashCode();
261 @SuppressWarnings("unchecked")
262 final PfConceptContainer<C, A> other = (PfConceptContainer<C, A>) otherConcept;
263 int retVal = key.compareTo(other.key);
268 if (!conceptMap.equals(other.conceptMap)) {
269 return (conceptMap.hashCode() - other.conceptMap.hashCode());
276 public PfConcept copyTo(@NonNull final PfConcept target) {
277 Assertions.instanceOf(target, PfConceptContainer.class);
279 @SuppressWarnings("unchecked")
280 final PfConceptContainer<C, A> copy = (PfConceptContainer<C, A>) target;
281 copy.setKey(new PfConceptKey(key));
282 final Map<PfConceptKey, C> newConceptMap = new TreeMap<>();
283 for (final Entry<PfConceptKey, C> conceptMapEntry : conceptMap.entrySet()) {
284 C newC = getConceptNewInstance();
285 conceptMapEntry.getValue().copyTo(newC);
286 newConceptMap.put(new PfConceptKey(conceptMapEntry.getKey()), newC);
288 copy.setConceptMap(newConceptMap);
294 public C get(final PfConceptKey conceptKey) {
295 return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
299 public C get(final String conceptKeyName) {
300 return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName);
304 public C get(final String conceptKeyName, final String conceptKeyVersion) {
305 return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName,
310 public Set<C> getAll(final String conceptKeyName) {
311 return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName);
315 public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
316 return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName,
321 * Get a new empty instance of a concept for this concept map.
323 * @return the new instance
325 @SuppressWarnings("unchecked")
326 private C getConceptNewInstance() {
328 String conceptClassName =
329 ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName();
330 return (C) Class.forName(conceptClassName).newInstance();
331 } catch (Exception ex) {
332 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
333 "failed to instantiate instance of container concept class", ex);