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