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.PfConcept;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.base.PfValidationMessage;
44 import org.onap.policy.models.base.PfValidationResult;
45 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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 ToscaTopologyTemplate extends PfConcept {
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(cascade = CascadeType.ALL)
70 private ToscaPolicies policies;
73 * The Default Constructor creates a {@link ToscaTopologyTemplate} object with a null key.
75 public ToscaTopologyTemplate() {
76 this(new PfReferenceKey(ToscaServiceTemplate.DEFAULT_NAME, ToscaServiceTemplate.DEFAULT_VERSION,
81 * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept
86 public ToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
93 * @param copyConcept the concept to copy from
95 public ToscaTopologyTemplate(final ToscaTopologyTemplate copyConcept) {
100 public List<PfKey> getKeys() {
101 final List<PfKey> keyList = getKey().getKeys();
103 if (policies != null) {
104 keyList.addAll(policies.getKeys());
111 public void clean() {
114 description = (description != null ? description.trim() : null);
116 if (policies != null) {
122 public PfValidationResult validate(PfValidationResult resultIn) {
123 PfValidationResult result = resultIn;
125 if (key.isNullKey()) {
126 result.addValidationMessage(
127 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
130 result = key.validate(result);
132 if (description != null && description.trim().length() == 0) {
133 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
134 "property description may not be blank"));
137 if (policies != null) {
138 result = policies.validate(result);
145 public int compareTo(final PfConcept otherConcept) {
146 if (otherConcept == null) {
149 if (this == otherConcept) {
152 if (getClass() != otherConcept.getClass()) {
153 return this.hashCode() - otherConcept.hashCode();
156 final ToscaTopologyTemplate other = (ToscaTopologyTemplate) otherConcept;
157 if (!key.equals(other.key)) {
158 return key.compareTo(other.key);
161 int result = ObjectUtils.compare(description, other.description);
166 return ObjectUtils.compare(policies, other.policies);
170 public PfConcept copyTo(@NonNull PfConcept target) {
171 final Object copyObject = target;
172 Assertions.instanceOf(copyObject, PfConcept.class);
174 final ToscaTopologyTemplate copy = ((ToscaTopologyTemplate) copyObject);
175 copy.setKey(new PfReferenceKey(key));
176 copy.setDescription(description);
177 copy.setPolicies(policies != null ? new ToscaPolicies(policies) : null);