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