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;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
46 import org.onap.policy.common.utils.validation.Assertions;
47 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
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;
75 @AttributeOverride(name = "name",
76 column = @Column(name = "type_name")),
77 @AttributeOverride(name = "version",
78 column = @Column(name = "type_version"))
80 private PfConceptKey type;
84 private Map<String, String> properties;
87 private List<PfConceptKey> targets;
91 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
93 public JpaToscaPolicy() {
94 this(new PfConceptKey());
98 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
102 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
103 this(key, new PfConceptKey());
107 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
110 * @param type the type of the policy
112 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
120 * @param copyConcept the concept to copy from
122 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
127 * Authorative constructor.
129 * @param authorativeConcept the authorative concept to copy from
131 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
132 this.fromAuthorative(authorativeConcept);
136 public ToscaPolicy toAuthorative() {
137 ToscaPolicy toscaPolicy = new ToscaPolicy();
138 super.setToscaEntity(toscaPolicy);
139 super.toAuthorative();
141 toscaPolicy.setType(type.getName());
143 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
144 toscaPolicy.setTypeVersion(type.getVersion());
147 toscaPolicy.setTypeVersion(null);
150 if (properties != null) {
151 Map<String, Object> propertyMap = new LinkedHashMap<>();
153 for (Entry<String, String> entry : properties.entrySet()) {
154 propertyMap.put(entry.getKey(), entry.getValue());
157 toscaPolicy.setProperties(propertyMap);
164 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
165 super.fromAuthorative(toscaPolicy);
167 type.setName(toscaPolicy.getType());
168 type.setVersion(toscaPolicy.getTypeVersion());
169 if (type.getVersion() == null) {
170 type.setVersion(PfKey.NULL_KEY_VERSION);
173 if (toscaPolicy.getProperties() != null) {
174 properties = new LinkedHashMap<>();
176 for (Entry<String, Object> propertyEntry : toscaPolicy.getProperties().entrySet()) {
177 // TODO: This is a HACK, we need to validate the properties against their
178 // TODO: their data type in their policy type definition in TOSCA, which means reading
179 // TODO: the policy type from the database and parsing the property value object correctly
180 // TODO: Here we are simply serializing the property value into a string and storing it
181 // TODO: unvalidated into the database
182 properties.put(propertyEntry.getKey(), propertyEntry.getValue().toString());
188 public List<PfKey> getKeys() {
189 final List<PfKey> keyList = super.getKeys();
191 keyList.addAll(type.getKeys());
193 if (targets != null) {
194 keyList.addAll(targets);
201 public void clean() {
206 if (targets != null) {
207 for (PfConceptKey target : targets) {
214 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
215 PfValidationResult result = super.validate(resultIn);
217 if (type == null || type.isNullKey()) {
218 result.addValidationMessage(new PfValidationMessage(type, this.getClass(), ValidationResult.INVALID,
219 "type is null or a null key"));
221 result = type.validate(result);
224 if (properties != null) {
225 result = validateProperties(result);
228 if (targets != null) {
229 result = validateTargets(result);
236 * Validate the policy properties.
238 * @param result The result of validations up to now
239 * @return the validation result
241 private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) {
242 PfValidationResult result = resultIn;
244 for (Entry<String, String> propertyEntry : properties.entrySet()) {
245 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
246 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
247 "policy property key may not be null "));
248 } else if (propertyEntry.getValue() == null) {
249 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
250 "policy property value may not be null "));
257 * Validate the policy targets.
259 * @param result The result of validations up to now
260 * @return the validation result
262 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
263 PfValidationResult result = resultIn;
265 for (PfConceptKey target : targets) {
266 if (target == null) {
267 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
268 "policy target may not be null "));
270 result = target.validate(result);
277 public int compareTo(final PfConcept otherConcept) {
278 if (otherConcept == null) {
282 if (this == otherConcept) {
286 if (getClass() != otherConcept.getClass()) {
287 return this.hashCode() - otherConcept.hashCode();
290 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
291 if (!super.equals(other)) {
292 return super.compareTo(other);
295 if (!type.equals(other.type)) {
296 return type.compareTo(other.type);
299 int retVal = PfUtils.compareObjects(properties, other.properties);
304 return PfUtils.compareObjects(targets, other.targets);
308 public PfConcept copyTo(@NonNull PfConcept target) {
309 final Object copyObject = target;
310 Assertions.instanceOf(copyObject, PfConcept.class);
312 final JpaToscaPolicy copy = ((JpaToscaPolicy) copyObject);
313 super.copyTo(target);
315 copy.setType(new PfConceptKey(type));
317 if (properties == null) {
318 copy.setProperties(null);
320 copy.setProperties(properties);
323 if (targets == null) {
324 copy.setTargets(null);
326 final List<PfConceptKey> newTargets = new ArrayList<>();
327 for (final PfConceptKey oldTarget : targets) {
328 newTargets.add(new PfConceptKey(oldTarget));
330 copy.setTargets(newTargets);