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.concepts;
26 import java.util.ArrayList;
27 import java.util.List;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Table;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.common.utils.validation.Assertions;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.PfUtils;
48 import org.onap.policy.models.base.PfValidationMessage;
49 import org.onap.policy.models.base.PfValidationResult;
50 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
54 * Class to represent the EntrySchema of list/map property in TOSCA definition.
56 * @author Chenfei Gao (cgao@research.att.com)
57 * @author Liam Fallon (liam.fallon@est.tech)
60 @Table(name = "ToscaEntrySchema")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @EqualsAndHashCode(callSuper = false)
64 public class ToscaEntrySchema extends PfConcept {
65 private static final long serialVersionUID = 3645882081163287058L;
68 private PfReferenceKey key;
71 private PfConceptKey type;
74 private String description;
77 private List<ToscaConstraint> constraints;
80 * The Default Constructor creates a {@link ToscaEntrySchema} object with a null key.
82 public ToscaEntrySchema() {
83 this(new PfReferenceKey());
87 * The Key Constructor creates a {@link ToscaEntrySchema} object with the given concept key.
91 public ToscaEntrySchema(@NonNull final PfReferenceKey key) {
92 this(key, new PfConceptKey());
96 * The full constructor creates a {@link ToscaEntrySchema} object with mandatory fields.
99 * @param type the type of the entry schema
101 public ToscaEntrySchema(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
109 * @param copyConcept the concept to copy from
111 public ToscaEntrySchema(final ToscaEntrySchema copyConcept) {
116 public List<PfKey> getKeys() {
117 final List<PfKey> keyList = getKey().getKeys();
119 keyList.addAll(type.getKeys());
121 if (constraints != null) {
122 for (ToscaConstraint constraint : constraints) {
123 keyList.addAll(constraint.getKeys());
131 public void clean() {
135 description = (description != null ? description.trim() : null);
137 if (constraints != null) {
138 for (ToscaConstraint constraint : constraints) {
145 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
146 PfValidationResult result = resultIn;
148 if (key.isNullKey()) {
149 result.addValidationMessage(
150 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
153 result = key.validate(result);
155 if (type == null || type.isNullKey()) {
156 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
157 "entry schema type may not be null"));
160 if (description != null && description.trim().length() == 0) {
161 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
162 "entry schema description may not be blank"));
166 if (constraints != null) {
167 for (ToscaConstraint constraint : constraints) {
168 if (constraint == null) {
169 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
170 "property constraint may not be null "));
172 result = constraint.validate(result);
181 public int compareTo(final PfConcept otherConcept) {
182 if (otherConcept == null) {
185 if (this == otherConcept) {
188 if (getClass() != otherConcept.getClass()) {
189 return this.hashCode() - otherConcept.hashCode();
192 final ToscaEntrySchema other = (ToscaEntrySchema) otherConcept;
193 if (!key.equals(other.key)) {
194 return key.compareTo(other.key);
197 if (!type.equals(other.type)) {
198 return type.compareTo(other.type);
201 int result = ObjectUtils.compare(description, other.description);
206 return PfUtils.compareObjects(constraints, other.constraints);
210 public PfConcept copyTo(@NonNull final PfConcept target) {
211 Assertions.instanceOf(target, ToscaEntrySchema.class);
213 final ToscaEntrySchema copy = ((ToscaEntrySchema) target);
214 copy.setKey(new PfReferenceKey(key));
215 copy.setType(new PfConceptKey(type));
216 copy.setDescription(description);
218 if (constraints != null) {
219 final List<ToscaConstraint> newConstraints = new ArrayList<>();
220 for (final ToscaConstraint constraint : constraints) {
221 newConstraints.add(constraint); // Constraints are immutable
223 copy.setConstraints(newConstraints);