Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTopologyTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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 java.util.List;
25 import javax.persistence.CascadeType;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.FetchType;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.JoinColumns;
34 import javax.persistence.OneToOne;
35 import javax.persistence.Table;
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.apache.commons.lang3.ObjectUtils;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfReferenceKey;
44 import org.onap.policy.models.base.PfValidationMessage;
45 import org.onap.policy.models.base.PfValidationResult;
46 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
48
49 /**
50  * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
51  * implemented.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 @Entity
56 @Table(name = "ToscaTopologyTemplate")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
61     private static final long serialVersionUID = 8969698734673232603L;
62
63     public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
64
65     @EmbeddedId
66     private PfReferenceKey key;
67
68     @Column(name = "description")
69     private String description;
70
71     // @formatter:off
72     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
73     @JoinColumns(
74             {
75                 @JoinColumn(name = "policyName",    referencedColumnName = "name"),
76                 @JoinColumn(name = "policyVersion", referencedColumnName = "version")
77             }
78         )
79     // @formatter:on
80     private JpaToscaPolicies policies;
81
82     /**
83      * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
84      */
85     public JpaToscaTopologyTemplate() {
86         this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
87                 DEFAULT_LOCAL_NAME));
88     }
89
90     /**
91      * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
92      *
93      * @param key the key
94      */
95     public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
96         this.key = key;
97     }
98
99     /**
100      * Copy constructor.
101      *
102      * @param copyConcept the concept to copy from
103      */
104     public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
105         super(copyConcept);
106         this.key = new PfReferenceKey(copyConcept.key);
107         this.description = copyConcept.description;
108         this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
109     }
110
111     /**
112      * Authorative constructor.
113      *
114      * @param authorativeConcept the authorative concept to copy from
115      */
116     public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
117         this.fromAuthorative(authorativeConcept);
118     }
119
120     @Override
121     public ToscaTopologyTemplate toAuthorative() {
122         final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
123
124         toscaTopologyTemplate.setDescription(description);
125
126         if (policies != null) {
127             toscaTopologyTemplate.setPolicies(policies.toAuthorative());
128         }
129
130         return toscaTopologyTemplate;
131     }
132
133     @Override
134     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
135         description = toscaTopologyTemplate.getDescription();
136
137         if (toscaTopologyTemplate.getPolicies() != null) {
138             policies = new JpaToscaPolicies();
139             policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
140         }
141     }
142
143     @Override
144     public List<PfKey> getKeys() {
145         final List<PfKey> keyList = getKey().getKeys();
146
147         if (policies != null) {
148             keyList.addAll(policies.getKeys());
149         }
150
151         return keyList;
152     }
153
154     @Override
155     public void clean() {
156         key.clean();
157
158         description = (description != null ? description.trim() : null);
159
160         if (policies != null) {
161             policies.clean();
162         }
163     }
164
165     @Override
166     public PfValidationResult validate(PfValidationResult resultIn) {
167         PfValidationResult result = resultIn;
168
169         if (key.isNullKey()) {
170             result.addValidationMessage(
171                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
172         }
173
174         result = key.validate(result);
175
176         if (description != null && description.trim().length() == 0) {
177             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
178                     "property description may not be blank"));
179         }
180
181         if (policies != null) {
182             result = policies.validate(result);
183         }
184
185         return result;
186     }
187
188     @Override
189     public int compareTo(final PfConcept otherConcept) {
190         int result = compareToWithoutEntities(otherConcept);
191         if (result != 0) {
192             return result;
193         }
194
195         return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies);
196     }
197
198     /**
199      * Compare this topology template to another topology template, ignoring contained entitites.
200      *
201      * @param otherConcept the other topology template
202      * @return the result of the comparison
203      */
204     public int compareToWithoutEntities(final PfConcept otherConcept) {
205         if (otherConcept == null) {
206             return -1;
207         }
208         if (this == otherConcept) {
209             return 0;
210         }
211         if (getClass() != otherConcept.getClass()) {
212             return getClass().getName().compareTo(otherConcept.getClass().getName());
213         }
214
215         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
216         if (!key.equals(other.key)) {
217             return key.compareTo(other.key);
218         }
219
220         return ObjectUtils.compare(description, other.description);
221     }
222 }