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;
26 import javax.persistence.CascadeType;
27 import javax.persistence.Column;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.FetchType;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.JoinColumns;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
42 import org.apache.commons.lang3.ObjectUtils;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.PfValidationMessage;
48 import org.onap.policy.models.base.PfValidationResult;
49 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
53 * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaTopologyTemplate")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
64 private static final long serialVersionUID = 8969698734673232603L;
66 public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
69 private PfReferenceKey key;
71 @Column(name = "description")
72 private String description;
75 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
78 @JoinColumn(name = "policyName", referencedColumnName = "name"),
79 @JoinColumn(name = "policyVersion", referencedColumnName = "version")
83 private JpaToscaPolicies policies;
86 * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
88 public JpaToscaTopologyTemplate() {
89 this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
94 * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
98 public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
105 * @param copyConcept the concept to copy from
107 public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
109 this.key = new PfReferenceKey(copyConcept.key);
110 this.description = copyConcept.description;
111 this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
115 * Authorative constructor.
117 * @param authorativeConcept the authorative concept to copy from
119 public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
120 this.fromAuthorative(authorativeConcept);
124 public ToscaTopologyTemplate toAuthorative() {
125 final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
127 toscaTopologyTemplate.setDescription(description);
129 if (policies != null) {
130 toscaTopologyTemplate.setPolicies(policies.toAuthorative());
133 return toscaTopologyTemplate;
137 public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
138 description = toscaTopologyTemplate.getDescription();
140 if (toscaTopologyTemplate.getPolicies() != null) {
141 policies = new JpaToscaPolicies();
142 policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
147 public List<PfKey> getKeys() {
148 final List<PfKey> keyList = getKey().getKeys();
150 if (policies != null) {
151 keyList.addAll(policies.getKeys());
158 public void clean() {
161 description = (description != null ? description.trim() : null);
163 if (policies != null) {
169 public PfValidationResult validate(PfValidationResult resultIn) {
170 PfValidationResult result = resultIn;
172 if (key.isNullKey()) {
173 result.addValidationMessage(
174 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
177 result = key.validate(result);
179 if (description != null && description.trim().length() == 0) {
180 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
181 "property description may not be blank"));
184 if (policies != null) {
185 result = policies.validate(result);
192 public int compareTo(final PfConcept otherConcept) {
193 int result = compareToWithoutEntities(otherConcept);
198 return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies);
202 * Compare this topology template to another topology template, ignoring contained entitites.
204 * @param otherConcept the other topology template
205 * @return the result of the comparison
207 public int compareToWithoutEntities(final PfConcept otherConcept) {
208 if (otherConcept == null) {
211 if (this == otherConcept) {
214 if (getClass() != otherConcept.getClass()) {
215 return getClass().getName().compareTo(otherConcept.getClass().getName());
218 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
219 if (!key.equals(other.key)) {
220 return key.compareTo(other.key);
223 return ObjectUtils.compare(description, other.description);