2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
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.tosca.simple.concepts;
24 import jakarta.persistence.ElementCollection;
25 import jakarta.persistence.Lob;
26 import jakarta.persistence.MappedSuperclass;
27 import java.io.Serial;
28 import java.util.Collection;
29 import java.util.LinkedHashMap;
30 import java.util.LinkedHashSet;
31 import java.util.List;
33 import java.util.Map.Entry;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.apache.commons.collections4.CollectionUtils;
39 import org.onap.policy.common.parameters.BeanValidationResult;
40 import org.onap.policy.common.parameters.annotations.NotBlank;
41 import org.onap.policy.common.parameters.annotations.NotNull;
42 import org.onap.policy.common.parameters.annotations.Valid;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfReferenceKey;
48 import org.onap.policy.models.base.PfUtils;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
51 import org.onap.policy.models.tosca.utils.ToscaUtils;
54 * Class to represent JPA TOSCA classes containing TOSCA properties.
58 @EqualsAndHashCode(callSuper = true)
59 public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProperties> extends JpaToscaEntityType<T>
60 implements PfAuthorative<T> {
62 private static final long serialVersionUID = -563659852901842616L;
66 private Map<@NotNull @NotBlank String, @NotNull @Valid JpaToscaProperty> properties;
69 * The Default Constructor creates a {@link JpaToscaWithToscaProperties} object with a
72 protected JpaToscaWithToscaProperties() {
73 this(new PfConceptKey());
77 * The Key Constructor creates a {@link JpaToscaWithToscaProperties} object with the
82 protected JpaToscaWithToscaProperties(@NonNull final PfConceptKey key) {
89 * @param copyConcept the concept to copy from
91 protected JpaToscaWithToscaProperties(final JpaToscaWithToscaProperties<T> copyConcept) {
93 this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
97 * Authorative constructor.
99 * @param authorativeConcept the authorative concept to copy from
101 protected JpaToscaWithToscaProperties(final T authorativeConcept) {
102 this.fromAuthorative(authorativeConcept);
106 public void fromAuthorative(final T authorativeConcept) {
107 super.fromAuthorative(authorativeConcept);
110 if (authorativeConcept.getProperties() != null) {
111 properties = new LinkedHashMap<>();
112 for (Entry<String, ToscaProperty> toscaPropertyEntry : authorativeConcept.getProperties().entrySet()) {
113 var jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
114 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
115 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
121 public T toAuthorative() {
122 var tosca = super.toAuthorative();
124 tosca.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
130 public List<PfKey> getKeys() {
131 final List<PfKey> keyList = super.getKeys();
133 PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys()));
139 public void clean() {
142 if (properties != null) {
143 for (JpaToscaProperty property : properties.values()) {
150 * Validates the fields of the object, including its key.
152 * @param fieldName name of the field containing this
153 * @return the result, or {@code null}
155 protected BeanValidationResult validateWithKey(@NonNull String fieldName) {
156 BeanValidationResult result = super.validate(fieldName);
158 validateKeyVersionNotNull(result, "key", getKey());
164 public int compareTo(final PfConcept otherConcept) {
165 if (this == otherConcept) {
169 int result = super.compareTo(otherConcept);
174 @SuppressWarnings("unchecked")
175 final JpaToscaWithToscaProperties<T> other = (JpaToscaWithToscaProperties<T>) otherConcept;
177 return PfUtils.compareMaps(properties, other.properties);
181 * Get the referenced data types.
183 * @return the referenced data types
185 public Collection<PfConceptKey> getReferencedDataTypes() {
186 if (properties == null) {
187 return CollectionUtils.emptyCollection();
190 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
192 for (JpaToscaProperty property : properties.values()) {
193 referencedDataTypes.add(property.getType());
195 if (property.getEntrySchema() != null) {
196 referencedDataTypes.add(property.getEntrySchema().getType());
200 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
202 return referencedDataTypes;