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