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.List;
29 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.Table;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
44 import org.onap.policy.common.utils.validation.Assertions;
45 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfConceptKey;
48 import org.onap.policy.models.base.PfKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.PfValidationMessage;
51 import org.onap.policy.models.base.PfValidationResult;
52 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
55 * Class to represent the policy in TOSCA definition.
57 * @author Chenfei Gao (cgao@research.att.com)
58 * @author Liam Fallon (liam.fallon@est.tech)
61 @Table(name = "ToscaPolicy")
62 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @EqualsAndHashCode(callSuper = true)
65 public class JpaToscaPolicy extends JpaToscaEntityType {
66 private static final long serialVersionUID = 3265174757061982805L;
71 @AttributeOverride(name = "name",
72 column = @Column(name = "type_name")),
73 @AttributeOverride(name = "version",
74 column = @Column(name = "type_version"))
76 private PfConceptKey type;
79 @Column(length = 10000)
80 private Map<String, String> properties;
83 private List<PfConceptKey> targets;
87 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
89 public JpaToscaPolicy() {
90 this(new PfConceptKey());
94 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
98 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
99 this(key, new PfConceptKey());
103 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
106 * @param type the type of the policy
108 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
116 * @param copyConcept the concept to copy from
118 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
124 public List<PfKey> getKeys() {
125 final List<PfKey> keyList = super.getKeys();
127 keyList.addAll(type.getKeys());
129 if (targets != null) {
130 keyList.addAll(targets);
137 public void clean() {
142 if (targets != null) {
143 for (PfConceptKey target : targets) {
150 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
151 PfValidationResult result = super.validate(resultIn);
153 if (type == null || type.isNullKey()) {
154 result.addValidationMessage(new PfValidationMessage(type, this.getClass(), ValidationResult.INVALID,
155 "type is null or a null key"));
157 result = type.validate(result);
160 if (properties != null) {
161 result = validateProperties(result);
164 if (targets != null) {
165 result = validateTargets(result);
172 * Validate the policy properties.
174 * @param result The result of validations up to now
175 * @return the validation result
177 private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) {
178 PfValidationResult result = resultIn;
180 for (Entry<String, String> propertyEntry : properties.entrySet()) {
181 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
182 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
183 "policy property key may not be null "));
184 } else if (propertyEntry.getValue() == null) {
185 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
186 "policy property value may not be null "));
193 * Validate the policy targets.
195 * @param result The result of validations up to now
196 * @return the validation result
198 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
199 PfValidationResult result = resultIn;
201 for (PfConceptKey target : targets) {
202 if (target == null) {
203 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
204 "policy target may not be null "));
206 result = target.validate(result);
213 public int compareTo(final PfConcept otherConcept) {
214 if (otherConcept == null) {
218 if (this == otherConcept) {
222 if (getClass() != otherConcept.getClass()) {
223 return this.hashCode() - otherConcept.hashCode();
226 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
227 if (!super.equals(other)) {
228 return super.compareTo(other);
231 if (!type.equals(other.type)) {
232 return type.compareTo(other.type);
235 int retVal = PfUtils.compareObjects(properties, other.properties);
240 return PfUtils.compareObjects(targets, other.targets);
244 public PfConcept copyTo(@NonNull PfConcept target) {
245 final Object copyObject = target;
246 Assertions.instanceOf(copyObject, PfConcept.class);
248 final JpaToscaPolicy copy = ((JpaToscaPolicy) copyObject);
249 super.copyTo(target);
251 copy.setType(new PfConceptKey(type));
253 if (properties == null) {
254 copy.setProperties(null);
256 copy.setProperties(properties);
259 if (targets == null) {
260 copy.setTargets(null);
262 final List<PfConceptKey> newTargets = new ArrayList<>();
263 for (final PfConceptKey oldTarget : targets) {
264 newTargets.add(new PfConceptKey(oldTarget));
266 copy.setTargets(newTargets);