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;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Table;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.common.utils.validation.Assertions;
43 import org.onap.policy.models.base.PfAuthorative;
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;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
57 * Class to represent the property in TOSCA definition.
59 * @author Chenfei Gao (cgao@research.att.com)
60 * @author Liam Fallon (liam.fallon@est.tech)
63 @Table(name = "ToscaProperty")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
68 private static final long serialVersionUID = 1675770231921107988L;
71 private PfReferenceKey key;
74 private PfConceptKey type;
77 private String description;
80 private boolean required = false;
82 @Column(name = "default")
83 private String defaultValue;
86 private Status status = Status.SUPPORTED;
89 private List<JpaToscaConstraint> constraints;
92 private JpaToscaEntrySchema entrySchema;
95 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
97 public JpaToscaProperty() {
98 this(new PfReferenceKey());
102 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
106 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
107 this(key, new PfConceptKey());
111 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
114 * @param type the key of the property type
116 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
124 * @param copyConcept the concept to copy from
126 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
131 * Authorative constructor.
133 * @param authorativeConcept the authorative concept to copy from
135 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
136 this.fromAuthorative(authorativeConcept);
140 public ToscaProperty toAuthorative() {
141 ToscaProperty toscaProperty = new ToscaProperty();
143 toscaProperty.setName(key.getLocalName());
145 toscaProperty.setType(type.getName());
146 toscaProperty.setTypeVersion(type.getVersion());
148 toscaProperty.setDescription(description);
149 toscaProperty.setRequired(required);
150 toscaProperty.setDefaultValue(defaultValue);
151 toscaProperty.setStatus(status);
153 if (constraints != null) {
154 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
156 for (JpaToscaConstraint constraint : constraints) {
157 toscaConstraints.add(constraint.toAuthorative());
160 toscaProperty.setConstraints(toscaConstraints);
163 if (entrySchema != null) {
164 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
167 return toscaProperty;
171 public void fromAuthorative(ToscaProperty toscaProperty) {
172 this.setKey(new PfReferenceKey());
173 getKey().setLocalName(toscaProperty.getName());
175 if (toscaProperty.getTypeVersion() != null) {
176 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
178 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
181 description = toscaProperty.getDescription();
182 required = toscaProperty.isRequired();
183 defaultValue = toscaProperty.getDefaultValue();
184 status = toscaProperty.getStatus();
186 if (toscaProperty.getConstraints() != null) {
187 constraints = new ArrayList<>();
189 for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
190 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
194 if (toscaProperty.getEntrySchema() != null) {
195 entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
200 public List<PfKey> getKeys() {
201 final List<PfKey> keyList = getKey().getKeys();
203 keyList.addAll(type.getKeys());
205 if (entrySchema != null) {
206 keyList.addAll(entrySchema.getKeys());
213 public void clean() {
218 if (description != null) {
219 description = description.trim();
222 if (defaultValue != null) {
223 defaultValue = defaultValue.trim();
226 if (entrySchema != null) {
232 public PfValidationResult validate(final PfValidationResult resultIn) {
233 PfValidationResult result = resultIn;
235 if (key.isNullKey()) {
236 result.addValidationMessage(
237 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
240 result = key.validate(result);
242 if (type == null || type.isNullKey()) {
243 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
244 "property type may not be null"));
247 return validateFields(result);
251 * Validate the property fields.
253 * @param resultIn the incoming validation results so far
254 * @return the validation results including this validation
256 private PfValidationResult validateFields(final PfValidationResult resultIn) {
257 PfValidationResult result = resultIn;
259 if (description != null && description.trim().length() == 0) {
260 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
261 "property description may not be blank"));
264 if (defaultValue != null && defaultValue.trim().length() == 0) {
265 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
266 "property default value may not be null"));
269 if (constraints != null) {
270 for (JpaToscaConstraint constraint : constraints) {
271 if (constraint == null) {
272 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
273 "property constraint may not be null "));
277 return (entrySchema != null ? entrySchema.validate(result) : result);
281 public int compareTo(final PfConcept otherConcept) {
282 if (otherConcept == null) {
285 if (this == otherConcept) {
288 if (getClass() != otherConcept.getClass()) {
289 return this.hashCode() - otherConcept.hashCode();
292 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
293 if (!key.equals(other.key)) {
294 return key.compareTo(other.key);
297 return compareFields(other);
301 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
303 * @param other the other ToscaProperty object
305 private int compareFields(final JpaToscaProperty other) {
306 if (!type.equals(other.type)) {
307 return type.compareTo(other.type);
310 int result = ObjectUtils.compare(description, other.description);
315 result = ObjectUtils.compare(required, other.required);
320 result = ObjectUtils.compare(defaultValue, other.defaultValue);
325 result = ObjectUtils.compare(status, other.status);
330 result = PfUtils.compareObjects(constraints, other.constraints);
335 return entrySchema.compareTo(other.entrySchema);
339 public PfConcept copyTo(@NonNull final PfConcept target) {
340 Assertions.instanceOf(target, JpaToscaProperty.class);
342 final JpaToscaProperty copy = ((JpaToscaProperty) target);
343 copy.setKey(new PfReferenceKey(key));
344 copy.setType(new PfConceptKey(type));
345 copy.setDescription(description);
346 copy.setRequired(required);
347 copy.setDefaultValue(defaultValue);
348 copy.setStatus(status);
350 if (constraints == null) {
351 copy.setConstraints(null);
353 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
354 for (final JpaToscaConstraint constraint : constraints) {
355 newConstraints.add(constraint); // Constraints are immutable
357 copy.setConstraints(newConstraints);
360 copy.setEntrySchema(entrySchema != null ? new JpaToscaEntrySchema(entrySchema) : null);