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 (properties != null) {
207 result = validateProperties(result);
210 if (targets != null) {
211 result = validateTargets(result);
214 if (triggers != null) {
215 result = validateTriggers(result);
222 * Validate the policy properties.
224 * @param result The result of validations up to now
225 * @return the validation result
227 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
228 PfValidationResult result = resultIn;
230 for (JpaToscaProperty property : properties.values()) {
231 if (property == null) {
232 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
233 "policy property may not be null "));
235 result = property.validate(result);
242 * Validate the policy targets.
244 * @param result The result of validations up to now
245 * @return the validation result
247 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
248 PfValidationResult result = resultIn;
250 for (PfConceptKey target : targets) {
251 if (target == null) {
252 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
253 "policy target may not be null "));
255 result = target.validate(result);
262 * Validate the policy triggers.
264 * @param result The result of validations up to now
265 * @return the validation result
267 private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
268 PfValidationResult result = resultIn;
270 for (JpaToscaTrigger trigger : triggers) {
271 if (trigger == null) {
272 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
273 "policy trigger may not be null "));
275 result = trigger.validate(result);
282 public int compareTo(final PfConcept otherConcept) {
283 if (otherConcept == null) {
286 if (this == otherConcept) {
289 if (getClass() != otherConcept.getClass()) {
290 return getClass().getName().compareTo(otherConcept.getClass().getName());
293 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
294 if (!super.equals(other)) {
295 return super.compareTo(other);
298 int retVal = PfUtils.compareObjects(properties, other.properties);
303 retVal = PfUtils.compareObjects(targets, other.targets);
308 return PfUtils.compareObjects(triggers, other.triggers);
312 * Get the data types referenced in a policy type.
314 * @return the data types referenced in a policy type
316 public Collection<PfConceptKey> getReferencedDataTypes() {
317 if (properties == null) {
318 return CollectionUtils.emptyCollection();
321 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
323 for (JpaToscaProperty property : properties.values()) {
324 referencedDataTypes.add(property.getType());
326 if (property.getEntrySchema() != null) {
327 referencedDataTypes.add(property.getEntrySchema().getType());
331 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
333 return referencedDataTypes;