Address sonar issues in models
[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-2020 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             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 where to put the validation results
213      */
214     private void validateConstraints(@NonNull final PfValidationResult result) {
215         for (JpaToscaConstraint constraint : constraints) {
216             if (constraint == null) {
217                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
218                         "data type constraint may not be null "));
219             }
220         }
221     }
222
223     /**
224      * Validate the properties.
225      *
226      * @param result The result of validations up to now
227      * @return the validation result
228      */
229     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
230         PfValidationResult result = resultIn;
231
232         for (JpaToscaProperty property : properties.values()) {
233             if (property == null) {
234                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
235                         "data type property may not be null "));
236             } else {
237                 result = property.validate(result);
238             }
239         }
240         return result;
241     }
242
243     @Override
244     public int compareTo(final PfConcept otherConcept) {
245         if (otherConcept == null) {
246             return -1;
247         }
248         if (this == otherConcept) {
249             return 0;
250         }
251         if (getClass() != otherConcept.getClass()) {
252             return getClass().getName().compareTo(otherConcept.getClass().getName());
253         }
254
255         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
256         if (!super.equals(other)) {
257             return super.compareTo(other);
258         }
259
260         int result = PfUtils.compareObjects(constraints, other.constraints);
261         if (result != 0) {
262             return result;
263         }
264
265         result = PfUtils.compareObjects(properties, other.properties);
266         if (result != 0) {
267             return result;
268         }
269
270         return 0;
271     }
272
273     /**
274      * Get the data types referenced in a data type.
275      *
276      * @return the data types referenced in a data type
277      */
278     public Collection<PfConceptKey> getReferencedDataTypes() {
279         if (properties == null) {
280             return CollectionUtils.emptyCollection();
281         }
282
283         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
284
285         for (JpaToscaProperty property : properties.values()) {
286             referencedDataTypes.add(property.getType());
287
288             if (property.getEntrySchema() != null) {
289                 referencedDataTypes.add(property.getEntrySchema().getType());
290             }
291         }
292
293         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
294
295         return referencedDataTypes;
296     }
297 }