Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaWithToscaProperties.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
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;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
35 import lombok.Data;
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;
52
53 /**
54  * Class to represent JPA TOSCA classes containing TOSCA properties.
55  */
56 @MappedSuperclass
57 @Data
58 @EqualsAndHashCode(callSuper = true)
59 public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProperties> extends JpaToscaEntityType<T>
60                 implements PfAuthorative<T> {
61     @Serial
62     private static final long serialVersionUID = -563659852901842616L;
63
64     @ElementCollection
65     @Lob
66     private Map<@NotNull @NotBlank String, @NotNull @Valid JpaToscaProperty> properties;
67
68     /**
69      * The Default Constructor creates a {@link JpaToscaWithToscaProperties} object with a
70      * null key.
71      */
72     protected JpaToscaWithToscaProperties() {
73         this(new PfConceptKey());
74     }
75
76     /**
77      * The Key Constructor creates a {@link JpaToscaWithToscaProperties} object with the
78      * given concept key.
79      *
80      * @param key the key
81      */
82     protected JpaToscaWithToscaProperties(@NonNull final PfConceptKey key) {
83         super(key);
84     }
85
86     /**
87      * Copy constructor.
88      *
89      * @param copyConcept the concept to copy from
90      */
91     protected JpaToscaWithToscaProperties(final JpaToscaWithToscaProperties<T> copyConcept) {
92         super(copyConcept);
93         this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties);
94     }
95
96     /**
97      * Authorative constructor.
98      *
99      * @param authorativeConcept the authorative concept to copy from
100      */
101     protected JpaToscaWithToscaProperties(final T authorativeConcept) {
102         this.fromAuthorative(authorativeConcept);
103     }
104
105     @Override
106     public void fromAuthorative(final T authorativeConcept) {
107         super.fromAuthorative(authorativeConcept);
108
109         // Set properties
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);
116             }
117         }
118     }
119
120     @Override
121     public T toAuthorative() {
122         var tosca = super.toAuthorative();
123
124         tosca.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
125
126         return tosca;
127     }
128
129     @Override
130     public List<PfKey> getKeys() {
131         final List<PfKey> keyList = super.getKeys();
132
133         PfUtils.mapMap(properties, property -> keyList.addAll(property.getKeys()));
134
135         return keyList;
136     }
137
138     @Override
139     public void clean() {
140         super.clean();
141
142         if (properties != null) {
143             for (JpaToscaProperty property : properties.values()) {
144                 property.clean();
145             }
146         }
147     }
148
149     /**
150      * Validates the fields of the object, including its key.
151      *
152      * @param fieldName name of the field containing this
153      * @return the result, or {@code null}
154      */
155     protected BeanValidationResult validateWithKey(@NonNull String fieldName) {
156         BeanValidationResult result = super.validate(fieldName);
157
158         validateKeyVersionNotNull(result, "key", getKey());
159
160         return result;
161     }
162
163     @Override
164     public int compareTo(final PfConcept otherConcept) {
165         if (this == otherConcept) {
166             return 0;
167         }
168
169         int result = super.compareTo(otherConcept);
170         if (result != 0) {
171             return result;
172         }
173
174         @SuppressWarnings("unchecked")
175         final JpaToscaWithToscaProperties<T> other = (JpaToscaWithToscaProperties<T>) otherConcept;
176
177         return PfUtils.compareMaps(properties, other.properties);
178     }
179
180     /**
181      * Get the referenced data types.
182      *
183      * @return the referenced data types
184      */
185     public Collection<PfConceptKey> getReferencedDataTypes() {
186         if (properties == null) {
187             return CollectionUtils.emptyCollection();
188         }
189
190         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
191
192         for (JpaToscaProperty property : properties.values()) {
193             referencedDataTypes.add(property.getType());
194
195             if (property.getEntrySchema() != null) {
196                 referencedDataTypes.add(property.getEntrySchema().getType());
197             }
198         }
199
200         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
201
202         return referencedDataTypes;
203     }
204 }