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.List;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
39 import org.onap.policy.common.utils.validation.Assertions;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfUtils;
44 import org.onap.policy.models.base.PfValidationMessage;
45 import org.onap.policy.models.base.PfValidationResult;
46 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
49 * Class to represent custom data type in TOSCA definition.
51 * @author Chenfei Gao (cgao@research.att.com)
52 * @author Liam Fallon (liam.fallon@est.tech)
55 @Table(name = "ToscaDataType")
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @EqualsAndHashCode(callSuper = true)
59 public class JpaToscaDataType extends JpaToscaEntityType {
60 private static final long serialVersionUID = -3922690413436539164L;
63 private List<JpaToscaConstraint> constraints;
66 private List<JpaToscaProperty> properties;
69 * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
71 public JpaToscaDataType() {
72 this(new PfConceptKey());
76 * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
80 public JpaToscaDataType(@NonNull final PfConceptKey key) {
87 * @param copyConcept the concept to copy from
89 public JpaToscaDataType(final JpaToscaDataType copyConcept) {
94 public List<PfKey> getKeys() {
95 final List<PfKey> keyList = super.getKeys();
97 if (constraints != null) {
98 for (JpaToscaConstraint constraint : constraints) {
99 keyList.addAll(constraint.getKeys());
103 if (properties != null) {
104 for (JpaToscaProperty property : properties) {
105 keyList.addAll(property.getKeys());
113 public void clean() {
116 if (constraints != null) {
117 for (JpaToscaConstraint constraint : constraints) {
122 if (properties != null) {
123 for (JpaToscaProperty property : properties) {
130 public PfValidationResult validate(final PfValidationResult resultIn) {
131 PfValidationResult result = super.validate(resultIn);
133 if (constraints != null) {
134 result = validateConstraints(result);
137 if (properties != null) {
138 result = validateProperties(result);
145 * Validate the constraints.
147 * @param result The result of validations up to now
148 * @return the validation result
150 private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
151 PfValidationResult result = resultIn;
153 for (JpaToscaConstraint constraint : constraints) {
154 if (constraint == null) {
155 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
156 "data type constraint may not be null "));
158 result = constraint.validate(result);
165 * Validate the properties.
167 * @param result The result of validations up to now
168 * @return the validation result
170 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
171 PfValidationResult result = resultIn;
173 for (JpaToscaProperty property : properties) {
174 if (property == null) {
175 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
176 "data type property may not be null "));
178 result = property.validate(result);
185 public int compareTo(final PfConcept otherConcept) {
186 if (otherConcept == null) {
189 if (this == otherConcept) {
192 if (getClass() != otherConcept.getClass()) {
193 return this.hashCode() - otherConcept.hashCode();
196 final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
197 if (!super.equals(other)) {
198 return super.compareTo(other);
201 int result = PfUtils.compareObjects(constraints, other.constraints);
206 result = PfUtils.compareObjects(properties, other.properties);
215 public PfConcept copyTo(@NonNull PfConcept target) {
216 final Object copyObject = target;
217 Assertions.instanceOf(copyObject, PfConcept.class);
219 final JpaToscaDataType copy = ((JpaToscaDataType) copyObject);
220 super.copyTo(target);
222 if (constraints == null) {
223 copy.setConstraints(null);
226 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
227 for (final JpaToscaConstraint constraint : constraints) {
228 newConstraints.add(constraint); // Constraints are immutable
230 copy.setConstraints(newConstraints);
233 if (properties == null) {
234 copy.setProperties(null);
237 final List<JpaToscaProperty> newProperties = new ArrayList<>();
238 for (final JpaToscaProperty property : properties) {
239 newProperties.add(new JpaToscaProperty(property));
241 copy.setProperties(newProperties);