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.LinkedHashMap;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Lob;
35 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.onap.policy.models.base.PfAuthorative;
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.PfReferenceKey;
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;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
52 * Class to represent the policy type in TOSCA definition.
54 * @author Chenfei Gao (cgao@research.att.com)
55 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaPolicyType")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = true)
63 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
64 private static final long serialVersionUID = -563659852901842616L;
68 private Map<String, JpaToscaProperty> properties;
71 private List<PfConceptKey> targets;
74 private List<JpaToscaTrigger> triggers;
77 * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
79 public JpaToscaPolicyType() {
80 this(new PfConceptKey());
84 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
88 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
95 * @param copyConcept the concept to copy from
97 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
99 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
100 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
101 this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
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 getClass().getName().compareTo(otherConcept.getClass().getName());
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);