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 * ================================================================================
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.parameters.annotations.NotBlank;
42 import org.onap.policy.common.parameters.annotations.NotNull;
43 import org.onap.policy.common.parameters.annotations.Valid;
44 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
45 import org.onap.policy.models.base.PfAuthorative;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfConceptKey;
48 import org.onap.policy.models.base.PfKey;
49 import org.onap.policy.models.base.PfReferenceKey;
50 import org.onap.policy.models.base.PfUtils;
51 import org.onap.policy.models.base.validation.annotations.VerifyKey;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
56 * Class to represent the property in TOSCA definition.
58 * @author Chenfei Gao (cgao@research.att.com)
59 * @author Liam Fallon (liam.fallon@est.tech)
62 @Table(name = "ToscaProperty")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
67 private static final long serialVersionUID = 1675770231921107988L;
72 private PfReferenceKey key;
77 private PfConceptKey type;
81 private String description;
84 private boolean required = false;
88 private String defaultValue;
91 private Status status = Status.SUPPORTED;
94 private List<@NotNull @Valid JpaToscaConstraint> constraints;
98 private JpaToscaSchemaDefinition entrySchema;
101 private Map<String, String> metadata;
104 * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
106 public JpaToscaProperty() {
107 this(new PfReferenceKey());
111 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
115 public JpaToscaProperty(@NonNull final PfReferenceKey key) {
116 this(key, new PfConceptKey());
120 * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
123 * @param type the key of the property type
125 public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
133 * @param copyConcept the concept to copy from
135 public JpaToscaProperty(final JpaToscaProperty copyConcept) {
137 this.key = new PfReferenceKey(copyConcept.key);
138 this.type = new PfConceptKey(copyConcept.type);
139 this.description = copyConcept.description;
140 this.required = copyConcept.required;
141 this.defaultValue = copyConcept.defaultValue;
142 this.status = copyConcept.status;
143 // Constraints are immutable
144 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
146 (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(copyConcept.entrySchema) : null);
147 this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
151 * Authorative constructor.
153 * @param authorativeConcept the authorative concept to copy from
155 public JpaToscaProperty(final ToscaProperty authorativeConcept) {
156 this.fromAuthorative(authorativeConcept);
160 public ToscaProperty toAuthorative() {
161 var toscaProperty = new ToscaProperty();
163 toscaProperty.setName(key.getLocalName());
165 toscaProperty.setType(type.getName());
166 toscaProperty.setTypeVersion(type.getVersion());
168 toscaProperty.setDescription(description);
169 toscaProperty.setRequired(required);
170 toscaProperty.setStatus(status);
172 if (defaultValue != null) {
173 toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
176 toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
178 if (entrySchema != null) {
179 toscaProperty.setEntrySchema(entrySchema.toAuthorative());
182 toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem));
184 return toscaProperty;
188 public void fromAuthorative(ToscaProperty toscaProperty) {
189 this.setKey(new PfReferenceKey());
190 getKey().setLocalName(toscaProperty.getName());
192 if (toscaProperty.getTypeVersion() != null) {
193 type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
195 type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
198 description = toscaProperty.getDescription();
199 required = toscaProperty.isRequired();
200 status = toscaProperty.getStatus();
202 if (toscaProperty.getDefaultValue() != null) {
203 defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
208 constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance);
210 if (toscaProperty.getEntrySchema() != null) {
211 entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema());
214 metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem);
218 public List<PfKey> getKeys() {
219 final List<PfKey> keyList = getKey().getKeys();
221 keyList.addAll(type.getKeys());
223 if (entrySchema != null) {
224 keyList.addAll(entrySchema.getKeys());
231 public void clean() {
236 if (description != null) {
237 description = description.trim();
240 if (defaultValue != null) {
241 defaultValue = defaultValue.trim();
244 if (entrySchema != null) {
248 metadata = PfUtils.mapMap(metadata, String::trim);
252 public int compareTo(final PfConcept otherConcept) {
253 if (otherConcept == null) {
256 if (this == otherConcept) {
259 if (getClass() != otherConcept.getClass()) {
260 return getClass().getName().compareTo(otherConcept.getClass().getName());
263 final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
264 if (!key.equals(other.key)) {
265 return key.compareTo(other.key);
268 return compareFields(other);
272 * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
274 * @param other the other ToscaProperty object
276 private int compareFields(final JpaToscaProperty other) {
277 if (!type.equals(other.type)) {
278 return type.compareTo(other.type);
281 int result = ObjectUtils.compare(description, other.description);
286 result = ObjectUtils.compare(required, other.required);
291 result = ObjectUtils.compare(defaultValue, other.defaultValue);
296 result = ObjectUtils.compare(status, other.status);
301 result = PfUtils.compareCollections(constraints, other.constraints);
306 result = entrySchema.compareTo(other.entrySchema);
311 return PfUtils.compareMaps(metadata, other.metadata);