2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 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.Collection;
27 import java.util.LinkedHashMap;
28 import java.util.LinkedHashSet;
29 import java.util.List;
31 import java.util.Map.Entry;
34 import javax.persistence.ElementCollection;
35 import javax.persistence.Entity;
36 import javax.persistence.Inheritance;
37 import javax.persistence.InheritanceType;
38 import javax.persistence.Lob;
39 import javax.persistence.Table;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
45 import org.apache.commons.collections4.CollectionUtils;
46 import org.onap.policy.models.base.PfAuthorative;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfReferenceKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.base.PfValidationMessage;
53 import org.onap.policy.models.base.PfValidationResult;
54 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
57 import org.onap.policy.models.tosca.utils.ToscaUtils;
60 * Class to represent the policy type in TOSCA definition.
62 * @author Chenfei Gao (cgao@research.att.com)
63 * @author Liam Fallon (liam.fallon@est.tech)
67 @Table(name = "ToscaPolicyType")
68 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @EqualsAndHashCode(callSuper = true)
71 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
72 private static final long serialVersionUID = -563659852901842616L;
76 private Map<String, JpaToscaProperty> properties;
79 private List<PfConceptKey> targets;
82 private List<JpaToscaTrigger> triggers;
85 * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
87 public JpaToscaPolicyType() {
88 this(new PfConceptKey());
92 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
96 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
103 * @param copyConcept the concept to copy from
105 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
107 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
108 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
109 this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
113 * Authorative constructor.
115 * @param authorativeConcept the authorative concept to copy from
117 public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
118 this.fromAuthorative(authorativeConcept);
122 public ToscaPolicyType toAuthorative() {
123 ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
124 super.setToscaEntity(toscaPolicyType);
125 super.toAuthorative();
127 if (properties != null) {
128 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
130 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
131 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
134 toscaPolicyType.setProperties(propertyMap);
137 return toscaPolicyType;
141 public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
142 super.fromAuthorative(toscaPolicyType);
145 if (toscaPolicyType.getProperties() != null) {
146 properties = new LinkedHashMap<>();
147 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
148 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
149 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
150 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
156 public List<PfKey> getKeys() {
157 final List<PfKey> keyList = super.getKeys();
159 if (properties != null) {
160 for (JpaToscaProperty property : properties.values()) {
161 keyList.addAll(property.getKeys());
165 if (targets != null) {
166 keyList.addAll(targets);
169 if (triggers != null) {
170 for (JpaToscaTrigger trigger : triggers) {
171 keyList.addAll(trigger.getKeys());
179 public void clean() {
182 if (properties != null) {
183 for (JpaToscaProperty property : properties.values()) {
188 if (targets != null) {
189 for (PfConceptKey target : targets) {
194 if (triggers != null) {
195 for (JpaToscaTrigger trigger : triggers) {
202 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
203 PfValidationResult result = super.validate(resultIn);
205 if (properties != null) {
206 result = validateProperties(result);
209 if (targets != null) {
210 result = validateTargets(result);
213 if (triggers != null) {
214 result = validateTriggers(result);
221 * Validate the policy properties.
223 * @param result The result of validations up to now
224 * @return the validation result
226 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
227 PfValidationResult result = resultIn;
229 for (JpaToscaProperty property : properties.values()) {
230 if (property == null) {
231 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
232 "policy property may not be null "));
234 result = property.validate(result);
241 * Validate the policy targets.
243 * @param result The result of validations up to now
244 * @return the validation result
246 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
247 PfValidationResult result = resultIn;
249 for (PfConceptKey target : targets) {
250 if (target == null) {
251 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
252 "policy target may not be null "));
254 result = target.validate(result);
261 * Validate the policy triggers.
263 * @param result The result of validations up to now
264 * @return the validation result
266 private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
267 PfValidationResult result = resultIn;
269 for (JpaToscaTrigger trigger : triggers) {
270 if (trigger == null) {
271 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
272 "policy trigger may not be null "));
274 result = trigger.validate(result);
281 public int compareTo(final PfConcept otherConcept) {
282 if (otherConcept == null) {
285 if (this == otherConcept) {
288 if (getClass() != otherConcept.getClass()) {
289 return getClass().getName().compareTo(otherConcept.getClass().getName());
292 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
293 if (!super.equals(other)) {
294 return super.compareTo(other);
297 int retVal = PfUtils.compareObjects(properties, other.properties);
302 retVal = PfUtils.compareObjects(targets, other.targets);
307 return PfUtils.compareObjects(triggers, other.triggers);
311 * Get the data types referenced in a policy type.
313 * @return the data types referenced in a policy type
315 public Collection<PfConceptKey> getReferencedDataTypes() {
316 if (properties == null) {
317 return CollectionUtils.emptyCollection();
320 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
322 for (JpaToscaProperty property : properties.values()) {
323 referencedDataTypes.add(property.getType());
325 if (property.getEntrySchema() != null) {
326 referencedDataTypes.add(property.getEntrySchema().getType());
330 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
332 return referencedDataTypes;