2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 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.parameters.annotations.NotBlank;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.parameters.annotations.Valid;
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.Validated;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
50 * Class to represent the EntrySchema of list/map property in TOSCA definition.
52 * @author Chenfei Gao (cgao@research.att.com)
53 * @author Liam Fallon (liam.fallon@est.tech)
56 @EqualsAndHashCode(callSuper = false)
58 public class JpaToscaSchemaDefinition extends Validated
59 implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<JpaToscaSchemaDefinition> {
61 private static final long serialVersionUID = 3645882081163287058L;
66 private PfConceptKey type;
70 private String description;
73 private List<@NotNull @Valid JpaToscaConstraint> constraints;
76 * The full constructor creates a {@link JpaToscaSchemaDefinition} object with mandatory fields.
78 * @param type the type of the entry schema
80 public JpaToscaSchemaDefinition(@NonNull final PfConceptKey type) {
87 * @param copyConcept the concept to copy from
89 public JpaToscaSchemaDefinition(@NonNull final JpaToscaSchemaDefinition copyConcept) {
90 copyConcept.copyTo(this);
94 * Authorative constructor.
96 * @param authorativeConcept the authorative concept to copy from
98 public JpaToscaSchemaDefinition(final ToscaSchemaDefinition authorativeConcept) {
99 this.fromAuthorative(authorativeConcept);
103 public ToscaSchemaDefinition toAuthorative() {
104 ToscaSchemaDefinition toscaEntrySchema = new ToscaSchemaDefinition();
106 toscaEntrySchema.setType(type.getName());
107 toscaEntrySchema.setTypeVersion(type.getVersion());
109 toscaEntrySchema.setDescription(description);
111 if (constraints != null) {
112 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
114 for (JpaToscaConstraint constraint : constraints) {
115 toscaConstraints.add(constraint.toAuthorative());
118 toscaEntrySchema.setConstraints(toscaConstraints);
121 return toscaEntrySchema;
125 public void fromAuthorative(final ToscaSchemaDefinition toscaEntrySchema) {
126 if (toscaEntrySchema.getTypeVersion() != null) {
127 type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion());
129 type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION);
132 description = toscaEntrySchema.getDescription();
134 if (toscaEntrySchema.getConstraints() != null) {
135 constraints = new ArrayList<>();
137 for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) {
138 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
143 public List<PfKey> getKeys() {
144 return type.getKeys();
147 public void clean() {
149 description = (description != null ? description.trim() : null);
153 public int compareTo(final JpaToscaSchemaDefinition other) {
161 int result = ObjectUtils.compare(description, other.description);
166 return PfUtils.compareCollections(constraints, other.constraints);
170 * Copy this entry schema to another.
172 * @param target the other schemaa
173 * @return the copied concept
175 public JpaToscaSchemaDefinition copyTo(@NonNull final JpaToscaSchemaDefinition target) {
176 Assertions.instanceOf(target, JpaToscaSchemaDefinition.class);
178 final JpaToscaSchemaDefinition copy = (target);
179 copy.setType(new PfConceptKey(type));
180 copy.setDescription(description);
182 if (constraints != null) {
183 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
184 for (final JpaToscaConstraint constraint : constraints) {
185 newConstraints.add(constraint); // Constraints are immutable
187 copy.setConstraints(newConstraints);