Repair policy types in TOSCA service template
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaServiceTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.persistence.CascadeType;
32 import javax.persistence.Column;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.OneToOne;
38 import javax.persistence.Table;
39 import lombok.Data;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42 import org.apache.commons.lang3.ObjectUtils;
43 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
54
55 /**
56  * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template
57  * are implemented.
58  *
59  * @author Liam Fallon (liam.fallon@est.tech)
60  */
61 @Entity
62 @Table(name = "ToscaServiceTemplate")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @Data
65 @EqualsAndHashCode(callSuper = true)
66 public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate>
67         implements PfAuthorative<ToscaServiceTemplate> {
68     private static final long serialVersionUID = 8084846046148349401L;
69
70     public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0";
71     public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
72     public static final String DEFAULT_VERSION = "1.0.0";
73
74     @Column
75     @SerializedName("tosca_definitions_version")
76     private String toscaDefinitionsVersion;
77
78     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
79     @SerializedName("data_types")
80     private JpaToscaDataTypes dataTypes;
81
82     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
83     @SerializedName("policy_types")
84     private JpaToscaPolicyTypes policyTypes;
85
86     @Column
87     @SerializedName("topology_template")
88     private JpaToscaTopologyTemplate topologyTemplate;
89
90
91     /**
92      * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
93      */
94     public JpaToscaServiceTemplate() {
95         this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
96     }
97
98     /**
99      * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
100      *
101      * @param key the key
102      */
103     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
104         this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
105     }
106
107     /**
108      * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
109      *
110      * @param key the key
111      * @param toscaDefinitionsVersion the TOSCA version string
112      */
113     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
114         super(key);
115         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
116     }
117
118     /**
119      * Copy constructor.
120      *
121      * @param copyConcept the concept to copy from
122      */
123     public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
124         super(copyConcept);
125         this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
126         this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null);
127         this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null);
128         this.topologyTemplate = (copyConcept.topologyTemplate != null
129                         ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate) : null);
130     }
131
132     /**
133      * Authorative constructor.
134      *
135      * @param authorativeConcept the authorative concept to copy from
136      */
137     public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
138         this.fromAuthorative(authorativeConcept);
139     }
140
141     @Override
142     public ToscaServiceTemplate toAuthorative() {
143         final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
144
145         super.setToscaEntity(toscaServiceTemplate);
146         super.toAuthorative();
147
148         toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
149
150         if (dataTypes != null) {
151             toscaServiceTemplate.setDataTypes(new LinkedHashMap<>());
152             List<Map<String, ToscaDataType>> dataTypeMapList = dataTypes.toAuthorative();
153             for (Map<String, ToscaDataType> dataTypeMap : dataTypeMapList) {
154                 toscaServiceTemplate.getDataTypes().putAll(dataTypeMap);
155             }
156         }
157
158         if (policyTypes != null) {
159             toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
160             List<Map<String, ToscaPolicyType>> policyTypeMapList = policyTypes.toAuthorative();
161             for (Map<String, ToscaPolicyType> policyTypeMap : policyTypeMapList) {
162                 toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap);
163             }
164         }
165
166         if (topologyTemplate != null) {
167             toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
168         }
169
170         return toscaServiceTemplate;
171     }
172
173     @Override
174     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
175         super.fromAuthorative(toscaServiceTemplate);
176
177         if (getKey().getName() == PfKey.NULL_KEY_NAME) {
178             getKey().setName(DEFAULT_NAME);
179         }
180
181         if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) {
182             getKey().setVersion(DEFAULT_VERSION);
183         }
184
185         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
186
187         if (toscaServiceTemplate.getDataTypes() != null) {
188             dataTypes = new JpaToscaDataTypes();
189             dataTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getDataTypes()));
190         }
191
192         if (toscaServiceTemplate.getPolicyTypes() != null) {
193             policyTypes = new JpaToscaPolicyTypes();
194             policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes()));
195         }
196
197
198         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
199             topologyTemplate = new JpaToscaTopologyTemplate();
200             topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
201         }
202     }
203
204     @Override
205     public List<PfKey> getKeys() {
206         final List<PfKey> keyList = super.getKeys();
207
208         if (dataTypes != null) {
209             keyList.addAll(dataTypes.getKeys());
210         }
211
212         if (policyTypes != null) {
213             keyList.addAll(policyTypes.getKeys());
214         }
215
216         if (topologyTemplate != null) {
217             keyList.addAll(topologyTemplate.getKeys());
218         }
219
220         return keyList;
221     }
222
223     @Override
224     public void clean() {
225         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
226
227         if (dataTypes != null) {
228             dataTypes.clean();
229         }
230
231         if (policyTypes != null) {
232             policyTypes.clean();
233         }
234
235         if (topologyTemplate != null) {
236             topologyTemplate.clean();
237         }
238     }
239
240     @Override
241     public PfValidationResult validate(final PfValidationResult resultIn) {
242         PfValidationResult result = super.validate(resultIn);
243
244         if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
245             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
246                     "service template tosca definitions version may not be null"));
247         }
248
249         if (dataTypes != null) {
250             result = dataTypes.validate(result);
251         }
252
253         if (policyTypes != null) {
254             result = policyTypes.validate(result);
255         }
256
257         return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
258     }
259
260     @Override
261     public int compareTo(final PfConcept otherConcept) {
262         if (otherConcept == null) {
263             return -1;
264         }
265         if (this == otherConcept) {
266             return 0;
267         }
268         if (getClass() != otherConcept.getClass()) {
269             return getClass().getName().compareTo(otherConcept.getClass().getName());
270         }
271
272         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
273         if (!super.equals(other)) {
274             return super.compareTo(other);
275         }
276
277         int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
278         if (result != 0) {
279             return result;
280         }
281
282         result = ObjectUtils.compare(dataTypes, other.dataTypes);
283         if (result != 0) {
284             return result;
285         }
286
287         result = ObjectUtils.compare(policyTypes, other.policyTypes);
288         if (result != 0) {
289             return result;
290         }
291
292         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
293     }
294 }