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 java.util.Collection;
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
28 import java.util.List;
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;
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> {
68 private static final long serialVersionUID = 8084846046148349401L;
70 public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_1_0";
71 public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
72 public static final String DEFAULT_VERSION = "1.0.0";
76 @SerializedName("tosca_definitions_version")
79 private String toscaDefinitionsVersion;
81 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
83 @JoinColumn(name = "dataTypesName", referencedColumnName = "name"),
84 @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version")
86 @SerializedName("data_types")
88 private JpaToscaDataTypes dataTypes;
90 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
92 @JoinColumn(name = "capabilityTypesName", referencedColumnName = "name"),
93 @JoinColumn(name = "capabilityTypesVersion", referencedColumnName = "version")
95 @SerializedName("capability_types")
97 private JpaToscaCapabilityTypes capabilityTypes;
99 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
101 @JoinColumn(name = "relationshipTypesName", referencedColumnName = "name"),
102 @JoinColumn(name = "relationshipTypesVersion", referencedColumnName = "version")
104 @SerializedName("relationship_types")
106 private JpaToscaRelationshipTypes relationshipTypes;
108 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
110 @JoinColumn(name = "nodeTypesName", referencedColumnName = "name"),
111 @JoinColumn(name = "nodeTypesVersion", referencedColumnName = "version")
113 @SerializedName("node_types")
115 private JpaToscaNodeTypes nodeTypes;
117 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
119 @JoinColumn(name = "policyTypesName", referencedColumnName = "name"),
120 @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
122 @SerializedName("policy_types")
124 private JpaToscaPolicyTypes policyTypes;
126 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
128 @JoinColumn(name = "topologyTemplateParentKeyName", referencedColumnName = "parentKeyName"),
129 @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion"),
130 @JoinColumn(name = "topologyTemplateParentLocalName", referencedColumnName = "parentLocalName"),
131 @JoinColumn(name = "topologyTemplateLocalName", referencedColumnName = "localName")
133 @SerializedName("topology_template")
135 private JpaToscaTopologyTemplate topologyTemplate;
139 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
141 public JpaToscaServiceTemplate() {
142 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
146 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
150 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
151 this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
155 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
158 * @param toscaDefinitionsVersion the TOSCA version string
160 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
162 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
168 * @param copyConcept the concept to copy from
170 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
172 this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
173 this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
174 this.capabilityTypes =
175 (copyConcept.capabilityTypes != null ? new JpaToscaCapabilityTypes(copyConcept.capabilityTypes) : null);
176 this.relationshipTypes =
177 (copyConcept.relationshipTypes != null ? new JpaToscaRelationshipTypes(copyConcept.relationshipTypes)
179 this.nodeTypes = (copyConcept.nodeTypes != null ? new JpaToscaNodeTypes(copyConcept.nodeTypes) : null);
180 this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
181 this.topologyTemplate =
182 (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate)
187 * Authorative constructor.
189 * @param authorativeConcept the authorative concept to copy from
191 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
192 this.fromAuthorative(authorativeConcept);
196 public ToscaServiceTemplate toAuthorative() {
197 final var toscaServiceTemplate = new ToscaServiceTemplate();
199 super.setToscaEntity(toscaServiceTemplate);
200 super.toAuthorative();
202 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
204 if (dataTypes != null) {
205 toscaServiceTemplate.setDataTypes(flattenMap(dataTypes.toAuthorative()));
208 if (capabilityTypes != null) {
209 toscaServiceTemplate.setCapabilityTypes(flattenMap(capabilityTypes.toAuthorative()));
212 if (relationshipTypes != null) {
213 toscaServiceTemplate.setRelationshipTypes(flattenMap(relationshipTypes.toAuthorative()));
216 if (nodeTypes != null) {
217 toscaServiceTemplate.setNodeTypes(flattenMap(nodeTypes.toAuthorative()));
220 if (policyTypes != null) {
221 toscaServiceTemplate.setPolicyTypes(flattenMap(policyTypes.toAuthorative()));
224 if (topologyTemplate != null) {
225 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
228 return toscaServiceTemplate;
232 * Takes a list of maps and flattens it into a single map.
234 * @param list list to be be flattened
235 * @return a map containing all of the elements from the list of maps
237 private <V> Map<String, V> flattenMap(List<Map<String, V>> list) {
238 Map<String, V> result = new LinkedHashMap<>();
240 for (Map<String, V> map : list) {
248 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
249 super.fromAuthorative(toscaServiceTemplate);
251 if (toscaServiceTemplate.getDefinedName() == null) {
252 getKey().setName(DEFAULT_NAME);
255 if (toscaServiceTemplate.getDefinedVersion() == null) {
256 getKey().setVersion(DEFAULT_VERSION);
259 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
261 if (toscaServiceTemplate.getDataTypes() != null) {
262 dataTypes = new JpaToscaDataTypes();
263 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
266 if (toscaServiceTemplate.getCapabilityTypes() != null) {
267 capabilityTypes = new JpaToscaCapabilityTypes();
268 capabilityTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getCapabilityTypes()));
271 if (toscaServiceTemplate.getRelationshipTypes() != null) {
272 relationshipTypes = new JpaToscaRelationshipTypes();
273 relationshipTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getRelationshipTypes()));
276 if (toscaServiceTemplate.getNodeTypes() != null) {
277 nodeTypes = new JpaToscaNodeTypes();
278 nodeTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getNodeTypes()));
281 if (toscaServiceTemplate.getPolicyTypes() != null) {
282 policyTypes = new JpaToscaPolicyTypes();
283 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
286 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
287 topologyTemplate = new JpaToscaTopologyTemplate();
288 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
293 public List<PfKey> getKeys() {
294 final List<PfKey> keyList = super.getKeys();
296 if (dataTypes != null) {
297 keyList.addAll(dataTypes.getKeys());
300 if (capabilityTypes != null) {
301 keyList.addAll(capabilityTypes.getKeys());
304 if (relationshipTypes != null) {
305 keyList.addAll(relationshipTypes.getKeys());
308 if (nodeTypes != null) {
309 keyList.addAll(nodeTypes.getKeys());
312 if (policyTypes != null) {
313 keyList.addAll(policyTypes.getKeys());
316 if (topologyTemplate != null) {
317 keyList.addAll(topologyTemplate.getKeys());
324 public void clean() {
325 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
327 if (dataTypes != null) {
331 if (capabilityTypes != null) {
332 capabilityTypes.clean();
335 if (relationshipTypes != null) {
336 relationshipTypes.clean();
339 if (nodeTypes != null) {
343 if (policyTypes != null) {
347 if (topologyTemplate != null) {
348 topologyTemplate.clean();
353 public BeanValidationResult validate(String fieldName) {
354 BeanValidationResult result = super.validate(fieldName);
356 // No point in validating cross references if the structure of the individual parts are not valid
357 if (!result.isValid()) {
361 validateReferencedDataTypes(result);
363 validatePolicyTypesInPolicies(result);
369 public int compareTo(final PfConcept otherConcept) {
370 int result = compareToWithoutEntities(otherConcept);
375 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
377 result = ObjectUtils.compare(dataTypes, other.dataTypes);
382 result = ObjectUtils.compare(capabilityTypes, other.capabilityTypes);
387 result = ObjectUtils.compare(relationshipTypes, other.relationshipTypes);
392 result = ObjectUtils.compare(nodeTypes, other.nodeTypes);
397 result = ObjectUtils.compare(policyTypes, other.policyTypes);
402 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
406 * Compare this service template to another service template, ignoring contained entitites.
408 * @param otherConcept the other topology template
409 * @return the result of the comparison
411 public int compareToWithoutEntities(final PfConcept otherConcept) {
412 if (otherConcept == null) {
415 if (this == otherConcept) {
418 if (getClass() != otherConcept.getClass()) {
419 return getClass().getName().compareTo(otherConcept.getClass().getName());
422 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
423 if (!super.equals(other)) {
424 return super.compareTo(other);
427 return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
431 * Validate that all data types referenced in policy types exist.
433 * @param result where the results are added
435 private void validateReferencedDataTypes(final BeanValidationResult result) {
436 if (policyTypes == null) {
440 if (dataTypes != null) {
441 for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
442 validateReferencedDataTypesExists(dataType.getReferencedDataTypes(), result);
446 for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
447 validateReferencedDataTypesExists(policyType.getReferencedDataTypes(), result);
452 * Validate that the referenced data types exist for a collection of data type keys.
454 * @param dataTypeKeyCollection the data type key collection
455 * @param result where the results are added
457 private void validateReferencedDataTypesExists(final Collection<PfConceptKey> dataTypeKeyCollection,
458 final BeanValidationResult result) {
459 for (PfConceptKey dataTypeKey : dataTypeKeyCollection) {
460 if (dataTypes == null || dataTypes.get(dataTypeKey) == null) {
461 addResult(result, "data type", dataTypeKey.getId(), NOT_FOUND);
467 * Validate that all policy types referenced in policies exist.
469 * @param result where the results are added
471 private void validatePolicyTypesInPolicies(BeanValidationResult result) {
472 if (topologyTemplate == null || topologyTemplate.getPolicies() == null
473 || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
477 if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) {
478 addResult(result, "policyTypes", policyTypes,
479 "no policy types are defined on the service template for the policies in the topology template");
483 for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
484 if (policyTypes.get(policy.getType()) == null) {
485 addResult(result, "policy type", policy.getType().getId(), NOT_FOUND);