Use annotations on parameterized types
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaNodeType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Set;
31 import javax.persistence.CascadeType;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.JoinColumns;
39 import javax.persistence.Lob;
40 import javax.persistence.OneToOne;
41 import javax.persistence.Table;
42 import lombok.Data;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
45 import org.apache.commons.collections4.CollectionUtils;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.common.parameters.BeanValidationResult;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Valid;
50 import org.onap.policy.models.base.PfAuthorative;
51 import org.onap.policy.models.base.PfConcept;
52 import org.onap.policy.models.base.PfConceptKey;
53 import org.onap.policy.models.base.PfKey;
54 import org.onap.policy.models.base.PfReferenceKey;
55 import org.onap.policy.models.base.PfUtils;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
58 import org.onap.policy.models.tosca.utils.ToscaUtils;
59
60 /**
61  * Class to represent the node type in TOSCA definition.
62  */
63
64 @Entity
65 @Table(name = "ToscaNodeType")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @Data
68 @EqualsAndHashCode(callSuper = true)
69 public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implements PfAuthorative<ToscaNodeType> {
70     private static final long serialVersionUID = -563659852901842616L;
71
72     @ElementCollection
73     @Lob
74     private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties;
75
76
77     // formatter:off
78     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
79     @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
80         @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
81     // @formatter:on
82     @Valid
83     private JpaToscaRequirements requirements;
84
85     /**
86      * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key.
87      */
88     public JpaToscaNodeType() {
89         this(new PfConceptKey());
90     }
91
92     /**
93      * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
94      *
95      * @param key the key
96      */
97     public JpaToscaNodeType(@NonNull final PfConceptKey key) {
98         super(key);
99     }
100
101     /**
102      * Copy constructor.
103      *
104      * @param copyConcept the concept to copy from
105      */
106     public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
107         super(copyConcept);
108         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
109         this.requirements =
110                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
111     }
112
113     /**
114      * Authorative constructor.
115      *
116      * @param authorativeConcept the authorative concept to copy from
117      */
118     public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
119         this.fromAuthorative(authorativeConcept);
120     }
121
122     @Override
123     public ToscaNodeType toAuthorative() {
124         ToscaNodeType toscaNodeType = new ToscaNodeType();
125         super.setToscaEntity(toscaNodeType);
126         super.toAuthorative();
127
128         toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
129
130         if (requirements != null) {
131             toscaNodeType.setRequirements(requirements.toAuthorative());
132         }
133
134         return toscaNodeType;
135     }
136
137     @Override
138     public void fromAuthorative(final ToscaNodeType toscaNodeType) {
139         super.fromAuthorative(toscaNodeType);
140
141         // Set properties
142         if (toscaNodeType.getProperties() != null) {
143             properties = new LinkedHashMap<>();
144             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) {
145                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
146                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
147                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
148             }
149         }
150
151         if (toscaNodeType.getRequirements() != null) {
152             requirements = new JpaToscaRequirements();
153             requirements.fromAuthorative(toscaNodeType.getRequirements());
154         }
155     }
156
157     @Override
158     public List<PfKey> getKeys() {
159         final List<PfKey> keyList = super.getKeys();
160
161         if (properties != null) {
162             for (JpaToscaProperty property : properties.values()) {
163                 keyList.addAll(property.getKeys());
164             }
165         }
166
167         if (requirements != null) {
168             keyList.addAll(requirements.getKeys());
169         }
170
171         return keyList;
172     }
173
174     @Override
175     public void clean() {
176         super.clean();
177
178         if (properties != null) {
179             for (JpaToscaProperty property : properties.values()) {
180                 property.clean();
181             }
182         }
183
184         if (requirements != null) {
185             requirements.clean();
186         }
187     }
188
189     @Override
190     public BeanValidationResult validate(String fieldName) {
191         BeanValidationResult result = super.validate(fieldName);
192
193         validateKeyVersionNotNull(result, "key", getKey());
194
195         return result;
196     }
197
198     @Override
199     public int compareTo(final PfConcept otherConcept) {
200         if (otherConcept == null) {
201             return -1;
202         }
203         if (this == otherConcept) {
204             return 0;
205         }
206         if (getClass() != otherConcept.getClass()) {
207             return getClass().getName().compareTo(otherConcept.getClass().getName());
208         }
209
210         final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
211         int result = super.compareTo(other);
212         if (result != 0) {
213             return result;
214         }
215
216         result = PfUtils.compareMaps(properties, other.properties);
217         if (result != 0) {
218             return result;
219         }
220
221         return ObjectUtils.compare(requirements, other.requirements);
222     }
223
224     /**
225      * Get the data types referenced in a node type.
226      *
227      * @return the data types referenced in a node type
228      */
229     public Collection<PfConceptKey> getReferencedDataTypes() {
230         if (properties == null) {
231             return CollectionUtils.emptyCollection();
232         }
233
234         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
235
236         for (JpaToscaProperty property : properties.values()) {
237             referencedDataTypes.add(property.getType());
238
239             if (property.getEntrySchema() != null) {
240                 referencedDataTypes.add(property.getEntrySchema().getType());
241             }
242         }
243
244         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
245
246         return referencedDataTypes;
247     }
248 }