2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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 java.util.List;
25 import javax.persistence.CascadeType;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.FetchType;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.JoinColumns;
34 import javax.persistence.OneToOne;
35 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.apache.commons.lang3.ObjectUtils;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfReferenceKey;
44 import org.onap.policy.models.base.PfValidationMessage;
45 import org.onap.policy.models.base.PfValidationResult;
46 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
50 * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
53 * @author Liam Fallon (liam.fallon@est.tech)
56 @Table(name = "ToscaTopologyTemplate")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
61 private static final long serialVersionUID = 8969698734673232603L;
63 public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
66 private PfReferenceKey key;
68 @Column(name = "description")
69 private String description;
72 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
75 @JoinColumn(name = "policyName", referencedColumnName = "name"),
76 @JoinColumn(name = "policyVersion", referencedColumnName = "version")
80 private JpaToscaPolicies policies;
83 * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
85 public JpaToscaTopologyTemplate() {
86 this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
91 * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
95 public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
102 * @param copyConcept the concept to copy from
104 public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
106 this.key = new PfReferenceKey(copyConcept.key);
107 this.description = copyConcept.description;
108 this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
112 * Authorative constructor.
114 * @param authorativeConcept the authorative concept to copy from
116 public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
117 this.fromAuthorative(authorativeConcept);
121 public ToscaTopologyTemplate toAuthorative() {
122 final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
124 toscaTopologyTemplate.setDescription(description);
126 if (policies != null) {
127 toscaTopologyTemplate.setPolicies(policies.toAuthorative());
130 return toscaTopologyTemplate;
134 public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
135 description = toscaTopologyTemplate.getDescription();
137 if (toscaTopologyTemplate.getPolicies() != null) {
138 policies = new JpaToscaPolicies();
139 policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
144 public List<PfKey> getKeys() {
145 final List<PfKey> keyList = getKey().getKeys();
147 if (policies != null) {
148 keyList.addAll(policies.getKeys());
155 public void clean() {
158 description = (description != null ? description.trim() : null);
160 if (policies != null) {
166 public PfValidationResult validate(PfValidationResult resultIn) {
167 PfValidationResult result = resultIn;
169 if (key.isNullKey()) {
170 result.addValidationMessage(
171 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
174 result = key.validate(result);
176 if (description != null && description.trim().length() == 0) {
177 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
178 "property description may not be blank"));
181 if (policies != null) {
182 result = policies.validate(result);
189 public int compareTo(final PfConcept otherConcept) {
190 int result = compareToWithoutEntities(otherConcept);
195 return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies);
199 * Compare this topology template to another topology template, ignoring contained entitites.
201 * @param otherConcept the other topology template
202 * @return the result of the comparison
204 public int compareToWithoutEntities(final PfConcept otherConcept) {
205 if (otherConcept == null) {
208 if (this == otherConcept) {
211 if (getClass() != otherConcept.getClass()) {
212 return getClass().getName().compareTo(otherConcept.getClass().getName());
215 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
216 if (!key.equals(other.key)) {
217 return key.compareTo(other.key);
220 return ObjectUtils.compare(description, other.description);