2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020, 2023 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 jakarta.persistence.Column;
27 import jakarta.persistence.ElementCollection;
28 import java.io.Serial;
29 import java.io.Serializable;
30 import java.util.ArrayList;
31 import java.util.List;
33 import lombok.EqualsAndHashCode;
34 import lombok.NoArgsConstructor;
35 import lombok.NonNull;
36 import org.apache.commons.lang3.ObjectUtils;
37 import org.onap.policy.common.parameters.annotations.NotBlank;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.parameters.annotations.Valid;
40 import org.onap.policy.common.utils.validation.Assertions;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.Validated;
46 import org.onap.policy.models.base.validation.annotations.VerifyKey;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
51 * Class to represent the EntrySchema of list/map property in TOSCA definition.
53 * @author Chenfei Gao (cgao@research.att.com)
54 * @author Liam Fallon (liam.fallon@est.tech)
57 @EqualsAndHashCode(callSuper = false)
59 public class JpaToscaSchemaDefinition extends Validated
60 implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<JpaToscaSchemaDefinition> {
63 private static final long serialVersionUID = 3645882081163287058L;
68 private PfConceptKey type;
72 private String description;
75 private List<@NotNull @Valid JpaToscaConstraint> constraints;
78 * The full constructor creates a {@link JpaToscaSchemaDefinition} object with mandatory fields.
80 * @param type the type of the entry schema
82 public JpaToscaSchemaDefinition(@NonNull final PfConceptKey type) {
89 * @param copyConcept the concept to copy from
91 public JpaToscaSchemaDefinition(@NonNull final JpaToscaSchemaDefinition copyConcept) {
92 copyConcept.copyTo(this);
96 * Authorative constructor.
98 * @param authorativeConcept the authorative concept to copy from
100 public JpaToscaSchemaDefinition(final ToscaSchemaDefinition authorativeConcept) {
101 this.fromAuthorative(authorativeConcept);
105 public ToscaSchemaDefinition toAuthorative() {
106 var toscaEntrySchema = new ToscaSchemaDefinition();
108 toscaEntrySchema.setType(type.getName());
109 toscaEntrySchema.setTypeVersion(type.getVersion());
111 toscaEntrySchema.setDescription(description);
113 if (constraints != null) {
114 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
116 for (JpaToscaConstraint constraint : constraints) {
117 toscaConstraints.add(constraint.toAuthorative());
120 toscaEntrySchema.setConstraints(toscaConstraints);
123 return toscaEntrySchema;
127 public void fromAuthorative(final ToscaSchemaDefinition toscaEntrySchema) {
128 if (toscaEntrySchema.getTypeVersion() != null) {
129 type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion());
131 type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION);
134 description = toscaEntrySchema.getDescription();
136 if (toscaEntrySchema.getConstraints() != null) {
137 constraints = new ArrayList<>();
139 for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) {
140 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
145 public List<PfKey> getKeys() {
146 return type.getKeys();
149 public void clean() {
151 description = (description != null ? description.trim() : null);
155 public int compareTo(final JpaToscaSchemaDefinition other) {
163 int result = ObjectUtils.compare(description, other.description);
168 return PfUtils.compareCollections(constraints, other.constraints);
172 * Copy this entry schema to another.
174 * @param target the other schemaa
175 * @return the copied concept
177 public JpaToscaSchemaDefinition copyTo(@NonNull final JpaToscaSchemaDefinition target) {
178 Assertions.instanceOf(target, JpaToscaSchemaDefinition.class);
180 final JpaToscaSchemaDefinition copy = (target);
181 copy.setType(new PfConceptKey(type));
182 copy.setDescription(description);
184 if (constraints != null) {
185 // Constraints are immutable
186 final List<JpaToscaConstraint> newConstraints = new ArrayList<>(constraints);
187 copy.setConstraints(newConstraints);