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.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
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;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
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;
63 * Class to represent the policy in TOSCA definition.
65 * @author Chenfei Gao (cgao@research.att.com)
66 * @author Liam Fallon (liam.fallon@est.tech)
69 @Table(name = "ToscaPolicy")
70 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
72 @EqualsAndHashCode(callSuper = true)
73 public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements PfAuthorative<ToscaPolicy> {
74 private static final long serialVersionUID = 3265174757061982805L;
77 private static final String METADATA_POLICY_ID_TAG = "policy-id";
78 private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
83 @AttributeOverride(name = "name",
84 column = @Column(name = "type_name")),
85 @AttributeOverride(name = "version",
86 column = @Column(name = "type_version"))
88 private PfConceptKey type;
92 private Map<String, String> properties;
95 private List<PfConceptKey> targets;
99 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
101 public JpaToscaPolicy() {
102 this(new PfConceptKey());
106 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
110 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
111 this(key, new PfConceptKey());
115 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
118 * @param type the type of the policy
120 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
128 * @param copyConcept the concept to copy from
130 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
135 * Authorative constructor.
137 * @param authorativeConcept the authorative concept to copy from
139 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
140 super(new PfConceptKey());
141 type = new PfConceptKey();
142 this.fromAuthorative(authorativeConcept);
146 public ToscaPolicy toAuthorative() {
147 ToscaPolicy toscaPolicy = new ToscaPolicy();
148 super.setToscaEntity(toscaPolicy);
149 super.toAuthorative();
151 toscaPolicy.setType(type.getName());
153 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
154 toscaPolicy.setTypeVersion(type.getVersion());
156 toscaPolicy.setTypeVersion(null);
159 if (properties != null) {
160 Map<String, Object> propertyMap = new LinkedHashMap<>();
162 final StandardCoder coder = new StandardCoder();
164 for (Entry<String, String> entry : properties.entrySet()) {
166 // TODO: This is a HACK, we need to validate the properties against their
167 // TODO: their data type in their policy type definition in TOSCA, which means reading
168 // TODO: the policy type from the database and parsing the property value object correctly
169 // TODO: Here we are simply reading a JSON string from the database and deserializing the
170 // TODO: property value from JSON
171 propertyMap.put(entry.getKey(), coder.decode(entry.getValue(), Object.class));
172 } catch (CoderException ce) {
173 String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey()
174 + ", value=" + entry.getValue();
175 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
179 toscaPolicy.setProperties(propertyMap);
186 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
187 super.fromAuthorative(toscaPolicy);
189 if (toscaPolicy.getType() != null) {
190 type.setName(toscaPolicy.getType());
193 type.setName(PfKey.NULL_KEY_NAME);
196 if (toscaPolicy.getTypeVersion() != null) {
197 type.setVersion(toscaPolicy.getTypeVersion());
200 type.setVersion(PfKey.NULL_KEY_VERSION);
203 if (toscaPolicy.getProperties() != null) {
204 properties = new LinkedHashMap<>();
206 final StandardCoder coder = new StandardCoder();
208 for (Entry<String, Object> propertyEntry : toscaPolicy.getProperties().entrySet()) {
209 // TODO: This is a HACK, we need to validate the properties against their
210 // TODO: their data type in their policy type definition in TOSCA, which means reading
211 // TODO: the policy type from the database and parsing the property value object correctly
212 // TODO: Here we are simply serializing the property value into a string and storing it
213 // TODO: unvalidated into the database
215 properties.put(propertyEntry.getKey(), coder.encode(propertyEntry.getValue()));
216 } catch (CoderException ce) {
217 String errorMessage = "error encoding property JSON value for database: key="
218 + propertyEntry.getKey() + ", value=" + propertyEntry.getValue();
219 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
224 // Add the property metadata if it doesn't exist already
225 if (toscaPolicy.getMetadata() == null) {
226 setMetadata(new LinkedHashMap<>());
229 // Add the policy name and version fields to the metadata
230 getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
231 getMetadata().put(METADATA_POLICY_VERSION_TAG, Integer.toString(getKey().getMajorVersion()));
235 public List<PfKey> getKeys() {
236 final List<PfKey> keyList = super.getKeys();
238 keyList.addAll(type.getKeys());
240 if (targets != null) {
241 keyList.addAll(targets);
248 public void clean() {
253 if (targets != null) {
254 for (PfConceptKey target : targets) {
261 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
262 PfValidationResult result = super.validate(resultIn);
264 if (type == null || type.isNullKey()) {
265 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
266 "type is null or a null key"));
268 result = type.validate(result);
271 if (properties != null) {
272 result = validateProperties(result);
275 if (targets != null) {
276 result = validateTargets(result);
283 * Validate the policy properties.
285 * @param result The result of validations up to now
286 * @return the validation result
288 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
289 PfValidationResult result = resultIn;
291 for (Entry<String, String> propertyEntry : properties.entrySet()) {
292 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
293 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
294 "policy property key may not be null "));
295 } else if (propertyEntry.getValue() == null) {
296 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
297 "policy property value may not be null "));
304 * Validate the policy targets.
306 * @param result The result of validations up to now
307 * @return the validation result
309 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
310 PfValidationResult result = resultIn;
312 for (PfConceptKey target : targets) {
313 if (target == null) {
314 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
315 "policy target may not be null "));
317 result = target.validate(result);
324 public int compareTo(final PfConcept otherConcept) {
325 if (otherConcept == null) {
329 if (this == otherConcept) {
333 if (getClass() != otherConcept.getClass()) {
334 return getClass().getName().compareTo(otherConcept.getClass().getName());
337 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
338 if (!super.equals(other)) {
339 return super.compareTo(other);
342 if (!type.equals(other.type)) {
343 return type.compareTo(other.type);
346 int retVal = PfUtils.compareObjects(properties, other.properties);
351 return PfUtils.compareObjects(targets, other.targets);
355 public PfConcept copyTo(@NonNull PfConcept target) {
356 final Object copyObject = target;
357 Assertions.instanceOf(copyObject, PfConcept.class);
359 final JpaToscaPolicy copy = ((JpaToscaPolicy) copyObject);
360 super.copyTo(target);
362 copy.setType(new PfConceptKey(type));
364 if (properties == null) {
365 copy.setProperties(null);
367 copy.setProperties(properties);
370 if (targets == null) {
371 copy.setTargets(null);
373 final List<PfConceptKey> newTargets = new ArrayList<>();
374 for (final PfConceptKey oldTarget : targets) {
375 newTargets.add(new PfConceptKey(oldTarget));
377 copy.setTargets(newTargets);