2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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
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.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;
60 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
63 * @author Liam Fallon (liam.fallon@est.tech)
67 @Table(name = "ToscaServiceTemplate")
68 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @EqualsAndHashCode(callSuper = true)
71 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
72 implements PfAuthorative<ToscaServiceTemplate> {
73 private static final long serialVersionUID = 8084846046148349401L;
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";
81 @SerializedName("tosca_definitions_version")
84 private String toscaDefinitionsVersion;
86 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
89 @JoinColumn(name = "dataTypesName", referencedColumnName = "name"),
90 @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version")
93 @SerializedName("data_types")
95 private JpaToscaDataTypes dataTypes;
97 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
100 @JoinColumn(name = "capabilityTypesName", referencedColumnName = "name"),
101 @JoinColumn(name = "capabilityTypesVersion", referencedColumnName = "version")
104 @SerializedName("capability_types")
106 private JpaToscaCapabilityTypes capabilityTypes;
108 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
111 @JoinColumn(name = "relationshipTypesName", referencedColumnName = "name"),
112 @JoinColumn(name = "relationshipTypesVersion", referencedColumnName = "version")
115 @SerializedName("relationship_types")
117 private JpaToscaRelationshipTypes relationshipTypes;
119 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
122 @JoinColumn(name = "nodeTypesName", referencedColumnName = "name"),
123 @JoinColumn(name = "nodeTypesVersion", referencedColumnName = "version")
126 @SerializedName("node_types")
128 private JpaToscaNodeTypes nodeTypes;
130 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
133 @JoinColumn(name = "policyTypesName", referencedColumnName = "name"),
134 @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
137 @SerializedName("policy_types")
139 private JpaToscaPolicyTypes policyTypes;
141 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
144 @JoinColumn(name = "topologyTemplateParentKeyName", referencedColumnName = "parentKeyName"),
145 @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion"),
146 @JoinColumn(name = "topologyTemplateParentLocalName", referencedColumnName = "parentLocalName"),
147 @JoinColumn(name = "topologyTemplateLocalName", referencedColumnName = "localName")
150 @SerializedName("topology_template")
152 private JpaToscaTopologyTemplate topologyTemplate;
156 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
158 public JpaToscaServiceTemplate() {
159 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
163 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
167 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
168 this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
172 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
175 * @param toscaDefinitionsVersion the TOSCA version string
177 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
179 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
185 * @param copyConcept the concept to copy from
187 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate 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)
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)
204 * Authorative constructor.
206 * @param authorativeConcept the authorative concept to copy from
208 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
209 this.fromAuthorative(authorativeConcept);
213 public ToscaServiceTemplate toAuthorative() {
214 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
216 super.setToscaEntity(toscaServiceTemplate);
217 super.toAuthorative();
219 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
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);
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);
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);
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);
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);
261 if (topologyTemplate != null) {
262 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
265 return toscaServiceTemplate;
269 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
270 super.fromAuthorative(toscaServiceTemplate);
272 if (toscaServiceTemplate.getDefinedName() == null) {
273 getKey().setName(DEFAULT_NAME);
276 if (toscaServiceTemplate.getDefinedVersion() == null) {
277 getKey().setVersion(DEFAULT_VERSION);
280 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
282 if (toscaServiceTemplate.getDataTypes() != null) {
283 dataTypes = new JpaToscaDataTypes();
284 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
287 if (toscaServiceTemplate.getCapabilityTypes() != null) {
288 capabilityTypes = new JpaToscaCapabilityTypes();
289 capabilityTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getCapabilityTypes()));
292 if (toscaServiceTemplate.getRelationshipTypes() != null) {
293 relationshipTypes = new JpaToscaRelationshipTypes();
294 relationshipTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getRelationshipTypes()));
297 if (toscaServiceTemplate.getNodeTypes() != null) {
298 nodeTypes = new JpaToscaNodeTypes();
299 nodeTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getNodeTypes()));
302 if (toscaServiceTemplate.getPolicyTypes() != null) {
303 policyTypes = new JpaToscaPolicyTypes();
304 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
307 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
308 topologyTemplate = new JpaToscaTopologyTemplate();
309 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
314 public List<PfKey> getKeys() {
315 final List<PfKey> keyList = super.getKeys();
317 if (dataTypes != null) {
318 keyList.addAll(dataTypes.getKeys());
321 if (capabilityTypes != null) {
322 keyList.addAll(capabilityTypes.getKeys());
325 if (relationshipTypes != null) {
326 keyList.addAll(relationshipTypes.getKeys());
329 if (nodeTypes != null) {
330 keyList.addAll(nodeTypes.getKeys());
333 if (policyTypes != null) {
334 keyList.addAll(policyTypes.getKeys());
337 if (topologyTemplate != null) {
338 keyList.addAll(topologyTemplate.getKeys());
345 public void clean() {
346 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
348 if (dataTypes != null) {
352 if (capabilityTypes != null) {
353 capabilityTypes.clean();
356 if (relationshipTypes != null) {
357 relationshipTypes.clean();
360 if (nodeTypes != null) {
364 if (policyTypes != null) {
368 if (topologyTemplate != null) {
369 topologyTemplate.clean();
374 public BeanValidationResult validate(String fieldName) {
375 BeanValidationResult result = super.validate(fieldName);
377 // No point in validating cross references if the structure of the individual parts are not valid
378 if (!result.isValid()) {
382 validateReferencedDataTypes(result);
384 validatePolicyTypesInPolicies(result);
390 public int compareTo(final PfConcept otherConcept) {
391 int result = compareToWithoutEntities(otherConcept);
396 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
398 result = ObjectUtils.compare(dataTypes, other.dataTypes);
403 result = ObjectUtils.compare(capabilityTypes, other.capabilityTypes);
408 result = ObjectUtils.compare(relationshipTypes, other.relationshipTypes);
413 result = ObjectUtils.compare(nodeTypes, other.nodeTypes);
418 result = ObjectUtils.compare(policyTypes, other.policyTypes);
423 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
427 * Compare this service template to another service template, ignoring contained entitites.
429 * @param otherConcept the other topology template
430 * @return the result of the comparison
432 public int compareToWithoutEntities(final PfConcept otherConcept) {
433 if (otherConcept == null) {
436 if (this == otherConcept) {
439 if (getClass() != otherConcept.getClass()) {
440 return getClass().getName().compareTo(otherConcept.getClass().getName());
443 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
444 if (!super.equals(other)) {
445 return super.compareTo(other);
448 return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
452 * Validate that all data types referenced in policy types exist.
454 * @param result the validation result object to use for the validation result
455 * @param result where the results are added
457 private void validateReferencedDataTypes(final BeanValidationResult result) {
458 if (policyTypes == null) {
462 if (dataTypes != null) {
463 for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
464 validateReferencedDataTypesExists(dataType.getReferencedDataTypes(), result);
468 for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
469 validateReferencedDataTypesExists(policyType.getReferencedDataTypes(), result);
474 * Validate that the referenced data types exist for a collection of data type keys.
476 * @param dataTypeKeyCollection the data type key collection
477 * @param result where the results are added
479 private void validateReferencedDataTypesExists(final Collection<PfConceptKey> dataTypeKeyCollection,
480 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);
489 * Validate that all policy types referenced in policies exist.
491 * @param result where the results are added
493 private void validatePolicyTypesInPolicies(BeanValidationResult result) {
494 if (topologyTemplate == null || topologyTemplate.getPolicies() == null
495 || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
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");
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);