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