Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaServiceTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021, 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.Entity;
28 import jakarta.persistence.FetchType;
29 import jakarta.persistence.Inheritance;
30 import jakarta.persistence.InheritanceType;
31 import jakarta.persistence.JoinColumn;
32 import jakarta.persistence.OneToOne;
33 import jakarta.persistence.Table;
34 import java.io.Serial;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Map;
40 import lombok.Data;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
43 import org.apache.commons.lang3.ObjectUtils;
44 import org.onap.policy.common.parameters.BeanValidationResult;
45 import org.onap.policy.common.parameters.annotations.NotBlank;
46 import org.onap.policy.common.parameters.annotations.NotNull;
47 import org.onap.policy.common.parameters.annotations.Valid;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53
54 /**
55  * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
56  * are implemented.
57  *
58  * @author Liam Fallon (liam.fallon@est.tech)
59  */
60
61 @Entity
62 @Table(name = "ToscaServiceTemplate")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @Data
65 @EqualsAndHashCode(callSuper = true)
66 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
67     implements PfAuthorative<ToscaServiceTemplate> {
68     @Serial
69     private static final long serialVersionUID = 8084846046148349401L;
70
71     public static final String DEFAULT_TOSCA_DEFINITIONS_VERSION = "tosca_simple_yaml_1_1_0";
72     public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
73     public static final String DEFAULT_VERSION = "1.0.0";
74
75     // @formatter:off
76     @Column
77     @SerializedName("tosca_definitions_version")
78     @NotNull
79     @NotBlank
80     private String toscaDefinitionsVersion;
81
82     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
83     @JoinColumn(name = "dataTypesName",    referencedColumnName = "name")
84     @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version")
85     @SerializedName("data_types")
86     @Valid
87     private JpaToscaDataTypes dataTypes;
88
89     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
90     @JoinColumn(name = "capabilityTypesName",    referencedColumnName = "name")
91     @JoinColumn(name = "capabilityTypesVersion", referencedColumnName = "version")
92     @SerializedName("capability_types")
93     @Valid
94     private JpaToscaCapabilityTypes capabilityTypes;
95
96     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
97     @JoinColumn(name = "relationshipTypesName",    referencedColumnName = "name")
98     @JoinColumn(name = "relationshipTypesVersion", referencedColumnName = "version")
99     @SerializedName("relationship_types")
100     @Valid
101     private JpaToscaRelationshipTypes relationshipTypes;
102
103     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
104     @JoinColumn(name = "nodeTypesName",    referencedColumnName = "name")
105     @JoinColumn(name = "nodeTypesVersion", referencedColumnName = "version")
106     @SerializedName("node_types")
107     @Valid
108     private JpaToscaNodeTypes nodeTypes;
109
110     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
111     @JoinColumn(name = "policyTypesName",    referencedColumnName = "name")
112     @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
113     @SerializedName("policy_types")
114     @Valid
115     private JpaToscaPolicyTypes policyTypes;
116
117     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
118     @JoinColumn(name = "topologyTemplateParentKeyName",    referencedColumnName = "parentKeyName")
119     @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion")
120     @JoinColumn(name = "topologyTemplateParentLocalName",  referencedColumnName = "parentLocalName")
121     @JoinColumn(name = "topologyTemplateLocalName",        referencedColumnName = "localName")
122     @SerializedName("topology_template")
123     @Valid
124     private JpaToscaTopologyTemplate topologyTemplate;
125     // @formatter:on
126
127     /**
128      * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
129      */
130     public JpaToscaServiceTemplate() {
131         this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
132     }
133
134     /**
135      * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
136      *
137      * @param key the key
138      */
139     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
140         this(key, DEFAULT_TOSCA_DEFINITIONS_VERSION);
141     }
142
143     /**
144      * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
145      *
146      * @param key                     the key
147      * @param toscaDefinitionsVersion the TOSCA version string
148      */
149     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
150         super(key);
151         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
152     }
153
154     /**
155      * Copy constructor.
156      *
157      * @param copyConcept the concept to copy from
158      */
159     public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
160         super(copyConcept);
161         this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
162         this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
163         this.capabilityTypes =
164             (copyConcept.capabilityTypes != null ? new JpaToscaCapabilityTypes(copyConcept.capabilityTypes) : null);
165         this.relationshipTypes =
166             (copyConcept.relationshipTypes != null ? new JpaToscaRelationshipTypes(copyConcept.relationshipTypes)
167                 : null);
168         this.nodeTypes = (copyConcept.nodeTypes != null ? new JpaToscaNodeTypes(copyConcept.nodeTypes) : null);
169         this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
170         this.topologyTemplate =
171             (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate)
172                 : null);
173     }
174
175     /**
176      * Authorative constructor.
177      *
178      * @param authorativeConcept the authorative concept to copy from
179      */
180     public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
181         this.fromAuthorative(authorativeConcept);
182     }
183
184     @Override
185     public ToscaServiceTemplate toAuthorative() {
186         final var toscaServiceTemplate = new ToscaServiceTemplate();
187
188         super.setToscaEntity(toscaServiceTemplate);
189         super.toAuthorative();
190
191         toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
192
193         if (dataTypes != null) {
194             toscaServiceTemplate.setDataTypes(flattenMap(dataTypes.toAuthorative()));
195         }
196
197         if (capabilityTypes != null) {
198             toscaServiceTemplate.setCapabilityTypes(flattenMap(capabilityTypes.toAuthorative()));
199         }
200
201         if (relationshipTypes != null) {
202             toscaServiceTemplate.setRelationshipTypes(flattenMap(relationshipTypes.toAuthorative()));
203         }
204
205         if (nodeTypes != null) {
206             toscaServiceTemplate.setNodeTypes(flattenMap(nodeTypes.toAuthorative()));
207         }
208
209         if (policyTypes != null) {
210             toscaServiceTemplate.setPolicyTypes(flattenMap(policyTypes.toAuthorative()));
211         }
212
213         if (topologyTemplate != null) {
214             toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
215         }
216
217         return toscaServiceTemplate;
218     }
219
220     /**
221      * Takes a list of maps and flattens it into a single map.
222      *
223      * @param list list to be flattened
224      * @return a map containing all the elements from the list of maps
225      */
226     private <V> Map<String, V> flattenMap(List<Map<String, V>> list) {
227         Map<String, V> result = new LinkedHashMap<>();
228
229         for (Map<String, V> map : list) {
230             result.putAll(map);
231         }
232
233         return result;
234     }
235
236     @Override
237     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
238         super.fromAuthorative(toscaServiceTemplate);
239
240         if (toscaServiceTemplate.getDefinedName() == null) {
241             getKey().setName(DEFAULT_NAME);
242         }
243
244         if (toscaServiceTemplate.getDefinedVersion() == null) {
245             getKey().setVersion(DEFAULT_VERSION);
246         }
247
248         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
249
250         if (toscaServiceTemplate.getDataTypes() != null) {
251             dataTypes = new JpaToscaDataTypes();
252             dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
253         }
254
255         if (toscaServiceTemplate.getCapabilityTypes() != null) {
256             capabilityTypes = new JpaToscaCapabilityTypes();
257             capabilityTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getCapabilityTypes()));
258         }
259
260         if (toscaServiceTemplate.getRelationshipTypes() != null) {
261             relationshipTypes = new JpaToscaRelationshipTypes();
262             relationshipTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getRelationshipTypes()));
263         }
264
265         if (toscaServiceTemplate.getNodeTypes() != null) {
266             nodeTypes = new JpaToscaNodeTypes();
267             nodeTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getNodeTypes()));
268         }
269
270         if (toscaServiceTemplate.getPolicyTypes() != null) {
271             policyTypes = new JpaToscaPolicyTypes();
272             policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
273         }
274
275         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
276             topologyTemplate = new JpaToscaTopologyTemplate();
277             topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
278         }
279     }
280
281     @Override
282     public List<PfKey> getKeys() {
283         final List<PfKey> keyList = super.getKeys();
284
285         if (dataTypes != null) {
286             keyList.addAll(dataTypes.getKeys());
287         }
288
289         if (capabilityTypes != null) {
290             keyList.addAll(capabilityTypes.getKeys());
291         }
292
293         if (relationshipTypes != null) {
294             keyList.addAll(relationshipTypes.getKeys());
295         }
296
297         if (nodeTypes != null) {
298             keyList.addAll(nodeTypes.getKeys());
299         }
300
301         if (policyTypes != null) {
302             keyList.addAll(policyTypes.getKeys());
303         }
304
305         if (topologyTemplate != null) {
306             keyList.addAll(topologyTemplate.getKeys());
307         }
308
309         return keyList;
310     }
311
312     @Override
313     public void clean() {
314         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
315
316         if (dataTypes != null) {
317             dataTypes.clean();
318         }
319
320         if (capabilityTypes != null) {
321             capabilityTypes.clean();
322         }
323
324         if (relationshipTypes != null) {
325             relationshipTypes.clean();
326         }
327
328         if (nodeTypes != null) {
329             nodeTypes.clean();
330         }
331
332         if (policyTypes != null) {
333             policyTypes.clean();
334         }
335
336         if (topologyTemplate != null) {
337             topologyTemplate.clean();
338         }
339     }
340
341     @Override
342     public BeanValidationResult validate(String fieldName) {
343         BeanValidationResult result = super.validate(fieldName);
344
345         // No point in validating cross-references if the structure of the individual parts are not valid
346         if (!result.isValid()) {
347             return result;
348         }
349
350         validateReferencedDataTypes(result);
351
352         validatePolicyTypesInPolicies(result);
353
354         return result;
355     }
356
357     @Override
358     public int compareTo(final PfConcept otherConcept) {
359         int result = compareToWithoutEntities(otherConcept);
360         if (result != 0) {
361             return result;
362         }
363
364         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
365
366         result = ObjectUtils.compare(dataTypes, other.dataTypes);
367         if (result != 0) {
368             return result;
369         }
370
371         result = ObjectUtils.compare(capabilityTypes, other.capabilityTypes);
372         if (result != 0) {
373             return result;
374         }
375
376         result = ObjectUtils.compare(relationshipTypes, other.relationshipTypes);
377         if (result != 0) {
378             return result;
379         }
380
381         result = ObjectUtils.compare(nodeTypes, other.nodeTypes);
382         if (result != 0) {
383             return result;
384         }
385
386         result = ObjectUtils.compare(policyTypes, other.policyTypes);
387         if (result != 0) {
388             return result;
389         }
390
391         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
392     }
393
394     /**
395      * Compare this service template to another service template, ignoring contained entitites.
396      *
397      * @param otherConcept the other topology template
398      * @return the result of the comparison
399      */
400     public int compareToWithoutEntities(final PfConcept otherConcept) {
401         if (otherConcept == null) {
402             return -1;
403         }
404         if (this == otherConcept) {
405             return 0;
406         }
407         if (getClass() != otherConcept.getClass()) {
408             return getClass().getName().compareTo(otherConcept.getClass().getName());
409         }
410
411         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
412         if (!super.equals(other)) {
413             return super.compareTo(other);
414         }
415
416         return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
417     }
418
419     /**
420      * Validate that all data types referenced in policy types exist.
421      *
422      * @param result where the results are added
423      */
424     private void validateReferencedDataTypes(final BeanValidationResult result) {
425         if (policyTypes == null) {
426             return;
427         }
428
429         if (dataTypes != null) {
430             for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
431                 validateReferencedDataTypesExists(dataType.getReferencedDataTypes(), result);
432             }
433         }
434
435         for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
436             validateReferencedDataTypesExists(policyType.getReferencedDataTypes(), result);
437         }
438     }
439
440     /**
441      * Validate that the referenced data types exist for a collection of data type keys.
442      *
443      * @param dataTypeKeyCollection the data type key collection
444      * @param result                where the results are added
445      */
446     private void validateReferencedDataTypesExists(final Collection<PfConceptKey> dataTypeKeyCollection,
447                                                    final BeanValidationResult result) {
448         for (PfConceptKey dataTypeKey : dataTypeKeyCollection) {
449             if (dataTypes == null || dataTypes.get(dataTypeKey) == null) {
450                 addResult(result, "data type", dataTypeKey.getId(), NOT_FOUND);
451             }
452         }
453     }
454
455     /**
456      * Validate that all policy types referenced in policies exist.
457      *
458      * @param result where the results are added
459      */
460     private void validatePolicyTypesInPolicies(BeanValidationResult result) {
461         if (topologyTemplate == null || topologyTemplate.getPolicies() == null
462             || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
463             return;
464         }
465
466         if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) {
467             addResult(result, "policyTypes", policyTypes,
468                 "no policy types are defined on the service template for the policies in the topology template");
469             return;
470         }
471
472         for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
473             if (policyTypes.get(policy.getType()) == null) {
474                 addResult(result, "policy type", policy.getType().getId(), NOT_FOUND);
475             }
476         }
477     }
478 }