Changes for Checkstyle 8.32
[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-2020 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.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
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.Lob;
39 import javax.persistence.Table;
40 import javax.ws.rs.core.Response;
41 import lombok.Data;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
47 import org.onap.policy.models.base.PfAuthorative;
48 import org.onap.policy.models.base.PfConcept;
49 import org.onap.policy.models.base.PfConceptKey;
50 import org.onap.policy.models.base.PfKey;
51 import org.onap.policy.models.base.PfModelRuntimeException;
52 import org.onap.policy.models.base.PfUtils;
53 import org.onap.policy.models.base.PfValidationMessage;
54 import org.onap.policy.models.base.PfValidationResult;
55 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
57
58 /**
59  * Class to represent the policy in TOSCA definition.
60  *
61  * @author Chenfei Gao (cgao@research.att.com)
62  * @author Liam Fallon (liam.fallon@est.tech)
63  */
64 @Entity
65 @Table(name = "ToscaPolicy")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @Data
68 @EqualsAndHashCode(callSuper = true)
69 public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements PfAuthorative<ToscaPolicy> {
70     private static final long serialVersionUID = 3265174757061982805L;
71
72     // Tags for metadata
73     private static final String METADATA_POLICY_ID_TAG = "policy-id";
74     private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
75
76     // @formatter:off
77     @Column
78     @AttributeOverrides({
79         @AttributeOverride(name = "name",
80                            column = @Column(name = "type_name")),
81         @AttributeOverride(name = "version",
82                            column = @Column(name = "type_version"))
83         })
84     private PfConceptKey type;
85
86     @ElementCollection
87     @Lob
88     private Map<String, String> properties = new LinkedHashMap<>();
89
90     @ElementCollection
91     private List<PfConceptKey> targets = new ArrayList<>();
92     // @formatter:on
93
94     /**
95      * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
96      */
97     public JpaToscaPolicy() {
98         this(new PfConceptKey());
99     }
100
101     /**
102      * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
103      *
104      * @param key the key
105      */
106     public JpaToscaPolicy(@NonNull final PfConceptKey key) {
107         this(key, new PfConceptKey());
108     }
109
110     /**
111      * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
112      *
113      * @param key the key
114      * @param type the type of the policy
115      */
116     public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
117         super(key);
118         this.type = type;
119     }
120
121     /**
122      * Copy constructor.
123      *
124      * @param copyConcept the concept to copy from
125      */
126     public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
127         super(copyConcept);
128         this.type = new PfConceptKey(copyConcept.type);
129         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
130         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
131     }
132
133     /**
134      * Authorative constructor.
135      *
136      * @param authorativeConcept the authorative concept to copy from
137      */
138     public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
139         super(new PfConceptKey());
140         type = new PfConceptKey();
141         this.fromAuthorative(authorativeConcept);
142     }
143
144     @Override
145     public ToscaPolicy toAuthorative() {
146         ToscaPolicy toscaPolicy = new ToscaPolicy();
147         super.setToscaEntity(toscaPolicy);
148         super.toAuthorative();
149
150         toscaPolicy.setType(type.getName());
151
152         if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
153             toscaPolicy.setTypeVersion(type.getVersion());
154         } else {
155             toscaPolicy.setTypeVersion(null);
156         }
157
158         if (properties != null) {
159             Map<String, Object> propertyMap = new LinkedHashMap<>();
160
161             final StandardCoder coder = new StandardCoder();
162
163             for (Entry<String, String> entry : properties.entrySet()) {
164                 try {
165                     // TODO: This is a HACK, we need to validate the properties against their
166                     // TODO: their data type in their policy type definition in TOSCA, which means reading
167                     // TODO: the policy type from the database and parsing the property value object correctly
168                     // TODO: Here we are simply reading a JSON string from the database and deserializing the
169                     // TODO: property value from JSON
170                     propertyMap.put(entry.getKey(), coder.decode(entry.getValue(), Object.class));
171                 } catch (CoderException ce) {
172                     String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey()
173                         + ", value=" + entry.getValue();
174                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
175                 }
176             }
177
178             toscaPolicy.setProperties(propertyMap);
179         }
180
181         return toscaPolicy;
182     }
183
184     @Override
185     public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
186         super.fromAuthorative(toscaPolicy);
187
188         if (toscaPolicy.getType() != null) {
189             type.setName(toscaPolicy.getType());
190         } else {
191             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
192                 "PolicyType type not specified, the type of the PolicyType for this policy must be specified in "
193                     + "the type field");
194         }
195
196         if (toscaPolicy.getTypeVersion() != null) {
197             type.setVersion(toscaPolicy.getTypeVersion());
198         } else {
199             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
200                 "PolicyType version not specified, the version of the PolicyType for this policy must be specified in "
201                     + "the type_version field");
202         }
203
204         if (toscaPolicy.getProperties() != null) {
205             properties = new LinkedHashMap<>();
206
207             final StandardCoder coder = new StandardCoder();
208
209             for (Entry<String, Object> propertyEntry : toscaPolicy.getProperties().entrySet()) {
210                 // TODO: This is a HACK, we need to validate the properties against their
211                 // TODO: their data type in their policy type definition in TOSCA, which means reading
212                 // TODO: the policy type from the database and parsing the property value object correctly
213                 // TODO: Here we are simply serializing the property value into a string and storing it
214                 // TODO: unvalidated into the database
215                 try {
216                     properties.put(propertyEntry.getKey(), coder.encode(propertyEntry.getValue()));
217                 } catch (CoderException ce) {
218                     String errorMessage = "error encoding property JSON value for database: key="
219                         + propertyEntry.getKey() + ", value=" + propertyEntry.getValue();
220                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
221                 }
222             }
223         }
224
225         // Add the property metadata if it doesn't exist already
226         if (toscaPolicy.getMetadata() == null) {
227             setMetadata(new LinkedHashMap<>());
228         }
229
230         // Add the policy name and version fields to the metadata
231         getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
232         getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
233     }
234
235     @Override
236     public List<PfKey> getKeys() {
237         final List<PfKey> keyList = super.getKeys();
238
239         keyList.addAll(type.getKeys());
240
241         if (targets != null) {
242             keyList.addAll(targets);
243         }
244
245         return keyList;
246     }
247
248     @Override
249     public void clean() {
250         super.clean();
251
252         type.clean();
253
254         if (targets != null) {
255             for (PfConceptKey target : targets) {
256                 target.clean();
257             }
258         }
259     }
260
261     @Override
262     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
263         PfValidationResult result = super.validate(resultIn);
264
265         if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
266             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
267                 "key version is a null version"));
268         }
269
270         if (type == null || type.isNullKey()) {
271             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
272                 "type is null or a null key"));
273         } else {
274             result = type.validate(result);
275         }
276
277         if (properties != null) {
278             validateProperties(result);
279         }
280
281         if (targets != null) {
282             result = validateTargets(result);
283         }
284
285         return result;
286     }
287
288     /**
289      * Validate the policy properties.
290      *
291      * @param result where to put the validation results
292      */
293     private void validateProperties(final PfValidationResult result) {
294
295         for (Entry<String, String> propertyEntry : properties.entrySet()) {
296             if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
297                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
298                     "policy property key may not be null "));
299             } else if (propertyEntry.getValue() == null) {
300                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
301                     "policy property value may not be null "));
302             }
303         }
304     }
305
306     /**
307      * Validate the policy targets.
308      *
309      * @param result The result of validations up to now
310      * @return the validation result
311      */
312     private PfValidationResult validateTargets(final PfValidationResult resultIn) {
313         PfValidationResult result = resultIn;
314
315         for (PfConceptKey target : targets) {
316             if (target == null) {
317                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
318                     "policy target may not be null "));
319             } else {
320                 result = target.validate(result);
321             }
322         }
323         return result;
324     }
325
326     @Override
327     public int compareTo(final PfConcept otherConcept) {
328         if (otherConcept == null) {
329             return -1;
330         }
331
332         if (this == otherConcept) {
333             return 0;
334         }
335
336         if (getClass() != otherConcept.getClass()) {
337             return getClass().getName().compareTo(otherConcept.getClass().getName());
338         }
339
340         final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
341         if (!super.equals(other)) {
342             return super.compareTo(other);
343         }
344
345         if (!type.equals(other.type)) {
346             return type.compareTo(other.type);
347         }
348
349         int retVal = PfUtils.compareObjects(properties, other.properties);
350         if (retVal != 0) {
351             return retVal;
352         }
353
354         return PfUtils.compareObjects(targets, other.targets);
355     }
356 }