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.OneToOne;
34 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
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;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
51 * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
54 * @author Liam Fallon (liam.fallon@est.tech)
57 @Table(name = "ToscaTopologyTemplate")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
62 private static final long serialVersionUID = 8969698734673232603L;
64 public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
67 private PfReferenceKey key;
69 @Column(name = "description")
70 private String description;
72 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
73 private JpaToscaPolicies policies;
76 * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
78 public JpaToscaTopologyTemplate() {
79 this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
84 * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
88 public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
95 * @param copyConcept the concept to copy from
97 public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
99 this.key = new PfReferenceKey(copyConcept.key);
100 this.description = copyConcept.description;
101 this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
105 * Authorative constructor.
107 * @param authorativeConcept the authorative concept to copy from
109 public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
110 this.fromAuthorative(authorativeConcept);
114 public ToscaTopologyTemplate toAuthorative() {
115 final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
117 toscaTopologyTemplate.setDescription(description);
119 if (policies != null) {
120 toscaTopologyTemplate.setPolicies(policies.toAuthorative());
123 return toscaTopologyTemplate;
127 public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
128 description = toscaTopologyTemplate.getDescription();
130 if (toscaTopologyTemplate.getPolicies() != null) {
131 policies = new JpaToscaPolicies();
132 policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
137 public List<PfKey> getKeys() {
138 final List<PfKey> keyList = getKey().getKeys();
140 if (policies != null) {
141 keyList.addAll(policies.getKeys());
148 public void clean() {
151 description = (description != null ? description.trim() : null);
153 if (policies != null) {
159 public PfValidationResult validate(PfValidationResult resultIn) {
160 PfValidationResult result = resultIn;
162 if (key.isNullKey()) {
163 result.addValidationMessage(
164 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
167 result = key.validate(result);
169 if (description != null && description.trim().length() == 0) {
170 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
171 "property description may not be blank"));
174 if (policies != null) {
175 result = policies.validate(result);
182 public int compareTo(final PfConcept otherConcept) {
183 int result = compareToWithoutEntities(otherConcept);
188 return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies);
192 * Compare this topology template to another topology template, ignoring contained entitites.
194 * @param otherConcept the other topology template
195 * @return the result of the comparison
197 public int compareToWithoutEntities(final PfConcept otherConcept) {
198 if (otherConcept == null) {
201 if (this == otherConcept) {
204 if (getClass() != otherConcept.getClass()) {
205 return getClass().getName().compareTo(otherConcept.getClass().getName());
208 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
209 if (!key.equals(other.key)) {
210 return key.compareTo(other.key);
213 return ObjectUtils.compare(description, other.description);