2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.tosca.simple.concepts;
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Lob;
32 import javax.persistence.MappedSuperclass;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.apache.commons.collections4.CollectionUtils;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotBlank;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.common.parameters.annotations.Valid;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfReferenceKey;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
49 import org.onap.policy.models.tosca.utils.ToscaUtils;
52 * Class to represent JPA TOSCA classes containing TOSCA properties.
56 @EqualsAndHashCode(callSuper = true)
57 public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProperties> extends JpaToscaEntityType<T>
58 implements PfAuthorative<T> {
59 private static final long serialVersionUID = -563659852901842616L;
63 private Map<@NotNull @NotBlank String, @NotNull @Valid JpaToscaProperty> properties;
66 * The Default Constructor creates a {@link JpaToscaWithToscaProperties} object with a
69 protected JpaToscaWithToscaProperties() {
70 this(new PfConceptKey());
74 * The Key Constructor creates a {@link JpaToscaWithToscaProperties} object with the
79 protected JpaToscaWithToscaProperties(@NonNull final PfConceptKey key) {
86 * @param copyConcept the concept to copy from
88 protected JpaToscaWithToscaProperties(final JpaToscaWithToscaProperties<T> copyConcept) {
90 this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
94 * Authorative constructor.
96 * @param authorativeConcept the authorative concept to copy from
98 protected JpaToscaWithToscaProperties(final T authorativeConcept) {
99 this.fromAuthorative(authorativeConcept);
103 public void fromAuthorative(final T authorativeConcept) {
104 super.fromAuthorative(authorativeConcept);
107 if (authorativeConcept.getProperties() != null) {
108 properties = new LinkedHashMap<>();
109 for (Entry<String, ToscaProperty> toscaPropertyEntry : authorativeConcept.getProperties().entrySet()) {
110 var jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
111 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
112 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
118 public T toAuthorative() {
119 var tosca = super.toAuthorative();
121 tosca.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
127 public List<PfKey> getKeys() {
128 final List<PfKey> keyList = super.getKeys();
130 PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys()));
136 public void clean() {
139 if (properties != null) {
140 for (JpaToscaProperty property : properties.values()) {
147 * Validates the fields of the object, including its key.
149 * @param fieldName name of the field containing this
150 * @return the result, or {@code null}
152 protected BeanValidationResult validateWithKey(@NonNull String fieldName) {
153 BeanValidationResult result = super.validate(fieldName);
155 validateKeyVersionNotNull(result, "key", getKey());
161 public int compareTo(final PfConcept otherConcept) {
162 if (this == otherConcept) {
166 int result = super.compareTo(otherConcept);
171 @SuppressWarnings("unchecked")
172 final JpaToscaWithToscaProperties<T> other = (JpaToscaWithToscaProperties<T>) otherConcept;
174 return PfUtils.compareMaps(properties, other.properties);
178 * Get the referenced data types.
180 * @return the referenced data types
182 public Collection<PfConceptKey> getReferencedDataTypes() {
183 if (properties == null) {
184 return CollectionUtils.emptyCollection();
187 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
189 for (JpaToscaProperty property : properties.values()) {
190 referencedDataTypes.add(property.getType());
192 if (property.getEntrySchema() != null) {
193 referencedDataTypes.add(property.getEntrySchema().getType());
197 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
199 return referencedDataTypes;