Implement validation and hierarchical get
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataType.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.Collection;
28 import java.util.LinkedHashMap;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34
35 import javax.persistence.ElementCollection;
36 import javax.persistence.Entity;
37 import javax.persistence.Inheritance;
38 import javax.persistence.InheritanceType;
39 import javax.persistence.Lob;
40 import javax.persistence.Table;
41
42 import lombok.Data;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
45
46 import org.apache.commons.collections4.CollectionUtils;
47 import org.onap.policy.models.base.PfAuthorative;
48 import org.onap.policy.models.base.PfConcept;
49 import org.onap.policy.models.base.PfConceptKey;
50 import org.onap.policy.models.base.PfKey;
51 import org.onap.policy.models.base.PfReferenceKey;
52 import org.onap.policy.models.base.PfUtils;
53 import org.onap.policy.models.base.PfValidationMessage;
54 import org.onap.policy.models.base.PfValidationResult;
55 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
59 import org.onap.policy.models.tosca.utils.ToscaUtils;
60
61 /**
62  * Class to represent custom data type in TOSCA definition.
63  *
64  * @author Chenfei Gao (cgao@research.att.com)
65  * @author Liam Fallon (liam.fallon@est.tech)
66  */
67 @Entity
68 @Table(name = "ToscaDataType")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @Data
71 @EqualsAndHashCode(callSuper = true)
72 public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> {
73     private static final long serialVersionUID = -3922690413436539164L;
74
75     @ElementCollection
76     private List<JpaToscaConstraint> constraints = new ArrayList<>();
77
78     @ElementCollection
79     @Lob
80     private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
81
82     /**
83      * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
84      */
85     public JpaToscaDataType() {
86         this(new PfConceptKey());
87     }
88
89     /**
90      * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
91      *
92      * @param key the key
93      */
94     public JpaToscaDataType(@NonNull final PfConceptKey key) {
95         super(key);
96     }
97
98     /**
99      * Copy constructor.
100      *
101      * @param copyConcept the concept to copy from
102      */
103     public JpaToscaDataType(final JpaToscaDataType copyConcept) {
104         super(copyConcept);
105         // Constraints are immutable
106         this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
107         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
108     }
109
110     /**
111      * Authorative constructor.
112      *
113      * @param authorativeConcept the authorative concept to copy from
114      */
115     public JpaToscaDataType(final ToscaDataType authorativeConcept) {
116         this.fromAuthorative(authorativeConcept);
117     }
118
119     @Override
120     public ToscaDataType toAuthorative() {
121         ToscaDataType toscaDataType = new ToscaDataType();
122         super.setToscaEntity(toscaDataType);
123         super.toAuthorative();
124
125         if (constraints != null) {
126             List<ToscaConstraint> toscaConstraints = new ArrayList<>();
127
128             for (JpaToscaConstraint constraint : constraints) {
129                 toscaConstraints.add(constraint.toAuthorative());
130             }
131
132             toscaDataType.setConstraints(toscaConstraints);
133         }
134
135         if (properties != null) {
136             Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
137
138             for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
139                 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
140             }
141
142             toscaDataType.setProperties(propertyMap);
143         }
144
145         return toscaDataType;
146     }
147
148     @Override
149     public void fromAuthorative(final ToscaDataType toscaDataType) {
150         super.fromAuthorative(toscaDataType);
151
152         if (toscaDataType.getConstraints() != null) {
153             constraints = new ArrayList<>();
154
155             for (ToscaConstraint toscaConstraint : toscaDataType.getConstraints()) {
156                 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
157             }
158         }
159
160         if (toscaDataType.getProperties() != null) {
161             properties = new LinkedHashMap<>();
162             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
163                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
164                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
165                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
166             }
167         }
168     }
169
170     @Override
171     public List<PfKey> getKeys() {
172         final List<PfKey> keyList = super.getKeys();
173
174         if (properties != null) {
175             for (JpaToscaProperty property : properties.values()) {
176                 keyList.addAll(property.getKeys());
177             }
178         }
179
180         return keyList;
181     }
182
183     @Override
184     public void clean() {
185         super.clean();
186
187         if (properties != null) {
188             for (JpaToscaProperty property : properties.values()) {
189                 property.clean();
190             }
191         }
192     }
193
194     @Override
195     public PfValidationResult validate(final PfValidationResult resultIn) {
196         PfValidationResult result = super.validate(resultIn);
197
198         if (constraints != null) {
199             result = validateConstraints(result);
200         }
201
202         if (properties != null) {
203             result = validateProperties(result);
204         }
205
206         return result;
207     }
208
209     /**
210      * Validate the constraints.
211      *
212      * @param result The result of validations up to now
213      * @return the validation result
214      */
215     private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
216         PfValidationResult result = resultIn;
217
218         for (JpaToscaConstraint constraint : constraints) {
219             if (constraint == null) {
220                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
221                         "data type constraint may not be null "));
222             }
223         }
224         return result;
225     }
226
227     /**
228      * Validate the properties.
229      *
230      * @param result The result of validations up to now
231      * @return the validation result
232      */
233     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
234         PfValidationResult result = resultIn;
235
236         for (JpaToscaProperty property : properties.values()) {
237             if (property == null) {
238                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
239                         "data type property may not be null "));
240             } else {
241                 result = property.validate(result);
242             }
243         }
244         return result;
245     }
246
247     @Override
248     public int compareTo(final PfConcept otherConcept) {
249         if (otherConcept == null) {
250             return -1;
251         }
252         if (this == otherConcept) {
253             return 0;
254         }
255         if (getClass() != otherConcept.getClass()) {
256             return getClass().getName().compareTo(otherConcept.getClass().getName());
257         }
258
259         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
260         if (!super.equals(other)) {
261             return super.compareTo(other);
262         }
263
264         int result = PfUtils.compareObjects(constraints, other.constraints);
265         if (result != 0) {
266             return result;
267         }
268
269         result = PfUtils.compareObjects(properties, other.properties);
270         if (result != 0) {
271             return result;
272         }
273
274         return 0;
275     }
276
277     /**
278      * Get the data types referenced in a data type.
279      *
280      * @return the data types referenced in a data type
281      */
282     public Collection<PfConceptKey> getReferencedDataTypes() {
283         if (properties == null) {
284             return CollectionUtils.emptyCollection();
285         }
286
287         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
288
289         for (JpaToscaProperty property : properties.values()) {
290             referencedDataTypes.add(property.getType());
291
292             if (property.getEntrySchema() != null) {
293                 referencedDataTypes.add(property.getEntrySchema().getType());
294             }
295         }
296
297         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
298
299         return referencedDataTypes;
300     }
301 }