Use annotations on parameterized types
[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-2021 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.NotNull;
45 import org.onap.policy.common.parameters.annotations.Valid;
46 import org.onap.policy.models.base.PfAuthorative;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfReferenceKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
54 import org.onap.policy.models.tosca.utils.ToscaUtils;
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<@NotNull @Valid JpaToscaConstraint> constraints;
72
73     @ElementCollection
74     @Lob
75     private Map<@NotNull String, @NotNull @Valid 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         toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
121         toscaDataType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
122
123         return toscaDataType;
124     }
125
126     @Override
127     public void fromAuthorative(final ToscaDataType toscaDataType) {
128         super.fromAuthorative(toscaDataType);
129
130         constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance);
131
132         if (toscaDataType.getProperties() != null) {
133             properties = new LinkedHashMap<>();
134             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
135                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
136                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
137                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
138             }
139         }
140     }
141
142     @Override
143     public List<PfKey> getKeys() {
144         final List<PfKey> keyList = super.getKeys();
145
146         if (properties != null) {
147             for (JpaToscaProperty property : properties.values()) {
148                 keyList.addAll(property.getKeys());
149             }
150         }
151
152         return keyList;
153     }
154
155     @Override
156     public void clean() {
157         super.clean();
158
159         if (properties != null) {
160             for (JpaToscaProperty property : properties.values()) {
161                 property.clean();
162             }
163         }
164     }
165
166     @Override
167     public int compareTo(final PfConcept otherConcept) {
168         if (otherConcept == null) {
169             return -1;
170         }
171         if (this == otherConcept) {
172             return 0;
173         }
174         if (getClass() != otherConcept.getClass()) {
175             return getClass().getName().compareTo(otherConcept.getClass().getName());
176         }
177
178         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
179         int result = super.compareTo(other);
180         if (result != 0) {
181             return result;
182         }
183
184         result = PfUtils.compareCollections(constraints, other.constraints);
185         if (result != 0) {
186             return result;
187         }
188
189         return PfUtils.compareMaps(properties, other.properties);
190     }
191
192     /**
193      * Get the data types referenced in a data type.
194      *
195      * @return the data types referenced in a data type
196      */
197     public Collection<PfConceptKey> getReferencedDataTypes() {
198         if (properties == null) {
199             return CollectionUtils.emptyCollection();
200         }
201
202         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
203
204         for (JpaToscaProperty property : properties.values()) {
205             referencedDataTypes.add(property.getType());
206
207             if (property.getEntrySchema() != null) {
208                 referencedDataTypes.add(property.getEntrySchema().getType());
209             }
210         }
211
212         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
213
214         return referencedDataTypes;
215     }
216 }