JPA concepts for TOSCA
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTopologyTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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 com.google.gson.annotations.SerializedName;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import javax.persistence.CascadeType;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
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.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.PfKey;
49 import org.onap.policy.models.base.PfReferenceKey;
50 import org.onap.policy.models.base.PfUtils;
51 import org.onap.policy.models.base.PfValidationMessage;
52 import org.onap.policy.models.base.PfValidationResult;
53 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
57
58 /**
59  * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
60  * implemented.
61  *
62  * @author Liam Fallon (liam.fallon@est.tech)
63  */
64 @Entity
65 @Table(name = "ToscaTopologyTemplate")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @Data
68 @EqualsAndHashCode(callSuper = false)
69 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
70     private static final long serialVersionUID = 8969698734673232603L;
71
72     public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
73
74     @EmbeddedId
75     private PfReferenceKey key;
76
77     @Column(name = "description")
78     private String description;
79
80     // @formatter:off
81     @ElementCollection
82     @Lob
83     private Map<String, JpaToscaParameter> inputs;
84
85     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
86     @JoinColumns(
87         {
88             @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name"),
89             @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version")
90         }
91     )
92     @SerializedName("data_types")
93     private JpaToscaNodeTemplates nodeTemplates;
94
95     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
96     @JoinColumns(
97             {
98                 @JoinColumn(name = "policyName",    referencedColumnName = "name"),
99                 @JoinColumn(name = "policyVersion", referencedColumnName = "version")
100             }
101         )
102     // @formatter:on
103     private JpaToscaPolicies policies;
104
105     /**
106      * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
107      */
108     public JpaToscaTopologyTemplate() {
109         this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
110                 DEFAULT_LOCAL_NAME));
111     }
112
113     /**
114      * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
115      *
116      * @param key the key
117      */
118     public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
119         this.key = key;
120     }
121
122     /**
123      * Copy constructor.
124      *
125      * @param copyConcept the concept to copy from
126      */
127     public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
128         super(copyConcept);
129         this.key = new PfReferenceKey(copyConcept.key);
130         this.description = copyConcept.description;
131         this.inputs = PfUtils.mapMap(copyConcept.inputs, JpaToscaParameter::new);
132         this.nodeTemplates =
133                 (copyConcept.nodeTemplates != null ? new JpaToscaNodeTemplates(copyConcept.nodeTemplates) : null);
134         this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
135     }
136
137     /**
138      * Authorative constructor.
139      *
140      * @param authorativeConcept the authorative concept to copy from
141      */
142     public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
143         this.fromAuthorative(authorativeConcept);
144     }
145
146     @Override
147     public ToscaTopologyTemplate toAuthorative() {
148         final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
149
150         toscaTopologyTemplate.setDescription(description);
151
152         if (inputs != null) {
153             Map<String, ToscaParameter> inputMap = new LinkedHashMap<>();
154
155             for (Map.Entry<String, JpaToscaParameter> entry : inputs.entrySet()) {
156                 inputMap.put(entry.getKey(), entry.getValue().toAuthorative());
157             }
158
159             toscaTopologyTemplate.setInputs(inputMap);
160         }
161
162         if (nodeTemplates != null) {
163             toscaTopologyTemplate.setNodeTemplates(new LinkedHashMap<>());
164             List<Map<String, ToscaNodeTemplate>> nodeTemplateMapList = nodeTemplates.toAuthorative();
165             for (Map<String, ToscaNodeTemplate> nodeTemplateMap : nodeTemplateMapList) {
166                 toscaTopologyTemplate.getNodeTemplates().putAll(nodeTemplateMap);
167             }
168         }
169
170         if (policies != null) {
171             toscaTopologyTemplate.setPolicies(policies.toAuthorative());
172         }
173
174         return toscaTopologyTemplate;
175     }
176
177     @Override
178     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
179         description = toscaTopologyTemplate.getDescription();
180
181         if (toscaTopologyTemplate.getInputs() != null) {
182             inputs = new LinkedHashMap<>();
183             for (Map.Entry<String, ToscaParameter> toscaInputEntry : toscaTopologyTemplate.getInputs().entrySet()) {
184                 JpaToscaParameter jpaInput = new JpaToscaParameter(toscaInputEntry.getValue());
185                 jpaInput.setKey(new PfReferenceKey(getKey(), toscaInputEntry.getKey()));
186                 inputs.put(toscaInputEntry.getKey(), jpaInput);
187             }
188         }
189
190         if (toscaTopologyTemplate.getNodeTemplates() != null) {
191             nodeTemplates = new JpaToscaNodeTemplates();
192             nodeTemplates.fromAuthorative(Collections.singletonList(toscaTopologyTemplate.getNodeTemplates()));
193         }
194
195         if (toscaTopologyTemplate.getPolicies() != null) {
196             policies = new JpaToscaPolicies();
197             policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
198         }
199     }
200
201     @Override
202     public List<PfKey> getKeys() {
203         final List<PfKey> keyList = getKey().getKeys();
204
205         if (inputs != null) {
206             for (JpaToscaParameter input : inputs.values()) {
207                 keyList.addAll(input.getKeys());
208             }
209         }
210
211         if (nodeTemplates != null) {
212             keyList.addAll(nodeTemplates.getKeys());
213         }
214
215         if (policies != null) {
216             keyList.addAll(policies.getKeys());
217         }
218
219         return keyList;
220     }
221
222     @Override
223     public void clean() {
224         key.clean();
225
226         description = (description != null ? description.trim() : null);
227
228         if (inputs != null) {
229             for (JpaToscaParameter input : inputs.values()) {
230                 input.clean();
231             }
232         }
233
234         if (nodeTemplates != null) {
235             nodeTemplates.clean();
236         }
237
238         if (policies != null) {
239             policies.clean();
240         }
241     }
242
243     @Override
244     public PfValidationResult validate(PfValidationResult resultIn) {
245         PfValidationResult result = resultIn;
246
247         if (key.isNullKey()) {
248             result.addValidationMessage(
249                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
250         }
251
252         result = key.validate(result);
253
254         if (description != null && description.trim().length() == 0) {
255             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
256                     "property description may not be blank"));
257         }
258
259         if (inputs != null) {
260             result = validateInputs(result);
261         }
262
263
264         if (nodeTemplates != null) {
265             result = nodeTemplates.validate(result);
266         }
267
268         if (policies != null) {
269             result = policies.validate(result);
270         }
271
272         return result;
273     }
274
275     /**
276      * Validate the inputs.
277      *
278      * @param resultIn The result of validations up to now
279      * @return the validation result
280      */
281     private PfValidationResult validateInputs(final PfValidationResult resultIn) {
282         PfValidationResult result = resultIn;
283
284         for (JpaToscaParameter input : inputs.values()) {
285             if (input == null) {
286                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
287                         "topology template input may not be null "));
288             } else {
289                 result = input.validate(result);
290             }
291         }
292         return result;
293     }
294
295     @Override
296     public int compareTo(final PfConcept otherConcept) {
297         int result = compareToWithoutEntities(otherConcept);
298         if (result != 0) {
299             return result;
300         }
301
302         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
303
304         result = PfUtils.compareObjects(inputs, other.inputs);
305         if (result != 0) {
306             return result;
307         }
308
309         result = ObjectUtils.compare(nodeTemplates, other.nodeTemplates);
310         if (result != 0) {
311             return result;
312         }
313
314         return ObjectUtils.compare(policies, other.policies);
315     }
316
317     /**
318      * Compare this topology template to another topology template, ignoring contained entities.
319      *
320      * @param otherConcept the other topology template
321      * @return the result of the comparison
322      */
323     public int compareToWithoutEntities(final PfConcept otherConcept) {
324         if (otherConcept == null) {
325             return -1;
326         }
327         if (this == otherConcept) {
328             return 0;
329         }
330         if (getClass() != otherConcept.getClass()) {
331             return getClass().getName().compareTo(otherConcept.getClass().getName());
332         }
333
334         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
335         if (!key.equals(other.key)) {
336             return key.compareTo(other.key);
337         }
338
339         return ObjectUtils.compare(description, other.description);
340     }
341 }