Implement validation and hierarchical get
[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 (properties != null) {
207             result = validateProperties(result);
208         }
209
210         if (targets != null) {
211             result = validateTargets(result);
212         }
213
214         if (triggers != null) {
215             result = validateTriggers(result);
216         }
217
218         return result;
219     }
220
221     /**
222      * Validate the policy properties.
223      *
224      * @param result The result of validations up to now
225      * @return the validation result
226      */
227     private PfValidationResult validateProperties(final PfValidationResult resultIn) {
228         PfValidationResult result = resultIn;
229
230         for (JpaToscaProperty property : properties.values()) {
231             if (property == null) {
232                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
233                         "policy property may not be null "));
234             } else {
235                 result = property.validate(result);
236             }
237         }
238         return result;
239     }
240
241     /**
242      * Validate the policy targets.
243      *
244      * @param result The result of validations up to now
245      * @return the validation result
246      */
247     private PfValidationResult validateTargets(final PfValidationResult resultIn) {
248         PfValidationResult result = resultIn;
249
250         for (PfConceptKey target : targets) {
251             if (target == null) {
252                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
253                         "policy target may not be null "));
254             } else {
255                 result = target.validate(result);
256             }
257         }
258         return result;
259     }
260
261     /**
262      * Validate the policy triggers.
263      *
264      * @param result The result of validations up to now
265      * @return the validation result
266      */
267     private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
268         PfValidationResult result = resultIn;
269
270         for (JpaToscaTrigger trigger : triggers) {
271             if (trigger == null) {
272                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
273                         "policy trigger may not be null "));
274             } else {
275                 result = trigger.validate(result);
276             }
277         }
278         return result;
279     }
280
281     @Override
282     public int compareTo(final PfConcept otherConcept) {
283         if (otherConcept == null) {
284             return -1;
285         }
286         if (this == otherConcept) {
287             return 0;
288         }
289         if (getClass() != otherConcept.getClass()) {
290             return getClass().getName().compareTo(otherConcept.getClass().getName());
291         }
292
293         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
294         if (!super.equals(other)) {
295             return super.compareTo(other);
296         }
297
298         int retVal = PfUtils.compareObjects(properties, other.properties);
299         if (retVal != 0) {
300             return retVal;
301         }
302
303         retVal = PfUtils.compareObjects(targets, other.targets);
304         if (retVal != 0) {
305             return retVal;
306         }
307
308         return PfUtils.compareObjects(triggers, other.triggers);
309     }
310
311     /**
312      * Get the data types referenced in a policy type.
313      *
314      * @return the data types referenced in a policy type
315      */
316     public Collection<PfConceptKey> getReferencedDataTypes() {
317         if (properties == null) {
318             return CollectionUtils.emptyCollection();
319         }
320
321         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
322
323         for (JpaToscaProperty property : properties.values()) {
324             referencedDataTypes.add(property.getType());
325
326             if (property.getEntrySchema() != null) {
327                 referencedDataTypes.add(property.getEntrySchema().getType());
328             }
329         }
330
331         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
332
333         return referencedDataTypes;
334     }
335 }