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;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.LinkedHashMap;
29 import java.util.List;
32 import javax.persistence.CascadeType;
33 import javax.persistence.Column;
34 import javax.persistence.Entity;
35 import javax.persistence.FetchType;
36 import javax.persistence.Inheritance;
37 import javax.persistence.InheritanceType;
38 import javax.persistence.JoinColumn;
39 import javax.persistence.JoinColumns;
40 import javax.persistence.OneToOne;
41 import javax.persistence.Table;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
47 import org.apache.commons.lang3.ObjectUtils;
48 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfValidationMessage;
54 import org.onap.policy.models.base.PfValidationResult;
55 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
61 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
64 * @author Liam Fallon (liam.fallon@est.tech)
68 @Table(name = "ToscaServiceTemplate")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
71 @EqualsAndHashCode(callSuper = true)
72 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
73 implements PfAuthorative<ToscaServiceTemplate> {
74 private static final long serialVersionUID = 8084846046148349401L;
76 public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0";
77 public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
78 public static final String DEFAULT_VERSION = "1.0.0";
82 @SerializedName("tosca_definitions_version")
83 private String toscaDefinitionsVersion;
85 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
88 @JoinColumn(name = "dataTypesName", referencedColumnName = "name"),
89 @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version")
92 @SerializedName("data_types")
93 private JpaToscaDataTypes dataTypes;
95 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
98 @JoinColumn(name = "policyTypesName", referencedColumnName = "name"),
99 @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
102 @SerializedName("policy_types")
103 private JpaToscaPolicyTypes policyTypes;
105 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
108 @JoinColumn(name = "topologyTemplateParentKeyName", referencedColumnName = "parentKeyName"),
109 @JoinColumn(name = "topologyTemplateParentKeyVersion", referencedColumnName = "parentKeyVersion"),
110 @JoinColumn(name = "topologyTemplateParentLocalName", referencedColumnName = "parentLocalName"),
111 @JoinColumn(name = "topologyTemplateLocalName", referencedColumnName = "localName")
114 @SerializedName("topology_template")
115 private JpaToscaTopologyTemplate topologyTemplate;
119 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
121 public JpaToscaServiceTemplate() {
122 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
126 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
130 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
131 this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
135 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
138 * @param toscaDefinitionsVersion the TOSCA version string
140 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
142 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
148 * @param copyConcept the concept to copy from
150 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
152 this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
153 this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
154 this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
155 this.topologyTemplate =
156 (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate)
161 * Authorative constructor.
163 * @param authorativeConcept the authorative concept to copy from
165 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
166 this.fromAuthorative(authorativeConcept);
170 public ToscaServiceTemplate toAuthorative() {
171 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
173 super.setToscaEntity(toscaServiceTemplate);
174 super.toAuthorative();
176 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
178 if (dataTypes != null) {
179 toscaServiceTemplate.setDataTypes(new LinkedHashMap<>());
180 List<Map<String, ToscaDataType>> dataTypeMapList = dataTypes.toAuthorative();
181 for (Map<String, ToscaDataType> dataTypeMap : dataTypeMapList) {
182 toscaServiceTemplate.getDataTypes().putAll(dataTypeMap);
186 if (policyTypes != null) {
187 toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
188 List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative();
189 for (Map<String, ToscaPolicyType> policyTypeMap : policyTypeMapList) {
190 toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap);
194 if (topologyTemplate != null) {
195 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
198 return toscaServiceTemplate;
202 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
203 super.fromAuthorative(toscaServiceTemplate);
205 if (PfKey.NULL_KEY_NAME.equals(getKey().getName())) {
206 getKey().setName(DEFAULT_NAME);
209 if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
210 getKey().setVersion(DEFAULT_VERSION);
213 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
215 if (toscaServiceTemplate.getDataTypes() != null) {
216 dataTypes = new JpaToscaDataTypes();
217 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
220 if (toscaServiceTemplate.getPolicyTypes() != null) {
221 policyTypes = new JpaToscaPolicyTypes();
222 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
225 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
226 topologyTemplate = new JpaToscaTopologyTemplate();
227 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
232 public List<PfKey> getKeys() {
233 final List<PfKey> keyList = super.getKeys();
235 if (dataTypes != null) {
236 keyList.addAll(dataTypes.getKeys());
239 if (policyTypes != null) {
240 keyList.addAll(policyTypes.getKeys());
243 if (topologyTemplate != null) {
244 keyList.addAll(topologyTemplate.getKeys());
251 public void clean() {
252 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
254 if (dataTypes != null) {
258 if (policyTypes != null) {
262 if (topologyTemplate != null) {
263 topologyTemplate.clean();
268 public PfValidationResult validate(final PfValidationResult resultIn) {
269 PfValidationResult result = super.validate(resultIn);
271 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
272 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
273 "service template tosca definitions version may not be null"));
276 if (dataTypes != null) {
277 result = dataTypes.validate(result);
280 if (policyTypes != null) {
281 result = policyTypes.validate(result);
284 if (topologyTemplate != null) {
285 result = topologyTemplate.validate(result);
288 // No point in validating cross references if the structure of the individual parts are not valid
289 if (!result.isOk()) {
293 validateReferencedDataTypes(result);
295 return validatePolicyTypesInPolicies(result);
299 public int compareTo(final PfConcept otherConcept) {
300 int result = compareToWithoutEntities(otherConcept);
305 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
307 result = ObjectUtils.compare(dataTypes, other.dataTypes);
312 result = ObjectUtils.compare(policyTypes, other.policyTypes);
317 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
321 * Compare this service template to another service template, ignoring contained entitites.
323 * @param otherConcept the other topology template
324 * @return the result of the comparison
326 public int compareToWithoutEntities(final PfConcept otherConcept) {
327 if (otherConcept == null) {
330 if (this == otherConcept) {
333 if (getClass() != otherConcept.getClass()) {
334 return getClass().getName().compareTo(otherConcept.getClass().getName());
337 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
338 if (!super.equals(other)) {
339 return super.compareTo(other);
342 return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
346 * Validate that all data types referenced in policy types exist.
348 * @param result the validation result object to use for the validation result
349 * @return the validation result object
351 private PfValidationResult validateReferencedDataTypes(final PfValidationResult result) {
352 if (policyTypes == null) {
356 if (dataTypes != null) {
357 for (JpaToscaDataType dataType : dataTypes.getAll(null)) {
358 validateReferencedDataTypesExists(dataType.getKey(), dataType.getReferencedDataTypes(), result);
362 for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
363 validateReferencedDataTypesExists(policyType.getKey(), policyType.getReferencedDataTypes(), result);
370 * Validate that the referenced data types exist for a collection of data type keys.
372 * @param referencingEntityKey the key of the referencing entity
373 * @param dataTypeKeyCollection the data type key collection
374 * @param result the result of the validation
376 private void validateReferencedDataTypesExists(final PfConceptKey referencingEntityKey,
377 final Collection<PfConceptKey> dataTypeKeyCollection, final PfValidationResult result) {
378 for (PfConceptKey dataTypeKey : dataTypeKeyCollection) {
379 if (dataTypes == null || dataTypes.get(dataTypeKey) == null) {
380 result.addValidationMessage(new PfValidationMessage(referencingEntityKey, this.getClass(),
381 ValidationResult.INVALID, "referenced data type " + dataTypeKey.getId() + " not found"));
387 * Validate that all policy types referenced in policies exist.
389 * @param result the validation result object to use for the validation result
390 * @return the validation result object
392 private PfValidationResult validatePolicyTypesInPolicies(PfValidationResult result) {
393 if (topologyTemplate == null || topologyTemplate.getPolicies() == null
394 || topologyTemplate.getPolicies().getConceptMap().isEmpty()) {
398 if (policyTypes == null || policyTypes.getConceptMap().isEmpty()) {
399 result.addValidationMessage(new PfValidationMessage(this.getKey(), this.getClass(),
400 ValidationResult.INVALID,
401 "no policy types are defined on the service template for the policies in the topology template"));
405 for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
406 if (policyTypes.get(policy.getType()) == null) {
407 result.addValidationMessage(
408 new PfValidationMessage(policy.getKey(), this.getClass(), ValidationResult.INVALID,
409 "policy type " + policy.getType().getId() + " referenced in policy not found"));