2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.ToscaConstraint;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
56 import org.onap.policy.models.tosca.utils.ToscaUtils;
59 * Class to represent custom data type in TOSCA definition.
61 * @author Chenfei Gao (cgao@research.att.com)
62 * @author Liam Fallon (liam.fallon@est.tech)
65 @Table(name = "ToscaDataType")
66 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
68 @EqualsAndHashCode(callSuper = true)
69 public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> {
70 private static final long serialVersionUID = -3922690413436539164L;
73 private List<JpaToscaConstraint> constraints = new ArrayList<>();
77 private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
80 * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
82 public JpaToscaDataType() {
83 this(new PfConceptKey());
87 * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
91 public JpaToscaDataType(@NonNull final PfConceptKey key) {
98 * @param copyConcept the concept to copy from
100 public JpaToscaDataType(final JpaToscaDataType copyConcept) {
102 // Constraints are immutable
103 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
104 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
108 * Authorative constructor.
110 * @param authorativeConcept the authorative concept to copy from
112 public JpaToscaDataType(final ToscaDataType authorativeConcept) {
113 this.fromAuthorative(authorativeConcept);
117 public ToscaDataType toAuthorative() {
118 ToscaDataType toscaDataType = new ToscaDataType();
119 super.setToscaEntity(toscaDataType);
120 super.toAuthorative();
122 if (constraints != null) {
123 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
125 for (JpaToscaConstraint constraint : constraints) {
126 toscaConstraints.add(constraint.toAuthorative());
129 toscaDataType.setConstraints(toscaConstraints);
132 if (properties != null) {
133 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
135 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
136 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
139 toscaDataType.setProperties(propertyMap);
142 return toscaDataType;
146 public void fromAuthorative(final ToscaDataType toscaDataType) {
147 super.fromAuthorative(toscaDataType);
149 if (toscaDataType.getConstraints() != null) {
150 constraints = new ArrayList<>();
152 for (ToscaConstraint toscaConstraint : toscaDataType.getConstraints()) {
153 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
157 if (toscaDataType.getProperties() != null) {
158 properties = new LinkedHashMap<>();
159 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
160 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
161 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
162 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
168 public List<PfKey> getKeys() {
169 final List<PfKey> keyList = super.getKeys();
171 if (properties != null) {
172 for (JpaToscaProperty property : properties.values()) {
173 keyList.addAll(property.getKeys());
181 public void clean() {
184 if (properties != null) {
185 for (JpaToscaProperty property : properties.values()) {
192 public PfValidationResult validate(final PfValidationResult resultIn) {
193 PfValidationResult result = super.validate(resultIn);
195 if (constraints != null) {
196 validateConstraints(result);
199 if (properties != null) {
200 result = validateProperties(result);
207 * Validate the constraints.
209 * @param result where to put the validation results
211 private void validateConstraints(@NonNull final PfValidationResult result) {
212 for (JpaToscaConstraint constraint : constraints) {
213 if (constraint == null) {
214 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
215 "data type constraint may not be null "));
221 * Validate the 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 "data type property may not be null "));
234 result = property.validate(result);
241 public int compareTo(final PfConcept otherConcept) {
242 if (otherConcept == null) {
245 if (this == otherConcept) {
248 if (getClass() != otherConcept.getClass()) {
249 return getClass().getName().compareTo(otherConcept.getClass().getName());
252 final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
253 if (!super.equals(other)) {
254 return super.compareTo(other);
257 int result = PfUtils.compareObjects(constraints, other.constraints);
262 result = PfUtils.compareObjects(properties, other.properties);
271 * Get the data types referenced in a data type.
273 * @return the data types referenced in a data type
275 public Collection<PfConceptKey> getReferencedDataTypes() {
276 if (properties == null) {
277 return CollectionUtils.emptyCollection();
280 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
282 for (JpaToscaProperty property : properties.values()) {
283 referencedDataTypes.add(property.getType());
285 if (property.getEntrySchema() != null) {
286 referencedDataTypes.add(property.getEntrySchema().getType());
290 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
292 return referencedDataTypes;