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.Table;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
42 import org.onap.policy.common.utils.validation.Assertions;
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;
74 private Map<String, JpaToscaProperty> properties;
77 * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
79 public JpaToscaDataType() {
80 this(new PfConceptKey());
84 * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
88 public JpaToscaDataType(@NonNull final PfConceptKey key) {
95 * @param copyConcept the concept to copy from
97 public JpaToscaDataType(final JpaToscaDataType copyConcept) {
102 * Authorative constructor.
104 * @param authorativeConcept the authorative concept to copy from
106 public JpaToscaDataType(final ToscaDataType authorativeConcept) {
107 this.fromAuthorative(authorativeConcept);
111 public ToscaDataType toAuthorative() {
112 ToscaDataType toscaDataType = new ToscaDataType();
113 super.setToscaEntity(toscaDataType);
114 super.toAuthorative();
116 if (constraints != null) {
117 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
119 for (JpaToscaConstraint constraint : constraints) {
120 toscaConstraints.add(constraint.toAuthorative());
123 toscaDataType.setConstraints(toscaConstraints);
126 if (properties != null) {
127 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
129 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
130 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
133 toscaDataType.setProperties(propertyMap);
136 return toscaDataType;
140 public void fromAuthorative(final ToscaDataType toscaDataType) {
141 super.fromAuthorative(toscaDataType);
143 if (toscaDataType.getConstraints() != null) {
144 constraints = new ArrayList<>();
146 for (ToscaConstraint toscaConstraint: toscaDataType.getConstraints()) {
147 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
151 if (toscaDataType.getProperties() != null) {
152 properties = new LinkedHashMap<>();
153 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
154 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
155 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
156 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
162 public List<PfKey> getKeys() {
163 final List<PfKey> keyList = super.getKeys();
165 if (properties != null) {
166 for (JpaToscaProperty property : properties.values()) {
167 keyList.addAll(property.getKeys());
175 public void clean() {
178 if (properties != null) {
179 for (JpaToscaProperty property : properties.values()) {
186 public PfValidationResult validate(final PfValidationResult resultIn) {
187 PfValidationResult result = super.validate(resultIn);
189 if (constraints != null) {
190 result = validateConstraints(result);
193 if (properties != null) {
194 result = validateProperties(result);
201 * Validate the constraints.
203 * @param result The result of validations up to now
204 * @return the validation result
206 private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
207 PfValidationResult result = resultIn;
209 for (JpaToscaConstraint constraint : constraints) {
210 if (constraint == null) {
211 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
212 "data type constraint may not be null "));
219 * Validate the properties.
221 * @param result The result of validations up to now
222 * @return the validation result
224 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
225 PfValidationResult result = resultIn;
227 for (JpaToscaProperty property : properties.values()) {
228 if (property == null) {
229 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
230 "data type property may not be null "));
232 result = property.validate(result);
239 public int compareTo(final PfConcept otherConcept) {
240 if (otherConcept == null) {
243 if (this == otherConcept) {
246 if (getClass() != otherConcept.getClass()) {
247 return this.hashCode() - otherConcept.hashCode();
250 final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
251 if (!super.equals(other)) {
252 return super.compareTo(other);
255 int result = PfUtils.compareObjects(constraints, other.constraints);
260 result = PfUtils.compareObjects(properties, other.properties);
269 public PfConcept copyTo(@NonNull PfConcept target) {
270 final Object copyObject = target;
271 Assertions.instanceOf(copyObject, PfConcept.class);
273 final JpaToscaDataType copy = ((JpaToscaDataType) copyObject);
274 super.copyTo(target);
276 if (constraints == null) {
277 copy.setConstraints(null);
279 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
280 for (final JpaToscaConstraint constraint : constraints) {
281 newConstraints.add(constraint); // Constraints are immutable
283 copy.setConstraints(newConstraints);
286 if (properties == null) {
287 copy.setProperties(null);
289 final Map<String, JpaToscaProperty> newProperties = new LinkedHashMap<>();
290 for (final Entry<String, JpaToscaProperty> propertyEntry : properties.entrySet()) {
291 newProperties.put(propertyEntry.getKey(), new JpaToscaProperty(propertyEntry.getValue()));
293 copy.setProperties(newProperties);