2 * ============LICENSE_START=======================================================
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
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.LinkedHashMap;
27 import java.util.List;
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;
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;
58 * Class to represent the policy in TOSCA definition.
60 * @author Chenfei Gao (cgao@research.att.com)
61 * @author Liam Fallon (liam.fallon@est.tech)
64 @Table(name = "ToscaPolicy")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @EqualsAndHashCode(callSuper = true)
68 public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements PfAuthorative<ToscaPolicy> {
69 private static final long serialVersionUID = 3265174757061982805L;
72 private static final String METADATA_POLICY_ID_TAG = "policy-id";
73 private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
78 @AttributeOverride(name = "name",
79 column = @Column(name = "type_name")),
80 @AttributeOverride(name = "version",
81 column = @Column(name = "type_version"))
83 private PfConceptKey type;
87 private Map<String, String> properties;
90 private List<PfConceptKey> targets;
94 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
96 public JpaToscaPolicy() {
97 this(new PfConceptKey());
101 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
105 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
106 this(key, new PfConceptKey());
110 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
113 * @param type the type of the policy
115 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
123 * @param copyConcept the concept to copy from
125 public JpaToscaPolicy(@NonNull final JpaToscaPolicy 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);
133 * Authorative constructor.
135 * @param authorativeConcept the authorative concept to copy from
137 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
138 super(new PfConceptKey());
139 type = new PfConceptKey();
140 this.fromAuthorative(authorativeConcept);
144 public ToscaPolicy toAuthorative() {
145 ToscaPolicy toscaPolicy = new ToscaPolicy();
146 super.setToscaEntity(toscaPolicy);
147 super.toAuthorative();
149 toscaPolicy.setType(type.getName());
151 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
152 toscaPolicy.setTypeVersion(type.getVersion());
154 toscaPolicy.setTypeVersion(null);
157 if (properties != null) {
158 Map<String, Object> propertyMap = new LinkedHashMap<>();
160 final StandardCoder coder = new StandardCoder();
162 for (Entry<String, String> entry : properties.entrySet()) {
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);
177 toscaPolicy.setProperties(propertyMap);
184 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
185 super.fromAuthorative(toscaPolicy);
187 if (toscaPolicy.getType() != null) {
188 type.setName(toscaPolicy.getType());
191 type.setName(PfKey.NULL_KEY_NAME);
194 if (toscaPolicy.getTypeVersion() != null) {
195 type.setVersion(toscaPolicy.getTypeVersion());
198 type.setVersion(PfKey.NULL_KEY_VERSION);
201 if (toscaPolicy.getProperties() != null) {
202 properties = new LinkedHashMap<>();
204 final StandardCoder coder = new StandardCoder();
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
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);
222 // Add the property metadata if it doesn't exist already
223 if (toscaPolicy.getMetadata() == null) {
224 setMetadata(new LinkedHashMap<>());
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()));
233 public List<PfKey> getKeys() {
234 final List<PfKey> keyList = super.getKeys();
236 keyList.addAll(type.getKeys());
238 if (targets != null) {
239 keyList.addAll(targets);
246 public void clean() {
251 if (targets != null) {
252 for (PfConceptKey target : targets) {
259 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
260 PfValidationResult result = super.validate(resultIn);
262 if (type == null || type.isNullKey()) {
263 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
264 "type is null or a null key"));
266 result = type.validate(result);
269 if (properties != null) {
270 result = validateProperties(result);
273 if (targets != null) {
274 result = validateTargets(result);
281 * Validate the policy properties.
283 * @param result The result of validations up to now
284 * @return the validation result
286 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
287 PfValidationResult result = resultIn;
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 "));
302 * Validate the policy targets.
304 * @param result The result of validations up to now
305 * @return the validation result
307 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
308 PfValidationResult result = resultIn;
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 "));
315 result = target.validate(result);
322 public int compareTo(final PfConcept otherConcept) {
323 if (otherConcept == null) {
327 if (this == otherConcept) {
331 if (getClass() != otherConcept.getClass()) {
332 return getClass().getName().compareTo(otherConcept.getClass().getName());
335 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
336 if (!super.equals(other)) {
337 return super.compareTo(other);
340 if (!type.equals(other.type)) {
341 return type.compareTo(other.type);
344 int retVal = PfUtils.compareObjects(properties, other.properties);
349 return PfUtils.compareObjects(targets, other.targets);