2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.OneToOne;
33 import javax.persistence.Table;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.ObjectUtils;
38 import org.onap.policy.models.base.PfAuthorative;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfReferenceKey;
42 import org.onap.policy.models.base.PfValidationMessage;
43 import org.onap.policy.models.base.PfValidationResult;
44 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
48 * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
51 * @author Liam Fallon (liam.fallon@est.tech)
54 @Table(name = "ToscaTopologyTemplate")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
59 private static final long serialVersionUID = 8969698734673232603L;
61 public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
64 private PfReferenceKey key;
66 @Column(name = "description")
67 private String description;
69 @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
70 private JpaToscaPolicies policies;
73 * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
75 public JpaToscaTopologyTemplate() {
76 this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
81 * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept
86 public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
93 * @param copyConcept the concept to copy from
95 public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
97 this.key = new PfReferenceKey(copyConcept.key);
98 this.description = copyConcept.description;
99 this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
103 * Authorative constructor.
105 * @param authorativeConcept the authorative concept to copy from
107 public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
108 this.fromAuthorative(authorativeConcept);
112 public ToscaTopologyTemplate toAuthorative() {
113 final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
115 toscaTopologyTemplate.setDescription(description);
117 if (policies != null) {
118 toscaTopologyTemplate.setPolicies(policies.toAuthorative());
121 return toscaTopologyTemplate;
125 public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
126 description = toscaTopologyTemplate.getDescription();
128 if (toscaTopologyTemplate.getPolicies() != null) {
129 policies = new JpaToscaPolicies();
130 policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
135 public List<PfKey> getKeys() {
136 final List<PfKey> keyList = getKey().getKeys();
138 if (policies != null) {
139 keyList.addAll(policies.getKeys());
146 public void clean() {
149 description = (description != null ? description.trim() : null);
151 if (policies != null) {
157 public PfValidationResult validate(PfValidationResult resultIn) {
158 PfValidationResult result = resultIn;
160 if (key.isNullKey()) {
161 result.addValidationMessage(
162 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
165 result = key.validate(result);
167 if (description != null && description.trim().length() == 0) {
168 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
169 "property description may not be blank"));
172 if (policies != null) {
173 result = policies.validate(result);
180 public int compareTo(final PfConcept otherConcept) {
181 if (otherConcept == null) {
184 if (this == otherConcept) {
187 if (getClass() != otherConcept.getClass()) {
188 return getClass().getName().compareTo(otherConcept.getClass().getName());
191 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
192 if (!key.equals(other.key)) {
193 return key.compareTo(other.key);
196 int result = ObjectUtils.compare(description, other.description);
201 return ObjectUtils.compare(policies, other.policies);