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
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.utils.validation.ParameterValidationUtils;
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.base.PfValidationMessage;
50 import org.onap.policy.models.base.PfValidationResult;
51 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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")
82 private String toscaDefinitionsVersion;
84 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
87 @JoinColumn(name = "dataTypesName", referencedColumnName = "name"),
88 @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version")
91 @SerializedName("data_types")
92 private JpaToscaDataTypes dataTypes;
94 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
97 @JoinColumn(name = "capabilityTypesName", referencedColumnName = "name"),
98 @JoinColumn(name = "capabilityTypesVersion", referencedColumnName = "version")
101 @SerializedName("capability_types")
102 private JpaToscaCapabilityTypes capabilityTypes;
104 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
107 @JoinColumn(name = "relationshipTypesName", referencedColumnName = "name"),
108 @JoinColumn(name = "relationshipTypesVersion", referencedColumnName = "version")
111 @SerializedName("relationship_types")
112 private JpaToscaRelationshipTypes relationshipTypes;
114 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
117 @JoinColumn(name = "nodeTypesName", referencedColumnName = "name"),
118 @JoinColumn(name = "nodeTypesVersion", referencedColumnName = "version")
121 @SerializedName("node_types")
122 private JpaToscaNodeTypes nodeTypes;
124 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
127 @JoinColumn(name = "policyTypesName", referencedColumnName = "name"),
128 @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
131 @SerializedName("policy_types")
132 private JpaToscaPolicyTypes policyTypes;
134 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
137 @JoinColumn(name = "topologyTemplateParentKeyName", referencedColumnName = "parentKeyName"),
138 @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion"),
139 @JoinColumn(name = "topologyTemplateParentLocalName", referencedColumnName = "parentLocalName"),
140 @JoinColumn(name = "topologyTemplateLocalName", referencedColumnName = "localName")
143 @SerializedName("topology_template")
144 private JpaToscaTopologyTemplate topologyTemplate;
148 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
150 public JpaToscaServiceTemplate() {
151 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
155 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
159 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
160 this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
164 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
167 * @param toscaDefinitionsVersion the TOSCA version string
169 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
171 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
177 * @param copyConcept the concept to copy from
179 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
181 this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
182 this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
183 this.capabilityTypes =
184 (copyConcept.capabilityTypes != null ? new JpaToscaCapabilityTypes(copyConcept.capabilityTypes) : null);
185 this.relationshipTypes =
186 (copyConcept.relationshipTypes != null ? new JpaToscaRelationshipTypes(copyConcept.relationshipTypes)
188 this.nodeTypes = (copyConcept.nodeTypes != null ? new JpaToscaNodeTypes(copyConcept.nodeTypes) : null);
189 this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
190 this.topologyTemplate =
191 (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate)
196 * Authorative constructor.
198 * @param authorativeConcept the authorative concept to copy from
200 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
201 this.fromAuthorative(authorativeConcept);
205 public ToscaServiceTemplate toAuthorative() {
206 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
208 super.setToscaEntity(toscaServiceTemplate);
209 super.toAuthorative();
211 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
213 if (dataTypes != null) {
214 toscaServiceTemplate.setDataTypes(new LinkedHashMap<>());
215 List<Map<String, ToscaDataType>> dataTypeMapList = dataTypes.toAuthorative();
216 for (Map<String, ToscaDataType> dataTypeMap : dataTypeMapList) {
217 toscaServiceTemplate.getDataTypes().putAll(dataTypeMap);
221 if (capabilityTypes != null) {
222 toscaServiceTemplate.setCapabilityTypes(new LinkedHashMap<>());
223 List<Map<String, ToscaCapabilityType>> capabilityTypeMapList = capabilityTypes.toAuthorative();
224 for (Map<String, ToscaCapabilityType> capabilityTypeMap : capabilityTypeMapList) {
225 toscaServiceTemplate.getCapabilityTypes().putAll(capabilityTypeMap);
229 if (relationshipTypes != null) {
230 toscaServiceTemplate.setRelationshipTypes(new LinkedHashMap<>());
231 List<Map<String, ToscaRelationshipType>> relationshipTypeMapList = relationshipTypes.toAuthorative();
232 for (Map<String, ToscaRelationshipType> relationshipTypeMap : relationshipTypeMapList) {
233 toscaServiceTemplate.getRelationshipTypes().putAll(relationshipTypeMap);
237 if (nodeTypes != null) {
238 toscaServiceTemplate.setNodeTypes(new LinkedHashMap<>());
239 List<Map<String, ToscaNodeType>> nodeTypeMapList = nodeTypes.toAuthorative();
240 for (Map<String, ToscaNodeType> nodeTypeMap : nodeTypeMapList) {
241 toscaServiceTemplate.getNodeTypes().putAll(nodeTypeMap);
245 if (policyTypes != null) {
246 toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
247 List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative();
248 for (Map<String, ToscaPolicyType> policyTypeMap : policyTypeMapList) {
249 toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap);
253 if (topologyTemplate != null) {
254 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
257 return toscaServiceTemplate;
261 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
262 super.fromAuthorative(toscaServiceTemplate);
264 if (PfKey.NULL_KEY_NAME.equals(getKey().getName())) {
265 getKey().setName(DEFAULT_NAME);
268 if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
269 getKey().setVersion(DEFAULT_VERSION);
272 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
274 if (toscaServiceTemplate.getDataTypes() != null) {
275 dataTypes = new JpaToscaDataTypes();
276 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
279 if (toscaServiceTemplate.getCapabilityTypes() != null) {
280 capabilityTypes = new JpaToscaCapabilityTypes();
281 capabilityTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getCapabilityTypes()));
284 if (toscaServiceTemplate.getRelationshipTypes() != null) {
285 relationshipTypes = new JpaToscaRelationshipTypes();
286 relationshipTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getRelationshipTypes()));
289 if (toscaServiceTemplate.getNodeTypes() != null) {
290 nodeTypes = new JpaToscaNodeTypes();
291 nodeTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getNodeTypes()));
294 if (toscaServiceTemplate.getPolicyTypes() != null) {
295 policyTypes = new JpaToscaPolicyTypes();
296 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
299 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
300 topologyTemplate = new JpaToscaTopologyTemplate();
301 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
306 public List<PfKey> getKeys() {
307 final List<PfKey> keyList = super.getKeys();
309 if (dataTypes != null) {
310 keyList.addAll(dataTypes.getKeys());
313 if (capabilityTypes != null) {
314 keyList.addAll(capabilityTypes.getKeys());
317 if (relationshipTypes != null) {
318 keyList.addAll(relationshipTypes.getKeys());
321 if (nodeTypes != null) {
322 keyList.addAll(nodeTypes.getKeys());
325 if (policyTypes != null) {
326 keyList.addAll(policyTypes.getKeys());
329 if (topologyTemplate != null) {
330 keyList.addAll(topologyTemplate.getKeys());
337 public void clean() {
338 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
340 if (dataTypes != null) {
344 if (capabilityTypes != null) {
345 capabilityTypes.clean();
348 if (relationshipTypes != null) {
349 relationshipTypes.clean();
352 if (nodeTypes != null) {
356 if (policyTypes != null) {
360 if (topologyTemplate != null) {
361 topologyTemplate.clean();
366 public PfValidationResult validate(final PfValidationResult resultIn) {
367 PfValidationResult result = super.validate(resultIn);
369 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
370 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
371 "service template tosca definitions version may not be null"));
374 if (dataTypes != null) {
375 result = dataTypes.validate(result);
378 if (capabilityTypes != null) {
379 result = capabilityTypes.validate(result);
382 if (relationshipTypes != null) {
383 result = relationshipTypes.validate(result);
386 if (nodeTypes != null) {
387 result = nodeTypes.validate(result);
390 if (policyTypes != null) {
391 result = policyTypes.validate(result);
394 if (topologyTemplate != null) {
395 result = topologyTemplate.validate(result);
398 // No point in validating cross references if the structure of the individual parts are not valid
399 if (!result.isOk()) {
403 validateReferencedDataTypes(result);
405 return validatePolicyTypesInPolicies(result);
409 public int compareTo(final PfConcept otherConcept) {
410 int result = compareToWithoutEntities(otherConcept);
415 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
417 result = ObjectUtils.compare(dataTypes, other.dataTypes);
422 result = ObjectUtils.compare(capabilityTypes, other.capabilityTypes);
427 result = ObjectUtils.compare(relationshipTypes, other.relationshipTypes);
432 result = ObjectUtils.compare(nodeTypes, other.nodeTypes);
437 result = ObjectUtils.compare(policyTypes, other.policyTypes);
442 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
446 * Compare this service template to another service template, ignoring contained entitites.
448 * @param otherConcept the other topology template
449 * @return the result of the comparison
451 public int compareToWithoutEntities(final PfConcept otherConcept) {
452 if (otherConcept == null) {
455 if (this == otherConcept) {
458 if (getClass() != otherConcept.getClass()) {
459 return getClass().getName().compareTo(otherConcept.getClass().getName());
462 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
463 if (!super.equals(other)) {
464 return super.compareTo(other);
467 return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
471 * Validate that all data types referenced in policy types exist.
473 * @param result the validation result object to use for the validation result
474 * @return the validation result object
476 private PfValidationResult validateReferencedDataTypes(final PfValidationResult result) {
477 if (policyTypes == null) {
481 if (dataTypes != null) {
482 for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
483 validateReferencedDataTypesExists(dataType.getKey(), dataType.getReferencedDataTypes(), result);
487 for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
488 validateReferencedDataTypesExists(policyType.getKey(), policyType.getReferencedDataTypes(), result);
495 * Validate that the referenced data types exist for a collection of data type keys.
497 * @param referencingEntityKey the key of the referencing entity
498 * @param dataTypeKeyCollection the data type key collection
499 * @param result the result of the validation
501 private void validateReferencedDataTypesExists(final PfConceptKey referencingEntityKey,
502 final Collection<PfConceptKey> dataTypeKeyCollection, final PfValidationResult result) {
503 for (PfConceptKey dataTypeKey : dataTypeKeyCollection) {
504 if (dataTypes == null || dataTypes.get(dataTypeKey) == null) {
505 result.addValidationMessage(new PfValidationMessage(referencingEntityKey, this.getClass(),
506 ValidationResult.INVALID, "referenced data type " + dataTypeKey.getId() + " not found"));
512 * Validate that all policy types referenced in policies exist.
514 * @param result the validation result object to use for the validation result
515 * @return the validation result object
517 private PfValidationResult validatePolicyTypesInPolicies(PfValidationResult result) {
518 if (topologyTemplate == null || topologyTemplate.getPolicies() == null
519 || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
523 if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) {
524 result.addValidationMessage(new PfValidationMessage(this.getKey(), this.getClass(),
525 ValidationResult.INVALID,
526 "no policy types are defined on the service template for the policies in the topology template"));
530 for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
531 if (policyTypes.get(policy.getType()) == null) {
532 result.addValidationMessage(
533 new PfValidationMessage(policy.getKey(), this.getClass(), ValidationResult.INVALID,
534 "policy type " + policy.getType().getId() + " referenced in policy not found"));