2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 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;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.Entity;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.Lob;
38 import javax.persistence.Table;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42 import org.apache.commons.collections4.CollectionUtils;
43 import org.onap.policy.common.parameters.BeanValidationResult;
44 import org.onap.policy.common.parameters.annotations.NotNull;
45 import org.onap.policy.common.parameters.annotations.Valid;
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.tosca.authorative.concepts.ToscaPolicyType;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
54 import org.onap.policy.models.tosca.utils.ToscaUtils;
57 * Class to represent the policy type in TOSCA definition.
59 * @author Chenfei Gao (cgao@research.att.com)
60 * @author Liam Fallon (liam.fallon@est.tech)
64 @Table(name = "ToscaPolicyType")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @EqualsAndHashCode(callSuper = true)
68 public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> {
69 private static final long serialVersionUID = -563659852901842616L;
73 private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties;
76 private List<@NotNull @Valid PfConceptKey> targets;
79 private List<@NotNull @Valid JpaToscaTrigger> triggers;
82 * The Default Constructor creates a {@link JpaToscaPolicyType} object with a null key.
84 public JpaToscaPolicyType() {
85 this(new PfConceptKey());
89 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
93 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
100 * @param copyConcept the concept to copy from
102 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
104 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
105 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
106 this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
110 * Authorative constructor.
112 * @param authorativeConcept the authorative concept to copy from
114 public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
115 this.fromAuthorative(authorativeConcept);
119 public ToscaPolicyType toAuthorative() {
120 ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
121 super.setToscaEntity(toscaPolicyType);
122 super.toAuthorative();
124 toscaPolicyType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
126 return toscaPolicyType;
130 public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
131 super.fromAuthorative(toscaPolicyType);
134 if (toscaPolicyType.getProperties() != null) {
135 properties = new LinkedHashMap<>();
136 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) {
137 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
138 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
139 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
145 public List<PfKey> getKeys() {
146 final List<PfKey> keyList = super.getKeys();
148 if (properties != null) {
149 for (JpaToscaProperty property : properties.values()) {
150 keyList.addAll(property.getKeys());
154 if (targets != null) {
155 keyList.addAll(targets);
158 if (triggers != null) {
159 for (JpaToscaTrigger trigger : triggers) {
160 keyList.addAll(trigger.getKeys());
168 public void clean() {
171 if (properties != null) {
172 for (JpaToscaProperty property : properties.values()) {
177 if (targets != null) {
178 for (PfConceptKey target : targets) {
183 if (triggers != null) {
184 for (JpaToscaTrigger trigger : triggers) {
191 public BeanValidationResult validate(String fieldName) {
192 BeanValidationResult result = super.validate(fieldName);
194 validateKeyVersionNotNull(result, "key", getKey());
200 public int compareTo(final PfConcept otherConcept) {
201 if (otherConcept == null) {
204 if (this == otherConcept) {
207 if (getClass() != otherConcept.getClass()) {
208 return getClass().getName().compareTo(otherConcept.getClass().getName());
211 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
212 int result = super.compareTo(other);
217 result = PfUtils.compareMaps(properties, other.properties);
222 result = PfUtils.compareCollections(targets, other.targets);
227 return PfUtils.compareCollections(triggers, other.triggers);
231 * Get the data types referenced in a policy type.
233 * @return the data types referenced in a policy type
235 public Collection<PfConceptKey> getReferencedDataTypes() {
236 if (properties == null) {
237 return CollectionUtils.emptyCollection();
240 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
242 for (JpaToscaProperty property : properties.values()) {
243 referencedDataTypes.add(property.getType());
245 if (property.getEntrySchema() != null) {
246 referencedDataTypes.add(property.getEntrySchema().getType());
250 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
252 return referencedDataTypes;