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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
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;
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;
55 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
58 * @author Liam Fallon (liam.fallon@est.tech)
62 @Table(name = "ToscaServiceTemplate")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @EqualsAndHashCode(callSuper = true)
66 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
67 implements PfAuthorative<ToscaServiceTemplate> {
69 private static final long serialVersionUID = 8084846046148349401L;
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";
77 @SerializedName("tosca_definitions_version")
80 private String toscaDefinitionsVersion;
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")
87 private JpaToscaDataTypes dataTypes;
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")
94 private JpaToscaCapabilityTypes capabilityTypes;
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")
101 private JpaToscaRelationshipTypes relationshipTypes;
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")
108 private JpaToscaNodeTypes nodeTypes;
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")
115 private JpaToscaPolicyTypes policyTypes;
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")
124 private JpaToscaTopologyTemplate topologyTemplate;
128 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
130 public JpaToscaServiceTemplate() {
131 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
135 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
139 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
140 this(key, DEFAULT_TOSCA_DEFINITIONS_VERSION);
144 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
147 * @param toscaDefinitionsVersion the TOSCA version string
149 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
151 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
157 * @param copyConcept the concept to copy from
159 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate 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)
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)
176 * Authorative constructor.
178 * @param authorativeConcept the authorative concept to copy from
180 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
181 this.fromAuthorative(authorativeConcept);
185 public ToscaServiceTemplate toAuthorative() {
186 final var toscaServiceTemplate = new ToscaServiceTemplate();
188 super.setToscaEntity(toscaServiceTemplate);
189 super.toAuthorative();
191 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
193 if (dataTypes != null) {
194 toscaServiceTemplate.setDataTypes(flattenMap(dataTypes.toAuthorative()));
197 if (capabilityTypes != null) {
198 toscaServiceTemplate.setCapabilityTypes(flattenMap(capabilityTypes.toAuthorative()));
201 if (relationshipTypes != null) {
202 toscaServiceTemplate.setRelationshipTypes(flattenMap(relationshipTypes.toAuthorative()));
205 if (nodeTypes != null) {
206 toscaServiceTemplate.setNodeTypes(flattenMap(nodeTypes.toAuthorative()));
209 if (policyTypes != null) {
210 toscaServiceTemplate.setPolicyTypes(flattenMap(policyTypes.toAuthorative()));
213 if (topologyTemplate != null) {
214 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
217 return toscaServiceTemplate;
221 * Takes a list of maps and flattens it into a single map.
223 * @param list list to be flattened
224 * @return a map containing all the elements from the list of maps
226 private <V> Map<String, V> flattenMap(List<Map<String, V>> list) {
227 Map<String, V> result = new LinkedHashMap<>();
229 for (Map<String, V> map : list) {
237 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
238 super.fromAuthorative(toscaServiceTemplate);
240 if (toscaServiceTemplate.getDefinedName() == null) {
241 getKey().setName(DEFAULT_NAME);
244 if (toscaServiceTemplate.getDefinedVersion() == null) {
245 getKey().setVersion(DEFAULT_VERSION);
248 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
250 if (toscaServiceTemplate.getDataTypes() != null) {
251 dataTypes = new JpaToscaDataTypes();
252 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
255 if (toscaServiceTemplate.getCapabilityTypes() != null) {
256 capabilityTypes = new JpaToscaCapabilityTypes();
257 capabilityTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getCapabilityTypes()));
260 if (toscaServiceTemplate.getRelationshipTypes() != null) {
261 relationshipTypes = new JpaToscaRelationshipTypes();
262 relationshipTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getRelationshipTypes()));
265 if (toscaServiceTemplate.getNodeTypes() != null) {
266 nodeTypes = new JpaToscaNodeTypes();
267 nodeTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getNodeTypes()));
270 if (toscaServiceTemplate.getPolicyTypes() != null) {
271 policyTypes = new JpaToscaPolicyTypes();
272 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
275 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
276 topologyTemplate = new JpaToscaTopologyTemplate();
277 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
282 public List<PfKey> getKeys() {
283 final List<PfKey> keyList = super.getKeys();
285 if (dataTypes != null) {
286 keyList.addAll(dataTypes.getKeys());
289 if (capabilityTypes != null) {
290 keyList.addAll(capabilityTypes.getKeys());
293 if (relationshipTypes != null) {
294 keyList.addAll(relationshipTypes.getKeys());
297 if (nodeTypes != null) {
298 keyList.addAll(nodeTypes.getKeys());
301 if (policyTypes != null) {
302 keyList.addAll(policyTypes.getKeys());
305 if (topologyTemplate != null) {
306 keyList.addAll(topologyTemplate.getKeys());
313 public void clean() {
314 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
316 if (dataTypes != null) {
320 if (capabilityTypes != null) {
321 capabilityTypes.clean();
324 if (relationshipTypes != null) {
325 relationshipTypes.clean();
328 if (nodeTypes != null) {
332 if (policyTypes != null) {
336 if (topologyTemplate != null) {
337 topologyTemplate.clean();
342 public BeanValidationResult validate(String fieldName) {
343 BeanValidationResult result = super.validate(fieldName);
345 // No point in validating cross-references if the structure of the individual parts are not valid
346 if (!result.isValid()) {
350 validateReferencedDataTypes(result);
352 validatePolicyTypesInPolicies(result);
358 public int compareTo(final PfConcept otherConcept) {
359 int result = compareToWithoutEntities(otherConcept);
364 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
366 result = ObjectUtils.compare(dataTypes, other.dataTypes);
371 result = ObjectUtils.compare(capabilityTypes, other.capabilityTypes);
376 result = ObjectUtils.compare(relationshipTypes, other.relationshipTypes);
381 result = ObjectUtils.compare(nodeTypes, other.nodeTypes);
386 result = ObjectUtils.compare(policyTypes, other.policyTypes);
391 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
395 * Compare this service template to another service template, ignoring contained entitites.
397 * @param otherConcept the other topology template
398 * @return the result of the comparison
400 public int compareToWithoutEntities(final PfConcept otherConcept) {
401 if (otherConcept == null) {
404 if (this == otherConcept) {
407 if (getClass() != otherConcept.getClass()) {
408 return getClass().getName().compareTo(otherConcept.getClass().getName());
411 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
412 if (!super.equals(other)) {
413 return super.compareTo(other);
416 return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
420 * Validate that all data types referenced in policy types exist.
422 * @param result where the results are added
424 private void validateReferencedDataTypes(final BeanValidationResult result) {
425 if (policyTypes == null) {
429 if (dataTypes != null) {
430 for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
431 validateReferencedDataTypesExists(dataType.getReferencedDataTypes(), result);
435 for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
436 validateReferencedDataTypesExists(policyType.getReferencedDataTypes(), result);
441 * Validate that the referenced data types exist for a collection of data type keys.
443 * @param dataTypeKeyCollection the data type key collection
444 * @param result where the results are added
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);
456 * Validate that all policy types referenced in policies exist.
458 * @param result where the results are added
460 private void validatePolicyTypesInPolicies(BeanValidationResult result) {
461 if (topologyTemplate == null || topologyTemplate.getPolicies() == null
462 || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
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");
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);