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