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