More sonars in models
[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.Lob;
39 import javax.persistence.OneToOne;
40 import javax.persistence.Table;
41 import lombok.Data;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
44 import org.apache.commons.collections4.CollectionUtils;
45 import org.apache.commons.lang3.ObjectUtils;
46 import org.onap.policy.common.parameters.BeanValidationResult;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
57 import org.onap.policy.models.tosca.utils.ToscaUtils;
58
59 /**
60  * Class to represent the node type in TOSCA definition.
61  */
62
63 @Entity
64 @Table(name = "ToscaNodeType")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @Data
67 @EqualsAndHashCode(callSuper = true)
68 public class JpaToscaNodeType extends JpaToscaEntityType<ToscaNodeType> implements PfAuthorative<ToscaNodeType> {
69     private static final long serialVersionUID = -563659852901842616L;
70
71     @ElementCollection
72     @Lob
73     private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties;
74
75
76     // formatter:off
77     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
78     @JoinColumn(name = "requirementsName", referencedColumnName = "name")
79     @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
80     // @formatter:on
81     @Valid
82     private JpaToscaRequirements requirements;
83
84     /**
85      * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key.
86      */
87     public JpaToscaNodeType() {
88         this(new PfConceptKey());
89     }
90
91     /**
92      * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
93      *
94      * @param key the key
95      */
96     public JpaToscaNodeType(@NonNull final PfConceptKey key) {
97         super(key);
98     }
99
100     /**
101      * Copy constructor.
102      *
103      * @param copyConcept the concept to copy from
104      */
105     public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
106         super(copyConcept);
107         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
108         this.requirements =
109                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
110     }
111
112     /**
113      * Authorative constructor.
114      *
115      * @param authorativeConcept the authorative concept to copy from
116      */
117     public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
118         this.fromAuthorative(authorativeConcept);
119     }
120
121     @Override
122     public ToscaNodeType toAuthorative() {
123         ToscaNodeType toscaNodeType = new ToscaNodeType();
124         super.setToscaEntity(toscaNodeType);
125         super.toAuthorative();
126
127         toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
128
129         if (requirements != null) {
130             toscaNodeType.setRequirements(requirements.toAuthorative());
131         }
132
133         return toscaNodeType;
134     }
135
136     @Override
137     public void fromAuthorative(final ToscaNodeType toscaNodeType) {
138         super.fromAuthorative(toscaNodeType);
139
140         // Set properties
141         if (toscaNodeType.getProperties() != null) {
142             properties = new LinkedHashMap<>();
143             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) {
144                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
145                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
146                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
147             }
148         }
149
150         if (toscaNodeType.getRequirements() != null) {
151             requirements = new JpaToscaRequirements();
152             requirements.fromAuthorative(toscaNodeType.getRequirements());
153         }
154     }
155
156     @Override
157     public List<PfKey> getKeys() {
158         final List<PfKey> keyList = super.getKeys();
159
160         if (properties != null) {
161             for (JpaToscaProperty property : properties.values()) {
162                 keyList.addAll(property.getKeys());
163             }
164         }
165
166         if (requirements != null) {
167             keyList.addAll(requirements.getKeys());
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         if (requirements != null) {
184             requirements.clean();
185         }
186     }
187
188     @Override
189     public BeanValidationResult validate(String fieldName) {
190         BeanValidationResult result = super.validate(fieldName);
191
192         validateKeyVersionNotNull(result, "key", getKey());
193
194         return result;
195     }
196
197     @Override
198     public int compareTo(final PfConcept otherConcept) {
199         if (otherConcept == null) {
200             return -1;
201         }
202         if (this == otherConcept) {
203             return 0;
204         }
205         if (getClass() != otherConcept.getClass()) {
206             return getClass().getName().compareTo(otherConcept.getClass().getName());
207         }
208
209         final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
210         int result = super.compareTo(other);
211         if (result != 0) {
212             return result;
213         }
214
215         result = PfUtils.compareMaps(properties, other.properties);
216         if (result != 0) {
217             return result;
218         }
219
220         return ObjectUtils.compare(requirements, other.requirements);
221     }
222
223     /**
224      * Get the data types referenced in a node type.
225      *
226      * @return the data types referenced in a node type
227      */
228     public Collection<PfConceptKey> getReferencedDataTypes() {
229         if (properties == null) {
230             return CollectionUtils.emptyCollection();
231         }
232
233         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
234
235         for (JpaToscaProperty property : properties.values()) {
236             referencedDataTypes.add(property.getType());
237
238             if (property.getEntrySchema() != null) {
239                 referencedDataTypes.add(property.getEntrySchema().getType());
240             }
241         }
242
243         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
244
245         return referencedDataTypes;
246     }
247 }