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;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.onap.policy.models.base.PfAuthorative;
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.PfReferenceKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.PfValidationMessage;
46 import org.onap.policy.models.base.PfValidationResult;
47 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
53 * Class to represent custom data type in TOSCA definition.
55 * @author Chenfei Gao (cgao@research.att.com)
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaDataType")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = true)
63 public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> {
64 private static final long serialVersionUID = -3922690413436539164L;
67 private List<JpaToscaConstraint> constraints;
70 private Map<String, JpaToscaProperty> properties;
73 * The Default Constructor creates a {@link JpaToscaDataType} object with a null key.
75 public JpaToscaDataType() {
76 this(new PfConceptKey());
80 * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
84 public JpaToscaDataType(@NonNull final PfConceptKey key) {
91 * @param copyConcept the concept to copy from
93 public JpaToscaDataType(final JpaToscaDataType copyConcept) {
95 // Constraints are immutable
96 this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
97 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
101 * Authorative constructor.
103 * @param authorativeConcept the authorative concept to copy from
105 public JpaToscaDataType(final ToscaDataType authorativeConcept) {
106 this.fromAuthorative(authorativeConcept);
110 public ToscaDataType toAuthorative() {
111 ToscaDataType toscaDataType = new ToscaDataType();
112 super.setToscaEntity(toscaDataType);
113 super.toAuthorative();
115 if (constraints != null) {
116 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
118 for (JpaToscaConstraint constraint : constraints) {
119 toscaConstraints.add(constraint.toAuthorative());
122 toscaDataType.setConstraints(toscaConstraints);
125 if (properties != null) {
126 Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>();
128 for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) {
129 propertyMap.put(entry.getKey(), entry.getValue().toAuthorative());
132 toscaDataType.setProperties(propertyMap);
135 return toscaDataType;
139 public void fromAuthorative(final ToscaDataType toscaDataType) {
140 super.fromAuthorative(toscaDataType);
142 if (toscaDataType.getConstraints() != null) {
143 constraints = new ArrayList<>();
145 for (ToscaConstraint toscaConstraint: toscaDataType.getConstraints()) {
146 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
150 if (toscaDataType.getProperties() != null) {
151 properties = new LinkedHashMap<>();
152 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) {
153 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
154 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
155 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
161 public List<PfKey> getKeys() {
162 final List<PfKey> keyList = super.getKeys();
164 if (properties != null) {
165 for (JpaToscaProperty property : properties.values()) {
166 keyList.addAll(property.getKeys());
174 public void clean() {
177 if (properties != null) {
178 for (JpaToscaProperty property : properties.values()) {
185 public PfValidationResult validate(final PfValidationResult resultIn) {
186 PfValidationResult result = super.validate(resultIn);
188 if (constraints != null) {
189 result = validateConstraints(result);
192 if (properties != null) {
193 result = validateProperties(result);
200 * Validate the constraints.
202 * @param result The result of validations up to now
203 * @return the validation result
205 private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) {
206 PfValidationResult result = resultIn;
208 for (JpaToscaConstraint constraint : constraints) {
209 if (constraint == null) {
210 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
211 "data type constraint may not be null "));
218 * Validate the properties.
220 * @param result The result of validations up to now
221 * @return the validation result
223 private PfValidationResult validateProperties(final PfValidationResult resultIn) {
224 PfValidationResult result = resultIn;
226 for (JpaToscaProperty property : properties.values()) {
227 if (property == null) {
228 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
229 "data type property may not be null "));
231 result = property.validate(result);
238 public int compareTo(final PfConcept otherConcept) {
239 if (otherConcept == null) {
242 if (this == otherConcept) {
245 if (getClass() != otherConcept.getClass()) {
246 return getClass().getName().compareTo(otherConcept.getClass().getName());
249 final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
250 if (!super.equals(other)) {
251 return super.compareTo(other);
254 int result = PfUtils.compareObjects(constraints, other.constraints);
259 result = PfUtils.compareObjects(properties, other.properties);