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.ElementCollection;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Table;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
42 import org.onap.policy.common.utils.validation.Assertions;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfReferenceKey;
48 import org.onap.policy.models.base.PfUtils;
49 import org.onap.policy.models.base.PfValidationMessage;
50 import org.onap.policy.models.base.PfValidationResult;
51 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
56 * Class to represent the policy type in TOSCA definition.
58 * @author Chenfei Gao (cgao@research.att.com)
59 * @author Liam Fallon (liam.fallon@est.tech)
63 @Table(name = "ToscaPolicyType")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = true)
67 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
68 private static final long serialVersionUID = -563659852901842616L;
71 private Map<String, JpaToscaProperty> properties;
74 private List<PfConceptKey> targets;
77 private List<JpaToscaTrigger> triggers;
80 * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
82 public JpaToscaPolicyType() {
83 this(new PfConceptKey());
87 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
91 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
98 * @param copyConcept the concept to copy from
100 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
105 * Authorative constructor.
107 * @param authorativeConcept the authorative concept to copy from
109 public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
110 this.fromAuthorative(authorativeConcept);
114 public ToscaPolicyType toAuthorative() {
115 ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
116 super.setToscaEntity(toscaPolicyType);
117 super.toAuthorative();
119 if (properties != null) {
120 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
122 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
123 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
126 toscaPolicyType.setProperties(propertyMap);
129 return toscaPolicyType;
133 public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
134 super.fromAuthorative(toscaPolicyType);
137 if (toscaPolicyType.getProperties() != null) {
138 properties = new LinkedHashMap<>();
139 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
140 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
141 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
142 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
148 public List<PfKey> getKeys() {
149 final List<PfKey> keyList = super.getKeys();
151 if (properties != null) {
152 for (JpaToscaProperty property : properties.values()) {
153 keyList.addAll(property.getKeys());
157 if (targets != null) {
158 keyList.addAll(targets);
161 if (triggers != null) {
162 for (JpaToscaTrigger trigger : triggers) {
163 keyList.addAll(trigger.getKeys());
171 public void clean() {
174 if (properties != null) {
175 for (JpaToscaProperty property : properties.values()) {
180 if (targets != null) {
181 for (PfConceptKey target : targets) {
186 if (triggers != null) {
187 for (JpaToscaTrigger trigger : triggers) {
194 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
195 PfValidationResult result = super.validate(resultIn);
197 if (properties != null) {
198 result = validateProperties(result);
201 if (targets != null) {
202 result = validateTargets(result);
205 if (triggers != null) {
206 result = validateTriggers(result);
213 * Validate the policy properties.
215 * @param result The result of validations up to now
216 * @return the validation result
218 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
219 PfValidationResult result = resultIn;
221 for (JpaToscaProperty property : properties.values()) {
222 if (property == null) {
223 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
224 "policy property may not be null "));
226 result = property.validate(result);
233 * Validate the policy targets.
235 * @param result The result of validations up to now
236 * @return the validation result
238 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
239 PfValidationResult result = resultIn;
241 for (PfConceptKey target : targets) {
242 if (target == null) {
243 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
244 "policy target may not be null "));
246 result = target.validate(result);
253 * Validate the policy triggers.
255 * @param result The result of validations up to now
256 * @return the validation result
258 private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
259 PfValidationResult result = resultIn;
261 for (JpaToscaTrigger trigger : triggers) {
262 if (trigger == null) {
263 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
264 "policy trigger may not be null "));
266 result = trigger.validate(result);
273 public int compareTo(final PfConcept otherConcept) {
274 if (otherConcept == null) {
277 if (this == otherConcept) {
280 if (getClass() != otherConcept.getClass()) {
281 return this.hashCode() - otherConcept.hashCode();
284 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
285 if (!super.equals(other)) {
286 return super.compareTo(other);
289 int retVal = PfUtils.compareObjects(properties, other.properties);
294 retVal = PfUtils.compareObjects(targets, other.targets);
299 return PfUtils.compareObjects(triggers, other.triggers);
303 public PfConcept copyTo(@NonNull PfConcept target) {
304 final Object copyObject = target;
305 Assertions.instanceOf(copyObject, PfConcept.class);
307 final JpaToscaPolicyType copy = ((JpaToscaPolicyType) copyObject);
308 super.copyTo(target);
310 if (properties == null) {
311 copy.setProperties(null);
313 final Map<String, JpaToscaProperty> newProperties = new LinkedHashMap<>();
314 for (final Entry<String, JpaToscaProperty> propertyEntry : properties.entrySet()) {
315 newProperties.put(propertyEntry.getKey(), new JpaToscaProperty(propertyEntry.getValue()));
317 copy.setProperties(newProperties);
320 if (targets == null) {
321 copy.setTargets(null);
323 final List<PfConceptKey> newTargets = new ArrayList<>();
324 for (final PfConceptKey oldTarget : targets) {
325 newTargets.add(new PfConceptKey(oldTarget));
327 copy.setTargets(newTargets);
330 if (triggers == null) {
331 copy.setTargets(null);
333 final List<JpaToscaTrigger> newTriggers = new ArrayList<>();
334 for (final JpaToscaTrigger trigger : triggers) {
335 newTriggers.add(new JpaToscaTrigger(trigger));
337 copy.setTriggers(newTriggers);