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