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