2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.OneToOne;
38 import javax.persistence.Table;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42 import org.apache.commons.lang3.ObjectUtils;
43 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
59 * @author Liam Fallon (liam.fallon@est.tech)
62 @Table(name = "ToscaServiceTemplate")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @EqualsAndHashCode(callSuper = true)
66 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
67 implements PfAuthorative<ToscaServiceTemplate> {
68 private static final long serialVersionUID = 8084846046148349401L;
70 public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0";
71 public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
72 public static final String DEFAULT_VERSION = "1.0.0";
75 @SerializedName("tosca_definitions_version")
76 private String toscaDefinitionsVersion;
78 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
79 @SerializedName("data_types")
80 private JpaToscaDataTypes dataTypes;
82 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
83 @SerializedName("policy_types")
84 private JpaToscaPolicyTypes policyTypes;
87 @SerializedName("topology_template")
88 private JpaToscaTopologyTemplate topologyTemplate;
92 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
94 public JpaToscaServiceTemplate() {
95 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
99 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
103 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
104 this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
108 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
111 * @param toscaDefinitionsVersion the TOSCA version string
113 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
115 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
121 * @param copyConcept the concept to copy from
123 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
125 this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
126 this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
127 this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
128 this.topologyTemplate = (copyConcept.topologyTemplate != null
129 ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate) : null);
133 * Authorative constructor.
135 * @param authorativeConcept the authorative concept to copy from
137 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
138 this.fromAuthorative(authorativeConcept);
142 public ToscaServiceTemplate toAuthorative() {
143 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
145 super.setToscaEntity(toscaServiceTemplate);
146 super.toAuthorative();
148 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
150 if (dataTypes != null) {
151 toscaServiceTemplate.setDataTypes(new LinkedHashMap<>());
152 List<Map<String, ToscaDataType>> dataTypeMapList = dataTypes.toAuthorative();
153 for (Map<String, ToscaDataType> dataTypeMap : dataTypeMapList) {
154 toscaServiceTemplate.getDataTypes().putAll(dataTypeMap);
158 if (policyTypes != null) {
159 toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
160 List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative();
161 for (Map<String, ToscaPolicyType> policyTypeMap : policyTypeMapList) {
162 toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap);
166 if (topologyTemplate != null) {
167 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
170 return toscaServiceTemplate;
174 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
175 super.fromAuthorative(toscaServiceTemplate);
177 if (getKey().getName() == PfKey.NULL_KEY_NAME) {
178 getKey().setName(DEFAULT_NAME);
181 if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) {
182 getKey().setVersion(DEFAULT_VERSION);
185 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
187 if (toscaServiceTemplate.getDataTypes() != null) {
188 dataTypes = new JpaToscaDataTypes();
189 dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
192 if (toscaServiceTemplate.getPolicyTypes() != null) {
193 policyTypes = new JpaToscaPolicyTypes();
194 policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
198 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
199 topologyTemplate = new JpaToscaTopologyTemplate();
200 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
205 public List<PfKey> getKeys() {
206 final List<PfKey> keyList = super.getKeys();
208 if (dataTypes != null) {
209 keyList.addAll(dataTypes.getKeys());
212 if (policyTypes != null) {
213 keyList.addAll(policyTypes.getKeys());
216 if (topologyTemplate != null) {
217 keyList.addAll(topologyTemplate.getKeys());
224 public void clean() {
225 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
227 if (dataTypes != null) {
231 if (policyTypes != null) {
235 if (topologyTemplate != null) {
236 topologyTemplate.clean();
241 public PfValidationResult validate(final PfValidationResult resultIn) {
242 PfValidationResult result = super.validate(resultIn);
244 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
245 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
246 "service template tosca definitions version may not be null"));
249 if (dataTypes != null) {
250 result = dataTypes.validate(result);
253 if (policyTypes != null) {
254 result = policyTypes.validate(result);
257 return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
261 public int compareTo(final PfConcept otherConcept) {
262 if (otherConcept == null) {
265 if (this == otherConcept) {
268 if (getClass() != otherConcept.getClass()) {
269 return getClass().getName().compareTo(otherConcept.getClass().getName());
272 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
273 if (!super.equals(other)) {
274 return super.compareTo(other);
277 int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
282 result = ObjectUtils.compare(dataTypes, other.dataTypes);
287 result = ObjectUtils.compare(policyTypes, other.policyTypes);
292 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);