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