fd8b134ce703be1da03d4cfb0df1b0ececc91cf7
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / ToscaServiceTemplate.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.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfValidationMessage;
46 import org.onap.policy.models.base.PfValidationResult;
47 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
48
49 /**
50  * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA
51  * service template are implemented.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 @Entity
56 @Table(name = "ToscaServiceTemplate")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = true)
60 public class ToscaServiceTemplate extends ToscaEntityType {
61     private static final long serialVersionUID = 8084846046148349401L;
62
63     @Column
64     @SerializedName("tosca_definitions_version")
65     private String toscaDefinitionsVersion;
66
67     @OneToOne(cascade = CascadeType.ALL)
68     @SerializedName("data_types")
69     private ToscaDataTypes dataTypes;
70
71     @OneToOne(cascade = CascadeType.ALL)
72     @SerializedName("policy_types")
73     private ToscaPolicyTypes policyTypes;
74
75     @SerializedName("topology_template")
76     private ToscaTopologyTemplate topologyTemplate;
77
78
79     /**
80      * The Default Constructor creates a {@link ToscaServiceTemplate} object with a null key.
81      */
82     public ToscaServiceTemplate() {
83         this(new PfConceptKey());
84     }
85
86     /**
87      * The Key Constructor creates a {@link ToscaServiceTemplate} object with the given concept key.
88      *
89      * @param key the key
90      */
91     public ToscaServiceTemplate(@NonNull final PfConceptKey key) {
92         this(key, "");
93     }
94
95     /**
96      * The full constructor creates a {@link ToscaServiceTemplate} object with all mandatory
97      * parameters.
98      *
99      * @param key the key
100      * @param toscaDefinitionsVersion the TOSCA version string
101      */
102     public ToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
103         super(key);
104         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
105     }
106
107     /**
108      * Copy constructor.
109      *
110      * @param copyConcept the concept to copy from
111      */
112     public ToscaServiceTemplate(final ToscaServiceTemplate copyConcept) {
113         super(copyConcept);
114     }
115
116     @Override
117     public List<PfKey> getKeys() {
118         final List<PfKey> keyList = super.getKeys();
119
120         if (dataTypes != null) {
121             keyList.addAll(dataTypes.getKeys());
122         }
123
124         if (policyTypes != null) {
125             keyList.addAll(policyTypes.getKeys());
126         }
127
128         if (topologyTemplate != null) {
129             keyList.addAll(topologyTemplate.getKeys());
130         }
131
132         return keyList;
133     }
134
135     @Override
136     public void clean() {
137         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
138
139         if (dataTypes != null) {
140             dataTypes.clean();
141         }
142
143         if (policyTypes != null) {
144             policyTypes.clean();
145         }
146
147         if (topologyTemplate != null) {
148             topologyTemplate.clean();
149         }
150     }
151
152     @Override
153     public PfValidationResult validate(final PfValidationResult resultIn) {
154         PfValidationResult result = super.validate(resultIn);
155
156         if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
157             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
158                     "service template tosca definitions version may not be null"));
159         }
160
161         if (dataTypes != null) {
162             result = dataTypes.validate(result);
163         }
164
165         if (policyTypes != null) {
166             result = policyTypes.validate(result);
167         }
168
169         return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
170     }
171
172     @Override
173     public int compareTo(final PfConcept otherConcept) {
174         if (otherConcept == null) {
175             return -1;
176         }
177         if (this == otherConcept) {
178             return 0;
179         }
180         if (getClass() != otherConcept.getClass()) {
181             return this.hashCode() - otherConcept.hashCode();
182         }
183
184         final ToscaServiceTemplate other = (ToscaServiceTemplate) otherConcept;
185         if (!super.equals(other)) {
186             return super.compareTo(other);
187         }
188
189         int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
190         if (result != 0) {
191             return result;
192         }
193
194         result = ObjectUtils.compare(dataTypes, other.dataTypes);
195         if (result != 0) {
196             return result;
197         }
198
199         result = ObjectUtils.compare(policyTypes, other.policyTypes);
200         if (result != 0) {
201             return result;
202         }
203
204         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
205     }
206
207     @Override
208     public PfConcept copyTo(@NonNull PfConcept target) {
209         final Object copyObject = target;
210         Assertions.instanceOf(copyObject, PfConcept.class);
211
212         final ToscaServiceTemplate copy = ((ToscaServiceTemplate) copyObject);
213         super.copyTo(target);
214         copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
215
216         copy.setDataTypes(dataTypes != null ? new ToscaDataTypes(dataTypes) : null);
217         copy.setPolicyTypes(policyTypes != null ? new ToscaPolicyTypes(policyTypes) : null);
218         copy.setTopologyTemplate(topologyTemplate != null ? new ToscaTopologyTemplate(topologyTemplate) : null);
219
220         return copy;
221     }
222 }