2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 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.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.utils.validation.Assertions;
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;
71 @OneToOne(cascade = CascadeType.ALL)
72 private JpaToscaPolicies policies;
75 * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
77 public JpaToscaTopologyTemplate() {
78 this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
83 * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept
88 public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
95 * @param copyConcept the concept to copy from
97 public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
102 * Authorative constructor.
104 * @param authorativeConcept the authorative concept to copy from
106 public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
107 this.fromAuthorative(authorativeConcept);
111 public ToscaTopologyTemplate toAuthorative() {
112 final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
114 toscaTopologyTemplate.setDescription(description);
116 if (policies != null) {
117 toscaTopologyTemplate.setPolicies(policies.toAuthorative());
120 return toscaTopologyTemplate;
124 public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
125 description = toscaTopologyTemplate.getDescription();
127 if (toscaTopologyTemplate.getPolicies() != null) {
128 policies = new JpaToscaPolicies();
129 policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
134 public List<PfKey> getKeys() {
135 final List<PfKey> keyList = getKey().getKeys();
137 if (policies != null) {
138 keyList.addAll(policies.getKeys());
145 public void clean() {
148 description = (description != null ? description.trim() : null);
150 if (policies != null) {
156 public PfValidationResult validate(PfValidationResult resultIn) {
157 PfValidationResult result = resultIn;
159 if (key.isNullKey()) {
160 result.addValidationMessage(
161 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
164 result = key.validate(result);
166 if (description != null && description.trim().length() == 0) {
167 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
168 "property description may not be blank"));
171 if (policies != null) {
172 result = policies.validate(result);
179 public int compareTo(final PfConcept otherConcept) {
180 if (otherConcept == null) {
183 if (this == otherConcept) {
186 if (getClass() != otherConcept.getClass()) {
187 return this.hashCode() - otherConcept.hashCode();
190 final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
191 if (!key.equals(other.key)) {
192 return key.compareTo(other.key);
195 int result = ObjectUtils.compare(description, other.description);
200 return ObjectUtils.compare(policies, other.policies);
204 public PfConcept copyTo(@NonNull PfConcept target) {
205 final Object copyObject = target;
206 Assertions.instanceOf(copyObject, PfConcept.class);
208 final JpaToscaTopologyTemplate copy = ((JpaToscaTopologyTemplate) copyObject);
209 copy.setKey(new PfReferenceKey(key));
210 copy.setDescription(description);
211 copy.setPolicies(policies != null ? new JpaToscaPolicies(policies) : null);