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