Update models-tosca for hetter handling
[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-2020 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.Collection;
28 import java.util.LinkedHashMap;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34
35 import javax.persistence.ElementCollection;
36 import javax.persistence.Entity;
37 import javax.persistence.Inheritance;
38 import javax.persistence.InheritanceType;
39 import javax.persistence.Lob;
40 import javax.persistence.Table;
41
42 import lombok.Data;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
45
46 import org.apache.commons.collections4.CollectionUtils;
47 import org.onap.policy.models.base.PfAuthorative;
48 import org.onap.policy.models.base.PfConcept;
49 import org.onap.policy.models.base.PfConceptKey;
50 import org.onap.policy.models.base.PfKey;
51 import org.onap.policy.models.base.PfReferenceKey;
52 import org.onap.policy.models.base.PfUtils;
53 import org.onap.policy.models.base.PfValidationMessage;
54 import org.onap.policy.models.base.PfValidationResult;
55 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
58 import org.onap.policy.models.tosca.utils.ToscaUtils;
59
60 /**
61  * Class to represent the policy type in TOSCA definition.
62  *
63  * @author Chenfei Gao (cgao@research.att.com)
64  * @author Liam Fallon (liam.fallon@est.tech)
65  */
66
67 @Entity
68 @Table(name = "ToscaPolicyType")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @Data
71 @EqualsAndHashCode(callSuper = true)
72 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
73     private static final long serialVersionUID = -563659852901842616L;
74
75     @ElementCollection
76     @Lob
77     private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
78
79     @ElementCollection
80     private List<PfConceptKey> targets = new ArrayList<>();
81
82     @ElementCollection
83     private List<JpaToscaTrigger> triggers = new ArrayList<>();
84
85     /**
86      * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
87      */
88     public JpaToscaPolicyType() {
89         this(new PfConceptKey());
90     }
91
92     /**
93      * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
94      *
95      * @param key the key
96      */
97     public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
98         super(key);
99     }
100
101     /**
102      * Copy constructor.
103      *
104      * @param copyConcept the concept to copy from
105      */
106     public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
107         super(copyConcept);
108         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
109         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
110         this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
111     }
112
113     /**
114      * Authorative constructor.
115      *
116      * @param authorativeConcept the authorative concept to copy from
117      */
118     public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
119         this.fromAuthorative(authorativeConcept);
120     }
121
122     @Override
123     public ToscaPolicyType toAuthorative() {
124         ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
125         super.setToscaEntity(toscaPolicyType);
126         super.toAuthorative();
127
128         if (properties != null) {
129             Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
130
131             for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
132                 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
133             }
134
135             toscaPolicyType.setProperties(propertyMap);
136         }
137
138         return toscaPolicyType;
139     }
140
141     @Override
142     public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
143         super.fromAuthorative(toscaPolicyType);
144
145         // Set properties
146         if (toscaPolicyType.getProperties() != null) {
147             properties = new LinkedHashMap<>();
148             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
149                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
150                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
151                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
152             }
153         }
154     }
155
156     @Override
157     public List<PfKey> getKeys() {
158         final List<PfKey> keyList = super.getKeys();
159
160         if (properties != null) {
161             for (JpaToscaProperty property : properties.values()) {
162                 keyList.addAll(property.getKeys());
163             }
164         }
165
166         if (targets != null) {
167             keyList.addAll(targets);
168         }
169
170         if (triggers != null) {
171             for (JpaToscaTrigger trigger : triggers) {
172                 keyList.addAll(trigger.getKeys());
173             }
174         }
175
176         return keyList;
177     }
178
179     @Override
180     public void clean() {
181         super.clean();
182
183         if (properties != null) {
184             for (JpaToscaProperty property : properties.values()) {
185                 property.clean();
186             }
187         }
188
189         if (targets != null) {
190             for (PfConceptKey target : targets) {
191                 target.clean();
192             }
193         }
194
195         if (triggers != null) {
196             for (JpaToscaTrigger trigger : triggers) {
197                 trigger.clean();
198             }
199         }
200     }
201
202     @Override
203     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
204         PfValidationResult result = super.validate(resultIn);
205
206         if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
207             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
208                     "key version is a null version"));
209         }
210
211         if (properties != null) {
212             result = validateProperties(result);
213         }
214
215         if (targets != null) {
216             result = validateTargets(result);
217         }
218
219         if (triggers != null) {
220             result = validateTriggers(result);
221         }
222
223         return result;
224     }
225
226     /**
227      * Validate the policy properties.
228      *
229      * @param result The result of validations up to now
230      * @return the validation result
231      */
232     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
233         PfValidationResult result = resultIn;
234
235         for (JpaToscaProperty property : properties.values()) {
236             if (property == null) {
237                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
238                         "policy property may not be null "));
239             } else {
240                 result = property.validate(result);
241             }
242         }
243         return result;
244     }
245
246     /**
247      * Validate the policy targets.
248      *
249      * @param result The result of validations up to now
250      * @return the validation result
251      */
252     private PfValidationResult validateTargets(final PfValidationResult resultIn) {
253         PfValidationResult result = resultIn;
254
255         for (PfConceptKey target : targets) {
256             if (target == null) {
257                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
258                         "policy target may not be null "));
259             } else {
260                 result = target.validate(result);
261             }
262         }
263         return result;
264     }
265
266     /**
267      * Validate the policy triggers.
268      *
269      * @param result The result of validations up to now
270      * @return the validation result
271      */
272     private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
273         PfValidationResult result = resultIn;
274
275         for (JpaToscaTrigger trigger : triggers) {
276             if (trigger == null) {
277                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
278                         "policy trigger may not be null "));
279             } else {
280                 result = trigger.validate(result);
281             }
282         }
283         return result;
284     }
285
286     @Override
287     public int compareTo(final PfConcept otherConcept) {
288         if (otherConcept == null) {
289             return -1;
290         }
291         if (this == otherConcept) {
292             return 0;
293         }
294         if (getClass() != otherConcept.getClass()) {
295             return getClass().getName().compareTo(otherConcept.getClass().getName());
296         }
297
298         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
299         if (!super.equals(other)) {
300             return super.compareTo(other);
301         }
302
303         int retVal = PfUtils.compareObjects(properties, other.properties);
304         if (retVal != 0) {
305             return retVal;
306         }
307
308         retVal = PfUtils.compareObjects(targets, other.targets);
309         if (retVal != 0) {
310             return retVal;
311         }
312
313         return PfUtils.compareObjects(triggers, other.triggers);
314     }
315
316     /**
317      * Get the data types referenced in a policy type.
318      *
319      * @return the data types referenced in a policy type
320      */
321     public Collection<PfConceptKey> getReferencedDataTypes() {
322         if (properties == null) {
323             return CollectionUtils.emptyCollection();
324         }
325
326         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
327
328         for (JpaToscaProperty property : properties.values()) {
329             referencedDataTypes.add(property.getType());
330
331             if (property.getEntrySchema() != null) {
332                 referencedDataTypes.add(property.getEntrySchema().getType());
333             }
334         }
335
336         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
337
338         return referencedDataTypes;
339     }
340 }