Add unit tests for policy type and policy filters
[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.FetchType;
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.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
85      * key.
86      *
87      * @param key the key
88      */
89     public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
90         this.key = key;
91     }
92
93     /**
94      * Copy constructor.
95      *
96      * @param copyConcept the concept to copy from
97      */
98     public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
99         super(copyConcept);
100     }
101
102     /**
103      * Authorative constructor.
104      *
105      * @param authorativeConcept the authorative concept to copy from
106      */
107     public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
108         this.fromAuthorative(authorativeConcept);
109     }
110
111     @Override
112     public ToscaTopologyTemplate toAuthorative() {
113         final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
114
115         toscaTopologyTemplate.setDescription(description);
116
117         if (policies != null) {
118             toscaTopologyTemplate.setPolicies(policies.toAuthorative());
119         }
120
121         return toscaTopologyTemplate;
122     }
123
124     @Override
125     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
126         description = toscaTopologyTemplate.getDescription();
127
128         if (toscaTopologyTemplate.getPolicies() != null) {
129             policies = new JpaToscaPolicies();
130             policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
131         }
132     }
133
134     @Override
135     public List<PfKey> getKeys() {
136         final List<PfKey> keyList = getKey().getKeys();
137
138         if (policies != null) {
139             keyList.addAll(policies.getKeys());
140         }
141
142         return keyList;
143     }
144
145     @Override
146     public void clean() {
147         key.clean();
148
149         description = (description != null ? description.trim() : null);
150
151         if (policies != null) {
152             policies.clean();
153         }
154     }
155
156     @Override
157     public PfValidationResult validate(PfValidationResult resultIn) {
158         PfValidationResult result = resultIn;
159
160         if (key.isNullKey()) {
161             result.addValidationMessage(
162                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
163         }
164
165         result = key.validate(result);
166
167         if (description != null && description.trim().length() == 0) {
168             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
169                     "property description may not be blank"));
170         }
171
172         if (policies != null) {
173             result = policies.validate(result);
174         }
175
176         return result;
177     }
178
179     @Override
180     public int compareTo(final PfConcept otherConcept) {
181         if (otherConcept == null) {
182             return -1;
183         }
184         if (this == otherConcept) {
185             return 0;
186         }
187         if (getClass() != otherConcept.getClass()) {
188             return this.hashCode() - otherConcept.hashCode();
189         }
190
191         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
192         if (!key.equals(other.key)) {
193             return key.compareTo(other.key);
194         }
195
196         int result = ObjectUtils.compare(description, other.description);
197         if (result != 0) {
198             return result;
199         }
200
201         return ObjectUtils.compare(policies, other.policies);
202     }
203
204     @Override
205     public PfConcept copyTo(@NonNull PfConcept target) {
206         final Object copyObject = target;
207         Assertions.instanceOf(copyObject, PfConcept.class);
208
209         final JpaToscaTopologyTemplate copy = ((JpaToscaTopologyTemplate) copyObject);
210         copy.setKey(new PfReferenceKey(key));
211         copy.setDescription(description);
212         copy.setPolicies(policies != null ? new JpaToscaPolicies(policies) : null);
213
214         return copy;
215     }
216 }