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.ArrayList;
27 import java.util.Collection;
28 import java.util.LinkedHashMap;
29 import java.util.LinkedHashSet;
30 import java.util.List;
32 import java.util.Map.Entry;
35 import javax.persistence.ElementCollection;
36 import javax.persistence.Entity;
37 import javax.persistence.Inheritance;
38 import javax.persistence.InheritanceType;
39 import javax.persistence.Lob;
40 import javax.persistence.Table;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
46 import org.apache.commons.collections4.CollectionUtils;
47 import org.onap.policy.models.base.PfAuthorative;
48 import org.onap.policy.models.base.PfConcept;
49 import org.onap.policy.models.base.PfConceptKey;
50 import org.onap.policy.models.base.PfKey;
51 import org.onap.policy.models.base.PfReferenceKey;
52 import org.onap.policy.models.base.PfUtils;
53 import org.onap.policy.models.base.PfValidationMessage;
54 import org.onap.policy.models.base.PfValidationResult;
55 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
58 import org.onap.policy.models.tosca.utils.ToscaUtils;
61 * Class to represent the policy type in TOSCA definition.
63 * @author Chenfei Gao (cgao@research.att.com)
64 * @author Liam Fallon (liam.fallon@est.tech)
68 @Table(name = "ToscaPolicyType")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
71 @EqualsAndHashCode(callSuper = true)
72 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
73 private static final long serialVersionUID = -563659852901842616L;
77 private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
80 private List<PfConceptKey> targets = new ArrayList<>();
83 private List<JpaToscaTrigger> triggers = new ArrayList<>();
86 * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
88 public JpaToscaPolicyType() {
89 this(new PfConceptKey());
93 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
97 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
104 * @param copyConcept the concept to copy from
106 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
108 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
109 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
110 this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
114 * Authorative constructor.
116 * @param authorativeConcept the authorative concept to copy from
118 public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
119 this.fromAuthorative(authorativeConcept);
123 public ToscaPolicyType toAuthorative() {
124 ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
125 super.setToscaEntity(toscaPolicyType);
126 super.toAuthorative();
128 if (properties != null) {
129 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
131 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
132 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
135 toscaPolicyType.setProperties(propertyMap);
138 return toscaPolicyType;
142 public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
143 super.fromAuthorative(toscaPolicyType);
146 if (toscaPolicyType.getProperties() != null) {
147 properties = new LinkedHashMap<>();
148 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
149 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
150 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
151 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
157 public List<PfKey> getKeys() {
158 final List<PfKey> keyList = super.getKeys();
160 if (properties != null) {
161 for (JpaToscaProperty property : properties.values()) {
162 keyList.addAll(property.getKeys());
166 if (targets != null) {
167 keyList.addAll(targets);
170 if (triggers != null) {
171 for (JpaToscaTrigger trigger : triggers) {
172 keyList.addAll(trigger.getKeys());
180 public void clean() {
183 if (properties != null) {
184 for (JpaToscaProperty property : properties.values()) {
189 if (targets != null) {
190 for (PfConceptKey target : targets) {
195 if (triggers != null) {
196 for (JpaToscaTrigger trigger : triggers) {
203 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
204 PfValidationResult result = super.validate(resultIn);
206 if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
207 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
208 "key version is a null version"));
211 if (properties != null) {
212 result = validateProperties(result);
215 if (targets != null) {
216 result = validateTargets(result);
219 if (triggers != null) {
220 result = validateTriggers(result);
227 * Validate the policy properties.
229 * @param result The result of validations up to now
230 * @return the validation result
232 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
233 PfValidationResult result = resultIn;
235 for (JpaToscaProperty property : properties.values()) {
236 if (property == null) {
237 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
238 "policy property may not be null "));
240 result = property.validate(result);
247 * Validate the policy targets.
249 * @param result The result of validations up to now
250 * @return the validation result
252 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
253 PfValidationResult result = resultIn;
255 for (PfConceptKey target : targets) {
256 if (target == null) {
257 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
258 "policy target may not be null "));
260 result = target.validate(result);
267 * Validate the policy triggers.
269 * @param result The result of validations up to now
270 * @return the validation result
272 private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
273 PfValidationResult result = resultIn;
275 for (JpaToscaTrigger trigger : triggers) {
276 if (trigger == null) {
277 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
278 "policy trigger may not be null "));
280 result = trigger.validate(result);
287 public int compareTo(final PfConcept otherConcept) {
288 if (otherConcept == null) {
291 if (this == otherConcept) {
294 if (getClass() != otherConcept.getClass()) {
295 return getClass().getName().compareTo(otherConcept.getClass().getName());
298 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
299 if (!super.equals(other)) {
300 return super.compareTo(other);
303 int retVal = PfUtils.compareObjects(properties, other.properties);
308 retVal = PfUtils.compareObjects(targets, other.targets);
313 return PfUtils.compareObjects(triggers, other.triggers);
317 * Get the data types referenced in a policy type.
319 * @return the data types referenced in a policy type
321 public Collection<PfConceptKey> getReferencedDataTypes() {
322 if (properties == null) {
323 return CollectionUtils.emptyCollection();
326 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
328 for (JpaToscaProperty property : properties.values()) {
329 referencedDataTypes.add(property.getType());
331 if (property.getEntrySchema() != null) {
332 referencedDataTypes.add(property.getEntrySchema().getType());
336 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
338 return referencedDataTypes;