Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTopologyTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
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 com.google.gson.annotations.SerializedName;
25 import jakarta.persistence.CascadeType;
26 import jakarta.persistence.Column;
27 import jakarta.persistence.ElementCollection;
28 import jakarta.persistence.EmbeddedId;
29 import jakarta.persistence.Entity;
30 import jakarta.persistence.FetchType;
31 import jakarta.persistence.Inheritance;
32 import jakarta.persistence.InheritanceType;
33 import jakarta.persistence.JoinColumn;
34 import jakarta.persistence.JoinColumns;
35 import jakarta.persistence.Lob;
36 import jakarta.persistence.OneToOne;
37 import jakarta.persistence.Table;
38 import java.io.Serial;
39 import java.util.Collections;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.Map;
43 import lombok.Data;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.common.parameters.annotations.NotBlank;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Valid;
50 import org.onap.policy.models.base.PfAuthorative;
51 import org.onap.policy.models.base.PfConcept;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.validation.annotations.VerifyKey;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
59
60 /**
61  * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
62  * implemented.
63  *
64  * @author Liam Fallon (liam.fallon@est.tech)
65  */
66 @Entity
67 @Table(name = "ToscaTopologyTemplate")
68 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
69 @Data
70 @EqualsAndHashCode(callSuper = false)
71 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
72     @Serial
73     private static final long serialVersionUID = 8969698734673232603L;
74
75     public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
76
77     @EmbeddedId
78     @VerifyKey
79     @NotNull
80     private PfReferenceKey key;
81
82     @Column(name = "description")
83     @NotBlank
84     private String description;
85
86     @ElementCollection
87     @Lob
88     private Map<@NotNull String, @NotNull @Valid JpaToscaParameter> inputs;
89
90     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
91     @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name")
92     @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version")
93     @SerializedName("data_types")
94     @Valid
95     private JpaToscaNodeTemplates nodeTemplates;
96
97     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
98     @JoinColumn(name = "policyName", referencedColumnName = "name")
99     @JoinColumn(name = "policyVersion", referencedColumnName = "version")
100     @Valid
101     private JpaToscaPolicies policies;
102
103     /**
104      * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
105      */
106     public JpaToscaTopologyTemplate() {
107         this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
108             DEFAULT_LOCAL_NAME));
109     }
110
111     /**
112      * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
113      *
114      * @param key the key
115      */
116     public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
117         this.key = key;
118     }
119
120     /**
121      * Copy constructor.
122      *
123      * @param copyConcept the concept to copy from
124      */
125     public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
126         super(copyConcept);
127         this.key = new PfReferenceKey(copyConcept.key);
128         this.description = copyConcept.description;
129         this.inputs = PfUtils.mapMap(copyConcept.inputs, JpaToscaParameter::new);
130         this.nodeTemplates =
131             (copyConcept.nodeTemplates != null ? new JpaToscaNodeTemplates(copyConcept.nodeTemplates) : null);
132         this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
133     }
134
135     /**
136      * Authorative constructor.
137      *
138      * @param authorativeConcept the authorative concept to copy from
139      */
140     public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
141         this.fromAuthorative(authorativeConcept);
142     }
143
144     @Override
145     public ToscaTopologyTemplate toAuthorative() {
146         final var toscaTopologyTemplate = new ToscaTopologyTemplate();
147
148         toscaTopologyTemplate.setDescription(description);
149
150         if (inputs != null) {
151             Map<String, ToscaParameter> inputMap = new LinkedHashMap<>();
152
153             for (Map.Entry<String, JpaToscaParameter> entry : inputs.entrySet()) {
154                 inputMap.put(entry.getKey(), entry.getValue().toAuthorative());
155             }
156
157             toscaTopologyTemplate.setInputs(inputMap);
158         }
159
160         if (nodeTemplates != null) {
161             toscaTopologyTemplate.setNodeTemplates(new LinkedHashMap<>());
162             List<Map<String, ToscaNodeTemplate>> nodeTemplateMapList = nodeTemplates.toAuthorative();
163             for (Map<String, ToscaNodeTemplate> nodeTemplateMap : nodeTemplateMapList) {
164                 toscaTopologyTemplate.getNodeTemplates().putAll(nodeTemplateMap);
165             }
166         }
167
168         if (policies != null) {
169             toscaTopologyTemplate.setPolicies(policies.toAuthorative());
170         }
171
172         return toscaTopologyTemplate;
173     }
174
175     @Override
176     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
177         description = toscaTopologyTemplate.getDescription();
178
179         if (toscaTopologyTemplate.getInputs() != null) {
180             inputs = new LinkedHashMap<>();
181             for (Map.Entry<String, ToscaParameter> toscaInputEntry : toscaTopologyTemplate.getInputs().entrySet()) {
182                 var jpaInput = new JpaToscaParameter(toscaInputEntry.getValue());
183                 jpaInput.setKey(new PfReferenceKey(getKey(), toscaInputEntry.getKey()));
184                 inputs.put(toscaInputEntry.getKey(), jpaInput);
185             }
186         }
187
188         if (toscaTopologyTemplate.getNodeTemplates() != null) {
189             nodeTemplates = new JpaToscaNodeTemplates();
190             nodeTemplates.fromAuthorative(Collections.singletonList(toscaTopologyTemplate.getNodeTemplates()));
191         }
192
193         if (toscaTopologyTemplate.getPolicies() != null) {
194             policies = new JpaToscaPolicies();
195             policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
196         }
197     }
198
199     @Override
200     public List<PfKey> getKeys() {
201         final List<PfKey> keyList = getKey().getKeys();
202
203         if (inputs != null) {
204             for (JpaToscaParameter input : inputs.values()) {
205                 keyList.addAll(input.getKeys());
206             }
207         }
208
209         if (nodeTemplates != null) {
210             keyList.addAll(nodeTemplates.getKeys());
211         }
212
213         if (policies != null) {
214             keyList.addAll(policies.getKeys());
215         }
216
217         return keyList;
218     }
219
220     @Override
221     public void clean() {
222         key.clean();
223
224         description = (description != null ? description.trim() : null);
225
226         if (inputs != null) {
227             for (JpaToscaParameter input : inputs.values()) {
228                 input.clean();
229             }
230         }
231
232         if (nodeTemplates != null) {
233             nodeTemplates.clean();
234         }
235
236         if (policies != null) {
237             policies.clean();
238         }
239     }
240
241     @Override
242     public int compareTo(final PfConcept otherConcept) {
243         int result = compareToWithoutEntities(otherConcept);
244         if (result != 0) {
245             return result;
246         }
247
248         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
249
250         result = PfUtils.compareObjects(inputs, other.inputs);
251         if (result != 0) {
252             return result;
253         }
254
255         result = ObjectUtils.compare(nodeTemplates, other.nodeTemplates);
256         if (result != 0) {
257             return result;
258         }
259
260         return ObjectUtils.compare(policies, other.policies);
261     }
262
263     /**
264      * Compare this topology template to another topology template, ignoring contained entities.
265      *
266      * @param otherConcept the other topology template
267      * @return the result of the comparison
268      */
269     public int compareToWithoutEntities(final PfConcept otherConcept) {
270         if (otherConcept == null) {
271             return -1;
272         }
273         if (this == otherConcept) {
274             return 0;
275         }
276         if (getClass() != otherConcept.getClass()) {
277             return getClass().getName().compareTo(otherConcept.getClass().getName());
278         }
279
280         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
281         if (!key.equals(other.key)) {
282             return key.compareTo(other.key);
283         }
284
285         return ObjectUtils.compare(description, other.description);
286     }
287 }