Use annotations to do validation
[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.common.parameters.annotations.Entries;
45 import org.onap.policy.common.parameters.annotations.Items;
46 import org.onap.policy.common.parameters.annotations.NotNull;
47 import org.onap.policy.common.parameters.annotations.Valid;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
56 import org.onap.policy.models.tosca.utils.ToscaUtils;
57
58 /**
59  * Class to represent custom data type in TOSCA definition.
60  *
61  * @author Chenfei Gao (cgao@research.att.com)
62  * @author Liam Fallon (liam.fallon@est.tech)
63  */
64 @Entity
65 @Table(name = "ToscaDataType")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @Data
68 @EqualsAndHashCode(callSuper = true)
69 public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> {
70     private static final long serialVersionUID = -3922690413436539164L;
71
72     @ElementCollection
73     @Items(notNull = {@NotNull}, valid = {@Valid})
74     private List<JpaToscaConstraint> constraints;
75
76     @ElementCollection
77     @Lob
78     @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
79     private Map<String, JpaToscaProperty> properties;
80
81     /**
82      * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
83      */
84     public JpaToscaDataType() {
85         this(new PfConceptKey());
86     }
87
88     /**
89      * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
90      *
91      * @param key the key
92      */
93     public JpaToscaDataType(@NonNull final PfConceptKey key) {
94         super(key);
95     }
96
97     /**
98      * Copy constructor.
99      *
100      * @param copyConcept the concept to copy from
101      */
102     public JpaToscaDataType(final JpaToscaDataType copyConcept) {
103         super(copyConcept);
104         // Constraints are immutable
105         this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
106         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
107     }
108
109     /**
110      * Authorative constructor.
111      *
112      * @param authorativeConcept the authorative concept to copy from
113      */
114     public JpaToscaDataType(final ToscaDataType authorativeConcept) {
115         this.fromAuthorative(authorativeConcept);
116     }
117
118     @Override
119     public ToscaDataType toAuthorative() {
120         ToscaDataType toscaDataType = new ToscaDataType();
121         super.setToscaEntity(toscaDataType);
122         super.toAuthorative();
123
124         toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
125         toscaDataType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
126
127         return toscaDataType;
128     }
129
130     @Override
131     public void fromAuthorative(final ToscaDataType toscaDataType) {
132         super.fromAuthorative(toscaDataType);
133
134         constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance);
135
136         if (toscaDataType.getProperties() != null) {
137             properties = new LinkedHashMap<>();
138             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
139                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
140                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
141                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
142             }
143         }
144     }
145
146     @Override
147     public List<PfKey> getKeys() {
148         final List<PfKey> keyList = super.getKeys();
149
150         if (properties != null) {
151             for (JpaToscaProperty property : properties.values()) {
152                 keyList.addAll(property.getKeys());
153             }
154         }
155
156         return keyList;
157     }
158
159     @Override
160     public void clean() {
161         super.clean();
162
163         if (properties != null) {
164             for (JpaToscaProperty property : properties.values()) {
165                 property.clean();
166             }
167         }
168     }
169
170     @Override
171     public int compareTo(final PfConcept otherConcept) {
172         if (otherConcept == null) {
173             return -1;
174         }
175         if (this == otherConcept) {
176             return 0;
177         }
178         if (getClass() != otherConcept.getClass()) {
179             return getClass().getName().compareTo(otherConcept.getClass().getName());
180         }
181
182         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
183         int result = super.compareTo(other);
184         if (result != 0) {
185             return result;
186         }
187
188         result = PfUtils.compareCollections(constraints, other.constraints);
189         if (result != 0) {
190             return result;
191         }
192
193         return PfUtils.compareMaps(properties, other.properties);
194     }
195
196     /**
197      * Get the data types referenced in a data type.
198      *
199      * @return the data types referenced in a data type
200      */
201     public Collection<PfConceptKey> getReferencedDataTypes() {
202         if (properties == null) {
203             return CollectionUtils.emptyCollection();
204         }
205
206         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
207
208         for (JpaToscaProperty property : properties.values()) {
209             referencedDataTypes.add(property.getType());
210
211             if (property.getEntrySchema() != null) {
212                 referencedDataTypes.add(property.getEntrySchema().getType());
213             }
214         }
215
216         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
217
218         return referencedDataTypes;
219     }
220 }