Merge "move actors code in drools-applications to policy/models"
[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 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 java.util.List;
24
25 import javax.persistence.CascadeType;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
33
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.utils.validation.Assertions;
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     @OneToOne(cascade = CascadeType.ALL)
72     private JpaToscaPolicies policies;
73
74     /**
75      * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
76      */
77     public JpaToscaTopologyTemplate() {
78         this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
79                 DEFAULT_LOCAL_NAME));
80     }
81
82     /**
83      * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept
84      * 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     }
100
101     /**
102      * Authorative constructor.
103      *
104      * @param authorativeConcept the authorative concept to copy from
105      */
106     public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
107         this.fromAuthorative(authorativeConcept);
108     }
109
110     @Override
111     public ToscaTopologyTemplate toAuthorative() {
112         final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
113
114         toscaTopologyTemplate.setDescription(description);
115
116         if (policies != null) {
117             toscaTopologyTemplate.setPolicies(policies.toAuthorative());
118         }
119
120         return toscaTopologyTemplate;
121     }
122
123     @Override
124     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
125         description = toscaTopologyTemplate.getDescription();
126
127         if (toscaTopologyTemplate.getPolicies() != null) {
128             policies = new JpaToscaPolicies();
129             policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
130         }
131     }
132
133     @Override
134     public List<PfKey> getKeys() {
135         final List<PfKey> keyList = getKey().getKeys();
136
137         if (policies != null) {
138             keyList.addAll(policies.getKeys());
139         }
140
141         return keyList;
142     }
143
144     @Override
145     public void clean() {
146         key.clean();
147
148         description = (description != null ? description.trim() : null);
149
150         if (policies != null) {
151             policies.clean();
152         }
153     }
154
155     @Override
156     public PfValidationResult validate(PfValidationResult resultIn) {
157         PfValidationResult result = resultIn;
158
159         if (key.isNullKey()) {
160             result.addValidationMessage(
161                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
162         }
163
164         result = key.validate(result);
165
166         if (description != null && description.trim().length() == 0) {
167             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
168                     "property description may not be blank"));
169         }
170
171         if (policies != null) {
172             result = policies.validate(result);
173         }
174
175         return result;
176     }
177
178     @Override
179     public int compareTo(final PfConcept otherConcept) {
180         if (otherConcept == null) {
181             return -1;
182         }
183         if (this == otherConcept) {
184             return 0;
185         }
186         if (getClass() != otherConcept.getClass()) {
187             return this.hashCode() - otherConcept.hashCode();
188         }
189
190         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
191         if (!key.equals(other.key)) {
192             return key.compareTo(other.key);
193         }
194
195         int result = ObjectUtils.compare(description, other.description);
196         if (result != 0) {
197             return result;
198         }
199
200         return ObjectUtils.compare(policies, other.policies);
201     }
202
203     @Override
204     public PfConcept copyTo(@NonNull PfConcept target) {
205         final Object copyObject = target;
206         Assertions.instanceOf(copyObject, PfConcept.class);
207
208         final JpaToscaTopologyTemplate copy = ((JpaToscaTopologyTemplate) copyObject);
209         copy.setKey(new PfReferenceKey(key));
210         copy.setDescription(description);
211         copy.setPolicies(policies != null ? new JpaToscaPolicies(policies) : null);
212
213         return copy;
214     }
215 }