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