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.Table;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
45 import org.onap.policy.common.utils.validation.Assertions;
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.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;
74 @AttributeOverride(name = "name",
75 column = @Column(name = "type_name")),
76 @AttributeOverride(name = "version",
77 column = @Column(name = "type_version"))
79 private PfConceptKey type;
82 @Column(length = 10000)
83 private Map<String, String> properties;
86 private List<PfConceptKey> targets;
90 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
92 public JpaToscaPolicy() {
93 this(new PfConceptKey());
97 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
101 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
102 this(key, new PfConceptKey());
106 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
109 * @param type the type of the policy
111 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
119 * @param copyConcept the concept to copy from
121 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
126 * Authorative constructor.
128 * @param authorativeConcept the authorative concept to copy from
130 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
131 this.fromAuthorative(authorativeConcept);
135 public ToscaPolicy toAuthorative() {
136 ToscaPolicy toscaPolicy = new ToscaPolicy();
137 super.setToscaEntity(toscaPolicy);
138 super.toAuthorative();
140 toscaPolicy.setType(type.getName());
142 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
143 toscaPolicy.setTypeVersion(type.getVersion());
146 toscaPolicy.setTypeVersion(null);
149 if (properties != null) {
150 Map<String, Object> propertyMap = new LinkedHashMap<>();
152 for (Entry<String, String> entry : properties.entrySet()) {
153 propertyMap.put(entry.getKey(), entry.getValue());
156 toscaPolicy.setProperties(propertyMap);
163 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
164 super.fromAuthorative(toscaPolicy);
166 type.setName(toscaPolicy.getType());
167 type.setVersion(toscaPolicy.getTypeVersion());
168 if (type.getVersion() == null) {
169 type.setVersion(PfKey.NULL_KEY_VERSION);
172 if (toscaPolicy.getProperties() != null) {
173 properties = new LinkedHashMap<>();
175 for (Entry<String, Object> propertyEntry : toscaPolicy.getProperties().entrySet()) {
176 // TODO: This is a HACK, we need to validate the properties against their
177 // TODO: their data type in their policy type definition in TOSCA, which means reading
178 // TODO: the policy type from the database and parsing the property value object correctly
179 // TODO: Here we are simply serializing the property value into a string and storing it
180 // TODO: unvalidated into the database
181 properties.put(propertyEntry.getKey(), propertyEntry.getValue().toString());
187 public List<PfKey> getKeys() {
188 final List<PfKey> keyList = super.getKeys();
190 keyList.addAll(type.getKeys());
192 if (targets != null) {
193 keyList.addAll(targets);
200 public void clean() {
205 if (targets != null) {
206 for (PfConceptKey target : targets) {
213 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
214 PfValidationResult result = super.validate(resultIn);
216 if (type == null || type.isNullKey()) {
217 result.addValidationMessage(new PfValidationMessage(type, this.getClass(), ValidationResult.INVALID,
218 "type is null or a null key"));
220 result = type.validate(result);
223 if (properties != null) {
224 result = validateProperties(result);
227 if (targets != null) {
228 result = validateTargets(result);
235 * Validate the policy properties.
237 * @param result The result of validations up to now
238 * @return the validation result
240 private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) {
241 PfValidationResult result = resultIn;
243 for (Entry<String, String> propertyEntry : properties.entrySet()) {
244 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
245 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
246 "policy property key may not be null "));
247 } else if (propertyEntry.getValue() == null) {
248 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
249 "policy property value may not be null "));
256 * Validate the policy targets.
258 * @param result The result of validations up to now
259 * @return the validation result
261 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
262 PfValidationResult result = resultIn;
264 for (PfConceptKey target : targets) {
265 if (target == null) {
266 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
267 "policy target may not be null "));
269 result = target.validate(result);
276 public int compareTo(final PfConcept otherConcept) {
277 if (otherConcept == null) {
281 if (this == otherConcept) {
285 if (getClass() != otherConcept.getClass()) {
286 return this.hashCode() - otherConcept.hashCode();
289 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
290 if (!super.equals(other)) {
291 return super.compareTo(other);
294 if (!type.equals(other.type)) {
295 return type.compareTo(other.type);
298 int retVal = PfUtils.compareObjects(properties, other.properties);
303 return PfUtils.compareObjects(targets, other.targets);
307 public PfConcept copyTo(@NonNull PfConcept target) {
308 final Object copyObject = target;
309 Assertions.instanceOf(copyObject, PfConcept.class);
311 final JpaToscaPolicy copy = ((JpaToscaPolicy) copyObject);
312 super.copyTo(target);
314 copy.setType(new PfConceptKey(type));
316 if (properties == null) {
317 copy.setProperties(null);
319 copy.setProperties(properties);
322 if (targets == null) {
323 copy.setTargets(null);
325 final List<PfConceptKey> newTargets = new ArrayList<>();
326 for (final PfConceptKey oldTarget : targets) {
327 newTargets.add(new PfConceptKey(oldTarget));
329 copy.setTargets(newTargets);