2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.simple.concepts;
26 import com.google.gson.annotations.SerializedName;
28 import java.util.List;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
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.common.utils.validation.Assertions;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfReferenceKey;
48 import org.onap.policy.models.base.PfUtils;
49 import org.onap.policy.models.base.PfValidationMessage;
50 import org.onap.policy.models.base.PfValidationResult;
51 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
54 * Class to represent the property in TOSCA definition.
56 * @author Chenfei Gao (cgao@research.att.com)
57 * @author Liam Fallon (liam.fallon@est.tech)
60 @Table(name = "ToscaProperty")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaToscaProperty extends PfConcept {
65 private static final long serialVersionUID = 1675770231921107988L;
68 SUPPORTED, UNSUPPORTED, EXPERIMENTAL, DEPRECATED
72 private PfReferenceKey key;
75 private PfConceptKey type;
78 private String description;
81 private boolean required = false;
83 @Column(name = "default")
84 @SerializedName("default")
85 private String defaultValue;
89 private Status status = Status.SUPPORTED;
92 private List<JpaToscaConstraint> constraints;
95 @SerializedName("entry_schema")
96 private JpaToscaEntrySchema entrySchema;
99 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
101 public JpaToscaProperty() {
102 this(new PfReferenceKey());
106 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
110 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
111 this(key, new PfConceptKey());
115 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
118 * @param type the key of the property type
120 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
128 * @param copyConcept the concept to copy from
130 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
135 public List<PfKey> getKeys() {
136 final List<PfKey> keyList = getKey().getKeys();
138 keyList.addAll(type.getKeys());
140 if (constraints != null) {
141 for (JpaToscaConstraint constraint : constraints) {
142 keyList.addAll(constraint.getKeys());
146 if (entrySchema != null) {
147 keyList.addAll(entrySchema.getKeys());
154 public void clean() {
159 if (description != null) {
160 description = description.trim();
163 if (defaultValue != null) {
164 defaultValue = defaultValue.trim();
167 if (constraints != null) {
168 for (JpaToscaConstraint constraint : constraints) {
173 if (entrySchema != null) {
179 public PfValidationResult validate(final PfValidationResult resultIn) {
180 PfValidationResult result = resultIn;
182 if (key.isNullKey()) {
183 result.addValidationMessage(
184 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
187 result = key.validate(result);
189 if (type == null || type.isNullKey()) {
190 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
191 "property type may not be null"));
194 return validateFields(result);
198 * Validate the property fields.
200 * @param resultIn the incoming validation results so far
201 * @return the validation results including this validation
203 private PfValidationResult validateFields(final PfValidationResult resultIn) {
204 PfValidationResult result = resultIn;
206 if (description != null && description.trim().length() == 0) {
207 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
208 "property description may not be blank"));
211 if (defaultValue != null && defaultValue.trim().length() == 0) {
212 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
213 "property default value may not be null"));
216 if (constraints != null) {
217 for (JpaToscaConstraint constraint : constraints) {
218 if (constraint == null) {
219 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220 "property constraint may not be null "));
222 result = constraint.validate(result);
226 return (entrySchema != null ? entrySchema.validate(result) : result);
230 public int compareTo(final PfConcept otherConcept) {
231 if (otherConcept == null) {
234 if (this == otherConcept) {
237 if (getClass() != otherConcept.getClass()) {
238 return this.hashCode() - otherConcept.hashCode();
241 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
242 if (!key.equals(other.key)) {
243 return key.compareTo(other.key);
246 return compareFields(other);
250 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty
253 * @param other the other ToscaProperty object
255 private int compareFields(final JpaToscaProperty other) {
256 if (!type.equals(other.type)) {
257 return type.compareTo(other.type);
260 int result = ObjectUtils.compare(description, other.description);
265 result = ObjectUtils.compare(required, other.required);
270 result = ObjectUtils.compare(defaultValue, other.defaultValue);
275 result = ObjectUtils.compare(status, other.status);
280 result = PfUtils.compareObjects(constraints, other.constraints);
285 return entrySchema.compareTo(other.entrySchema);
289 public PfConcept copyTo(@NonNull final PfConcept target) {
290 Assertions.instanceOf(target, JpaToscaProperty.class);
292 final JpaToscaProperty copy = ((JpaToscaProperty) target);
293 copy.setKey(new PfReferenceKey(key));
294 copy.setType(new PfConceptKey(type));
295 copy.setDescription(description);
296 copy.setRequired(required);
297 copy.setDefaultValue(defaultValue);
298 copy.setStatus(status);
299 copy.constraints = constraints; // Constraints are immutable
300 copy.setEntrySchema(entrySchema != null ? new JpaToscaEntrySchema(entrySchema) : null);