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