Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaProperty.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * SPDX-License-Identifier: Apache-2.0
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.models.tosca.simple.concepts;
26
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;
34 import java.util.Map;
35 import lombok.Data;
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;
52
53 /**
54  * Class to represent the property in TOSCA definition.
55  *
56  * @author Chenfei Gao (cgao@research.att.com)
57  * @author Liam Fallon (liam.fallon@est.tech)
58  */
59 @Data
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
62     @Serial
63     private static final long serialVersionUID = 1675770231921107988L;
64
65     @EmbeddedId
66     @VerifyKey
67     @NotNull
68     private PfReferenceKey key;
69
70     @Column
71     @VerifyKey
72     @NotNull
73     private PfConceptKey type;
74
75     @Column
76     @NotBlank
77     private String description;
78
79     @Column
80     private boolean required = false;
81
82     @Column
83     @NotBlank
84     private String defaultValue;
85
86     @Column
87     private Status status = Status.SUPPORTED;
88
89     @ElementCollection
90     private List<@NotNull @Valid JpaToscaConstraint> constraints;
91
92     @Column
93     @Valid
94     private JpaToscaSchemaDefinition entrySchema;
95
96     @ElementCollection
97     private Map<String, String> metadata;
98
99     /**
100      * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
101      */
102     public JpaToscaProperty() {
103         this(new PfReferenceKey());
104     }
105
106     /**
107      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
108      *
109      * @param key the key
110      */
111     public JpaToscaProperty(@NonNull final PfReferenceKey key) {
112         this(key, new PfConceptKey());
113     }
114
115     /**
116      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
117      *
118      * @param key the key
119      * @param type the key of the property type
120      */
121     public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
122         this.key = key;
123         this.type = type;
124     }
125
126     /**
127      * Copy constructor.
128      *
129      * @param copyConcept the concept to copy from
130      */
131     public JpaToscaProperty(final JpaToscaProperty copyConcept) {
132         super(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);
141         this.entrySchema =
142                 (copyConcept.entrySchema != null ? new JpaToscaSchemaDefinition(copyConcept.entrySchema) : null);
143         this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
144     }
145
146     /**
147      * Authorative constructor.
148      *
149      * @param authorativeConcept the authorative concept to copy from
150      */
151     public JpaToscaProperty(final ToscaProperty authorativeConcept) {
152         this.fromAuthorative(authorativeConcept);
153     }
154
155     @Override
156     public ToscaProperty toAuthorative() {
157         var toscaProperty = new ToscaProperty();
158
159         toscaProperty.setName(key.getLocalName());
160
161         toscaProperty.setType(type.getName());
162         toscaProperty.setTypeVersion(type.getVersion());
163
164         toscaProperty.setDescription(description);
165         toscaProperty.setRequired(required);
166         toscaProperty.setStatus(status);
167
168         if (defaultValue != null) {
169             toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
170         }
171
172         toscaProperty.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
173
174         if (entrySchema != null) {
175             toscaProperty.setEntrySchema(entrySchema.toAuthorative());
176         }
177
178         toscaProperty.setMetadata(PfUtils.mapMap(metadata, metadataItem -> metadataItem));
179
180         return toscaProperty;
181     }
182
183     @Override
184     public void fromAuthorative(ToscaProperty toscaProperty) {
185         this.setKey(new PfReferenceKey());
186         getKey().setLocalName(toscaProperty.getName());
187
188         if (toscaProperty.getTypeVersion() != null) {
189             type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
190         } else {
191             type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
192         }
193
194         description = toscaProperty.getDescription();
195         required = toscaProperty.isRequired();
196         status = toscaProperty.getStatus();
197
198         if (toscaProperty.getDefaultValue() != null) {
199             defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
200         } else {
201             defaultValue = null;
202         }
203
204         constraints = PfUtils.mapList(toscaProperty.getConstraints(), JpaToscaConstraint::newInstance);
205
206         if (toscaProperty.getEntrySchema() != null) {
207             entrySchema = new JpaToscaSchemaDefinition(toscaProperty.getEntrySchema());
208         }
209
210         metadata = PfUtils.mapMap(toscaProperty.getMetadata(), metadataItem -> metadataItem);
211     }
212
213     @Override
214     public List<PfKey> getKeys() {
215         final List<PfKey> keyList = getKey().getKeys();
216
217         keyList.addAll(type.getKeys());
218
219         if (entrySchema != null) {
220             keyList.addAll(entrySchema.getKeys());
221         }
222
223         return keyList;
224     }
225
226     @Override
227     public void clean() {
228         key.clean();
229
230         type.clean();
231
232         if (description != null) {
233             description = description.trim();
234         }
235
236         if (defaultValue != null) {
237             defaultValue = defaultValue.trim();
238         }
239
240         if (entrySchema != null) {
241             entrySchema.clean();
242         }
243
244         metadata = PfUtils.mapMap(metadata, String::trim);
245     }
246
247     @Override
248     public int compareTo(@NonNull final PfConcept otherConcept) {
249         if (this == otherConcept) {
250             return 0;
251         }
252         if (getClass() != otherConcept.getClass()) {
253             return getClass().getName().compareTo(otherConcept.getClass().getName());
254         }
255
256         final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
257         if (!key.equals(other.key)) {
258             return key.compareTo(other.key);
259         }
260
261         return compareFields(other);
262     }
263
264     /**
265      * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
266      *
267      * @param other the other ToscaProperty object
268      */
269     private int compareFields(final JpaToscaProperty other) {
270         if (!type.equals(other.type)) {
271             return type.compareTo(other.type);
272         }
273
274         int result = ObjectUtils.compare(description, other.description);
275         if (result != 0) {
276             return result;
277         }
278
279         result = ObjectUtils.compare(required, other.required);
280         if (result != 0) {
281             return result;
282         }
283
284         result = ObjectUtils.compare(defaultValue, other.defaultValue);
285         if (result != 0) {
286             return result;
287         }
288
289         result = ObjectUtils.compare(status, other.status);
290         if (result != 0) {
291             return result;
292         }
293
294         result = PfUtils.compareCollections(constraints, other.constraints);
295         if (result != 0) {
296             return result;
297         }
298
299         result = entrySchema.compareTo(other.entrySchema);
300         if (result != 0) {
301             return result;
302         }
303
304         return PfUtils.compareMaps(metadata, other.metadata);
305     }
306 }