376c2b3b2a218f8ae1384f0fa34d95eda702cc0c
[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 com.google.gson.annotations.SerializedName;
27
28 import java.util.List;
29
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Table;
37
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41
42 import org.apache.commons.lang3.ObjectUtils;
43 import org.onap.policy.common.utils.validation.Assertions;
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
53 /**
54  * Class to represent the property in TOSCA definition.
55  *
56  * @author Chenfei Gao (cgao@research.att.com)
57  * @author Liam Fallon (liam.fallon@est.tech)
58  */
59 @Entity
60 @Table(name = "ToscaProperty")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @Data
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaToscaProperty extends PfConcept {
65     private static final long serialVersionUID = 1675770231921107988L;
66
67     public enum Status {
68         SUPPORTED, UNSUPPORTED, EXPERIMENTAL, DEPRECATED
69     }
70
71     @EmbeddedId
72     private PfReferenceKey key;
73
74     @Column
75     private PfConceptKey type;
76
77     @Column
78     private String description;
79
80     @Column
81     private boolean required = false;
82
83     @Column(name = "default")
84     @SerializedName("default")
85     private String defaultValue;
86
87     @Column
88     @NonNull
89     private Status status = Status.SUPPORTED;
90
91     @ElementCollection
92     private List<JpaToscaConstraint> constraints;
93
94     @Column
95     @SerializedName("entry_schema")
96     private JpaToscaEntrySchema entrySchema;
97
98     /**
99      * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
100      */
101     public JpaToscaProperty() {
102         this(new PfReferenceKey());
103     }
104
105     /**
106      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
107      *
108      * @param key the key
109      */
110     public JpaToscaProperty(@NonNull final PfReferenceKey key) {
111         this(key, new PfConceptKey());
112     }
113
114     /**
115      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
116      *
117      * @param key the key
118      * @param type the key of the property type
119      */
120     public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
121         this.key = key;
122         this.type = type;
123     }
124
125     /**
126      * Copy constructor.
127      *
128      * @param copyConcept the concept to copy from
129      */
130     public JpaToscaProperty(final JpaToscaProperty copyConcept) {
131         super(copyConcept);
132     }
133
134     @Override
135     public List<PfKey> getKeys() {
136         final List<PfKey> keyList = getKey().getKeys();
137
138         keyList.addAll(type.getKeys());
139
140         if (constraints != null) {
141             for (JpaToscaConstraint constraint : constraints) {
142                 keyList.addAll(constraint.getKeys());
143             }
144         }
145
146         if (entrySchema != null) {
147             keyList.addAll(entrySchema.getKeys());
148         }
149
150         return keyList;
151     }
152
153     @Override
154     public void clean() {
155         key.clean();
156
157         type.clean();
158
159         if (description != null) {
160             description = description.trim();
161         }
162
163         if (defaultValue != null) {
164             defaultValue = defaultValue.trim();
165         }
166
167         if (constraints != null) {
168             for (JpaToscaConstraint constraint : constraints) {
169                 constraint.clean();
170             }
171         }
172
173         if (entrySchema != null) {
174             entrySchema.clean();
175         }
176     }
177
178     @Override
179     public PfValidationResult validate(final PfValidationResult resultIn) {
180         PfValidationResult result = resultIn;
181
182         if (key.isNullKey()) {
183             result.addValidationMessage(
184                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
185         }
186
187         result = key.validate(result);
188
189         if (type == null || type.isNullKey()) {
190             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
191                     "property type may not be null"));
192         }
193
194         return validateFields(result);
195     }
196
197     /**
198      * Validate the property fields.
199      *
200      * @param resultIn the incoming validation results so far
201      * @return the validation results including this validation
202      */
203     private PfValidationResult validateFields(final PfValidationResult resultIn) {
204         PfValidationResult result = resultIn;
205
206         if (description != null && description.trim().length() == 0) {
207             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
208                     "property description may not be blank"));
209         }
210
211         if (defaultValue != null && defaultValue.trim().length() == 0) {
212             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
213                     "property default value may not be null"));
214         }
215
216         if (constraints != null) {
217             for (JpaToscaConstraint constraint : constraints) {
218                 if (constraint == null) {
219                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220                             "property constraint may not be null "));
221                 } else {
222                     result = constraint.validate(result);
223                 }
224             }
225         }
226         return (entrySchema != null ? entrySchema.validate(result) : result);
227     }
228
229     @Override
230     public int compareTo(final PfConcept otherConcept) {
231         if (otherConcept == null) {
232             return -1;
233         }
234         if (this == otherConcept) {
235             return 0;
236         }
237         if (getClass() != otherConcept.getClass()) {
238             return this.hashCode() - otherConcept.hashCode();
239         }
240
241         final JpaToscaProperty other = (JpaToscaProperty) otherConcept;
242         if (!key.equals(other.key)) {
243             return key.compareTo(other.key);
244         }
245
246         return compareFields(other);
247     }
248
249     /**
250      * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty
251      * object.
252      *
253      * @param other the other ToscaProperty object
254      */
255     private int compareFields(final JpaToscaProperty other) {
256         if (!type.equals(other.type)) {
257             return type.compareTo(other.type);
258         }
259
260         int result = ObjectUtils.compare(description, other.description);
261         if (result != 0) {
262             return result;
263         }
264
265         result = ObjectUtils.compare(required, other.required);
266         if (result != 0) {
267             return result;
268         }
269
270         result = ObjectUtils.compare(defaultValue, other.defaultValue);
271         if (result != 0) {
272             return result;
273         }
274
275         result = ObjectUtils.compare(status, other.status);
276         if (result != 0) {
277             return result;
278         }
279
280         result = PfUtils.compareObjects(constraints, other.constraints);
281         if (result != 0) {
282             return result;
283         }
284
285         return entrySchema.compareTo(other.entrySchema);
286     }
287
288     @Override
289     public PfConcept copyTo(@NonNull final PfConcept target) {
290         Assertions.instanceOf(target, JpaToscaProperty.class);
291
292         final JpaToscaProperty copy = ((JpaToscaProperty) target);
293         copy.setKey(new PfReferenceKey(key));
294         copy.setType(new PfConceptKey(type));
295         copy.setDescription(description);
296         copy.setRequired(required);
297         copy.setDefaultValue(defaultValue);
298         copy.setStatus(status);
299         copy.constraints = constraints; // Constraints are immutable
300         copy.setEntrySchema(entrySchema != null ? new JpaToscaEntrySchema(entrySchema) : null);
301
302         return copy;
303     }
304 }