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