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