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