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