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