Merge "Aai Simulator for CQ"
[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_NAME = "ToscaServiceTemplateSimple";
68     public static final String DEFAULT_VERSION = "1.0.0";
69
70     @Column
71     @SerializedName("tosca_definitions_version")
72     private String toscaDefinitionsVersion;
73
74     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
75     @SerializedName("data_types")
76     private JpaToscaDataTypes dataTypes;
77
78     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
79     @SerializedName("policy_types")
80     private JpaToscaPolicyTypes policyTypes;
81
82     @Column
83     @SerializedName("topology_template")
84     private JpaToscaTopologyTemplate topologyTemplate;
85
86
87     /**
88      * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
89      */
90     public JpaToscaServiceTemplate() {
91         this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
92     }
93
94     /**
95      * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
96      *
97      * @param key the key
98      */
99     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
100         this(key, "");
101     }
102
103     /**
104      * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters.
105      *
106      * @param key the key
107      * @param toscaDefinitionsVersion the TOSCA version string
108      */
109     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
110         super(key);
111         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
112     }
113
114     /**
115      * Copy constructor.
116      *
117      * @param copyConcept the concept to copy from
118      */
119     public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
120         super(copyConcept);
121     }
122
123     /**
124      * Authorative constructor.
125      *
126      * @param authorativeConcept the authorative concept to copy from
127      */
128     public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) {
129         this.fromAuthorative(authorativeConcept);
130     }
131
132     @Override
133     public ToscaServiceTemplate toAuthorative() {
134         final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
135
136         super.setToscaEntity(toscaServiceTemplate);
137         super.toAuthorative();
138
139         toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
140
141         if (dataTypes != null) {
142             toscaServiceTemplate.setDataTypes(dataTypes.toAuthorative());
143         }
144
145         if (policyTypes != null) {
146             toscaServiceTemplate.setPolicyTypes(policyTypes.toAuthorative());
147         }
148
149         if (topologyTemplate != null) {
150             toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative());
151         }
152
153         return toscaServiceTemplate;
154     }
155
156     @Override
157     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
158         super.fromAuthorative(toscaServiceTemplate);
159
160         if (getKey().getName() == PfKey.NULL_KEY_NAME) {
161             getKey().setName(DEFAULT_NAME);
162         }
163
164         if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) {
165             getKey().setVersion(DEFAULT_VERSION);
166         }
167
168         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
169
170         if (toscaServiceTemplate.getDataTypes() != null) {
171             dataTypes = new JpaToscaDataTypes();
172             dataTypes.fromAuthorative(toscaServiceTemplate.getDataTypes());
173         }
174
175         if (toscaServiceTemplate.getPolicyTypes() != null) {
176             policyTypes = new JpaToscaPolicyTypes();
177             policyTypes.fromAuthorative(toscaServiceTemplate.getPolicyTypes());
178         }
179
180
181         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
182             topologyTemplate = new JpaToscaTopologyTemplate();
183             topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate());
184         }
185     }
186
187     @Override
188     public List<PfKey> getKeys() {
189         final List<PfKey> keyList = super.getKeys();
190
191         if (dataTypes != null) {
192             keyList.addAll(dataTypes.getKeys());
193         }
194
195         if (policyTypes != null) {
196             keyList.addAll(policyTypes.getKeys());
197         }
198
199         if (topologyTemplate != null) {
200             keyList.addAll(topologyTemplate.getKeys());
201         }
202
203         return keyList;
204     }
205
206     @Override
207     public void clean() {
208         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
209
210         if (dataTypes != null) {
211             dataTypes.clean();
212         }
213
214         if (policyTypes != null) {
215             policyTypes.clean();
216         }
217
218         if (topologyTemplate != null) {
219             topologyTemplate.clean();
220         }
221     }
222
223     @Override
224     public PfValidationResult validate(final PfValidationResult resultIn) {
225         PfValidationResult result = super.validate(resultIn);
226
227         if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
228             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
229                     "service template tosca definitions version may not be null"));
230         }
231
232         if (dataTypes != null) {
233             result = dataTypes.validate(result);
234         }
235
236         if (policyTypes != null) {
237             result = policyTypes.validate(result);
238         }
239
240         return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
241     }
242
243     @Override
244     public int compareTo(final PfConcept otherConcept) {
245         if (otherConcept == null) {
246             return -1;
247         }
248         if (this == otherConcept) {
249             return 0;
250         }
251         if (getClass() != otherConcept.getClass()) {
252             return this.hashCode() - otherConcept.hashCode();
253         }
254
255         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
256         if (!super.equals(other)) {
257             return super.compareTo(other);
258         }
259
260         int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
261         if (result != 0) {
262             return result;
263         }
264
265         result = ObjectUtils.compare(dataTypes, other.dataTypes);
266         if (result != 0) {
267             return result;
268         }
269
270         result = ObjectUtils.compare(policyTypes, other.policyTypes);
271         if (result != 0) {
272             return result;
273         }
274
275         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
276     }
277
278     @Override
279     public PfConcept copyTo(@NonNull PfConcept target) {
280         final Object copyObject = target;
281         Assertions.instanceOf(copyObject, PfConcept.class);
282
283         final JpaToscaServiceTemplate copy = ((JpaToscaServiceTemplate) copyObject);
284         super.copyTo(target);
285         copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
286
287         copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null);
288         copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null);
289         copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null);
290
291         return copy;
292     }
293 }