Changes for Checkstyle 8.32
[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.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.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
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     private PfReferenceKey key;
71
72     @Column
73     private PfConceptKey type;
74
75     @Column
76     private String description;
77
78     @Column
79     private boolean required = false;
80
81     @Column(name = "default")
82     private String defaultValue;
83
84     @Column
85     private Status status = Status.SUPPORTED;
86
87     @ElementCollection
88     private List<JpaToscaConstraint> constraints = new ArrayList<>();
89
90     @Column
91     private JpaToscaEntrySchema entrySchema;
92
93     @ElementCollection
94     private Map<String, String> metadata = new LinkedHashMap<>();
95
96     /**
97      * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
98      */
99     public JpaToscaProperty() {
100         this(new PfReferenceKey());
101     }
102
103     /**
104      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
105      *
106      * @param key the key
107      */
108     public JpaToscaProperty(@NonNull final PfReferenceKey key) {
109         this(key, new PfConceptKey());
110     }
111
112     /**
113      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
114      *
115      * @param key the key
116      * @param type the key of the property type
117      */
118     public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
119         this.key = key;
120         this.type = type;
121     }
122
123     /**
124      * Copy constructor.
125      *
126      * @param copyConcept the concept to copy from
127      */
128     public JpaToscaProperty(final JpaToscaProperty copyConcept) {
129         super(copyConcept);
130         this.key = new PfReferenceKey(copyConcept.key);
131         this.type = new PfConceptKey(copyConcept.type);
132         this.description = copyConcept.description;
133         this.required = copyConcept.required;
134         this.defaultValue = copyConcept.defaultValue;
135         this.status = copyConcept.status;
136         // Constraints are immutable
137         this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
138         this.entrySchema = (copyConcept.entrySchema != null ? new JpaToscaEntrySchema(copyConcept.entrySchema) : null);
139         this.metadata = (copyConcept.metadata != null ? new LinkedHashMap<>(copyConcept.metadata) : null);
140     }
141
142     /**
143      * Authorative constructor.
144      *
145      * @param authorativeConcept the authorative concept to copy from
146      */
147     public JpaToscaProperty(final ToscaProperty authorativeConcept) {
148         this.fromAuthorative(authorativeConcept);
149     }
150
151     @Override
152     public ToscaProperty toAuthorative() {
153         ToscaProperty toscaProperty = new ToscaProperty();
154
155         toscaProperty.setName(key.getLocalName());
156
157         toscaProperty.setType(type.getName());
158         toscaProperty.setTypeVersion(type.getVersion());
159
160         toscaProperty.setDescription(description);
161         toscaProperty.setRequired(required);
162         toscaProperty.setDefaultValue(defaultValue);
163         toscaProperty.setStatus(status);
164
165         if (constraints != null) {
166             List<ToscaConstraint> toscaConstraints = new ArrayList<>();
167
168             for (JpaToscaConstraint constraint : constraints) {
169                 toscaConstraints.add(constraint.toAuthorative());
170             }
171
172             toscaProperty.setConstraints(toscaConstraints);
173         }
174
175         if (entrySchema != null) {
176             toscaProperty.setEntrySchema(entrySchema.toAuthorative());
177         }
178
179         if (metadata != null) {
180             toscaProperty.setMetadata(new LinkedHashMap<>(metadata));
181         }
182
183         return toscaProperty;
184     }
185
186     @Override
187     public void fromAuthorative(ToscaProperty toscaProperty) {
188         this.setKey(new PfReferenceKey());
189         getKey().setLocalName(toscaProperty.getName());
190
191         if (toscaProperty.getTypeVersion() != null) {
192             type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion());
193         } else {
194             type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION);
195         }
196
197         description = toscaProperty.getDescription();
198         required = toscaProperty.isRequired();
199         defaultValue = toscaProperty.getDefaultValue();
200         status = toscaProperty.getStatus();
201
202         if (toscaProperty.getConstraints() != null) {
203             constraints = new ArrayList<>();
204
205             for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) {
206                 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
207             }
208         }
209
210         if (toscaProperty.getEntrySchema() != null) {
211             entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema());
212         }
213
214         // Add the property metadata if it doesn't exist already
215         if (toscaProperty.getMetadata() != null) {
216             metadata = new LinkedHashMap<>(toscaProperty.getMetadata());
217         }
218
219     }
220
221     @Override
222     public List<PfKey> getKeys() {
223         final List<PfKey> keyList = getKey().getKeys();
224
225         keyList.addAll(type.getKeys());
226
227         if (entrySchema != null) {
228             keyList.addAll(entrySchema.getKeys());
229         }
230
231         return keyList;
232     }
233
234     @Override
235     public void clean() {
236         key.clean();
237
238         type.clean();
239
240         if (description != null) {
241             description = description.trim();
242         }
243
244         if (defaultValue != null) {
245             defaultValue = defaultValue.trim();
246         }
247
248         if (entrySchema != null) {
249             entrySchema.clean();
250         }
251
252         if (metadata != null) {
253             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
254                 metadataEntry.setValue(metadataEntry.getValue().trim());
255             }
256         }
257     }
258
259     @Override
260     public PfValidationResult validate(final PfValidationResult resultIn) {
261         PfValidationResult result = resultIn;
262
263         if (key.isNullKey()) {
264             result.addValidationMessage(
265                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
266         }
267
268         result = key.validate(result);
269
270         if (type == null || type.isNullKey()) {
271             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
272                     "property type may not be null"));
273         }
274
275         return validateFields(result);
276     }
277
278     /**
279      * Validate the property fields.
280      *
281      * @param resultIn the incoming validation results so far
282      * @return the validation results including this validation
283      */
284     private PfValidationResult validateFields(final PfValidationResult resultIn) {
285         PfValidationResult result = resultIn;
286
287         if (description != null && description.trim().length() == 0) {
288             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
289                     "property description may not be blank"));
290         }
291
292         if (defaultValue != null && defaultValue.trim().length() == 0) {
293             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
294                     "property default value may not be null"));
295         }
296
297         if (constraints != null) {
298             for (JpaToscaConstraint constraint : constraints) {
299                 if (constraint == null) {
300                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
301                             "property constraint may not be null "));
302                 }
303             }
304         }
305         return (entrySchema != null ? entrySchema.validate(result) : result);
306     }
307
308     @Override
309     public int compareTo(final PfConcept otherConcept) {
310         if (otherConcept == null) {
311             return -1;
312         }
313         if (this == otherConcept) {
314             return 0;
315         }
316         if (getClass() != otherConcept.getClass()) {
317             return getClass().getName().compareTo(otherConcept.getClass().getName());
318         }
319
320         final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
321         if (!key.equals(other.key)) {
322             return key.compareTo(other.key);
323         }
324
325         return compareFields(other);
326     }
327
328     /**
329      * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object.
330      *
331      * @param other the other ToscaProperty object
332      */
333     private int compareFields(final JpaToscaProperty other) {
334         if (!type.equals(other.type)) {
335             return type.compareTo(other.type);
336         }
337
338         int result = ObjectUtils.compare(description, other.description);
339         if (result != 0) {
340             return result;
341         }
342
343         result = ObjectUtils.compare(required, other.required);
344         if (result != 0) {
345             return result;
346         }
347
348         result = ObjectUtils.compare(defaultValue, other.defaultValue);
349         if (result != 0) {
350             return result;
351         }
352
353         result = ObjectUtils.compare(status, other.status);
354         if (result != 0) {
355             return result;
356         }
357
358         result = PfUtils.compareObjects(constraints, other.constraints);
359         if (result != 0) {
360             return result;
361         }
362
363         return entrySchema.compareTo(other.entrySchema);
364     }
365 }