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.io.Serializable;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
34 import lombok.EqualsAndHashCode;
35 import lombok.NoArgsConstructor;
36 import lombok.NonNull;
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.utils.validation.Assertions;
40 import org.onap.policy.models.base.PfAuthorative;
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;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntrySchema;
52 * Class to represent the EntrySchema of list/map property in TOSCA definition.
54 * @author Chenfei Gao (cgao@research.att.com)
55 * @author Liam Fallon (liam.fallon@est.tech)
58 @EqualsAndHashCode(callSuper = false)
60 public class JpaToscaEntrySchema
61 implements PfAuthorative<ToscaEntrySchema>, Serializable, Comparable<JpaToscaEntrySchema> {
62 private static final long serialVersionUID = 3645882081163287058L;
65 private PfConceptKey type;
68 private String description;
71 private List<JpaToscaConstraint> constraints;
74 * The full constructor creates a {@link JpaToscaEntrySchema} object with mandatory fields.
76 * @param type the type of the entry schema
78 public JpaToscaEntrySchema(@NonNull final PfConceptKey type) {
85 * @param copyConcept the concept to copy from
87 public JpaToscaEntrySchema(@NonNull final JpaToscaEntrySchema copyConcept) {
88 copyConcept.copyTo(this);
92 * Authorative constructor.
94 * @param authorativeConcept the authorative concept to copy from
96 public JpaToscaEntrySchema(final ToscaEntrySchema authorativeConcept) {
97 this.fromAuthorative(authorativeConcept);
101 public ToscaEntrySchema toAuthorative() {
102 ToscaEntrySchema toscaEntrySchema = new ToscaEntrySchema();
104 toscaEntrySchema.setType(type.getName());
105 toscaEntrySchema.setTypeVersion(type.getVersion());
107 toscaEntrySchema.setDescription(description);
109 if (constraints != null) {
110 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
112 for (JpaToscaConstraint constraint : constraints) {
113 toscaConstraints.add(constraint.toAuthorative());
116 toscaEntrySchema.setConstraints(toscaConstraints);
119 return toscaEntrySchema;
123 public void fromAuthorative(final ToscaEntrySchema toscaEntrySchema) {
124 if (toscaEntrySchema.getTypeVersion() != null) {
125 type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion());
127 type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION);
130 description = toscaEntrySchema.getDescription();
132 if (toscaEntrySchema.getConstraints() != null) {
133 constraints = new ArrayList<>();
135 for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) {
136 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
141 public List<PfKey> getKeys() {
142 return type.getKeys();
145 public void clean() {
147 description = (description != null ? description.trim() : null);
151 * Validate the entry schema.
153 * @param resultIn the incoming result
154 * @return the ooutput result witht he result of this validation
156 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
157 PfValidationResult result = resultIn;
159 if (type == null || type.isNullKey()) {
160 result.addValidationMessage(new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION),
161 this.getClass(), ValidationResult.INVALID, "entry schema type may not be null"));
164 if (description != null && description.trim().length() == 0) {
165 result.addValidationMessage(new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION),
166 this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank"));
170 if (constraints != null) {
171 for (JpaToscaConstraint constraint : constraints) {
172 if (constraint == null) {
173 result.addValidationMessage(
174 new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION),
175 this.getClass(), ValidationResult.INVALID, "property constraint may not be null "));
184 public int compareTo(final JpaToscaEntrySchema other) {
192 int result = ObjectUtils.compare(description, other.description);
197 return PfUtils.compareObjects(constraints, other.constraints);
201 * Copy this entry schema to another.
203 * @param target the other schemaa
204 * @return the copied concept
206 public JpaToscaEntrySchema copyTo(@NonNull final JpaToscaEntrySchema target) {
207 Assertions.instanceOf(target, JpaToscaEntrySchema.class);
209 final JpaToscaEntrySchema copy = (target);
210 copy.setType(new PfConceptKey(type));
211 copy.setDescription(description);
213 if (constraints != null) {
214 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
215 for (final JpaToscaConstraint constraint : constraints) {
216 newConstraints.add(constraint); // Constraints are immutable
218 copy.setConstraints(newConstraints);