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