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;
32 import javax.persistence.Column;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.EmbeddedId;
35 import javax.persistence.Entity;
36 import javax.persistence.Inheritance;
37 import javax.persistence.InheritanceType;
38 import javax.persistence.Table;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
44 import org.apache.commons.lang3.ObjectUtils;
45 import org.onap.policy.models.base.PfAuthorative;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfConceptKey;
48 import org.onap.policy.models.base.PfKey;
49 import org.onap.policy.models.base.PfReferenceKey;
50 import org.onap.policy.models.base.PfUtils;
51 import org.onap.policy.models.base.PfValidationMessage;
52 import org.onap.policy.models.base.PfValidationResult;
53 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
59 * Class to represent the property in TOSCA definition.
61 * @author Chenfei Gao (cgao@research.att.com)
62 * @author Liam Fallon (liam.fallon@est.tech)
65 @Table(name = "ToscaProperty")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
68 @EqualsAndHashCode(callSuper = false)
69 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
70 private static final long serialVersionUID = 1675770231921107988L;
73 private PfReferenceKey key;
76 private PfConceptKey type;
79 private String description;
82 private boolean required = false;
84 @Column(name = "default")
85 private String defaultValue;
88 private Status status = Status.SUPPORTED;
91 private List<JpaToscaConstraint> constraints = new ArrayList<>();
94 private JpaToscaEntrySchema entrySchema;
97 private Map<String, String> metadata = new LinkedHashMap<>();
100 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
102 public JpaToscaProperty() {
103 this(new PfReferenceKey());
107 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
111 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
112 this(key, new PfConceptKey());
116 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
119 * @param type the key of the property type
121 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
129 * @param copyConcept the concept to copy from
131 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
133 this.key = new PfReferenceKey(copyConcept.key);
134 this.type = new PfConceptKey(copyConcept.type);
135 this.description = copyConcept.description;
136 this.required = copyConcept.required;
137 this.defaultValue = copyConcept.defaultValue;
138 this.status = copyConcept.status;
139 // Constraints are immutable
140 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
141 this.entrySchema = (copyConcept.entrySchema != null ? new JpaToscaEntrySchema(copyConcept.entrySchema) : null);
142 this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
146 * Authorative constructor.
148 * @param authorativeConcept the authorative concept to copy from
150 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
151 this.fromAuthorative(authorativeConcept);
155 public ToscaProperty toAuthorative() {
156 ToscaProperty toscaProperty = new ToscaProperty();
158 toscaProperty.setName(key.getLocalName());
160 toscaProperty.setType(type.getName());
161 toscaProperty.setTypeVersion(type.getVersion());
163 toscaProperty.setDescription(description);
164 toscaProperty.setRequired(required);
165 toscaProperty.setDefaultValue(defaultValue);
166 toscaProperty.setStatus(status);
168 if (constraints != null) {
169 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
171 for (JpaToscaConstraint constraint : constraints) {
172 toscaConstraints.add(constraint.toAuthorative());
175 toscaProperty.setConstraints(toscaConstraints);
178 if (entrySchema != null) {
179 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
182 if (metadata != null) {
183 toscaProperty.setMetadata(new LinkedHashMap<>(metadata));
186 return toscaProperty;
190 public void fromAuthorative(ToscaProperty toscaProperty) {
191 this.setKey(new PfReferenceKey());
192 getKey().setLocalName(toscaProperty.getName());
194 if (toscaProperty.getTypeVersion() != null) {
195 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
197 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
200 description = toscaProperty.getDescription();
201 required = toscaProperty.isRequired();
202 defaultValue = toscaProperty.getDefaultValue();
203 status = toscaProperty.getStatus();
205 if (toscaProperty.getConstraints() != null) {
206 constraints = new ArrayList<>();
208 for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
209 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
213 if (toscaProperty.getEntrySchema() != null) {
214 entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
217 // Add the property metadata if it doesn't exist already
218 if (toscaProperty.getMetadata() != null) {
219 metadata = new LinkedHashMap<>(toscaProperty.getMetadata());
225 public List<PfKey> getKeys() {
226 final List<PfKey> keyList = getKey().getKeys();
228 keyList.addAll(type.getKeys());
230 if (entrySchema != null) {
231 keyList.addAll(entrySchema.getKeys());
238 public void clean() {
243 if (description != null) {
244 description = description.trim();
247 if (defaultValue != null) {
248 defaultValue = defaultValue.trim();
251 if (entrySchema != null) {
255 if (metadata != null) {
256 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
257 metadataEntry.setValue(metadataEntry.getValue().trim());
263 public PfValidationResult validate(final PfValidationResult resultIn) {
264 PfValidationResult result = resultIn;
266 if (key.isNullKey()) {
267 result.addValidationMessage(
268 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
271 result = key.validate(result);
273 if (type == null || type.isNullKey()) {
274 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
275 "property type may not be null"));
278 return validateFields(result);
282 * Validate the property fields.
284 * @param resultIn the incoming validation results so far
285 * @return the validation results including this validation
287 private PfValidationResult validateFields(final PfValidationResult resultIn) {
288 PfValidationResult result = resultIn;
290 if (description != null && description.trim().length() == 0) {
291 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
292 "property description may not be blank"));
295 if (defaultValue != null && defaultValue.trim().length() == 0) {
296 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
297 "property default value may not be null"));
300 if (constraints != null) {
301 for (JpaToscaConstraint constraint : constraints) {
302 if (constraint == null) {
303 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
304 "property constraint may not be null "));
308 return (entrySchema != null ? entrySchema.validate(result) : result);
312 public int compareTo(final PfConcept otherConcept) {
313 if (otherConcept == null) {
316 if (this == otherConcept) {
319 if (getClass() != otherConcept.getClass()) {
320 return getClass().getName().compareTo(otherConcept.getClass().getName());
323 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
324 if (!key.equals(other.key)) {
325 return key.compareTo(other.key);
328 return compareFields(other);
332 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
334 * @param other the other ToscaProperty object
336 private int compareFields(final JpaToscaProperty other) {
337 if (!type.equals(other.type)) {
338 return type.compareTo(other.type);
341 int result = ObjectUtils.compare(description, other.description);
346 result = ObjectUtils.compare(required, other.required);
351 result = ObjectUtils.compare(defaultValue, other.defaultValue);
356 result = ObjectUtils.compare(status, other.status);
361 result = PfUtils.compareObjects(constraints, other.constraints);
366 return entrySchema.compareTo(other.entrySchema);