86d67e4d8d14c1a5725a6eecc44fcb89b80ea988
[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.Lob;
37 import javax.persistence.Table;
38
39 import lombok.Data;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42
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     @Lob
75     private Map<String, JpaToscaProperty> properties;
76
77     /**
78      * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
79      */
80     public JpaToscaDataType() {
81         this(new PfConceptKey());
82     }
83
84     /**
85      * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
86      *
87      * @param key the key
88      */
89     public JpaToscaDataType(@NonNull final PfConceptKey key) {
90         super(key);
91     }
92
93     /**
94      * Copy constructor.
95      *
96      * @param copyConcept the concept to copy from
97      */
98     public JpaToscaDataType(final JpaToscaDataType copyConcept) {
99         super(copyConcept);
100         // Constraints are immutable
101         this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
102         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
103     }
104
105     /**
106      * Authorative constructor.
107      *
108      * @param authorativeConcept the authorative concept to copy from
109      */
110     public JpaToscaDataType(final ToscaDataType authorativeConcept) {
111         this.fromAuthorative(authorativeConcept);
112     }
113
114     @Override
115     public ToscaDataType toAuthorative() {
116         ToscaDataType toscaDataType = new ToscaDataType();
117         super.setToscaEntity(toscaDataType);
118         super.toAuthorative();
119
120         if (constraints != null) {
121             List<ToscaConstraint> toscaConstraints = new ArrayList<>();
122
123             for (JpaToscaConstraint constraint : constraints) {
124                 toscaConstraints.add(constraint.toAuthorative());
125             }
126
127             toscaDataType.setConstraints(toscaConstraints);
128         }
129
130         if (properties != null) {
131             Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
132
133             for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
134                 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
135             }
136
137             toscaDataType.setProperties(propertyMap);
138         }
139
140         return toscaDataType;
141     }
142
143     @Override
144     public void fromAuthorative(final ToscaDataType toscaDataType) {
145         super.fromAuthorative(toscaDataType);
146
147         if (toscaDataType.getConstraints() != null) {
148             constraints = new ArrayList<>();
149
150             for (ToscaConstraint toscaConstraint : toscaDataType.getConstraints()) {
151                 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
152             }
153         }
154
155         if (toscaDataType.getProperties() != null) {
156             properties = new LinkedHashMap<>();
157             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
158                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
159                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
160                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
161             }
162         }
163     }
164
165     @Override
166     public List<PfKey> getKeys() {
167         final List<PfKey> keyList = super.getKeys();
168
169         if (properties != null) {
170             for (JpaToscaProperty property : properties.values()) {
171                 keyList.addAll(property.getKeys());
172             }
173         }
174
175         return keyList;
176     }
177
178     @Override
179     public void clean() {
180         super.clean();
181
182         if (properties != null) {
183             for (JpaToscaProperty property : properties.values()) {
184                 property.clean();
185             }
186         }
187     }
188
189     @Override
190     public PfValidationResult validate(final PfValidationResult resultIn) {
191         PfValidationResult result = super.validate(resultIn);
192
193         if (constraints != null) {
194             result = validateConstraints(result);
195         }
196
197         if (properties != null) {
198             result = validateProperties(result);
199         }
200
201         return result;
202     }
203
204     /**
205      * Validate the constraints.
206      *
207      * @param result The result of validations up to now
208      * @return the validation result
209      */
210     private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
211         PfValidationResult result = resultIn;
212
213         for (JpaToscaConstraint constraint : constraints) {
214             if (constraint == null) {
215                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
216                         "data type constraint may not be null "));
217             }
218         }
219         return result;
220     }
221
222     /**
223      * Validate the properties.
224      *
225      * @param result The result of validations up to now
226      * @return the validation result
227      */
228     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
229         PfValidationResult result = resultIn;
230
231         for (JpaToscaProperty property : properties.values()) {
232             if (property == null) {
233                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
234                         "data type property may not be null "));
235             } else {
236                 result = property.validate(result);
237             }
238         }
239         return result;
240     }
241
242     @Override
243     public int compareTo(final PfConcept otherConcept) {
244         if (otherConcept == null) {
245             return -1;
246         }
247         if (this == otherConcept) {
248             return 0;
249         }
250         if (getClass() != otherConcept.getClass()) {
251             return getClass().getName().compareTo(otherConcept.getClass().getName());
252         }
253
254         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
255         if (!super.equals(other)) {
256             return super.compareTo(other);
257         }
258
259         int result = PfUtils.compareObjects(constraints, other.constraints);
260         if (result != 0) {
261             return result;
262         }
263
264         result = PfUtils.compareObjects(properties, other.properties);
265         if (result != 0) {
266             return result;
267         }
268
269         return 0;
270     }
271 }