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 javax.persistence.ElementCollection;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
39 import org.onap.policy.common.utils.validation.Assertions;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfUtils;
44 import org.onap.policy.models.base.PfValidationMessage;
45 import org.onap.policy.models.base.PfValidationResult;
46 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
49 * Class to represent the policy type in TOSCA definition.
51 * @author Chenfei Gao (cgao@research.att.com)
52 * @author Liam Fallon (liam.fallon@est.tech)
56 @Table(name = "ToscaPolicyType")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = true)
60 public class JpaToscaPolicyType extends JpaToscaEntityType {
61 private static final long serialVersionUID = -563659852901842616L;
64 private List<JpaToscaProperty> properties;
67 private List<PfConceptKey> targets;
70 private List<JpaToscaTrigger> triggers;
73 * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
75 public JpaToscaPolicyType() {
76 this(new PfConceptKey());
80 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
84 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
91 * @param copyConcept the concept to copy from
93 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
99 public List<PfKey> getKeys() {
100 final List<PfKey> keyList = super.getKeys();
102 if (properties != null) {
103 for (JpaToscaProperty property : properties) {
104 keyList.addAll(property.getKeys());
108 if (targets != null) {
109 keyList.addAll(targets);
112 if (triggers != null) {
113 for (JpaToscaTrigger trigger : triggers) {
114 keyList.addAll(trigger.getKeys());
122 public void clean() {
125 if (properties != null) {
126 for (JpaToscaProperty property : properties) {
131 if (targets != null) {
132 for (PfConceptKey target : targets) {
137 if (triggers != null) {
138 for (JpaToscaTrigger trigger : triggers) {
145 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
146 PfValidationResult result = super.validate(resultIn);
148 if (properties != null) {
149 result = validateProperties(result);
152 if (targets != null) {
153 result = validateTargets(result);
156 if (triggers != null) {
157 result = validateTriggers(result);
164 * Validate the policy properties.
166 * @param result The result of validations up to now
167 * @return the validation result
169 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
170 PfValidationResult result = resultIn;
172 for (JpaToscaProperty property : properties) {
173 if (property == null) {
174 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
175 "policy property may not be null "));
177 result = property.validate(result);
184 * Validate the policy targets.
186 * @param result The result of validations up to now
187 * @return the validation result
189 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
190 PfValidationResult result = resultIn;
192 for (PfConceptKey target : targets) {
193 if (target == null) {
194 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
195 "policy target may not be null "));
197 result = target.validate(result);
204 * Validate the policy triggers.
206 * @param result The result of validations up to now
207 * @return the validation result
209 private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
210 PfValidationResult result = resultIn;
212 for (JpaToscaTrigger trigger : triggers) {
213 if (trigger == null) {
214 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
215 "policy trigger may not be null "));
217 result = trigger.validate(result);
224 public int compareTo(final PfConcept otherConcept) {
225 if (otherConcept == null) {
228 if (this == otherConcept) {
231 if (getClass() != otherConcept.getClass()) {
232 return this.hashCode() - otherConcept.hashCode();
235 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
236 if (!super.equals(other)) {
237 return super.compareTo(other);
240 int retVal = PfUtils.compareObjects(properties, other.properties);
245 retVal = PfUtils.compareObjects(targets, other.targets);
250 return PfUtils.compareObjects(triggers, other.triggers);
254 public PfConcept copyTo(@NonNull PfConcept target) {
255 final Object copyObject = target;
256 Assertions.instanceOf(copyObject, PfConcept.class);
258 final JpaToscaPolicyType copy = ((JpaToscaPolicyType) copyObject);
259 super.copyTo(target);
261 final List<JpaToscaProperty> newProperties = new ArrayList<>();
263 if (properties == null) {
264 copy.setProperties(null);
266 for (final JpaToscaProperty property : properties) {
267 newProperties.add(new JpaToscaProperty(property));
269 copy.setProperties(newProperties);
272 if (targets == null) {
273 copy.setTargets(null);
275 final List<PfConceptKey> newTargets = new ArrayList<>();
276 for (final PfConceptKey oldTarget : targets) {
277 newTargets.add(new PfConceptKey(oldTarget));
279 copy.setTargets(newTargets);
282 if (triggers == null) {
283 copy.setTargets(null);
285 final List<JpaToscaTrigger> newTriggers = new ArrayList<>();
286 for (final JpaToscaTrigger trigger : triggers) {
287 newTriggers.add(new JpaToscaTrigger(trigger));
289 copy.setTriggers(newTriggers);