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