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;
87 private Status status = Status.SUPPORTED;
90 private List<JpaToscaConstraint> constraints;
93 private JpaToscaEntrySchema entrySchema;
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) {
132 * Authorative constructor.
134 * @param authorativeConcept the authorative concept to copy from
136 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
137 this.fromAuthorative(authorativeConcept);
141 public ToscaProperty toAuthorative() {
142 ToscaProperty toscaProperty = new ToscaProperty();
144 toscaProperty.setName(key.getLocalName());
146 toscaProperty.setType(type.getName());
147 toscaProperty.setTypeVersion(type.getVersion());
149 toscaProperty.setDescription(description);
150 toscaProperty.setRequired(required);
151 toscaProperty.setDefaultValue(defaultValue);
152 toscaProperty.setStatus(status);
154 if (constraints != null) {
155 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
157 for (JpaToscaConstraint constraint : constraints) {
158 toscaConstraints.add(constraint.toAuthorative());
161 toscaProperty.setConstraints(toscaConstraints);
164 if (entrySchema != null) {
165 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
168 return toscaProperty;
172 public void fromAuthorative(ToscaProperty toscaProperty) {
173 this.setKey(new PfReferenceKey());
174 getKey().setLocalName(toscaProperty.getName());
176 if (toscaProperty.getTypeVersion() != null) {
177 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
179 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
182 description = toscaProperty.getDescription();
183 required = toscaProperty.isRequired();
184 defaultValue = toscaProperty.getDefaultValue();
185 status = toscaProperty.getStatus();
187 if (toscaProperty.getConstraints() != null) {
188 constraints = new ArrayList<>();
190 for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
191 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
195 if (toscaProperty.getEntrySchema() != null) {
196 entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
201 public List<PfKey> getKeys() {
202 final List<PfKey> keyList = getKey().getKeys();
204 keyList.addAll(type.getKeys());
206 if (entrySchema != null) {
207 keyList.addAll(entrySchema.getKeys());
214 public void clean() {
219 if (description != null) {
220 description = description.trim();
223 if (defaultValue != null) {
224 defaultValue = defaultValue.trim();
227 if (entrySchema != null) {
233 public PfValidationResult validate(final PfValidationResult resultIn) {
234 PfValidationResult result = resultIn;
236 if (key.isNullKey()) {
237 result.addValidationMessage(
238 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
241 result = key.validate(result);
243 if (type == null || type.isNullKey()) {
244 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
245 "property type may not be null"));
248 return validateFields(result);
252 * Validate the property fields.
254 * @param resultIn the incoming validation results so far
255 * @return the validation results including this validation
257 private PfValidationResult validateFields(final PfValidationResult resultIn) {
258 PfValidationResult result = resultIn;
260 if (description != null && description.trim().length() == 0) {
261 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
262 "property description may not be blank"));
265 if (defaultValue != null && defaultValue.trim().length() == 0) {
266 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
267 "property default value may not be null"));
270 if (constraints != null) {
271 for (JpaToscaConstraint constraint : constraints) {
272 if (constraint == null) {
273 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
274 "property constraint may not be null "));
278 return (entrySchema != null ? entrySchema.validate(result) : result);
282 public int compareTo(final PfConcept otherConcept) {
283 if (otherConcept == null) {
286 if (this == otherConcept) {
289 if (getClass() != otherConcept.getClass()) {
290 return this.hashCode() - otherConcept.hashCode();
293 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
294 if (!key.equals(other.key)) {
295 return key.compareTo(other.key);
298 return compareFields(other);
302 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
304 * @param other the other ToscaProperty object
306 private int compareFields(final JpaToscaProperty other) {
307 if (!type.equals(other.type)) {
308 return type.compareTo(other.type);
311 int result = ObjectUtils.compare(description, other.description);
316 result = ObjectUtils.compare(required, other.required);
321 result = ObjectUtils.compare(defaultValue, other.defaultValue);
326 result = ObjectUtils.compare(status, other.status);
331 result = PfUtils.compareObjects(constraints, other.constraints);
336 return entrySchema.compareTo(other.entrySchema);
340 public PfConcept copyTo(@NonNull final PfConcept target) {
341 Assertions.instanceOf(target, JpaToscaProperty.class);
343 final JpaToscaProperty copy = ((JpaToscaProperty) target);
344 copy.setKey(new PfReferenceKey(key));
345 copy.setType(new PfConceptKey(type));
346 copy.setDescription(description);
347 copy.setRequired(required);
348 copy.setDefaultValue(defaultValue);
349 copy.setStatus(status);
351 if (constraints == null) {
352 copy.setConstraints(null);
354 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
355 for (final JpaToscaConstraint constraint : constraints) {
356 newConstraints.add(constraint); // Constraints are immutable
358 copy.setConstraints(newConstraints);
361 copy.setEntrySchema(entrySchema != null ? new JpaToscaEntrySchema(entrySchema) : null);