2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2021 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 java.util.ArrayList;
28 import java.util.LinkedHashMap;
29 import java.util.List;
31 import javax.persistence.Column;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.EmbeddedId;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.ObjectUtils;
38 import org.onap.policy.common.parameters.annotations.NotBlank;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.common.parameters.annotations.Valid;
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.validation.annotations.VerifyKey;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
53 * Class to represent the property in TOSCA definition.
55 * @author Chenfei Gao (cgao@research.att.com)
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
61 private static final long serialVersionUID = 1675770231921107988L;
66 private PfReferenceKey key;
71 private PfConceptKey type;
75 private String description;
78 private boolean required = false;
82 private String defaultValue;
85 private Status status = Status.SUPPORTED;
88 private List<@NotNull @Valid JpaToscaConstraint> constraints;
92 private JpaToscaSchemaDefinition entrySchema;
95 private Map<String, String> metadata;
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);
140 (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(copyConcept.entrySchema) : null);
141 this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
145 * Authorative constructor.
147 * @param authorativeConcept the authorative concept to copy from
149 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
150 this.fromAuthorative(authorativeConcept);
154 public ToscaProperty toAuthorative() {
155 var toscaProperty = new ToscaProperty();
157 toscaProperty.setName(key.getLocalName());
159 toscaProperty.setType(type.getName());
160 toscaProperty.setTypeVersion(type.getVersion());
162 toscaProperty.setDescription(description);
163 toscaProperty.setRequired(required);
164 toscaProperty.setStatus(status);
166 if (defaultValue != null) {
167 toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
170 toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
172 if (entrySchema != null) {
173 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
176 toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem));
178 return toscaProperty;
182 public void fromAuthorative(ToscaProperty toscaProperty) {
183 this.setKey(new PfReferenceKey());
184 getKey().setLocalName(toscaProperty.getName());
186 if (toscaProperty.getTypeVersion() != null) {
187 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
189 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
192 description = toscaProperty.getDescription();
193 required = toscaProperty.isRequired();
194 status = toscaProperty.getStatus();
196 if (toscaProperty.getDefaultValue() != null) {
197 defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
202 constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance);
204 if (toscaProperty.getEntrySchema() != null) {
205 entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema());
208 metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem);
212 public List<PfKey> getKeys() {
213 final List<PfKey> keyList = getKey().getKeys();
215 keyList.addAll(type.getKeys());
217 if (entrySchema != null) {
218 keyList.addAll(entrySchema.getKeys());
225 public void clean() {
230 if (description != null) {
231 description = description.trim();
234 if (defaultValue != null) {
235 defaultValue = defaultValue.trim();
238 if (entrySchema != null) {
242 metadata = PfUtils.mapMap(metadata, String::trim);
246 public int compareTo(final PfConcept otherConcept) {
247 if (otherConcept == null) {
250 if (this == otherConcept) {
253 if (getClass() != otherConcept.getClass()) {
254 return getClass().getName().compareTo(otherConcept.getClass().getName());
257 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
258 if (!key.equals(other.key)) {
259 return key.compareTo(other.key);
262 return compareFields(other);
266 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
268 * @param other the other ToscaProperty object
270 private int compareFields(final JpaToscaProperty other) {
271 if (!type.equals(other.type)) {
272 return type.compareTo(other.type);
275 int result = ObjectUtils.compare(description, other.description);
280 result = ObjectUtils.compare(required, other.required);
285 result = ObjectUtils.compare(defaultValue, other.defaultValue);
290 result = ObjectUtils.compare(status, other.status);
295 result = PfUtils.compareCollections(constraints, other.constraints);
300 result = entrySchema.compareTo(other.entrySchema);
305 return PfUtils.compareMaps(metadata, other.metadata);