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