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 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;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
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.ToscaProperty;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
55 * Class to represent the property in TOSCA definition.
57 * @author Chenfei Gao (cgao@research.att.com)
58 * @author Liam Fallon (liam.fallon@est.tech)
61 @Table(name = "ToscaProperty")
62 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @EqualsAndHashCode(callSuper = false)
65 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
66 private static final long serialVersionUID = 1675770231921107988L;
69 private PfReferenceKey key;
72 private PfConceptKey type;
75 private String description;
78 private boolean required = false;
80 @Column(name = "default")
81 private String defaultValue;
84 private Status status = Status.SUPPORTED;
87 private List<JpaToscaConstraint> constraints;
90 private JpaToscaSchemaDefinition entrySchema;
93 private Map<String, String> metadata;
96 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
98 public JpaToscaProperty() {
99 this(new PfReferenceKey());
103 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
107 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
108 this(key, new PfConceptKey());
112 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
115 * @param type the key of the property type
117 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
125 * @param copyConcept the concept to copy from
127 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
129 this.key = new PfReferenceKey(copyConcept.key);
130 this.type = new PfConceptKey(copyConcept.type);
131 this.description = copyConcept.description;
132 this.required = copyConcept.required;
133 this.defaultValue = copyConcept.defaultValue;
134 this.status = copyConcept.status;
135 // Constraints are immutable
136 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
138 (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(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.setStatus(status);
164 if (defaultValue != null) {
165 toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
168 toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
170 if (entrySchema != null) {
171 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
174 toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem));
176 return toscaProperty;
180 public void fromAuthorative(ToscaProperty toscaProperty) {
181 this.setKey(new PfReferenceKey());
182 getKey().setLocalName(toscaProperty.getName());
184 if (toscaProperty.getTypeVersion() != null) {
185 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
187 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
190 description = toscaProperty.getDescription();
191 required = toscaProperty.isRequired();
192 status = toscaProperty.getStatus();
194 if (toscaProperty.getDefaultValue() != null) {
195 defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
200 constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance);
202 if (toscaProperty.getEntrySchema() != null) {
203 entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema());
206 metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem);
210 public List<PfKey> getKeys() {
211 final List<PfKey> keyList = getKey().getKeys();
213 keyList.addAll(type.getKeys());
215 if (entrySchema != null) {
216 keyList.addAll(entrySchema.getKeys());
223 public void clean() {
228 if (description != null) {
229 description = description.trim();
232 if (defaultValue != null) {
233 defaultValue = defaultValue.trim();
236 if (entrySchema != null) {
240 metadata = PfUtils.mapMap(metadata, String::trim);
244 public PfValidationResult validate(final PfValidationResult resultIn) {
245 PfValidationResult result = resultIn;
247 if (key.isNullKey()) {
248 result.addValidationMessage(
249 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
252 result = key.validate(result);
254 if (type == null || type.isNullKey()) {
255 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
256 "property type may not be null"));
259 return validateFields(result);
263 * Validate the property fields.
265 * @param resultIn the incoming validation results so far
266 * @return the validation results including this validation
268 private PfValidationResult validateFields(final PfValidationResult resultIn) {
269 PfValidationResult result = resultIn;
271 if (description != null && description.trim().length() == 0) {
272 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
273 "property description may not be blank"));
276 if (defaultValue != null && defaultValue.trim().length() == 0) {
277 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
278 "property default value may not be null"));
281 if (constraints != null) {
282 for (JpaToscaConstraint constraint : constraints) {
283 if (constraint == null) {
284 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
285 "property constraint may not be null "));
289 return (entrySchema != null ? entrySchema.validate(result) : result);
293 public int compareTo(final PfConcept otherConcept) {
294 if (otherConcept == null) {
297 if (this == otherConcept) {
300 if (getClass() != otherConcept.getClass()) {
301 return getClass().getName().compareTo(otherConcept.getClass().getName());
304 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
305 if (!key.equals(other.key)) {
306 return key.compareTo(other.key);
309 return compareFields(other);
313 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
315 * @param other the other ToscaProperty object
317 private int compareFields(final JpaToscaProperty other) {
318 if (!type.equals(other.type)) {
319 return type.compareTo(other.type);
322 int result = ObjectUtils.compare(description, other.description);
327 result = ObjectUtils.compare(required, other.required);
332 result = ObjectUtils.compare(defaultValue, other.defaultValue);
337 result = ObjectUtils.compare(status, other.status);
342 result = PfUtils.compareCollections(constraints, other.constraints);
347 result = entrySchema.compareTo(other.entrySchema);
352 return PfUtils.compareMaps(metadata, other.metadata);