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.Collections;
27 import java.util.LinkedHashMap;
28 import java.util.List;
31 import javax.persistence.CascadeType;
32 import javax.persistence.Column;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.JoinColumns;
39 import javax.persistence.OneToOne;
40 import javax.persistence.Table;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
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.base.PfValidationMessage;
53 import org.onap.policy.models.base.PfValidationResult;
54 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
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_0_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 = "policyTypesName", referencedColumnName = "name"),
98 @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version")
101 @SerializedName("policy_types")
102 private JpaToscaPolicyTypes policyTypes;
105 @SerializedName("topology_template")
106 private JpaToscaTopologyTemplate topologyTemplate;
110 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
112 public JpaToscaServiceTemplate() {
113 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
117 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
121 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
122 this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
126 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
129 * @param toscaDefinitionsVersion the TOSCA version string
131 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
133 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
139 * @param copyConcept the concept to copy from
141 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
143 this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
144 this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
145 this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
146 this.topologyTemplate =
147 (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate)
152 * Authorative constructor.
154 * @param authorativeConcept the authorative concept to copy from
156 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
157 this.fromAuthorative(authorativeConcept);
161 public ToscaServiceTemplate toAuthorative() {
162 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
164 super.setToscaEntity(toscaServiceTemplate);
165 super.toAuthorative();
167 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
169 if (dataTypes != null) {
170 toscaServiceTemplate.setDataTypes(new LinkedHashMap<>());
171 List<Map<String, ToscaDataType>> dataTypeMapList = dataTypes.toAuthorative();
172 for (Map<String, ToscaDataType> dataTypeMap : dataTypeMapList) {
173 toscaServiceTemplate.getDataTypes().putAll(dataTypeMap);
177 if (policyTypes != null) {
178 toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
179 List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative();
180 for (Map<String, ToscaPolicyType> policyTypeMap : policyTypeMapList) {
181 toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap);
185 if (topologyTemplate != null) {
186 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
189 return toscaServiceTemplate;
193 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
194 super.fromAuthorative(toscaServiceTemplate);
196 if (PfKey.NULL_KEY_NAME.equals(getKey().getName())) {
197 getKey().setName(DEFAULT_NAME);
200 if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
201 getKey().setVersion(DEFAULT_VERSION);
204 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
206 if (toscaServiceTemplate.getDataTypes() != null) {
207 dataTypes = new JpaToscaDataTypes();
208 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
211 if (toscaServiceTemplate.getPolicyTypes() != null) {
212 policyTypes = new JpaToscaPolicyTypes();
213 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
216 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
217 topologyTemplate = new JpaToscaTopologyTemplate();
218 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
223 public List<PfKey> getKeys() {
224 final List<PfKey> keyList = super.getKeys();
226 if (dataTypes != null) {
227 keyList.addAll(dataTypes.getKeys());
230 if (policyTypes != null) {
231 keyList.addAll(policyTypes.getKeys());
234 if (topologyTemplate != null) {
235 keyList.addAll(topologyTemplate.getKeys());
242 public void clean() {
243 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
245 if (dataTypes != null) {
249 if (policyTypes != null) {
253 if (topologyTemplate != null) {
254 topologyTemplate.clean();
259 public PfValidationResult validate(final PfValidationResult resultIn) {
260 PfValidationResult result = super.validate(resultIn);
262 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
263 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
264 "service template tosca definitions version may not be null"));
267 if (dataTypes != null) {
268 result = dataTypes.validate(result);
271 if (policyTypes != null) {
272 result = policyTypes.validate(result);
275 if (topologyTemplate != null) {
276 result = topologyTemplate.validate(result);
279 // No point in validating cross references if the structure of the individual parts are not valid
280 if (!result.isOk()) {
284 validateDatatypesInPolicyTypes(result);
286 return validatePolicyTypesInPolicies(result);
290 public int compareTo(final PfConcept otherConcept) {
291 int result = compareToWithoutEntities(otherConcept);
296 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
298 result = ObjectUtils.compare(dataTypes, other.dataTypes);
303 result = ObjectUtils.compare(policyTypes, other.policyTypes);
308 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
312 * Compare this service template to another service template, ignoring contained entitites.
314 * @param otherConcept the other topology template
315 * @return the result of the comparison
317 public int compareToWithoutEntities(final PfConcept otherConcept) {
318 if (otherConcept == null) {
321 if (this == otherConcept) {
324 if (getClass() != otherConcept.getClass()) {
325 return getClass().getName().compareTo(otherConcept.getClass().getName());
328 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
329 if (!super.equals(other)) {
330 return super.compareTo(other);
333 return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
337 * Validate that all data types referenced in policy types exist.
339 * @param result the validation result object to use for the validation result
340 * @return the validation result object
342 private PfValidationResult validateDatatypesInPolicyTypes(final PfValidationResult result) {
343 if (policyTypes == null) {
347 for (JpaToscaPolicyType policyType : policyTypes.getAll(null)) {
348 for (PfConceptKey datatypeKey : policyType.getReferencedDataTypes()) {
349 if (dataTypes == null || dataTypes.get(datatypeKey) == null) {
350 result.addValidationMessage(
351 new PfValidationMessage(policyType.getKey(), this.getClass(), ValidationResult.INVALID,
352 "data type " + datatypeKey + " referenced in policy type not found"));
361 * Validate that all policy types referenced in policies exist.
363 * @param result the validation result object to use for the validation result
364 * @return the validation result object
366 private PfValidationResult validatePolicyTypesInPolicies(PfValidationResult result) {
367 if (topologyTemplate == null || topologyTemplate.getPolicies() == null) {
371 if (policyTypes == null) {
372 result.addValidationMessage(new PfValidationMessage(this.getKey(), this.getClass(),
373 ValidationResult.INVALID,
374 "no policy types are defined on the service template for the policies in the topology template"));
378 for (JpaToscaPolicy policy : topologyTemplate.getPolicies().getAll(null)) {
379 if (policyTypes.get(policy.getType()) == null) {
380 result.addValidationMessage(
381 new PfValidationMessage(policy.getKey(), this.getClass(), ValidationResult.INVALID,
382 "policy type " + policy.getType().getId() + " referenced in policy not found"));