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 java.util.ArrayList;
27 import java.util.List;
28 import javax.persistence.Column;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.models.base.PfAuthorative;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfReferenceKey;
44 import org.onap.policy.models.base.PfUtils;
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.ToscaConstraint;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
53 * Class to represent the property in TOSCA definition.
55 * @author Chenfei Gao (cgao@research.att.com)
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaProperty")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
64 private static final long serialVersionUID = 1675770231921107988L;
67 private PfReferenceKey key;
70 private PfConceptKey type;
73 private String description;
76 private boolean required = false;
78 @Column(name = "default")
79 private String defaultValue;
82 private Status status = Status.SUPPORTED;
85 private List<JpaToscaConstraint> constraints;
88 private JpaToscaEntrySchema entrySchema;
91 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
93 public JpaToscaProperty() {
94 this(new PfReferenceKey());
98 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
102 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
103 this(key, new PfConceptKey());
107 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
110 * @param type the key of the property type
112 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
120 * @param copyConcept the concept to copy from
122 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
124 this.key = new PfReferenceKey(copyConcept.key);
125 this.type = new PfConceptKey(copyConcept.type);
126 this.description = copyConcept.description;
127 this.required = copyConcept.required;
128 this.defaultValue = copyConcept.defaultValue;
129 this.status = copyConcept.status;
130 // Constraints are immutable
131 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
132 this.entrySchema = (copyConcept.entrySchema != null ? new JpaToscaEntrySchema(copyConcept.entrySchema) : null);
136 * Authorative constructor.
138 * @param authorativeConcept the authorative concept to copy from
140 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
141 this.fromAuthorative(authorativeConcept);
145 public ToscaProperty toAuthorative() {
146 ToscaProperty toscaProperty = new ToscaProperty();
148 toscaProperty.setName(key.getLocalName());
150 toscaProperty.setType(type.getName());
151 toscaProperty.setTypeVersion(type.getVersion());
153 toscaProperty.setDescription(description);
154 toscaProperty.setRequired(required);
155 toscaProperty.setDefaultValue(defaultValue);
156 toscaProperty.setStatus(status);
158 if (constraints != null) {
159 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
161 for (JpaToscaConstraint constraint : constraints) {
162 toscaConstraints.add(constraint.toAuthorative());
165 toscaProperty.setConstraints(toscaConstraints);
168 if (entrySchema != null) {
169 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
172 return toscaProperty;
176 public void fromAuthorative(ToscaProperty toscaProperty) {
177 this.setKey(new PfReferenceKey());
178 getKey().setLocalName(toscaProperty.getName());
180 if (toscaProperty.getTypeVersion() != null) {
181 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
183 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
186 description = toscaProperty.getDescription();
187 required = toscaProperty.isRequired();
188 defaultValue = toscaProperty.getDefaultValue();
189 status = toscaProperty.getStatus();
191 if (toscaProperty.getConstraints() != null) {
192 constraints = new ArrayList<>();
194 for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
195 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
199 if (toscaProperty.getEntrySchema() != null) {
200 entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
205 public List<PfKey> getKeys() {
206 final List<PfKey> keyList = getKey().getKeys();
208 keyList.addAll(type.getKeys());
210 if (entrySchema != null) {
211 keyList.addAll(entrySchema.getKeys());
218 public void clean() {
223 if (description != null) {
224 description = description.trim();
227 if (defaultValue != null) {
228 defaultValue = defaultValue.trim();
231 if (entrySchema != null) {
237 public PfValidationResult validate(final PfValidationResult resultIn) {
238 PfValidationResult result = resultIn;
240 if (key.isNullKey()) {
241 result.addValidationMessage(
242 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
245 result = key.validate(result);
247 if (type == null || type.isNullKey()) {
248 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
249 "property type may not be null"));
252 return validateFields(result);
256 * Validate the property fields.
258 * @param resultIn the incoming validation results so far
259 * @return the validation results including this validation
261 private PfValidationResult validateFields(final PfValidationResult resultIn) {
262 PfValidationResult result = resultIn;
264 if (description != null && description.trim().length() == 0) {
265 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
266 "property description may not be blank"));
269 if (defaultValue != null && defaultValue.trim().length() == 0) {
270 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
271 "property default value may not be null"));
274 if (constraints != null) {
275 for (JpaToscaConstraint constraint : constraints) {
276 if (constraint == null) {
277 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
278 "property constraint may not be null "));
282 return (entrySchema != null ? entrySchema.validate(result) : result);
286 public int compareTo(final PfConcept otherConcept) {
287 if (otherConcept == null) {
290 if (this == otherConcept) {
293 if (getClass() != otherConcept.getClass()) {
294 return getClass().getName().compareTo(otherConcept.getClass().getName());
297 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
298 if (!key.equals(other.key)) {
299 return key.compareTo(other.key);
302 return compareFields(other);
306 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
308 * @param other the other ToscaProperty object
310 private int compareFields(final JpaToscaProperty other) {
311 if (!type.equals(other.type)) {
312 return type.compareTo(other.type);
315 int result = ObjectUtils.compare(description, other.description);
320 result = ObjectUtils.compare(required, other.required);
325 result = ObjectUtils.compare(defaultValue, other.defaultValue);
330 result = ObjectUtils.compare(status, other.status);
335 result = PfUtils.compareObjects(constraints, other.constraints);
340 return entrySchema.compareTo(other.entrySchema);