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.concepts;
26 import java.util.ArrayList;
27 import java.util.List;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
40 import org.onap.policy.common.utils.validation.Assertions;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.PfValidationMessage;
46 import org.onap.policy.models.base.PfValidationResult;
47 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
50 * Class to represent the policy in TOSCA definition.
52 * @author Chenfei Gao (cgao@research.att.com)
53 * @author Liam Fallon (liam.fallon@est.tech)
56 @Table(name = "ToscaPolicy")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = true)
60 public class ToscaPolicy extends ToscaEntityType {
61 private static final long serialVersionUID = 3265174757061982805L;
64 private PfConceptKey type;
67 private List<ToscaProperty> properties;
70 private List<PfConceptKey> targets;
73 * The Default Constructor creates a {@link ToscaPolicy} object with a null key.
75 public ToscaPolicy() {
76 this(new PfConceptKey());
80 * The Key Constructor creates a {@link ToscaPolicy} object with the given concept key.
84 public ToscaPolicy(@NonNull final PfConceptKey key) {
85 this(key, new PfConceptKey());
89 * The full Constructor creates a {@link ToscaPolicy} object with all mandatory fields.
92 * @param type the type of the policy
94 public ToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
102 * @param copyConcept the concept to copy from
104 public ToscaPolicy(@NonNull final ToscaPolicy copyConcept) {
110 public List<PfKey> getKeys() {
111 final List<PfKey> keyList = super.getKeys();
113 keyList.addAll(type.getKeys());
115 if (properties != null) {
116 for (ToscaProperty property : properties) {
117 keyList.addAll(property.getKeys());
121 if (targets != null) {
122 keyList.addAll(targets);
129 public void clean() {
134 if (properties != null) {
135 for (ToscaProperty property : properties) {
140 if (targets != null) {
141 for (PfConceptKey target : targets) {
148 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
149 PfValidationResult result = super.validate(resultIn);
151 if (type == null || type.isNullKey()) {
152 result.addValidationMessage(new PfValidationMessage(type, this.getClass(), ValidationResult.INVALID,
153 "type is null or a null key"));
156 result = type.validate(result);
159 if (properties != null) {
160 result = validateProperties(result);
163 if (targets != null) {
164 result = validateTargets(result);
171 * Validate the policy properties.
173 * @param result The result of validations up to now
174 * @return the validation result
176 private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) {
177 PfValidationResult result = resultIn;
179 for (ToscaProperty property : properties) {
180 if (property == null) {
181 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
182 "policy property may not be null "));
184 result = property.validate(result);
191 * Validate the policy targets.
193 * @param result The result of validations up to now
194 * @return the validation result
196 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
197 PfValidationResult result = resultIn;
199 for (PfConceptKey target : targets) {
200 if (target == null) {
201 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
202 "policy target may not be null "));
204 result = target.validate(result);
211 public int compareTo(final PfConcept otherConcept) {
212 if (otherConcept == null) {
216 if (this == otherConcept) {
220 if (getClass() != otherConcept.getClass()) {
221 return this.hashCode() - otherConcept.hashCode();
224 final ToscaPolicy other = (ToscaPolicy) otherConcept;
225 if (!super.equals(other)) {
226 return super.compareTo(other);
229 if (!type.equals(other.type)) {
230 return type.compareTo(other.type);
233 int retVal = PfUtils.compareObjects(properties, other.properties);
238 return PfUtils.compareObjects(targets, other.targets);
242 public PfConcept copyTo(@NonNull PfConcept target) {
243 final Object copyObject = target;
244 Assertions.instanceOf(copyObject, PfConcept.class);
246 final ToscaPolicy copy = ((ToscaPolicy) copyObject);
247 super.copyTo(target);
249 copy.setType(new PfConceptKey(type));
251 if (properties == null) {
252 copy.setProperties(null);
254 final List<ToscaProperty> newProperties = new ArrayList<>();
255 for (final ToscaProperty property : properties) {
256 newProperties.add(new ToscaProperty(property));
258 copy.setProperties(newProperties);
261 if (targets == null) {
262 copy.setTargets(null);
264 final List<PfConceptKey> newTargets = new ArrayList<>();
265 for (final PfConceptKey oldTarget : targets) {
266 newTargets.add(new PfConceptKey(oldTarget));
268 copy.setTargets(newTargets);