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;
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;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
43 import org.apache.commons.collections4.CollectionUtils;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfReferenceKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.PfValidationMessage;
51 import org.onap.policy.models.base.PfValidationResult;
52 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
55 import org.onap.policy.models.tosca.utils.ToscaUtils;
58 * Class to represent the policy type in TOSCA definition.
60 * @author Chenfei Gao (cgao@research.att.com)
61 * @author Liam Fallon (liam.fallon@est.tech)
65 @Table(name = "ToscaPolicyType")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
68 @EqualsAndHashCode(callSuper = true)
69 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
70 private static final long serialVersionUID = -563659852901842616L;
74 private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
77 private List<PfConceptKey> targets = new ArrayList<>();
80 private List<JpaToscaTrigger> triggers = new ArrayList<>();
83 * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
85 public JpaToscaPolicyType() {
86 this(new PfConceptKey());
90 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
94 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
101 * @param copyConcept the concept to copy from
103 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
105 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
106 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
107 this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
111 * Authorative constructor.
113 * @param authorativeConcept the authorative concept to copy from
115 public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
116 this.fromAuthorative(authorativeConcept);
120 public ToscaPolicyType toAuthorative() {
121 ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
122 super.setToscaEntity(toscaPolicyType);
123 super.toAuthorative();
125 if (properties != null) {
126 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
128 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
129 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
132 toscaPolicyType.setProperties(propertyMap);
135 return toscaPolicyType;
139 public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
140 super.fromAuthorative(toscaPolicyType);
143 if (toscaPolicyType.getProperties() != null) {
144 properties = new LinkedHashMap<>();
145 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
146 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
147 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
148 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
154 public List<PfKey> getKeys() {
155 final List<PfKey> keyList = super.getKeys();
157 if (properties != null) {
158 for (JpaToscaProperty property : properties.values()) {
159 keyList.addAll(property.getKeys());
163 if (targets != null) {
164 keyList.addAll(targets);
167 if (triggers != null) {
168 for (JpaToscaTrigger trigger : triggers) {
169 keyList.addAll(trigger.getKeys());
177 public void clean() {
180 if (properties != null) {
181 for (JpaToscaProperty property : properties.values()) {
186 if (targets != null) {
187 for (PfConceptKey target : targets) {
192 if (triggers != null) {
193 for (JpaToscaTrigger trigger : triggers) {
200 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
201 PfValidationResult result = super.validate(resultIn);
203 if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
204 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
205 "key version is a null version"));
208 if (properties != null) {
209 result = validateProperties(result);
212 if (targets != null) {
213 result = validateTargets(result);
216 if (triggers != null) {
217 result = validateTriggers(result);
224 * Validate the policy properties.
226 * @param result The result of validations up to now
227 * @return the validation result
229 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
230 PfValidationResult result = resultIn;
232 for (JpaToscaProperty property : properties.values()) {
233 if (property == null) {
234 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
235 "policy property may not be null "));
237 result = property.validate(result);
244 * Validate the policy targets.
246 * @param result The result of validations up to now
247 * @return the validation result
249 private PfValidationResult validateTargets(final PfValidationResult resultIn) {
250 PfValidationResult result = resultIn;
252 for (PfConceptKey target : targets) {
253 if (target == null) {
254 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
255 "policy target may not be null "));
257 result = target.validate(result);
264 * Validate the policy triggers.
266 * @param result The result of validations up to now
267 * @return the validation result
269 private PfValidationResult validateTriggers(final PfValidationResult resultIn) {
270 PfValidationResult result = resultIn;
272 for (JpaToscaTrigger trigger : triggers) {
273 if (trigger == null) {
274 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
275 "policy trigger may not be null "));
277 result = trigger.validate(result);
284 public int compareTo(final PfConcept otherConcept) {
285 if (otherConcept == null) {
288 if (this == otherConcept) {
291 if (getClass() != otherConcept.getClass()) {
292 return getClass().getName().compareTo(otherConcept.getClass().getName());
295 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
296 if (!super.equals(other)) {
297 return super.compareTo(other);
300 int retVal = PfUtils.compareObjects(properties, other.properties);
305 retVal = PfUtils.compareObjects(targets, other.targets);
310 return PfUtils.compareObjects(triggers, other.triggers);
314 * Get the data types referenced in a policy type.
316 * @return the data types referenced in a policy type
318 public Collection<PfConceptKey> getReferencedDataTypes() {
319 if (properties == null) {
320 return CollectionUtils.emptyCollection();
323 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
325 for (JpaToscaProperty property : properties.values()) {
326 referencedDataTypes.add(property.getType());
328 if (property.getEntrySchema() != null) {
329 referencedDataTypes.add(property.getEntrySchema().getType());
333 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
335 return referencedDataTypes;