2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 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;
25 import jakarta.persistence.CascadeType;
26 import jakarta.persistence.Column;
27 import jakarta.persistence.ElementCollection;
28 import jakarta.persistence.EmbeddedId;
29 import jakarta.persistence.Entity;
30 import jakarta.persistence.FetchType;
31 import jakarta.persistence.Inheritance;
32 import jakarta.persistence.InheritanceType;
33 import jakarta.persistence.JoinColumn;
34 import jakarta.persistence.JoinColumns;
35 import jakarta.persistence.Lob;
36 import jakarta.persistence.OneToOne;
37 import jakarta.persistence.Table;
38 import java.io.Serial;
39 import java.util.Collections;
40 import java.util.LinkedHashMap;
41 import java.util.List;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.common.parameters.annotations.NotBlank;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Valid;
50 import org.onap.policy.models.base.PfAuthorative;
51 import org.onap.policy.models.base.PfConcept;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.validation.annotations.VerifyKey;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
61 * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
64 * @author Liam Fallon (liam.fallon@est.tech)
67 @Table(name = "ToscaTopologyTemplate")
68 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @EqualsAndHashCode(callSuper = false)
71 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
73 private static final long serialVersionUID = 8969698734673232603L;
75 public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
80 private PfReferenceKey key;
82 @Column(name = "description")
84 private String description;
88 private Map<@NotNull String, @NotNull @Valid JpaToscaParameter> inputs;
90 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
91 @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name")
92 @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version")
93 @SerializedName("data_types")
95 private JpaToscaNodeTemplates nodeTemplates;
97 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
98 @JoinColumn(name = "policyName", referencedColumnName = "name")
99 @JoinColumn(name = "policyVersion", referencedColumnName = "version")
101 private JpaToscaPolicies policies;
104 * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
106 public JpaToscaTopologyTemplate() {
107 this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
108 DEFAULT_LOCAL_NAME));
112 * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
116 public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
123 * @param copyConcept the concept to copy from
125 public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
127 this.key = new PfReferenceKey(copyConcept.key);
128 this.description = copyConcept.description;
129 this.inputs = PfUtils.mapMap(copyConcept.inputs, JpaToscaParameter::new);
131 (copyConcept.nodeTemplates != null ? new JpaToscaNodeTemplates(copyConcept.nodeTemplates) : null);
132 this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
136 * Authorative constructor.
138 * @param authorativeConcept the authorative concept to copy from
140 public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
141 this.fromAuthorative(authorativeConcept);
145 public ToscaTopologyTemplate toAuthorative() {
146 final var toscaTopologyTemplate = new ToscaTopologyTemplate();
148 toscaTopologyTemplate.setDescription(description);
150 if (inputs != null) {
151 Map<String, ToscaParameter> inputMap = new LinkedHashMap<>();
153 for (Map.Entry<String, JpaToscaParameter> entry : inputs.entrySet()) {
154 inputMap.put(entry.getKey(), entry.getValue().toAuthorative());
157 toscaTopologyTemplate.setInputs(inputMap);
160 if (nodeTemplates != null) {
161 toscaTopologyTemplate.setNodeTemplates(new LinkedHashMap<>());
162 List<Map<String, ToscaNodeTemplate>> nodeTemplateMapList = nodeTemplates.toAuthorative();
163 for (Map<String, ToscaNodeTemplate> nodeTemplateMap : nodeTemplateMapList) {
164 toscaTopologyTemplate.getNodeTemplates().putAll(nodeTemplateMap);
168 if (policies != null) {
169 toscaTopologyTemplate.setPolicies(policies.toAuthorative());
172 return toscaTopologyTemplate;
176 public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
177 description = toscaTopologyTemplate.getDescription();
179 if (toscaTopologyTemplate.getInputs() != null) {
180 inputs = new LinkedHashMap<>();
181 for (Map.Entry<String, ToscaParameter> toscaInputEntry : toscaTopologyTemplate.getInputs().entrySet()) {
182 var jpaInput = new JpaToscaParameter(toscaInputEntry.getValue());
183 jpaInput.setKey(new PfReferenceKey(getKey(), toscaInputEntry.getKey()));
184 inputs.put(toscaInputEntry.getKey(), jpaInput);
188 if (toscaTopologyTemplate.getNodeTemplates() != null) {
189 nodeTemplates = new JpaToscaNodeTemplates();
190 nodeTemplates.fromAuthorative(Collections.singletonList(toscaTopologyTemplate.getNodeTemplates()));
193 if (toscaTopologyTemplate.getPolicies() != null) {
194 policies = new JpaToscaPolicies();
195 policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
200 public List<PfKey> getKeys() {
201 final List<PfKey> keyList = getKey().getKeys();
203 if (inputs != null) {
204 for (JpaToscaParameter input : inputs.values()) {
205 keyList.addAll(input.getKeys());
209 if (nodeTemplates != null) {
210 keyList.addAll(nodeTemplates.getKeys());
213 if (policies != null) {
214 keyList.addAll(policies.getKeys());
221 public void clean() {
224 description = (description != null ? description.trim() : null);
226 if (inputs != null) {
227 for (JpaToscaParameter input : inputs.values()) {
232 if (nodeTemplates != null) {
233 nodeTemplates.clean();
236 if (policies != null) {
242 public int compareTo(final PfConcept otherConcept) {
243 int result = compareToWithoutEntities(otherConcept);
248 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
250 result = PfUtils.compareObjects(inputs, other.inputs);
255 result = ObjectUtils.compare(nodeTemplates, other.nodeTemplates);
260 return ObjectUtils.compare(policies, other.policies);
264 * Compare this topology template to another topology template, ignoring contained entities.
266 * @param otherConcept the other topology template
267 * @return the result of the comparison
269 public int compareToWithoutEntities(final PfConcept otherConcept) {
270 if (otherConcept == null) {
273 if (this == otherConcept) {
276 if (getClass() != otherConcept.getClass()) {
277 return getClass().getName().compareTo(otherConcept.getClass().getName());
280 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
281 if (!key.equals(other.key)) {
282 return key.compareTo(other.key);
285 return ObjectUtils.compare(description, other.description);