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.FetchType;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.OneToOne;
34 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.onap.policy.common.utils.validation.Assertions;
42 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfValidationMessage;
48 import org.onap.policy.models.base.PfValidationResult;
49 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaServiceTemplate")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = true)
63 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
64 implements PfAuthorative<ToscaServiceTemplate> {
65 private static final long serialVersionUID = 8084846046148349401L;
67 public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
68 public static final String DEFAULT_VERSION = "1.0.0";
71 @SerializedName("tosca_definitions_version")
72 private String toscaDefinitionsVersion;
74 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
75 @SerializedName("data_types")
76 private JpaToscaDataTypes dataTypes;
78 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
79 @SerializedName("policy_types")
80 private JpaToscaPolicyTypes policyTypes;
83 @SerializedName("topology_template")
84 private JpaToscaTopologyTemplate topologyTemplate;
88 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
90 public JpaToscaServiceTemplate() {
91 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
95 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
99 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
104 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
107 * @param toscaDefinitionsVersion the TOSCA version string
109 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
111 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
117 * @param copyConcept the concept to copy from
119 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
124 * Authorative constructor.
126 * @param authorativeConcept the authorative concept to copy from
128 public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
129 this.fromAuthorative(authorativeConcept);
133 public ToscaServiceTemplate toAuthorative() {
134 final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
136 super.setToscaEntity(toscaServiceTemplate);
137 super.toAuthorative();
139 toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
141 if (dataTypes != null) {
142 toscaServiceTemplate.setDataTypes(dataTypes.toAuthorative());
145 if (policyTypes != null) {
146 toscaServiceTemplate.setPolicyTypes(policyTypes.toAuthorative());
149 if (topologyTemplate != null) {
150 toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
153 return toscaServiceTemplate;
157 public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
158 super.fromAuthorative(toscaServiceTemplate);
160 if (getKey().getName() == PfKey.NULL_KEY_NAME) {
161 getKey().setName(DEFAULT_NAME);
164 if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) {
165 getKey().setVersion(DEFAULT_VERSION);
168 toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
170 if (toscaServiceTemplate.getDataTypes() != null) {
171 dataTypes = new JpaToscaDataTypes();
172 dataTypes.fromAuthorative(toscaServiceTemplate.getDataTypes());
175 if (toscaServiceTemplate.getPolicyTypes() != null) {
176 policyTypes = new JpaToscaPolicyTypes();
177 policyTypes.fromAuthorative(toscaServiceTemplate.getPolicyTypes());
181 if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
182 topologyTemplate = new JpaToscaTopologyTemplate();
183 topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
188 public List<PfKey> getKeys() {
189 final List<PfKey> keyList = super.getKeys();
191 if (dataTypes != null) {
192 keyList.addAll(dataTypes.getKeys());
195 if (policyTypes != null) {
196 keyList.addAll(policyTypes.getKeys());
199 if (topologyTemplate != null) {
200 keyList.addAll(topologyTemplate.getKeys());
207 public void clean() {
208 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
210 if (dataTypes != null) {
214 if (policyTypes != null) {
218 if (topologyTemplate != null) {
219 topologyTemplate.clean();
224 public PfValidationResult validate(final PfValidationResult resultIn) {
225 PfValidationResult result = super.validate(resultIn);
227 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
228 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
229 "service template tosca definitions version may not be null"));
232 if (dataTypes != null) {
233 result = dataTypes.validate(result);
236 if (policyTypes != null) {
237 result = policyTypes.validate(result);
240 return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
244 public int compareTo(final PfConcept otherConcept) {
245 if (otherConcept == null) {
248 if (this == otherConcept) {
251 if (getClass() != otherConcept.getClass()) {
252 return this.hashCode() - otherConcept.hashCode();
255 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
256 if (!super.equals(other)) {
257 return super.compareTo(other);
260 int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
265 result = ObjectUtils.compare(dataTypes, other.dataTypes);
270 result = ObjectUtils.compare(policyTypes, other.policyTypes);
275 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
279 public PfConcept copyTo(@NonNull PfConcept target) {
280 final Object copyObject = target;
281 Assertions.instanceOf(copyObject, PfConcept.class);
283 final JpaToscaServiceTemplate copy = ((JpaToscaServiceTemplate) copyObject);
284 super.copyTo(target);
285 copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
287 copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null);
288 copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null);
289 copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null);