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