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