2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.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.tosca.authorative.concepts.ToscaCapabilityType;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
57 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
60 * @author Liam Fallon (liam.fallon@est.tech)
64 @Table(name = "ToscaServiceTemplate")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @EqualsAndHashCode(callSuper = true)
68 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
69 implements PfAuthorative<ToscaServiceTemplate> {
70 private static final long serialVersionUID = 8084846046148349401L;
72 public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_1_0";
73 public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
74 public static final String DEFAULT_VERSION = "1.0.0";
78 @SerializedName("tosca_definitions_version")
79 private String toscaDefinitionsVersion;
81 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
84 @JoinColumn(name = "dataTypesName", referencedColumnName = "name"),
85 @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version")
88 @SerializedName("data_types")
89 private JpaToscaDataTypes dataTypes;
91 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
94 @JoinColumn(name = "capabilityTypesName", referencedColumnName = "name"),
95 @JoinColumn(name = "capabilityTypesVersion", referencedColumnName = "version")
98 @SerializedName("capability_types")
99 private JpaToscaCapabilityTypes capabilityTypes;
101 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
104 @JoinColumn(name = "relationshipTypesName", referencedColumnName = "name"),
105 @JoinColumn(name = "relationshipTypesVersion", referencedColumnName = "version")
108 @SerializedName("relationship_types")
109 private JpaToscaRelationshipTypes relationshipTypes;
111 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
114 @JoinColumn(name = "nodeTypesName", referencedColumnName = "name"),
115 @JoinColumn(name = "nodeTypesVersion", referencedColumnName = "version")
118 @SerializedName("node_types")
119 private JpaToscaNodeTypes nodeTypes;
121 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
124 @JoinColumn(name = "policyTypesName", referencedColumnName = "name"),
125 @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
128 @SerializedName("policy_types")
129 private JpaToscaPolicyTypes policyTypes;
131 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
134 @JoinColumn(name = "topologyTemplateParentKeyName", referencedColumnName = "parentKeyName"),
135 @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion"),
136 @JoinColumn(name = "topologyTemplateParentLocalName", referencedColumnName = "parentLocalName"),
137 @JoinColumn(name = "topologyTemplateLocalName", referencedColumnName = "localName")
140 @SerializedName("topology_template")
141 private JpaToscaTopologyTemplate topologyTemplate;
145 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
147 public JpaToscaServiceTemplate() {
148 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
152 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
156 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
157 this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
161 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
164 * @param toscaDefinitionsVersion the TOSCA version string
166 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
168 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
174 * @param copyConcept the concept to copy from
176 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
178 this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
179 this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
180 this.capabilityTypes =
181 (copyConcept.capabilityTypes != null ? new JpaToscaCapabilityTypes(copyConcept.capabilityTypes) : null);
182 this.relationshipTypes =
183 (copyConcept.relationshipTypes != null ? new JpaToscaRelationshipTypes(copyConcept.relationshipTypes)
185 this.nodeTypes = (copyConcept.nodeTypes != null ? new JpaToscaNodeTypes(copyConcept.nodeTypes) : null);
186 this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
187 this.topologyTemplate =
188 (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate)
193 * Authorative constructor.
195 * @param authorativeConcept the authorative concept to copy from
197 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
198 this.fromAuthorative(authorativeConcept);
202 public ToscaServiceTemplate toAuthorative() {
203 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
205 super.setToscaEntity(toscaServiceTemplate);
206 super.toAuthorative();
208 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
210 if (dataTypes != null) {
211 toscaServiceTemplate.setDataTypes(new LinkedHashMap<>());
212 List<Map<String, ToscaDataType>> dataTypeMapList = dataTypes.toAuthorative();
213 for (Map<String, ToscaDataType> dataTypeMap : dataTypeMapList) {
214 toscaServiceTemplate.getDataTypes().putAll(dataTypeMap);
218 if (capabilityTypes != null) {
219 toscaServiceTemplate.setCapabilityTypes(new LinkedHashMap<>());
220 List<Map<String, ToscaCapabilityType>> capabilityTypeMapList = capabilityTypes.toAuthorative();
221 for (Map<String, ToscaCapabilityType> capabilityTypeMap : capabilityTypeMapList) {
222 toscaServiceTemplate.getCapabilityTypes().putAll(capabilityTypeMap);
226 if (relationshipTypes != null) {
227 toscaServiceTemplate.setRelationshipTypes(new LinkedHashMap<>());
228 List<Map<String, ToscaRelationshipType>> relationshipTypeMapList = relationshipTypes.toAuthorative();
229 for (Map<String, ToscaRelationshipType> relationshipTypeMap : relationshipTypeMapList) {
230 toscaServiceTemplate.getRelationshipTypes().putAll(relationshipTypeMap);
234 if (nodeTypes != null) {
235 toscaServiceTemplate.setNodeTypes(new LinkedHashMap<>());
236 List<Map<String, ToscaNodeType>> nodeTypeMapList = nodeTypes.toAuthorative();
237 for (Map<String, ToscaNodeType> nodeTypeMap : nodeTypeMapList) {
238 toscaServiceTemplate.getNodeTypes().putAll(nodeTypeMap);
242 if (policyTypes != null) {
243 toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
244 List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative();
245 for (Map<String, ToscaPolicyType> policyTypeMap : policyTypeMapList) {
246 toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap);
250 if (topologyTemplate != null) {
251 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
254 return toscaServiceTemplate;
258 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
259 super.fromAuthorative(toscaServiceTemplate);
261 if (PfKey.NULL_KEY_NAME.equals(getKey().getName())) {
262 getKey().setName(DEFAULT_NAME);
265 if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
266 getKey().setVersion(DEFAULT_VERSION);
269 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
271 if (toscaServiceTemplate.getDataTypes() != null) {
272 dataTypes = new JpaToscaDataTypes();
273 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
276 if (toscaServiceTemplate.getCapabilityTypes() != null) {
277 capabilityTypes = new JpaToscaCapabilityTypes();
278 capabilityTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getCapabilityTypes()));
281 if (toscaServiceTemplate.getRelationshipTypes() != null) {
282 relationshipTypes = new JpaToscaRelationshipTypes();
283 relationshipTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getRelationshipTypes()));
286 if (toscaServiceTemplate.getNodeTypes() != null) {
287 nodeTypes = new JpaToscaNodeTypes();
288 nodeTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getNodeTypes()));
291 if (toscaServiceTemplate.getPolicyTypes() != null) {
292 policyTypes = new JpaToscaPolicyTypes();
293 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
296 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
297 topologyTemplate = new JpaToscaTopologyTemplate();
298 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
303 public List<PfKey> getKeys() {
304 final List<PfKey> keyList = super.getKeys();
306 if (dataTypes != null) {
307 keyList.addAll(dataTypes.getKeys());
310 if (capabilityTypes != null) {
311 keyList.addAll(capabilityTypes.getKeys());
314 if (relationshipTypes != null) {
315 keyList.addAll(relationshipTypes.getKeys());
318 if (nodeTypes != null) {
319 keyList.addAll(nodeTypes.getKeys());
322 if (policyTypes != null) {
323 keyList.addAll(policyTypes.getKeys());
326 if (topologyTemplate != null) {
327 keyList.addAll(topologyTemplate.getKeys());
334 public void clean() {
335 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
337 if (dataTypes != null) {
341 if (capabilityTypes != null) {
342 capabilityTypes.clean();
345 if (relationshipTypes != null) {
346 relationshipTypes.clean();
349 if (nodeTypes != null) {
353 if (policyTypes != null) {
357 if (topologyTemplate != null) {
358 topologyTemplate.clean();
363 public BeanValidationResult validate(String fieldName) {
364 BeanValidationResult result = super.validate(fieldName);
366 result.addResult(validateNotBlank("toscaDefinitionsVersion", toscaDefinitionsVersion, true));
368 validateOptional(result, "dataTypes", dataTypes);
369 validateOptional(result, "capabilityTypes", capabilityTypes);
370 validateOptional(result, "relationshipTypes", relationshipTypes);
371 validateOptional(result, "nodeTypes", nodeTypes);
372 validateOptional(result, "policyTypes", policyTypes);
373 validateOptional(result, "topologyTemplate", topologyTemplate);
375 // No point in validating cross references if the structure of the individual parts are not valid
376 if (!result.isValid()) {
380 validateReferencedDataTypes(result);
382 validatePolicyTypesInPolicies(result);
388 public int compareTo(final PfConcept otherConcept) {
389 int result = compareToWithoutEntities(otherConcept);
394 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
396 result = ObjectUtils.compare(dataTypes, other.dataTypes);
401 result = ObjectUtils.compare(capabilityTypes, other.capabilityTypes);
406 result = ObjectUtils.compare(relationshipTypes, other.relationshipTypes);
411 result = ObjectUtils.compare(nodeTypes, other.nodeTypes);
416 result = ObjectUtils.compare(policyTypes, other.policyTypes);
421 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
425 * Compare this service template to another service template, ignoring contained entitites.
427 * @param otherConcept the other topology template
428 * @return the result of the comparison
430 public int compareToWithoutEntities(final PfConcept otherConcept) {
431 if (otherConcept == null) {
434 if (this == otherConcept) {
437 if (getClass() != otherConcept.getClass()) {
438 return getClass().getName().compareTo(otherConcept.getClass().getName());
441 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
442 if (!super.equals(other)) {
443 return super.compareTo(other);
446 return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
450 * Validate that all data types referenced in policy types exist.
452 * @param result the validation result object to use for the validation result
453 * @param result where the results are added
455 private void validateReferencedDataTypes(final BeanValidationResult result) {
456 if (policyTypes == null) {
460 if (dataTypes != null) {
461 for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
462 validateReferencedDataTypesExists(dataType.getReferencedDataTypes(), result);
466 for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
467 validateReferencedDataTypesExists(policyType.getReferencedDataTypes(), result);
472 * Validate that the referenced data types exist for a collection of data type keys.
474 * @param dataTypeKeyCollection the data type key collection
475 * @param result where the results are added
477 private void validateReferencedDataTypesExists(
478 final Collection<PfConceptKey> dataTypeKeyCollection, final BeanValidationResult result) {
479 for (PfConceptKey dataTypeKey : dataTypeKeyCollection) {
480 if (dataTypes == null || dataTypes.get(dataTypeKey) == null) {
481 addResult(result, "data type", dataTypeKey.getId(), NOT_FOUND);
487 * Validate that all policy types referenced in policies exist.
489 * @param result where the results are added
491 private void validatePolicyTypesInPolicies(BeanValidationResult result) {
492 if (topologyTemplate == null || topologyTemplate.getPolicies() == null
493 || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
497 if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) {
498 addResult(result, "policyTypes", policyTypes,
499 "no policy types are defined on the service template for the policies in the topology template");
503 for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
504 if (policyTypes.get(policy.getType()) == null) {
505 addResult(result, "policy type", policy.getType().getId(), NOT_FOUND);