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