2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 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;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
32 import lombok.EqualsAndHashCode;
33 import lombok.NoArgsConstructor;
34 import lombok.NonNull;
35 import org.apache.commons.lang3.ObjectUtils;
36 import org.onap.policy.common.utils.validation.Assertions;
37 import org.onap.policy.models.base.PfAuthorative;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfKey;
40 import org.onap.policy.models.base.PfUtils;
41 import org.onap.policy.models.base.PfValidationMessage;
42 import org.onap.policy.models.base.PfValidationResult;
43 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
48 * Class to represent the EntrySchema of list/map property in TOSCA definition.
50 * @author Chenfei Gao (cgao@research.att.com)
51 * @author Liam Fallon (liam.fallon@est.tech)
54 @EqualsAndHashCode(callSuper = false)
56 public class JpaToscaSchemaDefinition
57 implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<JpaToscaSchemaDefinition> {
59 private static final long serialVersionUID = 3645882081163287058L;
61 // Recurring string constants
62 private static final String ENTRY_SCHEMA = "EntrySchema";
65 private PfConceptKey type;
68 private String description;
71 private List<JpaToscaConstraint> constraints;
74 * The full constructor creates a {@link JpaToscaSchemaDefinition} object with mandatory fields.
76 * @param type the type of the entry schema
78 public JpaToscaSchemaDefinition(@NonNull final PfConceptKey type) {
85 * @param copyConcept the concept to copy from
87 public JpaToscaSchemaDefinition(@NonNull final JpaToscaSchemaDefinition copyConcept) {
88 copyConcept.copyTo(this);
92 * Authorative constructor.
94 * @param authorativeConcept the authorative concept to copy from
96 public JpaToscaSchemaDefinition(final ToscaSchemaDefinition authorativeConcept) {
97 this.fromAuthorative(authorativeConcept);
101 public ToscaSchemaDefinition toAuthorative() {
102 ToscaSchemaDefinition toscaEntrySchema = new ToscaSchemaDefinition();
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 ToscaSchemaDefinition 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(ENTRY_SCHEMA, 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(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION),
166 this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank"));
169 if (constraints != null) {
170 for (JpaToscaConstraint constraint : constraints) {
171 if (constraint == null) {
172 result.addValidationMessage(
173 new PfValidationMessage(new PfConceptKey(ENTRY_SCHEMA, PfKey.NULL_KEY_VERSION),
174 this.getClass(), ValidationResult.INVALID, "property constraint may not be null "));
183 public int compareTo(final JpaToscaSchemaDefinition other) {
191 int result = ObjectUtils.compare(description, other.description);
196 return PfUtils.compareCollections(constraints, other.constraints);
200 * Copy this entry schema to another.
202 * @param target the other schemaa
203 * @return the copied concept
205 public JpaToscaSchemaDefinition copyTo(@NonNull final JpaToscaSchemaDefinition target) {
206 Assertions.instanceOf(target, JpaToscaSchemaDefinition.class);
208 final JpaToscaSchemaDefinition copy = (target);
209 copy.setType(new PfConceptKey(type));
210 copy.setDescription(description);
212 if (constraints != null) {
213 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
214 for (final JpaToscaConstraint constraint : constraints) {
215 newConstraints.add(constraint); // Constraints are immutable
217 copy.setConstraints(newConstraints);