2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.BeanValidationResult;
37 import org.onap.policy.common.utils.validation.Assertions;
38 import org.onap.policy.models.base.PfAuthorative;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.base.Validated;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
47 * Class to represent the EntrySchema of list/map property in TOSCA definition.
49 * @author Chenfei Gao (cgao@research.att.com)
50 * @author Liam Fallon (liam.fallon@est.tech)
53 @EqualsAndHashCode(callSuper = false)
55 public class JpaToscaSchemaDefinition extends Validated
56 implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<JpaToscaSchemaDefinition> {
58 private static final long serialVersionUID = 3645882081163287058L;
61 private PfConceptKey type;
64 private String description;
67 private List<JpaToscaConstraint> constraints;
70 * The full constructor creates a {@link JpaToscaSchemaDefinition} object with mandatory fields.
72 * @param type the type of the entry schema
74 public JpaToscaSchemaDefinition(@NonNull final PfConceptKey type) {
81 * @param copyConcept the concept to copy from
83 public JpaToscaSchemaDefinition(@NonNull final JpaToscaSchemaDefinition copyConcept) {
84 copyConcept.copyTo(this);
88 * Authorative constructor.
90 * @param authorativeConcept the authorative concept to copy from
92 public JpaToscaSchemaDefinition(final ToscaSchemaDefinition authorativeConcept) {
93 this.fromAuthorative(authorativeConcept);
97 public ToscaSchemaDefinition toAuthorative() {
98 ToscaSchemaDefinition toscaEntrySchema = new ToscaSchemaDefinition();
100 toscaEntrySchema.setType(type.getName());
101 toscaEntrySchema.setTypeVersion(type.getVersion());
103 toscaEntrySchema.setDescription(description);
105 if (constraints != null) {
106 List<ToscaConstraint> toscaConstraints = new ArrayList<>();
108 for (JpaToscaConstraint constraint : constraints) {
109 toscaConstraints.add(constraint.toAuthorative());
112 toscaEntrySchema.setConstraints(toscaConstraints);
115 return toscaEntrySchema;
119 public void fromAuthorative(final ToscaSchemaDefinition toscaEntrySchema) {
120 if (toscaEntrySchema.getTypeVersion() != null) {
121 type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion());
123 type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION);
126 description = toscaEntrySchema.getDescription();
128 if (toscaEntrySchema.getConstraints() != null) {
129 constraints = new ArrayList<>();
131 for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) {
132 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
137 public List<PfKey> getKeys() {
138 return type.getKeys();
141 public void clean() {
143 description = (description != null ? description.trim() : null);
147 public BeanValidationResult validate(@NonNull String fieldName) {
148 BeanValidationResult result = new BeanValidationResult(fieldName, this);
150 result.addResult(validateKeyNotNull("type", type));
151 result.addResult(validateNotBlank("description", description, false));
153 validateList(result, "constraints", constraints, Validated::validateNotNull);
159 public int compareTo(final JpaToscaSchemaDefinition other) {
167 int result = ObjectUtils.compare(description, other.description);
172 return PfUtils.compareCollections(constraints, other.constraints);
176 * Copy this entry schema to another.
178 * @param target the other schemaa
179 * @return the copied concept
181 public JpaToscaSchemaDefinition copyTo(@NonNull final JpaToscaSchemaDefinition target) {
182 Assertions.instanceOf(target, JpaToscaSchemaDefinition.class);
184 final JpaToscaSchemaDefinition copy = (target);
185 copy.setType(new PfConceptKey(type));
186 copy.setDescription(description);
188 if (constraints != null) {
189 final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
190 for (final JpaToscaConstraint constraint : constraints) {
191 newConstraints.add(constraint); // Constraints are immutable
193 copy.setConstraints(newConstraints);