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