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.ToscaConstraint;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
59 import org.onap.policy.models.tosca.utils.ToscaUtils;
62 * Class to represent custom data type in TOSCA definition.
64 * @author Chenfei Gao (cgao@research.att.com)
65 * @author Liam Fallon (liam.fallon@est.tech)
68 @Table(name = "ToscaDataType")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
71 @EqualsAndHashCode(callSuper = true)
72 public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> {
73 private static final long serialVersionUID = -3922690413436539164L;
76 private List<JpaToscaConstraint> constraints = new ArrayList<>();
80 private Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
83 * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
85 public JpaToscaDataType() {
86 this(new PfConceptKey());
90 * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
94 public JpaToscaDataType(@NonNull final PfConceptKey key) {
101 * @param copyConcept the concept to copy from
103 public JpaToscaDataType(final JpaToscaDataType copyConcept) {
105 // Constraints are immutable
106 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
107 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
111 * Authorative constructor.
113 * @param authorativeConcept the authorative concept to copy from
115 public JpaToscaDataType(final ToscaDataType authorativeConcept) {
116 this.fromAuthorative(authorativeConcept);
120 public ToscaDataType toAuthorative() {
121 ToscaDataType toscaDataType = new ToscaDataType();
122 super.setToscaEntity(toscaDataType);
123 super.toAuthorative();
125 if (constraints != null) {
126 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
128 for (JpaToscaConstraint constraint : constraints) {
129 toscaConstraints.add(constraint.toAuthorative());
132 toscaDataType.setConstraints(toscaConstraints);
135 if (properties != null) {
136 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
138 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
139 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
142 toscaDataType.setProperties(propertyMap);
145 return toscaDataType;
149 public void fromAuthorative(final ToscaDataType toscaDataType) {
150 super.fromAuthorative(toscaDataType);
152 if (toscaDataType.getConstraints() != null) {
153 constraints = new ArrayList<>();
155 for (ToscaConstraint toscaConstraint : toscaDataType.getConstraints()) {
156 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
160 if (toscaDataType.getProperties() != null) {
161 properties = new LinkedHashMap<>();
162 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
163 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
164 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
165 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
171 public List<PfKey> getKeys() {
172 final List<PfKey> keyList = super.getKeys();
174 if (properties != null) {
175 for (JpaToscaProperty property : properties.values()) {
176 keyList.addAll(property.getKeys());
184 public void clean() {
187 if (properties != null) {
188 for (JpaToscaProperty property : properties.values()) {
195 public PfValidationResult validate(final PfValidationResult resultIn) {
196 PfValidationResult result = super.validate(resultIn);
198 if (constraints != null) {
199 result = validateConstraints(result);
202 if (properties != null) {
203 result = validateProperties(result);
210 * Validate the constraints.
212 * @param result The result of validations up to now
213 * @return the validation result
215 private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
216 PfValidationResult result = resultIn;
218 for (JpaToscaConstraint constraint : constraints) {
219 if (constraint == null) {
220 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
221 "data type constraint may not be null "));
228 * Validate the properties.
230 * @param result The result of validations up to now
231 * @return the validation result
233 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
234 PfValidationResult result = resultIn;
236 for (JpaToscaProperty property : properties.values()) {
237 if (property == null) {
238 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
239 "data type property may not be null "));
241 result = property.validate(result);
248 public int compareTo(final PfConcept otherConcept) {
249 if (otherConcept == null) {
252 if (this == otherConcept) {
255 if (getClass() != otherConcept.getClass()) {
256 return getClass().getName().compareTo(otherConcept.getClass().getName());
259 final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
260 if (!super.equals(other)) {
261 return super.compareTo(other);
264 int result = PfUtils.compareObjects(constraints, other.constraints);
269 result = PfUtils.compareObjects(properties, other.properties);
278 * Get the data types referenced in a data type.
280 * @return the data types referenced in a data type
282 public Collection<PfConceptKey> getReferencedDataTypes() {
283 if (properties == null) {
284 return CollectionUtils.emptyCollection();
287 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
289 for (JpaToscaProperty property : properties.values()) {
290 referencedDataTypes.add(property.getType());
292 if (property.getEntrySchema() != null) {
293 referencedDataTypes.add(property.getEntrySchema().getType());
297 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
299 return referencedDataTypes;