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