Add support for legacy guard policies
[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.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 JpaToscaServiceTemplate extends JpaToscaEntityType {
61     private static final long serialVersionUID = 8084846046148349401L;
62
63     public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
64     public static final String DEFAULT_VERSION = "1.0.0";
65
66     @Column
67     @SerializedName("tosca_definitions_version")
68     private String toscaDefinitionsVersion;
69
70     @OneToOne(cascade = CascadeType.ALL)
71     @SerializedName("data_types")
72     private JpaToscaDataTypes dataTypes;
73
74     @OneToOne(cascade = CascadeType.ALL)
75     @SerializedName("policy_types")
76     private JpaToscaPolicyTypes policyTypes;
77
78     @SerializedName("topology_template")
79     private JpaToscaTopologyTemplate topologyTemplate;
80
81
82     /**
83      * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key.
84      */
85     public JpaToscaServiceTemplate() {
86         this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
87     }
88
89     /**
90      * The Key Constructor creates a {@link JpaToscaServiceTemplate} object with the given concept key.
91      *
92      * @param key the key
93      */
94     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
95         this(key, "");
96     }
97
98     /**
99      * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory
100      * parameters.
101      *
102      * @param key the key
103      * @param toscaDefinitionsVersion the TOSCA version string
104      */
105     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) {
106         super(key);
107         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
108     }
109
110     /**
111      * Copy constructor.
112      *
113      * @param copyConcept the concept to copy from
114      */
115     public JpaToscaServiceTemplate(final JpaToscaServiceTemplate copyConcept) {
116         super(copyConcept);
117     }
118
119     @Override
120     public List<PfKey> getKeys() {
121         final List<PfKey> keyList = super.getKeys();
122
123         if (dataTypes != null) {
124             keyList.addAll(dataTypes.getKeys());
125         }
126
127         if (policyTypes != null) {
128             keyList.addAll(policyTypes.getKeys());
129         }
130
131         if (topologyTemplate != null) {
132             keyList.addAll(topologyTemplate.getKeys());
133         }
134
135         return keyList;
136     }
137
138     @Override
139     public void clean() {
140         toscaDefinitionsVersion = toscaDefinitionsVersion.trim();
141
142         if (dataTypes != null) {
143             dataTypes.clean();
144         }
145
146         if (policyTypes != null) {
147             policyTypes.clean();
148         }
149
150         if (topologyTemplate != null) {
151             topologyTemplate.clean();
152         }
153     }
154
155     @Override
156     public PfValidationResult validate(final PfValidationResult resultIn) {
157         PfValidationResult result = super.validate(resultIn);
158
159         if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) {
160             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
161                     "service template tosca definitions version may not be null"));
162         }
163
164         if (dataTypes != null) {
165             result = dataTypes.validate(result);
166         }
167
168         if (policyTypes != null) {
169             result = policyTypes.validate(result);
170         }
171
172         return (topologyTemplate != null ? topologyTemplate.validate(result) : result);
173     }
174
175     @Override
176     public int compareTo(final PfConcept otherConcept) {
177         if (otherConcept == null) {
178             return -1;
179         }
180         if (this == otherConcept) {
181             return 0;
182         }
183         if (getClass() != otherConcept.getClass()) {
184             return this.hashCode() - otherConcept.hashCode();
185         }
186
187         final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept;
188         if (!super.equals(other)) {
189             return super.compareTo(other);
190         }
191
192         int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
193         if (result != 0) {
194             return result;
195         }
196
197         result = ObjectUtils.compare(dataTypes, other.dataTypes);
198         if (result != 0) {
199             return result;
200         }
201
202         result = ObjectUtils.compare(policyTypes, other.policyTypes);
203         if (result != 0) {
204             return result;
205         }
206
207         return ObjectUtils.compare(topologyTemplate, other.topologyTemplate);
208     }
209
210     @Override
211     public PfConcept copyTo(@NonNull PfConcept target) {
212         final Object copyObject = target;
213         Assertions.instanceOf(copyObject, PfConcept.class);
214
215         final JpaToscaServiceTemplate copy = ((JpaToscaServiceTemplate) copyObject);
216         super.copyTo(target);
217         copy.setToscaDefinitionsVersion(toscaDefinitionsVersion);
218
219         copy.setDataTypes(dataTypes != null ? new JpaToscaDataTypes(dataTypes) : null);
220         copy.setPolicyTypes(policyTypes != null ? new JpaToscaPolicyTypes(policyTypes) : null);
221         copy.setTopologyTemplate(topologyTemplate != null ? new JpaToscaTopologyTemplate(topologyTemplate) : null);
222
223         return copy;
224     }
225 }