Merge "Complete filters for Database Fetches"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import javax.persistence.ElementCollection;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Lob;
37 import javax.persistence.Table;
38
39 import lombok.Data;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42
43 import org.onap.policy.common.utils.validation.Assertions;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfReferenceKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.PfValidationMessage;
51 import org.onap.policy.models.base.PfValidationResult;
52 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
55
56 /**
57  * Class to represent the policy type in TOSCA definition.
58  *
59  * @author Chenfei Gao (cgao@research.att.com)
60  * @author Liam Fallon (liam.fallon@est.tech)
61  */
62
63 @Entity
64 @Table(name = "ToscaPolicyType")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @Data
67 @EqualsAndHashCode(callSuper = true)
68 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
69     private static final long serialVersionUID = -563659852901842616L;
70
71     @ElementCollection
72     @Lob
73     private Map<String, JpaToscaProperty> properties;
74
75     @ElementCollection
76     private List<PfConceptKey> targets;
77
78     @ElementCollection
79     private List<JpaToscaTrigger> triggers;
80
81     /**
82      * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
83      */
84     public JpaToscaPolicyType() {
85         this(new PfConceptKey());
86     }
87
88     /**
89      * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
90      *
91      * @param key the key
92      */
93     public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
94         super(key);
95     }
96
97     /**
98      * Copy constructor.
99      *
100      * @param copyConcept the concept to copy from
101      */
102     public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
103         super(copyConcept);
104     }
105
106     /**
107      * Authorative constructor.
108      *
109      * @param authorativeConcept the authorative concept to copy from
110      */
111     public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
112         this.fromAuthorative(authorativeConcept);
113     }
114
115     @Override
116     public ToscaPolicyType toAuthorative() {
117         ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
118         super.setToscaEntity(toscaPolicyType);
119         super.toAuthorative();
120
121         if (properties != null) {
122             Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
123
124             for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
125                 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
126             }
127
128             toscaPolicyType.setProperties(propertyMap);
129         }
130
131         return toscaPolicyType;
132     }
133
134     @Override
135     public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
136         super.fromAuthorative(toscaPolicyType);
137
138         // Set properties
139         if (toscaPolicyType.getProperties() != null) {
140             properties = new LinkedHashMap<>();
141             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
142                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
143                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
144                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
145             }
146         }
147     }
148
149     @Override
150     public List<PfKey> getKeys() {
151         final List<PfKey> keyList = super.getKeys();
152
153         if (properties != null) {
154             for (JpaToscaProperty property : properties.values()) {
155                 keyList.addAll(property.getKeys());
156             }
157         }
158
159         if (targets != null) {
160             keyList.addAll(targets);
161         }
162
163         if (triggers != null) {
164             for (JpaToscaTrigger trigger : triggers) {
165                 keyList.addAll(trigger.getKeys());
166             }
167         }
168
169         return keyList;
170     }
171
172     @Override
173     public void clean() {
174         super.clean();
175
176         if (properties != null) {
177             for (JpaToscaProperty property : properties.values()) {
178                 property.clean();
179             }
180         }
181
182         if (targets != null) {
183             for (PfConceptKey target : targets) {
184                 target.clean();
185             }
186         }
187
188         if (triggers != null) {
189             for (JpaToscaTrigger trigger : triggers) {
190                 trigger.clean();
191             }
192         }
193     }
194
195     @Override
196     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
197         PfValidationResult result = super.validate(resultIn);
198
199         if (properties != null) {
200             result = validateProperties(result);
201         }
202
203         if (targets != null) {
204             result = validateTargets(result);
205         }
206
207         if (triggers != null) {
208             result = validateTriggers(result);
209         }
210
211         return result;
212     }
213
214     /**
215      * Validate the policy properties.
216      *
217      * @param result The result of validations up to now
218      * @return the validation result
219      */
220     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
221         PfValidationResult result = resultIn;
222
223         for (JpaToscaProperty property : properties.values()) {
224             if (property == null) {
225                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
226                         "policy property may not be null "));
227             } else {
228                 result = property.validate(result);
229             }
230         }
231         return result;
232     }
233
234     /**
235      * Validate the policy targets.
236      *
237      * @param result The result of validations up to now
238      * @return the validation result
239      */
240     private PfValidationResult validateTargets(final PfValidationResult resultIn) {
241         PfValidationResult result = resultIn;
242
243         for (PfConceptKey target : targets) {
244             if (target == null) {
245                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
246                         "policy target may not be null "));
247             } else {
248                 result = target.validate(result);
249             }
250         }
251         return result;
252     }
253
254     /**
255      * Validate the policy triggers.
256      *
257      * @param result The result of validations up to now
258      * @return the validation result
259      */
260     private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
261         PfValidationResult result = resultIn;
262
263         for (JpaToscaTrigger trigger : triggers) {
264             if (trigger == null) {
265                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
266                         "policy trigger may not be null "));
267             } else {
268                 result = trigger.validate(result);
269             }
270         }
271         return result;
272     }
273
274     @Override
275     public int compareTo(final PfConcept otherConcept) {
276         if (otherConcept == null) {
277             return -1;
278         }
279         if (this == otherConcept) {
280             return 0;
281         }
282         if (getClass() != otherConcept.getClass()) {
283             return this.hashCode() - otherConcept.hashCode();
284         }
285
286         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
287         if (!super.equals(other)) {
288             return super.compareTo(other);
289         }
290
291         int retVal = PfUtils.compareObjects(properties, other.properties);
292         if (retVal != 0) {
293             return retVal;
294         }
295
296         retVal = PfUtils.compareObjects(targets, other.targets);
297         if (retVal != 0) {
298             return retVal;
299         }
300
301         return PfUtils.compareObjects(triggers, other.triggers);
302     }
303
304     @Override
305     public PfConcept copyTo(@NonNull PfConcept target) {
306         final Object copyObject = target;
307         Assertions.instanceOf(copyObject, PfConcept.class);
308
309         final JpaToscaPolicyType copy = ((JpaToscaPolicyType) copyObject);
310         super.copyTo(target);
311
312         if (properties == null) {
313             copy.setProperties(null);
314         } else {
315             final Map<String, JpaToscaProperty> newProperties = new LinkedHashMap<>();
316             for (final Entry<String, JpaToscaProperty> propertyEntry : properties.entrySet()) {
317                 newProperties.put(propertyEntry.getKey(), new JpaToscaProperty(propertyEntry.getValue()));
318             }
319             copy.setProperties(newProperties);
320         }
321
322         if (targets == null) {
323             copy.setTargets(null);
324         } else {
325             final List<PfConceptKey> newTargets = new ArrayList<>();
326             for (final PfConceptKey oldTarget : targets) {
327                 newTargets.add(new PfConceptKey(oldTarget));
328             }
329             copy.setTargets(newTargets);
330         }
331
332         if (triggers == null) {
333             copy.setTargets(null);
334         } else {
335             final List<JpaToscaTrigger> newTriggers = new ArrayList<>();
336             for (final JpaToscaTrigger trigger : triggers) {
337                 newTriggers.add(new JpaToscaTrigger(trigger));
338             }
339             copy.setTriggers(newTriggers);
340         }
341
342         return copy;
343     }
344 }