Merge "Implement validation and hierarchical get"
[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
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import javax.persistence.CascadeType;
33 import javax.persistence.Column;
34 import javax.persistence.Entity;
35 import javax.persistence.FetchType;
36 import javax.persistence.Inheritance;
37 import javax.persistence.InheritanceType;
38 import javax.persistence.JoinColumn;
39 import javax.persistence.JoinColumns;
40 import javax.persistence.OneToOne;
41 import javax.persistence.Table;
42
43 import lombok.Data;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
46
47 import org.apache.commons.lang3.ObjectUtils;
48 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfValidationMessage;
54 import org.onap.policy.models.base.PfValidationResult;
55 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
59
60 /**
61  * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
62  * are implemented.
63  *
64  * @author Liam Fallon (liam.fallon@est.tech)
65  */
66
67 @Entity
68 @Table(name = "ToscaServiceTemplate")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @Data
71 @EqualsAndHashCode(callSuper = true)
72 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
73         implements PfAuthorative<ToscaServiceTemplate> {
74     private static final long serialVersionUID = 8084846046148349401L;
75
76     public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0";
77     public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
78     public static final String DEFAULT_VERSION = "1.0.0";
79
80     // @formatter:off
81     @Column
82     @SerializedName("tosca_definitions_version")
83     private String toscaDefinitionsVersion;
84
85     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
86     @JoinColumns(
87             {
88                 @JoinColumn(name = "dataTypesName",    referencedColumnName = "name"),
89                 @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version")
90             }
91         )
92     @SerializedName("data_types")
93     private JpaToscaDataTypes dataTypes;
94
95     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
96     @JoinColumns(
97             {
98                 @JoinColumn(name = "policyTypesName",    referencedColumnName = "name"),
99                 @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
100             }
101         )
102     @SerializedName("policy_types")
103     private JpaToscaPolicyTypes policyTypes;
104
105     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
106     @JoinColumns(
107             {
108                 @JoinColumn(name = "topologyTemplateParentKeyName",    referencedColumnName = "parentKeyName"),
109                 @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion"),
110                 @JoinColumn(name = "topologyTemplateParentLocalName",  referencedColumnName = "parentLocalName"),
111                 @JoinColumn(name = "topologyTemplateLocalName",        referencedColumnName = "localName")
112             }
113         )
114     @SerializedName("topology_template")
115     private JpaToscaTopologyTemplate topologyTemplate;
116     // @formatter:on
117
118     /**
119      * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
120      */
121     public JpaToscaServiceTemplate() {
122         this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
123     }
124
125     /**
126      * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
127      *
128      * @param key the key
129      */
130     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
131         this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
132     }
133
134     /**
135      * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
136      *
137      * @param key the key
138      * @param toscaDefinitionsVersion the TOSCA version string
139      */
140     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
141         super(key);
142         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
143     }
144
145     /**
146      * Copy constructor.
147      *
148      * @param copyConcept the concept to copy from
149      */
150     public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
151         super(copyConcept);
152         this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
153         this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
154         this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
155         this.topologyTemplate =
156                 (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate)
157                         : null);
158     }
159
160     /**
161      * Authorative constructor.
162      *
163      * @param authorativeConcept the authorative concept to copy from
164      */
165     public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
166         this.fromAuthorative(authorativeConcept);
167     }
168
169     @Override
170     public ToscaServiceTemplate toAuthorative() {
171         final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
172
173         super.setToscaEntity(toscaServiceTemplate);
174         super.toAuthorative();
175
176         toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
177
178         if (dataTypes != null) {
179             toscaServiceTemplate.setDataTypes(new LinkedHashMap<>());
180             List<Map<String, ToscaDataType>> dataTypeMapList = dataTypes.toAuthorative();
181             for (Map<String, ToscaDataType> dataTypeMap : dataTypeMapList) {
182                 toscaServiceTemplate.getDataTypes().putAll(dataTypeMap);
183             }
184         }
185
186         if (policyTypes != null) {
187             toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
188             List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative();
189             for (Map<String, ToscaPolicyType> policyTypeMap : policyTypeMapList) {
190                 toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap);
191             }
192         }
193
194         if (topologyTemplate != null) {
195             toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
196         }
197
198         return toscaServiceTemplate;
199     }
200
201     @Override
202     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
203         super.fromAuthorative(toscaServiceTemplate);
204
205         if (PfKey.NULL_KEY_NAME.equals(getKey().getName())) {
206             getKey().setName(DEFAULT_NAME);
207         }
208
209         if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
210             getKey().setVersion(DEFAULT_VERSION);
211         }
212
213         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
214
215         if (toscaServiceTemplate.getDataTypes() != null) {
216             dataTypes = new JpaToscaDataTypes();
217             dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
218         }
219
220         if (toscaServiceTemplate.getPolicyTypes() != null) {
221             policyTypes = new JpaToscaPolicyTypes();
222             policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
223         }
224
225         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
226             topologyTemplate = new JpaToscaTopologyTemplate();
227             topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
228         }
229     }
230
231     @Override
232     public List<PfKey> getKeys() {
233         final List<PfKey> keyList = super.getKeys();
234
235         if (dataTypes != null) {
236             keyList.addAll(dataTypes.getKeys());
237         }
238
239         if (policyTypes != null) {
240             keyList.addAll(policyTypes.getKeys());
241         }
242
243         if (topologyTemplate != null) {
244             keyList.addAll(topologyTemplate.getKeys());
245         }
246
247         return keyList;
248     }
249
250     @Override
251     public void clean() {
252         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
253
254         if (dataTypes != null) {
255             dataTypes.clean();
256         }
257
258         if (policyTypes != null) {
259             policyTypes.clean();
260         }
261
262         if (topologyTemplate != null) {
263             topologyTemplate.clean();
264         }
265     }
266
267     @Override
268     public PfValidationResult validate(final PfValidationResult resultIn) {
269         PfValidationResult result = super.validate(resultIn);
270
271         if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
272             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
273                     "service template tosca definitions version may not be null"));
274         }
275
276         if (dataTypes != null) {
277             result = dataTypes.validate(result);
278         }
279
280         if (policyTypes != null) {
281             result = policyTypes.validate(result);
282         }
283
284         if (topologyTemplate != null) {
285             result = topologyTemplate.validate(result);
286         }
287
288         // No point in validating cross references if the structure of the individual parts are not valid
289         if (!result.isOk()) {
290             return result;
291         }
292
293         validateReferencedDataTypes(result);
294
295         return validatePolicyTypesInPolicies(result);
296     }
297
298     @Override
299     public int compareTo(final PfConcept otherConcept) {
300         int result = compareToWithoutEntities(otherConcept);
301         if (result != 0) {
302             return result;
303         }
304
305         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
306
307         result = ObjectUtils.compare(dataTypes, other.dataTypes);
308         if (result != 0) {
309             return result;
310         }
311
312         result = ObjectUtils.compare(policyTypes, other.policyTypes);
313         if (result != 0) {
314             return result;
315         }
316
317         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
318     }
319
320     /**
321      * Compare this service template to another service template, ignoring contained entitites.
322      *
323      * @param otherConcept the other topology template
324      * @return the result of the comparison
325      */
326     public int compareToWithoutEntities(final PfConcept otherConcept) {
327         if (otherConcept == null) {
328             return -1;
329         }
330         if (this == otherConcept) {
331             return 0;
332         }
333         if (getClass() != otherConcept.getClass()) {
334             return getClass().getName().compareTo(otherConcept.getClass().getName());
335         }
336
337         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
338         if (!super.equals(other)) {
339             return super.compareTo(other);
340         }
341
342         return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
343     }
344
345     /**
346      * Validate that all data types referenced in policy types exist.
347      *
348      * @param result the validation result object to use for the validation result
349      * @return the validation result object
350      */
351     private PfValidationResult validateReferencedDataTypes(final PfValidationResult result) {
352         if (policyTypes == null) {
353             return result;
354         }
355
356         if (dataTypes != null) {
357             for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
358                 validateReferencedDataTypesExists(dataType.getKey(), dataType.getReferencedDataTypes(), result);
359             }
360         }
361
362         for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
363             validateReferencedDataTypesExists(policyType.getKey(), policyType.getReferencedDataTypes(), result);
364         }
365
366         return result;
367     }
368
369     /**
370      * Validate that the referenced data types exist for a collection of data type keys.
371      *
372      * @param referencingEntityKey the key of the referencing entity
373      * @param dataTypeKeyCollection the data type key collection
374      * @param result the result of the validation
375      */
376     private void validateReferencedDataTypesExists(final PfConceptKey referencingEntityKey,
377             final Collection<PfConceptKey> dataTypeKeyCollection, final PfValidationResult result) {
378         for (PfConceptKey dataTypeKey : dataTypeKeyCollection) {
379             if (dataTypes == null || dataTypes.get(dataTypeKey) == null) {
380                 result.addValidationMessage(new PfValidationMessage(referencingEntityKey, this.getClass(),
381                         ValidationResult.INVALID, "referenced data type " + dataTypeKey.getId() + " not found"));
382             }
383         }
384     }
385
386     /**
387      * Validate that all policy types referenced in policies exist.
388      *
389      * @param result the validation result object to use for the validation result
390      * @return the validation result object
391      */
392     private PfValidationResult validatePolicyTypesInPolicies(PfValidationResult result) {
393         if (topologyTemplate == null || topologyTemplate.getPolicies() == null
394                 || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
395             return result;
396         }
397
398         if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) {
399             result.addValidationMessage(new PfValidationMessage(this.getKey(), this.getClass(),
400                     ValidationResult.INVALID,
401                     "no policy types are defined on the service template for the policies in the topology template"));
402             return result;
403         }
404
405         for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
406             if (policyTypes.get(policy.getType()) == null) {
407                 result.addValidationMessage(
408                         new PfValidationMessage(policy.getKey(), this.getClass(), ValidationResult.INVALID,
409                                 "policy type " + policy.getType().getId() + " referenced in policy not found"));
410             }
411         }
412
413         return result;
414     }
415 }