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