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 ToscaServiceTemplate extends ToscaEntityType {
61 private static final long serialVersionUID = 8084846046148349401L;
64 @SerializedName("tosca_definitions_version")
65 private String toscaDefinitionsVersion;
67 @OneToOne(cascade = CascadeType.ALL)
68 @SerializedName("data_types")
69 private ToscaDataTypes dataTypes;
71 @OneToOne(cascade = CascadeType.ALL)
72 @SerializedName("policy_types")
73 private ToscaPolicyTypes policyTypes;
75 @SerializedName("topology_template")
76 private ToscaTopologyTemplate topologyTemplate;
80 * The Default Constructor creates a {@link ToscaServiceTemplate} object with a null key.
82 public ToscaServiceTemplate() {
83 this(new PfConceptKey());
87 * The Key Constructor creates a {@link ToscaServiceTemplate} object with the given concept key.
91 public ToscaServiceTemplate(@NonNull final PfConceptKey key) {
96 * The full constructor creates a {@link ToscaServiceTemplate} object with all mandatory
100 * @param toscaDefinitionsVersion the TOSCA version string
102 public ToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
104 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
110 * @param copyConcept the concept to copy from
112 public ToscaServiceTemplate(final ToscaServiceTemplate copyConcept) {
117 public List<PfKey> getKeys() {
118 final List<PfKey> keyList = super.getKeys();
120 if (dataTypes != null) {
121 keyList.addAll(dataTypes.getKeys());
124 if (policyTypes != null) {
125 keyList.addAll(policyTypes.getKeys());
128 if (topologyTemplate != null) {
129 keyList.addAll(topologyTemplate.getKeys());
136 public void clean() {
137 toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
139 if (dataTypes != null) {
143 if (policyTypes != null) {
147 if (topologyTemplate != null) {
148 topologyTemplate.clean();
153 public PfValidationResult validate(final PfValidationResult resultIn) {
154 PfValidationResult result = super.validate(resultIn);
156 if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
157 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
158 "service template tosca definitions version may not be null"));
161 if (dataTypes != null) {
162 result = dataTypes.validate(result);
165 if (policyTypes != null) {
166 result = policyTypes.validate(result);
169 return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
173 public int compareTo(final PfConcept otherConcept) {
174 if (otherConcept == null) {
177 if (this == otherConcept) {
180 if (getClass() != otherConcept.getClass()) {
181 return this.hashCode() - otherConcept.hashCode();
184 final ToscaServiceTemplate other = (ToscaServiceTemplate) otherConcept;
185 if (!super.equals(other)) {
186 return super.compareTo(other);
189 int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
194 result = ObjectUtils.compare(dataTypes, other.dataTypes);
199 result = ObjectUtils.compare(policyTypes, other.policyTypes);
204 return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
208 public PfConcept copyTo(@NonNull PfConcept target) {
209 final Object copyObject = target;
210 Assertions.instanceOf(copyObject, PfConcept.class);
212 final ToscaServiceTemplate copy = ((ToscaServiceTemplate) copyObject);
213 super.copyTo(target);
214 copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
216 copy.setDataTypes(dataTypes != null ? new ToscaDataTypes(dataTypes) : null);
217 copy.setPolicyTypes(policyTypes != null ? new ToscaPolicyTypes(policyTypes) : null);
218 copy.setTopologyTemplate(topologyTemplate != null ? new ToscaTopologyTemplate(topologyTemplate) : null);