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.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfValidationMessage;
46 import org.onap.policy.models.base.PfValidationResult;
47 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
50 * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA
51 * service template are implemented.
53 * @author Liam Fallon (liam.fallon@est.tech)
56 @Table(name = "ToscaServiceTemplate")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = true)
60 public class JpaToscaServiceTemplate extends JpaToscaEntityType {
61 private static final long serialVersionUID = 8084846046148349401L;
63 public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
64 public static final String DEFAULT_VERSION = "1.0.0";
67 @SerializedName("tosca_definitions_version")
68 private String toscaDefinitionsVersion;
70 @OneToOne(cascade = CascadeType.ALL)
71 @SerializedName("data_types")
72 private JpaToscaDataTypes dataTypes;
74 @OneToOne(cascade = CascadeType.ALL)
75 @SerializedName("policy_types")
76 private JpaToscaPolicyTypes policyTypes;
78 @SerializedName("topology_template")
79 private JpaToscaTopologyTemplate topologyTemplate;
83 * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
85 public JpaToscaServiceTemplate() {
86 this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
90 * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
94 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
99 * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory
103 * @param toscaDefinitionsVersion the TOSCA version string
105 public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
107 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
113 * @param copyConcept the concept to copy from
115 public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
120 public List<PfKey> getKeys() {
121 final List<PfKey> keyList = super.getKeys();
123 if (dataTypes != null) {
124 keyList.addAll(dataTypes.getKeys());
127 if (policyTypes != null) {
128 keyList.addAll(policyTypes.getKeys());
131 if (topologyTemplate != null) {
132 keyList.addAll(topologyTemplate.getKeys());
139 public void clean() {
140 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
142 if (dataTypes != null) {
146 if (policyTypes != null) {
150 if (topologyTemplate != null) {
151 topologyTemplate.clean();
156 public PfValidationResult validate(final PfValidationResult resultIn) {
157 PfValidationResult result = super.validate(resultIn);
159 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
160 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
161 "service template tosca definitions version may not be null"));
164 if (dataTypes != null) {
165 result = dataTypes.validate(result);
168 if (policyTypes != null) {
169 result = policyTypes.validate(result);
172 return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
176 public int compareTo(final PfConcept otherConcept) {
177 if (otherConcept == null) {
180 if (this == otherConcept) {
183 if (getClass() != otherConcept.getClass()) {
184 return this.hashCode() - otherConcept.hashCode();
187 final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
188 if (!super.equals(other)) {
189 return super.compareTo(other);
192 int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
197 result = ObjectUtils.compare(dataTypes, other.dataTypes);
202 result = ObjectUtils.compare(policyTypes, other.policyTypes);
207 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
211 public PfConcept copyTo(@NonNull PfConcept target) {
212 final Object copyObject = target;
213 Assertions.instanceOf(copyObject, PfConcept.class);
215 final JpaToscaServiceTemplate copy = ((JpaToscaServiceTemplate) copyObject);
216 super.copyTo(target);
217 copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
219 copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null);
220 copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null);
221 copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null);