Remove unused imports
[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 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 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 java.util.Map.Entry;
31 import javax.persistence.Column;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.Entity;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.Table;
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
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.PfValidationMessage;
50 import org.onap.policy.models.base.PfValidationResult;
51 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status;
55
56 /**
57  * Class to represent the property in TOSCA definition.
58  *
59  * @author Chenfei Gao (cgao@research.att.com)
60  * @author Liam Fallon (liam.fallon@est.tech)
61  */
62 @Entity
63 @Table(name = "ToscaProperty")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @Data
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
68     private static final long serialVersionUID = 1675770231921107988L;
69
70     @EmbeddedId
71     private PfReferenceKey key;
72
73     @Column
74     private PfConceptKey type;
75
76     @Column
77     private String description;
78
79     @Column
80     private boolean required = false;
81
82     @Column(name = "default")
83     private String defaultValue;
84
85     @Column
86     private Status status = Status.SUPPORTED;
87
88     @ElementCollection
89     private List<JpaToscaConstraint> constraints = new ArrayList<>();
90
91     @Column
92     private JpaToscaEntrySchema entrySchema;
93
94     @ElementCollection
95     private Map<String, String> metadata = new LinkedHashMap<>();
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 = (copyConcept.entrySchema != null ? new JpaToscaEntrySchema(copyConcept.entrySchema) : null);
140         this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
141     }
142
143     /**
144      * Authorative constructor.
145      *
146      * @param authorativeConcept the authorative concept to copy from
147      */
148     public JpaToscaProperty(final ToscaProperty authorativeConcept) {
149         this.fromAuthorative(authorativeConcept);
150     }
151
152     @Override
153     public ToscaProperty toAuthorative() {
154         ToscaProperty toscaProperty = new ToscaProperty();
155
156         toscaProperty.setName(key.getLocalName());
157
158         toscaProperty.setType(type.getName());
159         toscaProperty.setTypeVersion(type.getVersion());
160
161         toscaProperty.setDescription(description);
162         toscaProperty.setRequired(required);
163         toscaProperty.setStatus(status);
164
165         if (defaultValue != null) {
166             toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
167         }
168
169         if (constraints != null) {
170             List<ToscaConstraint> toscaConstraints = new ArrayList<>();
171
172             for (JpaToscaConstraint constraint : constraints) {
173                 toscaConstraints.add(constraint.toAuthorative());
174             }
175
176             toscaProperty.setConstraints(toscaConstraints);
177         }
178
179         if (entrySchema != null) {
180             toscaProperty.setEntrySchema(entrySchema.toAuthorative());
181         }
182
183         if (metadata != null) {
184             toscaProperty.setMetadata(new LinkedHashMap<>(metadata));
185         }
186
187         return toscaProperty;
188     }
189
190     @Override
191     public void fromAuthorative(ToscaProperty toscaProperty) {
192         this.setKey(new PfReferenceKey());
193         getKey().setLocalName(toscaProperty.getName());
194
195         if (toscaProperty.getTypeVersion() != null) {
196             type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
197         } else {
198             type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
199         }
200
201         description = toscaProperty.getDescription();
202         required = toscaProperty.isRequired();
203         status = toscaProperty.getStatus();
204
205         if (toscaProperty.getDefaultValue() != null) {
206             defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
207         } else {
208             defaultValue = null;
209         }
210
211         if (toscaProperty.getConstraints() != null) {
212             constraints = new ArrayList<>();
213
214             for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
215                 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
216             }
217         }
218
219         if (toscaProperty.getEntrySchema() != null) {
220             entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
221         }
222
223         // Add the property metadata if it doesn't exist already
224         if (toscaProperty.getMetadata() != null) {
225             metadata = new LinkedHashMap<>(toscaProperty.getMetadata());
226         }
227
228     }
229
230     @Override
231     public List<PfKey> getKeys() {
232         final List<PfKey> keyList = getKey().getKeys();
233
234         keyList.addAll(type.getKeys());
235
236         if (entrySchema != null) {
237             keyList.addAll(entrySchema.getKeys());
238         }
239
240         return keyList;
241     }
242
243     @Override
244     public void clean() {
245         key.clean();
246
247         type.clean();
248
249         if (description != null) {
250             description = description.trim();
251         }
252
253         if (defaultValue != null) {
254             defaultValue = defaultValue.trim();
255         }
256
257         if (entrySchema != null) {
258             entrySchema.clean();
259         }
260
261         if (metadata != null) {
262             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
263                 metadataEntry.setValue(metadataEntry.getValue().trim());
264             }
265         }
266     }
267
268     @Override
269     public PfValidationResult validate(final PfValidationResult resultIn) {
270         PfValidationResult result = resultIn;
271
272         if (key.isNullKey()) {
273             result.addValidationMessage(
274                 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
275         }
276
277         result = key.validate(result);
278
279         if (type == null || type.isNullKey()) {
280             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
281                 "property type may not be null"));
282         }
283
284         return validateFields(result);
285     }
286
287     /**
288      * Validate the property fields.
289      *
290      * @param resultIn the incoming validation results so far
291      * @return the validation results including this validation
292      */
293     private PfValidationResult validateFields(final PfValidationResult resultIn) {
294         PfValidationResult result = resultIn;
295
296         if (description != null && description.trim().length() == 0) {
297             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
298                 "property description may not be blank"));
299         }
300
301         if (defaultValue != null && defaultValue.trim().length() == 0) {
302             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
303                 "property default value may not be null"));
304         }
305
306         if (constraints != null) {
307             for (JpaToscaConstraint constraint : constraints) {
308                 if (constraint == null) {
309                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
310                         "property constraint may not be null "));
311                 }
312             }
313         }
314         return (entrySchema != null ? entrySchema.validate(result) : result);
315     }
316
317     @Override
318     public int compareTo(final PfConcept otherConcept) {
319         if (otherConcept == null) {
320             return -1;
321         }
322         if (this == otherConcept) {
323             return 0;
324         }
325         if (getClass() != otherConcept.getClass()) {
326             return getClass().getName().compareTo(otherConcept.getClass().getName());
327         }
328
329         final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
330         if (!key.equals(other.key)) {
331             return key.compareTo(other.key);
332         }
333
334         return compareFields(other);
335     }
336
337     /**
338      * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
339      *
340      * @param other the other ToscaProperty object
341      */
342     private int compareFields(final JpaToscaProperty other) {
343         if (!type.equals(other.type)) {
344             return type.compareTo(other.type);
345         }
346
347         int result = ObjectUtils.compare(description, other.description);
348         if (result != 0) {
349             return result;
350         }
351
352         result = ObjectUtils.compare(required, other.required);
353         if (result != 0) {
354             return result;
355         }
356
357         result = ObjectUtils.compare(defaultValue, other.defaultValue);
358         if (result != 0) {
359             return result;
360         }
361
362         result = ObjectUtils.compare(status, other.status);
363         if (result != 0) {
364             return result;
365         }
366
367         result = PfUtils.compareObjects(constraints, other.constraints);
368         if (result != 0) {
369             return result;
370         }
371
372         return entrySchema.compareTo(other.entrySchema);
373     }
374 }