2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.simple.concepts;
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
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;
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;
59 * Class to represent the policy in TOSCA definition.
61 * @author Chenfei Gao (cgao@research.att.com)
62 * @author Liam Fallon (liam.fallon@est.tech)
65 @Table(name = "ToscaPolicy")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
68 @EqualsAndHashCode(callSuper = true)
69 public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements PfAuthorative<ToscaPolicy> {
70 private static final long serialVersionUID = 3265174757061982805L;
73 private static final String METADATA_POLICY_ID_TAG = "policy-id";
74 private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
79 @AttributeOverride(name = "name",
80 column = @Column(name = "type_name")),
81 @AttributeOverride(name = "version",
82 column = @Column(name = "type_version"))
84 private PfConceptKey type;
88 private Map<String, String> properties = new LinkedHashMap<>();
91 private List<PfConceptKey> targets = new ArrayList<>();
95 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
97 public JpaToscaPolicy() {
98 this(new PfConceptKey());
102 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
106 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
107 this(key, new PfConceptKey());
111 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
114 * @param type the type of the policy
116 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
124 * @param copyConcept the concept to copy from
126 public JpaToscaPolicy(@NonNull final JpaToscaPolicy 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);
134 * Authorative constructor.
136 * @param authorativeConcept the authorative concept to copy from
138 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
139 super(new PfConceptKey());
140 type = new PfConceptKey();
141 this.fromAuthorative(authorativeConcept);
145 public ToscaPolicy toAuthorative() {
146 ToscaPolicy toscaPolicy = new ToscaPolicy();
147 super.setToscaEntity(toscaPolicy);
148 super.toAuthorative();
150 toscaPolicy.setType(type.getName());
152 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
153 toscaPolicy.setTypeVersion(type.getVersion());
155 toscaPolicy.setTypeVersion(null);
158 if (properties != null) {
159 Map<String, Object> propertyMap = new LinkedHashMap<>();
161 final StandardCoder coder = new StandardCoder();
163 for (Entry<String, String> entry : properties.entrySet()) {
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);
178 toscaPolicy.setProperties(propertyMap);
185 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
186 super.fromAuthorative(toscaPolicy);
188 if (toscaPolicy.getType() != null) {
189 type.setName(toscaPolicy.getType());
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 "
196 if (toscaPolicy.getTypeVersion() != null) {
197 type.setVersion(toscaPolicy.getTypeVersion());
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");
204 if (toscaPolicy.getProperties() != null) {
205 properties = new LinkedHashMap<>();
207 final StandardCoder coder = new StandardCoder();
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
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);
225 // Add the property metadata if it doesn't exist already
226 if (toscaPolicy.getMetadata() == null) {
227 setMetadata(new LinkedHashMap<>());
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());
236 public List<PfKey> getKeys() {
237 final List<PfKey> keyList = super.getKeys();
239 keyList.addAll(type.getKeys());
241 if (targets != null) {
242 keyList.addAll(targets);
249 public void clean() {
254 if (targets != null) {
255 for (PfConceptKey target : targets) {
262 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
263 PfValidationResult result = super.validate(resultIn);
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"));
270 if (type == null || type.isNullKey()) {
271 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
272 "type is null or a null key"));
274 result = type.validate(result);
277 if (properties != null) {
278 validateProperties(result);
281 if (targets != null) {
282 result = validateTargets(result);
289 * Validate the policy properties.
291 * @param result where to put the validation results
293 private void validateProperties(final PfValidationResult result) {
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 "));
307 * Validate the policy targets.
309 * @param result The result of validations up to now
310 * @return the validation result
312 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
313 PfValidationResult result = resultIn;
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 "));
320 result = target.validate(result);
327 public int compareTo(final PfConcept otherConcept) {
328 if (otherConcept == null) {
332 if (this == otherConcept) {
336 if (getClass() != otherConcept.getClass()) {
337 return getClass().getName().compareTo(otherConcept.getClass().getName());
340 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
341 if (!super.equals(other)) {
342 return super.compareTo(other);
345 if (!type.equals(other.type)) {
346 return type.compareTo(other.type);
349 int retVal = PfUtils.compareObjects(properties, other.properties);
354 return PfUtils.compareObjects(targets, other.targets);