2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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.LinkedHashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
32 import javax.persistence.ElementCollection;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Lob;
37 import javax.persistence.Table;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfReferenceKey;
48 import org.onap.policy.models.base.PfUtils;
49 import org.onap.policy.models.base.PfValidationMessage;
50 import org.onap.policy.models.base.PfValidationResult;
51 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
57 * Class to represent custom data type in TOSCA definition.
59 * @author Chenfei Gao (cgao@research.att.com)
60 * @author Liam Fallon (liam.fallon@est.tech)
63 @Table(name = "ToscaDataType")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = true)
67 public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> {
68 private static final long serialVersionUID = -3922690413436539164L;
71 private List<JpaToscaConstraint> constraints;
75 private Map<String, JpaToscaProperty> properties;
78 * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
80 public JpaToscaDataType() {
81 this(new PfConceptKey());
85 * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
89 public JpaToscaDataType(@NonNull final PfConceptKey key) {
96 * @param copyConcept the concept to copy from
98 public JpaToscaDataType(final JpaToscaDataType copyConcept) {
100 // Constraints are immutable
101 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
102 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
106 * Authorative constructor.
108 * @param authorativeConcept the authorative concept to copy from
110 public JpaToscaDataType(final ToscaDataType authorativeConcept) {
111 this.fromAuthorative(authorativeConcept);
115 public ToscaDataType toAuthorative() {
116 ToscaDataType toscaDataType = new ToscaDataType();
117 super.setToscaEntity(toscaDataType);
118 super.toAuthorative();
120 if (constraints != null) {
121 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
123 for (JpaToscaConstraint constraint : constraints) {
124 toscaConstraints.add(constraint.toAuthorative());
127 toscaDataType.setConstraints(toscaConstraints);
130 if (properties != null) {
131 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
133 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
134 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
137 toscaDataType.setProperties(propertyMap);
140 return toscaDataType;
144 public void fromAuthorative(final ToscaDataType toscaDataType) {
145 super.fromAuthorative(toscaDataType);
147 if (toscaDataType.getConstraints() != null) {
148 constraints = new ArrayList<>();
150 for (ToscaConstraint toscaConstraint : toscaDataType.getConstraints()) {
151 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
155 if (toscaDataType.getProperties() != null) {
156 properties = new LinkedHashMap<>();
157 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
158 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
159 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
160 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
166 public List<PfKey> getKeys() {
167 final List<PfKey> keyList = super.getKeys();
169 if (properties != null) {
170 for (JpaToscaProperty property : properties.values()) {
171 keyList.addAll(property.getKeys());
179 public void clean() {
182 if (properties != null) {
183 for (JpaToscaProperty property : properties.values()) {
190 public PfValidationResult validate(final PfValidationResult resultIn) {
191 PfValidationResult result = super.validate(resultIn);
193 if (constraints != null) {
194 result = validateConstraints(result);
197 if (properties != null) {
198 result = validateProperties(result);
205 * Validate the constraints.
207 * @param result The result of validations up to now
208 * @return the validation result
210 private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
211 PfValidationResult result = resultIn;
213 for (JpaToscaConstraint constraint : constraints) {
214 if (constraint == null) {
215 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
216 "data type constraint may not be null "));
223 * Validate the properties.
225 * @param result The result of validations up to now
226 * @return the validation result
228 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
229 PfValidationResult result = resultIn;
231 for (JpaToscaProperty property : properties.values()) {
232 if (property == null) {
233 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
234 "data type property may not be null "));
236 result = property.validate(result);
243 public int compareTo(final PfConcept otherConcept) {
244 if (otherConcept == null) {
247 if (this == otherConcept) {
250 if (getClass() != otherConcept.getClass()) {
251 return getClass().getName().compareTo(otherConcept.getClass().getName());
254 final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
255 if (!super.equals(other)) {
256 return super.compareTo(other);
259 int result = PfUtils.compareObjects(constraints, other.constraints);
264 result = PfUtils.compareObjects(properties, other.properties);