2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 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.LinkedHashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import javax.persistence.Column;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.Entity;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.Table;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.PfUtils;
48 import org.onap.policy.models.base.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
56 * Class to represent the property in TOSCA definition.
58 * @author Chenfei Gao (cgao@research.att.com)
59 * @author Liam Fallon (liam.fallon@est.tech)
62 @Table(name = "ToscaProperty")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
67 private static final long serialVersionUID = 1675770231921107988L;
70 private PfReferenceKey key;
73 private PfConceptKey type;
76 private String description;
79 private boolean required = false;
81 @Column(name = "default")
82 private String defaultValue;
85 private Status status = Status.SUPPORTED;
88 private List<JpaToscaConstraint> constraints = new ArrayList<>();
91 private JpaToscaEntrySchema entrySchema;
94 private Map<String, String> metadata = new LinkedHashMap<>();
97 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
99 public JpaToscaProperty() {
100 this(new PfReferenceKey());
104 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
108 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
109 this(key, new PfConceptKey());
113 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
116 * @param type the key of the property type
118 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
126 * @param copyConcept the concept to copy from
128 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
130 this.key = new PfReferenceKey(copyConcept.key);
131 this.type = new PfConceptKey(copyConcept.type);
132 this.description = copyConcept.description;
133 this.required = copyConcept.required;
134 this.defaultValue = copyConcept.defaultValue;
135 this.status = copyConcept.status;
136 // Constraints are immutable
137 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
138 this.entrySchema = (copyConcept.entrySchema != null ? new JpaToscaEntrySchema(copyConcept.entrySchema) : null);
139 this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
143 * Authorative constructor.
145 * @param authorativeConcept the authorative concept to copy from
147 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
148 this.fromAuthorative(authorativeConcept);
152 public ToscaProperty toAuthorative() {
153 ToscaProperty toscaProperty = new ToscaProperty();
155 toscaProperty.setName(key.getLocalName());
157 toscaProperty.setType(type.getName());
158 toscaProperty.setTypeVersion(type.getVersion());
160 toscaProperty.setDescription(description);
161 toscaProperty.setRequired(required);
162 toscaProperty.setDefaultValue(defaultValue);
163 toscaProperty.setStatus(status);
165 if (constraints != null) {
166 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
168 for (JpaToscaConstraint constraint : constraints) {
169 toscaConstraints.add(constraint.toAuthorative());
172 toscaProperty.setConstraints(toscaConstraints);
175 if (entrySchema != null) {
176 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
179 if (metadata != null) {
180 toscaProperty.setMetadata(new LinkedHashMap<>(metadata));
183 return toscaProperty;
187 public void fromAuthorative(ToscaProperty toscaProperty) {
188 this.setKey(new PfReferenceKey());
189 getKey().setLocalName(toscaProperty.getName());
191 if (toscaProperty.getTypeVersion() != null) {
192 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
194 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
197 description = toscaProperty.getDescription();
198 required = toscaProperty.isRequired();
199 defaultValue = toscaProperty.getDefaultValue();
200 status = toscaProperty.getStatus();
202 if (toscaProperty.getConstraints() != null) {
203 constraints = new ArrayList<>();
205 for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
206 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
210 if (toscaProperty.getEntrySchema() != null) {
211 entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
214 // Add the property metadata if it doesn't exist already
215 if (toscaProperty.getMetadata() != null) {
216 metadata = new LinkedHashMap<>(toscaProperty.getMetadata());
222 public List<PfKey> getKeys() {
223 final List<PfKey> keyList = getKey().getKeys();
225 keyList.addAll(type.getKeys());
227 if (entrySchema != null) {
228 keyList.addAll(entrySchema.getKeys());
235 public void clean() {
240 if (description != null) {
241 description = description.trim();
244 if (defaultValue != null) {
245 defaultValue = defaultValue.trim();
248 if (entrySchema != null) {
252 if (metadata != null) {
253 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
254 metadataEntry.setValue(metadataEntry.getValue().trim());
260 public PfValidationResult validate(final PfValidationResult resultIn) {
261 PfValidationResult result = resultIn;
263 if (key.isNullKey()) {
264 result.addValidationMessage(
265 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
268 result = key.validate(result);
270 if (type == null || type.isNullKey()) {
271 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
272 "property type may not be null"));
275 return validateFields(result);
279 * Validate the property fields.
281 * @param resultIn the incoming validation results so far
282 * @return the validation results including this validation
284 private PfValidationResult validateFields(final PfValidationResult resultIn) {
285 PfValidationResult result = resultIn;
287 if (description != null && description.trim().length() == 0) {
288 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
289 "property description may not be blank"));
292 if (defaultValue != null && defaultValue.trim().length() == 0) {
293 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
294 "property default value may not be null"));
297 if (constraints != null) {
298 for (JpaToscaConstraint constraint : constraints) {
299 if (constraint == null) {
300 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
301 "property constraint may not be null "));
305 return (entrySchema != null ? entrySchema.validate(result) : result);
309 public int compareTo(final PfConcept otherConcept) {
310 if (otherConcept == null) {
313 if (this == otherConcept) {
316 if (getClass() != otherConcept.getClass()) {
317 return getClass().getName().compareTo(otherConcept.getClass().getName());
320 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
321 if (!key.equals(other.key)) {
322 return key.compareTo(other.key);
325 return compareFields(other);
329 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
331 * @param other the other ToscaProperty object
333 private int compareFields(final JpaToscaProperty other) {
334 if (!type.equals(other.type)) {
335 return type.compareTo(other.type);
338 int result = ObjectUtils.compare(description, other.description);
343 result = ObjectUtils.compare(required, other.required);
348 result = ObjectUtils.compare(defaultValue, other.defaultValue);
353 result = ObjectUtils.compare(status, other.status);
358 result = PfUtils.compareObjects(constraints, other.constraints);
363 return entrySchema.compareTo(other.entrySchema);