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.common.utils.coder.YamlJsonTranslator;
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 = new ArrayList<>();
92 private JpaToscaEntrySchema entrySchema;
95 private Map<String, String> metadata = new LinkedHashMap<>();
98 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
100 public JpaToscaProperty() {
101 this(new PfReferenceKey());
105 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
109 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
110 this(key, new PfConceptKey());
114 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
117 * @param type the key of the property type
119 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
127 * @param copyConcept the concept to copy from
129 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
131 this.key = new PfReferenceKey(copyConcept.key);
132 this.type = new PfConceptKey(copyConcept.type);
133 this.description = copyConcept.description;
134 this.required = copyConcept.required;
135 this.defaultValue = copyConcept.defaultValue;
136 this.status = copyConcept.status;
137 // Constraints are immutable
138 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
139 this.entrySchema = (copyConcept.entrySchema != null ? new JpaToscaEntrySchema(copyConcept.entrySchema) : null);
140 this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
144 * Authorative constructor.
146 * @param authorativeConcept the authorative concept to copy from
148 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
149 this.fromAuthorative(authorativeConcept);
153 public ToscaProperty toAuthorative() {
154 ToscaProperty toscaProperty = new ToscaProperty();
156 toscaProperty.setName(key.getLocalName());
158 toscaProperty.setType(type.getName());
159 toscaProperty.setTypeVersion(type.getVersion());
161 toscaProperty.setDescription(description);
162 toscaProperty.setRequired(required);
163 toscaProperty.setStatus(status);
165 if (defaultValue != null) {
166 toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
169 if (constraints != null) {
170 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
172 for (JpaToscaConstraint constraint : constraints) {
173 toscaConstraints.add(constraint.toAuthorative());
176 toscaProperty.setConstraints(toscaConstraints);
179 if (entrySchema != null) {
180 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
183 if (metadata != null) {
184 toscaProperty.setMetadata(new LinkedHashMap<>(metadata));
187 return toscaProperty;
191 public void fromAuthorative(ToscaProperty toscaProperty) {
192 this.setKey(new PfReferenceKey());
193 getKey().setLocalName(toscaProperty.getName());
195 if (toscaProperty.getTypeVersion() != null) {
196 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
198 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
201 description = toscaProperty.getDescription();
202 required = toscaProperty.isRequired();
203 status = toscaProperty.getStatus();
205 if (toscaProperty.getDefaultValue() != null) {
206 defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
211 if (toscaProperty.getConstraints() != null) {
212 constraints = new ArrayList<>();
214 for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
215 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
219 if (toscaProperty.getEntrySchema() != null) {
220 entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
223 // Add the property metadata if it doesn't exist already
224 if (toscaProperty.getMetadata() != null) {
225 metadata = new LinkedHashMap<>(toscaProperty.getMetadata());
231 public List<PfKey> getKeys() {
232 final List<PfKey> keyList = getKey().getKeys();
234 keyList.addAll(type.getKeys());
236 if (entrySchema != null) {
237 keyList.addAll(entrySchema.getKeys());
244 public void clean() {
249 if (description != null) {
250 description = description.trim();
253 if (defaultValue != null) {
254 defaultValue = defaultValue.trim();
257 if (entrySchema != null) {
261 if (metadata != null) {
262 for (Entry<String, String> metadataEntry : metadata.entrySet()) {
263 metadataEntry.setValue(metadataEntry.getValue().trim());
269 public PfValidationResult validate(final PfValidationResult resultIn) {
270 PfValidationResult result = resultIn;
272 if (key.isNullKey()) {
273 result.addValidationMessage(
274 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
277 result = key.validate(result);
279 if (type == null || type.isNullKey()) {
280 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
281 "property type may not be null"));
284 return validateFields(result);
288 * Validate the property fields.
290 * @param resultIn the incoming validation results so far
291 * @return the validation results including this validation
293 private PfValidationResult validateFields(final PfValidationResult resultIn) {
294 PfValidationResult result = resultIn;
296 if (description != null && description.trim().length() == 0) {
297 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
298 "property description may not be blank"));
301 if (defaultValue != null && defaultValue.trim().length() == 0) {
302 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
303 "property default value may not be null"));
306 if (constraints != null) {
307 for (JpaToscaConstraint constraint : constraints) {
308 if (constraint == null) {
309 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
310 "property constraint may not be null "));
314 return (entrySchema != null ? entrySchema.validate(result) : result);
318 public int compareTo(final PfConcept otherConcept) {
319 if (otherConcept == null) {
322 if (this == otherConcept) {
325 if (getClass() != otherConcept.getClass()) {
326 return getClass().getName().compareTo(otherConcept.getClass().getName());
329 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
330 if (!key.equals(other.key)) {
331 return key.compareTo(other.key);
334 return compareFields(other);
338 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
340 * @param other the other ToscaProperty object
342 private int compareFields(final JpaToscaProperty other) {
343 if (!type.equals(other.type)) {
344 return type.compareTo(other.type);
347 int result = ObjectUtils.compare(description, other.description);
352 result = ObjectUtils.compare(required, other.required);
357 result = ObjectUtils.compare(defaultValue, other.defaultValue);
362 result = ObjectUtils.compare(status, other.status);
367 result = PfUtils.compareObjects(constraints, other.constraints);
372 return entrySchema.compareTo(other.entrySchema);