2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation.
7 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * SPDX-License-Identifier: Apache-2.0
22 * ============LICENSE_END=========================================================
25 package org.onap.policy.models.tosca.simple.concepts;
27 import jakarta.persistence.Column;
28 import jakarta.persistence.ElementCollection;
29 import jakarta.persistence.EmbeddedId;
30 import java.io.Serial;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.parameters.annotations.NotBlank;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.common.parameters.annotations.Valid;
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.validation.annotations.VerifyKey;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
54 * Class to represent the property in TOSCA definition.
56 * @author Chenfei Gao (cgao@research.att.com)
57 * @author Liam Fallon (liam.fallon@est.tech)
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
63 private static final long serialVersionUID = 1675770231921107988L;
68 private PfReferenceKey key;
73 private PfConceptKey type;
77 private String description;
80 private boolean required = false;
84 private String defaultValue;
87 private Status status = Status.SUPPORTED;
90 private List<@NotNull @Valid JpaToscaConstraint> constraints;
94 private JpaToscaSchemaDefinition entrySchema;
97 private Map<String, String> metadata;
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);
142 (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(copyConcept.entrySchema) : null);
143 this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
147 * Authorative constructor.
149 * @param authorativeConcept the authorative concept to copy from
151 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
152 this.fromAuthorative(authorativeConcept);
156 public ToscaProperty toAuthorative() {
157 var toscaProperty = new ToscaProperty();
159 toscaProperty.setName(key.getLocalName());
161 toscaProperty.setType(type.getName());
162 toscaProperty.setTypeVersion(type.getVersion());
164 toscaProperty.setDescription(description);
165 toscaProperty.setRequired(required);
166 toscaProperty.setStatus(status);
168 if (defaultValue != null) {
169 toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
172 toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
174 if (entrySchema != null) {
175 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
178 toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem));
180 return toscaProperty;
184 public void fromAuthorative(ToscaProperty toscaProperty) {
185 this.setKey(new PfReferenceKey());
186 getKey().setLocalName(toscaProperty.getName());
188 if (toscaProperty.getTypeVersion() != null) {
189 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
191 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
194 description = toscaProperty.getDescription();
195 required = toscaProperty.isRequired();
196 status = toscaProperty.getStatus();
198 if (toscaProperty.getDefaultValue() != null) {
199 defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
204 constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance);
206 if (toscaProperty.getEntrySchema() != null) {
207 entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema());
210 metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem);
214 public List<PfKey> getKeys() {
215 final List<PfKey> keyList = getKey().getKeys();
217 keyList.addAll(type.getKeys());
219 if (entrySchema != null) {
220 keyList.addAll(entrySchema.getKeys());
227 public void clean() {
232 if (description != null) {
233 description = description.trim();
236 if (defaultValue != null) {
237 defaultValue = defaultValue.trim();
240 if (entrySchema != null) {
244 metadata = PfUtils.mapMap(metadata, String::trim);
248 public int compareTo(@NonNull final PfConcept otherConcept) {
249 if (this == otherConcept) {
252 if (getClass() != otherConcept.getClass()) {
253 return getClass().getName().compareTo(otherConcept.getClass().getName());
256 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
257 if (!key.equals(other.key)) {
258 return key.compareTo(other.key);
261 return compareFields(other);
265 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
267 * @param other the other ToscaProperty object
269 private int compareFields(final JpaToscaProperty other) {
270 if (!type.equals(other.type)) {
271 return type.compareTo(other.type);
274 int result = ObjectUtils.compare(description, other.description);
279 result = ObjectUtils.compare(required, other.required);
284 result = ObjectUtils.compare(defaultValue, other.defaultValue);
289 result = ObjectUtils.compare(status, other.status);
294 result = PfUtils.compareCollections(constraints, other.constraints);
299 result = entrySchema.compareTo(other.entrySchema);
304 return PfUtils.compareMaps(metadata, other.metadata);