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