2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import com.google.gson.annotations.SerializedName;
25 import java.util.List;
27 import javax.persistence.CascadeType;
28 import javax.persistence.Column;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
39 import org.apache.commons.lang3.ObjectUtils;
40 import org.onap.policy.common.utils.validation.Assertions;
41 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfValidationMessage;
47 import org.onap.policy.models.base.PfValidationResult;
48 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
55 * @author Liam Fallon (liam.fallon@est.tech)
58 @Table(name = "ToscaServiceTemplate")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @EqualsAndHashCode(callSuper = true)
62 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
63 implements PfAuthorative<ToscaServiceTemplate> {
64 private static final long serialVersionUID = 8084846046148349401L;
66 public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
67 public static final String DEFAULT_VERSION = "1.0.0";
70 @SerializedName("tosca_definitions_version")
71 private String toscaDefinitionsVersion;
73 @OneToOne(cascade = CascadeType.ALL)
74 @SerializedName("data_types")
75 private JpaToscaDataTypes dataTypes;
77 @OneToOne(cascade = CascadeType.ALL)
78 @SerializedName("policy_types")
79 private JpaToscaPolicyTypes policyTypes;
81 @SerializedName("topology_template")
82 private JpaToscaTopologyTemplate topologyTemplate;
86 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
88 public JpaToscaServiceTemplate() {
89 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
93 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
97 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
102 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
105 * @param toscaDefinitionsVersion the TOSCA version string
107 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
109 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
115 * @param copyConcept the concept to copy from
117 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
122 * Authorative constructor.
124 * @param authorativeConcept the authorative concept to copy from
126 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
127 this.fromAuthorative(authorativeConcept);
131 public ToscaServiceTemplate toAuthorative() {
132 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
134 super.setToscaEntity(toscaServiceTemplate);
135 super.toAuthorative();
137 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
139 if (dataTypes != null) {
140 toscaServiceTemplate.setDataTypes(dataTypes.toAuthorative());
143 if (policyTypes != null) {
144 toscaServiceTemplate.setPolicyTypes(policyTypes.toAuthorative());
147 if (topologyTemplate != null) {
148 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
151 return toscaServiceTemplate;
155 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
156 super.fromAuthorative(toscaServiceTemplate);
158 if (getKey().getName() == PfKey.NULL_KEY_NAME) {
159 getKey().setName(DEFAULT_NAME);
162 if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) {
163 getKey().setVersion(DEFAULT_VERSION);
166 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
168 if (toscaServiceTemplate.getDataTypes() != null) {
169 dataTypes = new JpaToscaDataTypes();
170 dataTypes.fromAuthorative(toscaServiceTemplate.getDataTypes());
173 if (toscaServiceTemplate.getPolicyTypes() != null) {
174 policyTypes = new JpaToscaPolicyTypes();
175 policyTypes.fromAuthorative(toscaServiceTemplate.getPolicyTypes());
179 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
180 topologyTemplate = new JpaToscaTopologyTemplate();
181 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
186 public List<PfKey> getKeys() {
187 final List<PfKey> keyList = super.getKeys();
189 if (dataTypes != null) {
190 keyList.addAll(dataTypes.getKeys());
193 if (policyTypes != null) {
194 keyList.addAll(policyTypes.getKeys());
197 if (topologyTemplate != null) {
198 keyList.addAll(topologyTemplate.getKeys());
205 public void clean() {
206 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
208 if (dataTypes != null) {
212 if (policyTypes != null) {
216 if (topologyTemplate != null) {
217 topologyTemplate.clean();
222 public PfValidationResult validate(final PfValidationResult resultIn) {
223 PfValidationResult result = super.validate(resultIn);
225 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
226 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
227 "service template tosca definitions version may not be null"));
230 if (dataTypes != null) {
231 result = dataTypes.validate(result);
234 if (policyTypes != null) {
235 result = policyTypes.validate(result);
238 return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
242 public int compareTo(final PfConcept otherConcept) {
243 if (otherConcept == null) {
246 if (this == otherConcept) {
249 if (getClass() != otherConcept.getClass()) {
250 return this.hashCode() - otherConcept.hashCode();
253 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
254 if (!super.equals(other)) {
255 return super.compareTo(other);
258 int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
263 result = ObjectUtils.compare(dataTypes, other.dataTypes);
268 result = ObjectUtils.compare(policyTypes, other.policyTypes);
273 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
277 public PfConcept copyTo(@NonNull PfConcept target) {
278 final Object copyObject = target;
279 Assertions.instanceOf(copyObject, PfConcept.class);
281 final JpaToscaServiceTemplate copy = ((JpaToscaServiceTemplate) copyObject);
282 super.copyTo(target);
283 copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
285 copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null);
286 copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null);
287 copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null);