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 java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import javax.persistence.CascadeType;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.JoinColumns;
39 import javax.persistence.Lob;
40 import javax.persistence.OneToOne;
41 import javax.persistence.Table;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
45 import org.apache.commons.lang3.ObjectUtils;
46 import org.onap.policy.common.parameters.annotations.NotBlank;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.base.validation.annotations.VerifyKey;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
60 * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
63 * @author Liam Fallon (liam.fallon@est.tech)
66 @Table(name = "ToscaTopologyTemplate")
67 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
69 @EqualsAndHashCode(callSuper = false)
70 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
71 private static final long serialVersionUID = 8969698734673232603L;
73 public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
78 private PfReferenceKey key;
80 @Column(name = "description")
82 private String description;
87 private Map<@NotNull String, @NotNull @Valid JpaToscaParameter> inputs;
89 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
91 @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name"),
92 @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version")
94 @SerializedName("data_types")
96 private JpaToscaNodeTemplates nodeTemplates;
98 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
100 @JoinColumn(name = "policyName", referencedColumnName = "name"),
101 @JoinColumn(name = "policyVersion", referencedColumnName = "version")
106 private JpaToscaPolicies policies;
109 * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
111 public JpaToscaTopologyTemplate() {
112 this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
113 DEFAULT_LOCAL_NAME));
117 * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
121 public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
128 * @param copyConcept the concept to copy from
130 public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
132 this.key = new PfReferenceKey(copyConcept.key);
133 this.description = copyConcept.description;
134 this.inputs = PfUtils.mapMap(copyConcept.inputs, JpaToscaParameter::new);
136 (copyConcept.nodeTemplates != null ? new JpaToscaNodeTemplates(copyConcept.nodeTemplates) : null);
137 this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
141 * Authorative constructor.
143 * @param authorativeConcept the authorative concept to copy from
145 public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
146 this.fromAuthorative(authorativeConcept);
150 public ToscaTopologyTemplate toAuthorative() {
151 final var toscaTopologyTemplate = new ToscaTopologyTemplate();
153 toscaTopologyTemplate.setDescription(description);
155 if (inputs != null) {
156 Map<String, ToscaParameter> inputMap = new LinkedHashMap<>();
158 for (Map.Entry<String, JpaToscaParameter> entry : inputs.entrySet()) {
159 inputMap.put(entry.getKey(), entry.getValue().toAuthorative());
162 toscaTopologyTemplate.setInputs(inputMap);
165 if (nodeTemplates != null) {
166 toscaTopologyTemplate.setNodeTemplates(new LinkedHashMap<>());
167 List<Map<String, ToscaNodeTemplate>> nodeTemplateMapList = nodeTemplates.toAuthorative();
168 for (Map<String, ToscaNodeTemplate> nodeTemplateMap : nodeTemplateMapList) {
169 toscaTopologyTemplate.getNodeTemplates().putAll(nodeTemplateMap);
173 if (policies != null) {
174 toscaTopologyTemplate.setPolicies(policies.toAuthorative());
177 return toscaTopologyTemplate;
181 public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
182 description = toscaTopologyTemplate.getDescription();
184 if (toscaTopologyTemplate.getInputs() != null) {
185 inputs = new LinkedHashMap<>();
186 for (Map.Entry<String, ToscaParameter> toscaInputEntry : toscaTopologyTemplate.getInputs().entrySet()) {
187 var jpaInput = new JpaToscaParameter(toscaInputEntry.getValue());
188 jpaInput.setKey(new PfReferenceKey(getKey(), toscaInputEntry.getKey()));
189 inputs.put(toscaInputEntry.getKey(), jpaInput);
193 if (toscaTopologyTemplate.getNodeTemplates() != null) {
194 nodeTemplates = new JpaToscaNodeTemplates();
195 nodeTemplates.fromAuthorative(Collections.singletonList(toscaTopologyTemplate.getNodeTemplates()));
198 if (toscaTopologyTemplate.getPolicies() != null) {
199 policies = new JpaToscaPolicies();
200 policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
205 public List<PfKey> getKeys() {
206 final List<PfKey> keyList = getKey().getKeys();
208 if (inputs != null) {
209 for (JpaToscaParameter input : inputs.values()) {
210 keyList.addAll(input.getKeys());
214 if (nodeTemplates != null) {
215 keyList.addAll(nodeTemplates.getKeys());
218 if (policies != null) {
219 keyList.addAll(policies.getKeys());
226 public void clean() {
229 description = (description != null ? description.trim() : null);
231 if (inputs != null) {
232 for (JpaToscaParameter input : inputs.values()) {
237 if (nodeTemplates != null) {
238 nodeTemplates.clean();
241 if (policies != null) {
247 public int compareTo(final PfConcept otherConcept) {
248 int result = compareToWithoutEntities(otherConcept);
253 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
255 result = PfUtils.compareObjects(inputs, other.inputs);
260 result = ObjectUtils.compare(nodeTemplates, other.nodeTemplates);
265 return ObjectUtils.compare(policies, other.policies);
269 * Compare this topology template to another topology template, ignoring contained entities.
271 * @param otherConcept the other topology template
272 * @return the result of the comparison
274 public int compareToWithoutEntities(final PfConcept otherConcept) {
275 if (otherConcept == null) {
278 if (this == otherConcept) {
281 if (getClass() != otherConcept.getClass()) {
282 return getClass().getName().compareTo(otherConcept.getClass().getName());
285 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
286 if (!key.equals(other.key)) {
287 return key.compareTo(other.key);
290 return ObjectUtils.compare(description, other.description);