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