JPA concepts for TOSCA
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30 import javax.persistence.CascadeType;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.Entity;
33 import javax.persistence.FetchType;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.JoinColumn;
37 import javax.persistence.JoinColumns;
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.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.base.PfValidationMessage;
53 import org.onap.policy.models.base.PfValidationResult;
54 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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<String, JpaToscaProperty> properties;
74
75
76     // formatter:off
77     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
78     @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
79         @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
80     // @formatter:on
81     private JpaToscaRequirements requirements;
82
83     /**
84      * The Default Constructor creates a {@link JpaToscaNodeType} object with a null key.
85      */
86     public JpaToscaNodeType() {
87         this(new PfConceptKey());
88     }
89
90     /**
91      * The Key Constructor creates a {@link JpaToscaNodeType} object with the given concept key.
92      *
93      * @param key the key
94      */
95     public JpaToscaNodeType(@NonNull final PfConceptKey key) {
96         super(key);
97     }
98
99     /**
100      * Copy constructor.
101      *
102      * @param copyConcept the concept to copy from
103      */
104     public JpaToscaNodeType(final JpaToscaNodeType copyConcept) {
105         super(copyConcept);
106         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
107         this.requirements =
108                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
109     }
110
111     /**
112      * Authorative constructor.
113      *
114      * @param authorativeConcept the authorative concept to copy from
115      */
116     public JpaToscaNodeType(final ToscaNodeType authorativeConcept) {
117         this.fromAuthorative(authorativeConcept);
118     }
119
120     @Override
121     public ToscaNodeType toAuthorative() {
122         ToscaNodeType toscaNodeType = new ToscaNodeType();
123         super.setToscaEntity(toscaNodeType);
124         super.toAuthorative();
125
126         toscaNodeType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
127
128         if (requirements != null) {
129             toscaNodeType.setRequirements(requirements.toAuthorative());
130         }
131
132         return toscaNodeType;
133     }
134
135     @Override
136     public void fromAuthorative(final ToscaNodeType toscaNodeType) {
137         super.fromAuthorative(toscaNodeType);
138
139         // Set properties
140         if (toscaNodeType.getProperties() != null) {
141             properties = new LinkedHashMap<>();
142             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaNodeType.getProperties().entrySet()) {
143                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
144                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
145                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
146             }
147         }
148
149         if (toscaNodeType.getRequirements() != null) {
150             requirements = new JpaToscaRequirements();
151             requirements.fromAuthorative(toscaNodeType.getRequirements());
152         }
153     }
154
155     @Override
156     public List<PfKey> getKeys() {
157         final List<PfKey> keyList = super.getKeys();
158
159         if (properties != null) {
160             for (JpaToscaProperty property : properties.values()) {
161                 keyList.addAll(property.getKeys());
162             }
163         }
164
165         if (requirements != null) {
166             keyList.addAll(requirements.getKeys());
167         }
168
169         return keyList;
170     }
171
172     @Override
173     public void clean() {
174         super.clean();
175
176         if (properties != null) {
177             for (JpaToscaProperty property : properties.values()) {
178                 property.clean();
179             }
180         }
181
182         if (requirements != null) {
183             requirements.clean();
184         }
185     }
186
187     @Override
188     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
189         PfValidationResult result = super.validate(resultIn);
190
191         if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
192             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
193                     "key version is a null version"));
194         }
195
196         if (properties != null) {
197             result = validateProperties(result);
198         }
199
200         if (requirements != null) {
201             result = requirements.validate(result);
202         }
203
204         return result;
205     }
206
207     /**
208      * Validate the capabiltiy type properties.
209      *
210      * @param resultIn The result of validations up to now
211      * @return the validation result
212      */
213     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
214         PfValidationResult result = resultIn;
215
216         for (JpaToscaProperty property : properties.values()) {
217             if (property == null) {
218                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
219                         "node type property may not be null "));
220             } else {
221                 result = property.validate(result);
222             }
223         }
224         return result;
225     }
226
227     @Override
228     public int compareTo(final PfConcept otherConcept) {
229         if (otherConcept == null) {
230             return -1;
231         }
232         if (this == otherConcept) {
233             return 0;
234         }
235         if (getClass() != otherConcept.getClass()) {
236             return getClass().getName().compareTo(otherConcept.getClass().getName());
237         }
238
239         final JpaToscaNodeType other = (JpaToscaNodeType) otherConcept;
240         int result = super.compareTo(other);
241         if (result != 0) {
242             return result;
243         }
244
245         result = PfUtils.compareMaps(properties, other.properties);
246         if (result != 0) {
247             return result;
248         }
249
250         return ObjectUtils.compare(requirements, other.requirements);
251     }
252
253     /**
254      * Get the data types referenced in a node type.
255      *
256      * @return the data types referenced in a node type
257      */
258     public Collection<PfConceptKey> getReferencedDataTypes() {
259         if (properties == null) {
260             return CollectionUtils.emptyCollection();
261         }
262
263         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
264
265         for (JpaToscaProperty property : properties.values()) {
266             referencedDataTypes.add(property.getType());
267
268             if (property.getEntrySchema() != null) {
269                 referencedDataTypes.add(property.getEntrySchema().getType());
270             }
271         }
272
273         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
274
275         return referencedDataTypes;
276     }
277 }